abhradeep.kundu
BAN USERvoid lookAndSay(int input){
System.out.println(input);
List arr = new ArrayList(1);
arr.add(input);
lookAndSay(arr, 1);
}
void lookAndSay(List input, int cnt){
int count = 1;
if(cnt >= 10) {
return;
}
List newInput = new ArrayList();
for(int i = 0; i < input.size(); i++) {
if(i == input.size()-1) {
newInput.add(count);
newInput.add(input.get(i));
break;
}
if(input.get(i) == input.get(i+1)){
count++;
}else {
newInput.add(count);
newInput.add(input.get(i));
count =1;
}
}
for(int i = 0; i < newInput.size(); i++)
System.out.print(newInput.get(i));
cnt++;
System.out.println();
lookAndSay(newInput, cnt);
}
Both the code will fail when the array contain only negative numbers.... even if it is not advised to take sum in integer... because it is an integer array so two of the elements one is 2^32-1 and another any positive integer will fail the logic.
- abhradeep.kundu August 18, 2012
O(n) Time complexity
- abhradeep.kundu March 06, 2013