MAQ Interview Question
Java DevelopersCountry: India
Interview Type: Written Test
incomplete question. how about the 10th digit ?? should it describe the number of occurrences of the number 10 ?? or 0 ?? ... stupid
I took it as this:
13432 => 12110
number has:
1 '4'
2 '3'
1 '2'
1 '1'
0 '0'
public static int totalDigitNumber(int n)
{
System.out.print(n);
//get lowest sig fig, incrament place, and divid by 10. loop till 0.
boolean isNegative = false;
if(n<0)
{
isNegative = true;
n*=-1;
}
int totalDigit = 0;
for(; n>0; n/=10)
{
int lsd = n%10;
totalDigit+=Math.pow(10,lsd);
}
if(isNegative)
totalDigit*=-1;
System.out.println(" => " + totalDigit);
return totalDigit;
}
{cannot be formed: because it may lead to infinite loop with repeating (10 digit) number. I n the code below I considered array of size 10 instead of 10 digit number}
void generate(){
int a[]={9,0,0,0,0,0,0,0,0,0};
int k=0;
while(true)
{
int b[]={0,0,0,0,0,0,0,0,0,0};
for(int i=0;i<10;i++)
{
// System.out.println(a[i]+"---"+b[a[i]]);
b[a[i]]=b[a[i]]+1;
}
for(int i=0;i<10;i++)
{
a[i]=b[i];
System.out.print(a[i]+" ");
}
System.out.println();
k++;
}
}
#include<stdio.h>
void toarray(int *p, unsigned long i);
int main(){
unsigned long i;
int a[9];
int count;
for(i = 100000000; i <= 999999999; i++){
int k;
toarray(a, i);
for(k = 0; k < 9; k++){
int j;
count = 0;
for(j = 0; j < 9; j++){
if((k + 1)%9 == a[j]){
count++;
}
}
if(count != a[k]){
break;
}
}
if(k == 9 && count == a[k-1]){
printf("%lu\n", i);
}
}
}
void toarray(int *p, unsigned long i){
int j;
int k = 10;
for(j = 8; j >= 0; j--){
p[j] = i % k;
i = i / k;
}
}
ans : 210010005
Checked it for 4 - 9 digit numbers .. above code is for a 9 digit number .
ps: it is a brute force method.
2100010006
- Shubham November 08, 2013