avico81
BAN USERThat can't be a MS interview Question!
1. Weight 3 balls against 3 balls: either one of these groups weight more or the heavy ball is in the 3rd group.
2. Out of the heavier ball group, Weight 1 ball against another: other one of them is heavier or the heavy ball is the one you didn't touch
public static void main(String[] args) {
LinkedList<Integer> l1 = arr2ll(3, 6, 5);
LinkedList<Integer> l2 = arr2ll(1, 2, 3);
LinkedList<Integer> l = new LinkedList<Integer>();
int car = 0;
for(Iterator<Integer> i1 = l1.iterator(), i2 = l2.iterator(); i1.hasNext() || i2.hasNext(); ){
int n1 = i1.hasNext() ? i1.next() : 0;
int n2 = i2.hasNext() ? i2.next() : 0;
int sum = (n1 + n2 + car) % 10;
l.add(sum);
car = (n1 + n2 + car) / 10;
}
if(car > 0)
l.add(car);
printLL(l);
}
public static void printLL(LinkedList ll)
{
for(Iterator iter = ll.iterator(); iter.hasNext();)
System.out.println(iter.next());
}
public static <T extends Object> LinkedList<T> arr2ll(T...arr){
LinkedList<T> ll = new LinkedList<T>();
for(T t : arr)
ll.add(t);
return ll;
}
- avico81 August 27, 2019