Correct Solution in Java Array


#1

public class Solution {
public int solve(int[] A, int B, int C) {
int n = C;
int c=0,k=-1,p,result=0;
int m=A.length;
while(n>0){
n=n/10;
++c;
}
//System.out.println©;
if(B<c){
if(isPresent(A,0) && B>1)
return (m-1)*(int)Math.pow(m,B-1);
else
return (int)Math.pow(m,B);
}
else if(B>c)
return 0;
else{
int[] arr = new int[c];
n=C;
while(n>0){
arr[++k]=n%10;
n/=10;
}
//System.out.println(k);

        for(int i=B;i>0;i--){
            p = findLess(A,arr[k],B,i);
            //System.out.println(p);
            result+=(p*(int)Math.pow(m,i-1));
            //System.out.println(result);
            if(!isPresent(A,arr[k])){
                //System.out.println(i +"abc"+ k);
                break;
                }
            k--; 
        }
    }  
    return result;
}

public int findLess(int[] A,int n, int B, int i){
    int count = 0;
    for(int j=0; j<A.length; j++){
        if(A[j]<n)
            count++;
    }
    //System.out.println(count);
    if(B == i && B>1 ){
        if(isPresent(A,0))
            count--;
    }
    //System.out.println(count);
    return count;
}

public boolean isPresent(int[] A,int n){
    for(int i=0; i<A.length; i++){
        if(A[i] == n)
           return true;
    }
    return false; 
}

}