InMobi Interview Question
Country: India
Interview Type: Written Test
For simplicity, I considered the input as an array instead of digits, however logic is same.
#include <iostream>
#include <set>
using namespace std;
int main()
{
int n[5] = { 2, 2, 2, 2, 4 };
set<int> nset;
for (int i = 0; i < 5; i++)
{
nset.insert(n[i]);
}
int sum = 0;
for (int k : nset)
sum += k;
cout << sum << endl;
}
This problem can be imagined as a permutation problem.
Observe the following. Suppose the number *n* has no of decimal digit *d*.
The problem has no solution if d > 10. You can generalise it to any base now.
Now, start with generating permutations of :
*1* out of D = [ 0, 1, 2, ...., 8, 9 ].
Then, *2* out of D ( note for leading 0s )
Then *3* out of D ( note for leading 0s )
....
Finally *d* out of D, and there, we need to find how many of them are smaller then n.
One can theoretically reach that no, given a number.
So, let's solve it for 42.
Clearly for
d = 1 -> 1-9 ( 9 ) options
d = 2 -> There are total 10 * 9 options, out of which 9 has leading zeros.
That takes the toll to 81. How many of them are larger than 42 ?
Obvious,
There are [5,6,...9] for the first digit and [0,1,...,9] for the next.
Also there are 4 for first and [3,5,...9] for second digit.
Hence, there are 5 * 9 + 6 unique digited nos greater than 42.
Hence, the total is : 9 + 81 - 51 = 39
Now, let's verify with ZoomBA:
n = 42
t = sum ( [1: n + 1 ] ) -> {
x = str($.o)
#|set( x.value )| == #|x| ? 1 : 0
}
println( t )
//specialNumbers : numbers which don't have any digit repeated.
public void init() {
ncr = new long[11][11];
fac = new long[11];
fac[0] = 1;
for (int i = 1; i < 11; ++i)
fac[i] = fac[i-1] * i;
for (int i = 0; i < 11; ++i)
for (int j = 0; j < 11; j++)
ncr[i][j] = 0;
ncr[0][0] = 1;
for (int i = 1; i < 11; ++i) {
ncr[i][0] = 1;
for (int j = 1; j <= i; ++j)
ncr[i][j] = ncr[i-1][j] + ncr[i-1][j-1];
}
}
/**
* Input and set bounds.
* Max(specialNumbers) is 9876543210. (pigeonhole principle).
*/
private String getN() {
String mx = "9876543210";
String input = IO.nextLine();
if (input.length() > mx.length())
return mx;
else if (input.length() < mx.length())
return input;
else if (input.compareTo(mx) > 0)
return mx;
else return input;
}
void solve() {
numStr = getN();
len = numStr.length();
boolean[] isAvail = new boolean[10];
Arrays.fill(isAvail, true);
long ans = count(0, isAvail, 10);
for (int i = 1; i < len; i++) {
// 'i' length specialNumbers.
ans += fac[i - 1] * ncr[9][i - 1] * 9;
}
IO.writer.println(ans);
}
/**
* count specialNumbers of length 'len' which are less than or equal to "numStr".
* Iterate "numStr" left to right, maintaining which digits we have used already.
* id : position in "numStr"
* isAvail : maintains which digits we have used
* cnt : number of digits not yet used
*/
long count(int id, boolean[] isAvail, int cnt) {
if (id == len) return 1;
long ans = 0;
int x = numStr.charAt(id) - '0';
int c = 0;
for (int i = 0; i < x; ++i) {
if (cnt == 10 && i == 0) // this case handled in solve() function
continue;
if (isAvail[i])
c++;
}
// count of specialNumbers less than "numStr"
ans = ncr[cnt - 1][len - id - 1] * fac[len - id - 1] * c;
if (isAvail[x]) {
isAvail[x] = false;
ans += count(id + 1, isAvail, cnt - 1);
isAvail[x] = true;
}
return ans;
}
Test cases:
100000000000
9876543210
1000000
999999
453
3456
1
10
102
Answers:
8877690
8877690
168570
168570
342
1939
1
10
91
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int no = scan.nextInt();
int count = 0;
for(int i=1;i<=no;i++)
{
String a = Integer.toString(i);
char[] b =a.toCharArray();
int flag = 0;
if(a.length()>1)
{
for(int j=1;j<a.length();j++)
{
//System.out.println(b[j-1]+"=="+b[j]);
if( (b[j-1]==b[j]))
{
flag =1;
// System.out.println("Has Repeated Numbers!");
break;
}
}
}
if(flag == 0)
{
count++;
//System.out.println("count:"+count+" --> "+"No:"+a);
}
}
System.out.println(count);
}
}
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int no = scan.nextInt();
int count = 0;
for(int i=1;i<=no;i++)
{
String a = Integer.toString(i);
char[] b =a.toCharArray();
int flag = 0;
if(a.length()>1)
{
for(int j=1;j<a.length();j++)
{
//System.out.println(b[j-1]+"=="+b[j]);
if( (b[j-1]==b[j]))
{
flag =1;
// System.out.println("Has Repeated Numbers!");
break;
}
}
}
if(flag == 0)
{
count++;
//System.out.println("count:"+count+" --> "+"No:"+a);
}
}
System.out.println(count);
}
}
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int no = scan.nextInt();
int count = 0;
for(int i=1;i<=no;i++)
{
String a = Integer.toString(i);
char[] b =a.toCharArray();
int flag = 0;
if(a.length()>1)
{
for(int j=1;j<a.length();j++)
{
//System.out.println(b[j-1]+"=="+b[j]);
if( (b[j-1]==b[j]))
{
flag =1;
// System.out.println("Has Repeated Numbers!");
break;
}
}
}
if(flag == 0)
{
count++;
//System.out.println("count:"+count+" --> "+"No:"+a);
}
}
System.out.println(count);
}
}
- srterpe December 05, 2016