varun
BAN USERpublic class CountOfPreviousValue {
public static void main(String[] args) {
int n = 7;
System.out.println(findNthInSeries(n));
}
private static String findNthInSeries(int n) {
String curr = "1";
for (int i = 1; i <n ; i++) {
curr = generateSeq(curr);
}
return curr;
}
private static String generateSeq(String curr) {
String out="";
if(curr.equals("1")){
return "11";
}
else{
for (int i = 1; i <=curr.length() ; i++) {
int count=1;
while(i<curr.length() &&(curr.charAt(i-1)==curr.charAt(i))){
count++;
i++;
}
out = out+count+curr.charAt(i-1);
}
return out;
}
}
}
Number at any index is showing the count of numbers at previous index. eg. for index 2 value is 11 which implies that at index 1 there is 1(count) of number 1. similarly value at index 4 is 1211 --> that at index 3 there is 1(count) of number 2 and 1(count) of number 1
- varun May 05, 2015private static int convert(String s) {
int res = 0;
int i =0;
boolean isNegative = false;
if(s.length() ==0 ){
System.out.println("Invalid string");
return res;
}
if(s.toCharArray()[0] == '-'){
isNegative = true;
i++;
}
for(;i<s.length();i++){
res = 10*res +Character.getNumericValue(s.toCharArray()[i]);
}
if(isNegative){
res=-res;
}
if(res >Integer.MAX_VALUE){
System.out.println("Bigger than max int val");
return Integer.MAX_VALUE;
}
if(res <Integer.MIN_VALUE){
System.out.println("smaller than min int val");
return Integer.MIN_VALUE;
}
return res;
}
class binaryProcessor{
int count =0;
public void countNodesLessThan(Node root) {
countNodesLess(root.left,root.value);
countNodesLess(root.right,root.value);
}
private void countNodesLess(Node node, int value) {
if(node == null){
return;
}
if(node.value<value){
count++;
}
countNodesLess(node.left,node.value);
countNodesLess(node.right,node.value);
}
}
- varun April 23, 2015You can try writting something like this...
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
public class UnionIterator implements Iterator<String> {
Collection<Iterator<String>> c;
public UnionIterator(Collection<Iterator<String>> c) {
this.c=c;
}
@Override
public boolean hasNext() {
if(!c.isEmpty()){
for (Iterator iterator = c.iterator(); iterator.hasNext();) {
Iterator<String> iterator2 = (Iterator<String>) iterator.next();
if(iterator2.hasNext()){
return true;
}
}
}
return false;
}
@Override
public String next() {
for (Iterator iterator = c.iterator(); iterator.hasNext();) {
Iterator<String> iterator2 = (Iterator<String>) iterator.next();
if(iterator2.hasNext()){
return iterator2.next();
}
}
return null;
}
@Override
public void remove() {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
List l1 = new ArrayList<String>();
List l2 = new ArrayList<String>();
l1.add("abc");l1.add("def");
l2.add("fer");l2.add("hgy");
Collection<Iterator<String>> ct = new ArrayList<Iterator<String>>();
ct.add(l1.iterator());
ct.add(l2.iterator());
UnionIterator ut = new UnionIterator(ct);
while(ut.hasNext()){
System.out.println(ut.next());
}
}
}
nope... its not sum....you got it wrong, what i mean to say that value at any index represent the number of occurence of particular digit in previous index value.
- varun May 05, 2015so 1211 represent that there is 1 occurence of number 2 and 1 occurance of number 1 in prev value i.e 21. Dont look at these numbers as integers.. look at them as strings which represents as "no of occurances+value of that digit"