manish
BAN USERThe concurrentHashMap iterators are not fail-fast in the sense that they do not throw a ConcurrentModificationException. They also do not guarantee that once the iterator is created it will list/show all elements that are added after its creation. The iterators do however guarantee that any updates or removal of items will be reflected correctly in their behaviour. They also guarantee that no element will be returned more than once while traversal. which is a big draw back
hence in nut shell The no-synchronized Iterator is a major drawback i know for ConcurrentHashMap .
valid point , but this can be solved by just checking for null in the for loop , isn't it . And sorry for being ambiguous but when i mentioned first loop i meant for loo only . the first if loop is a very basic precaution which might happen in rare of teh rare cases. okay i think this modification will be able o handle the case you are talking about
for(int i=0;i<value-1;i++)
{
if(p2==null) return null;
p2=p2.next;
}
its good thing you were able to think , as i obviously missed it .. Thanks for suggestion , Do let me know if the code still can fail for some case.
- manish April 11, 20131.if n is greater than length of loop while executing first loop itself your p2 will point to null so its a handled case there will be no exception , you can handle null return type from calling method
2.If there is a loop in system : the complexity of solution not the rune time in total will increase as that will be another boolean based method to detect that once at the beginning of the code . You need two node pointers for that as well one slow runner and one fast runner
boolean detectLoop(LinkedListNode head)
{
LinkedListNode slowRunner = head;
LinkedListNode fastRunner = head;
while (fastRunner!=null && fastRunner.next!=null)
{
slowRunner = slowRunner.next;// one step at a time
fastRunner=fastRunner.next.next;// two steps at a time
if(slowRunner==fastRunner)
{
return true;
}
}
return false;
}
The code is simple enough to explain what i am trying hence not summing it up
LinkedListNode nthToLast(LinkedListNode head,int value)
{
LinkedListNode p1=head;
LinkedListNode p2=head;
if (p2==null) return null;
for(int i=0;i<value-1;i++)
{
p2=p2.next;
}
if(p2==null)return null;
while(p2.next!=null)
{
p2=p2.next;
p1=p1.next;
}
return p1;
}
This algo will take O(n) time and O(1) space.
null values can be handled in a much better way here and hence more and more way to break this as well , do mention if u see this method not returning the expected o/p in any case
this solution will give you the exclusive list as u have mentioned in 1 and 2
after that you can just add them using a Hashtable(in case you want to remove the duplicates eg: if((table.containsKey(element)) go next else table.put(element))
package com.april.eighth;
import java.util.Arrays;
import java.util.List;
public class Exclusion {
public static void main(String[] args) {
List<Integer> A = Arrays.asList(1, 3, 4, 6,8,10, 17, 34);
List<Integer> B = Arrays.asList(2, 8, 17, 33, 44, 66, 89, 100, 123);
for(int element:A)
{
int temp=B.indexOf(element);
if(temp==-1)
{
//element which are in A but not in B
//store it where you want list or aarray
}
}
for(int element:B)
{
int temp=A.indexOf(element);
if(temp==-1)
{
//element which are in B but not in A
// store it where you want list or aarray
}
}
}
}
- manish April 08, 2013O(n) based solution
123456784987654321
start with a number
123456784 987654321
^Starting from right find the first place where the left-digit is less-than the right-digit is here. Digit "x" is 4
123456784 987654321
^find the smallest digit larger than 4 to the right
123456785 4 98764321
^place it to the left of 4
123456785 4 12346789
123456785123446789
^sort the digits to the right of 5. Since all of them except the '4' were already in descending order, all we need to do is reverse their order, and find the correct place for the '4'
Example 5,6,7,4
from right first place where left < right is 6
smallest digit larger than 6 is 7 move it to left of 6
so now the no is
5764
sort the no post 7
5746 as expected
why not answer will be anana ? than ana ? was there a condition that overlap is limited to one character?
- manish July 06, 2016