Imagination Technologies Interview Question
Graphics ProgrammersCountry: India
Interview Type: Phone Interview
that is the correct answer:
swap the first occurence of 01 with 10 for the next higher
swap the first occurence of 10 with 01 for the next lower
while taking into account the max int and lowest boundary conditions.
for(int i = n-1 ; i >= -n ; i--){
if( length == CountNumberOfOnes(i)){
cout << "Lower value with same number of 0's :" << i << endl;
break;
}
}
for(int i = n+1 ; i <= (n*10) ; i++){
if( length == CountNumberOfOnes(i)){
cout << "Higher value with same number of 0's :" << i << endl;
break;
}
}
Simply swap the first occurrence of the 01 pair or 10 pair, and you will reach a step up or step down integer. Code should look something like:
- Anonymous August 14, 2012