Saturday, December 24, 2011

Given a 10 digit number,find the greatest continuous 4 digit number.

given a 10 digit number,find the greatest continuous 4 digit number.
Ex:9164352435
Ans : 9164

int indexoflargest4digitnumber(int[] number)    {
if(number == 0)
return -1;
int result = 0;
int length = number.length;
int count = 0;
while ( count <= length-4 ) { //9164916500

if( number[result] < number[count] )
result = count;
else if ( number[result] == number[count]){
if( (number[result+1] <= number[count+1]) ||
(number[result+2] <= number[count+2]) ||
(number[result+3] < number[count+3]))
result = count;
}
count++;
}
return result;
}

0 comments:

Post a Comment