Amazon Interview Question
SDE1sCountry: India
Interview Type: In-Person
I think it can be even optimised, as when you get the binary represantation of a number you can convert it to int and then multiply by 9. You'll get:
1 => 9
10 => 90
11 => 99
100 => 900
101 => 909
110 => 990
etc.
So it will still check every possible number, brute forced. I don't see any better way of doing it either tho.
#include<stdio.h>
unsigned int ninezero(unsigned int num1,int divNum)
{
if ( num1% divNum == 0 )
return num1;
while(1)
{
num1=num1*10+9;
if (num1 % divNum == 0)
break;
num1/=10;
num1=num1*10+0;
if (num1 % divNum ==0 )
break;
}
return num1;
}
int main()
{
unsigned int num1=9;
int divNum;
printf("enter a number");
scanf("%d",&divNum);
printf("the number is %d",ninezero(num1,divNum));
return 0;
}
Not a good solution, taking time for large input, but it's working. Any improvements in the code or new solution are most welcome.
{
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
class FindDivisible
{
public static void main(String wat[])
{
int num=Integer.parseInt(wat[0]);
boolean flag=true;
Pattern pat=Pattern.compile("[1-8]+");
Matcher mat;
/* if(9%num==0)
{
System.out.println("Result is : 9");
flag=false;
}*/
long sum=num;
String sumInString;
while(flag)
{
try
{
sumInString=Long.toString(sum);
mat=pat.matcher(sumInString);
if(!mat.find())
{
char [] tempChar=sumInString.toCharArray();
if((tempChar[0]=='9')&&(tempChar[tempChar.length-1]=='0'||tempChar[tempChar.length-1]=='9'))
{
System.out.println("Result is :"+sumInString);
flag=false;
}
}
sum+=num;
}
catch(Exception ex){ break;}
}
}
}
}
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int nine_zero(int n)
{
vector<int> values;
values.push_back(9%n);
int index = 0;
while ( values[index] != 0 ) {
index++;
cout << " " << index << endl;
if ( index%2 ) {
values.push_back((values[(index-1)/2]*10)%n);
} else {
values.push_back((values[index-1]+9)%n);
}
}
// output index
index++;
string output;
while (index != 0 ) {
output = ((index%2)==1? '9' :'0') + output;
index /=2;
}
cout << "Res:" << output << endl;
return 0;
}
I haven't put any error handling.
public void FindDivisor (int inputnumber,int firstnumber,int secondnumber)
// takes the input number and the two numbers that an be used to for the divisor number
{
int number=1;
int divisor=divisornumber(number,firstnumber,secondnumber);
while (divisor%inputnumber!=0)
{ number++;
divisor=divisornumber(number,firstnumber,secondnumber);
}
System.out.println(" The Dividor is " +divisor);
}
public String ReturnBinaryString(int number) {
String S="";
if (number==0)
return "0";
else if (number ==1)
return "1";
else return (S.concat(ReturnBinaryString(number/2)) +(number%2));
}
public int divisornumber(int number,int firstnumber,int secondnumber)
{
String bibaryString=ReturnBinaryString(number);
// convert the in to String
String firststring=""+firstnumber;
String secondstring=""+secondnumber;
//replace all the 0's with first number and 1's with second number
bibaryString=bibaryString.replaceAll("0", firststring);
bibaryString=bibaryString.replaceAll("1",secondstring);
//covert the number to integer back
return (Integer.parseInt(bibaryString));
}
I haven't put any error handling.
public void FindDivisor (int inputnumber,int firstnumber,int secondnumber)
// takes the input number and the two numbers that an be used to for the divisor number
{
int number=1;
int divisor=divisornumber(number,firstnumber,secondnumber);
while (divisor%inputnumber!=0)
{ number++;
divisor=divisornumber(number,firstnumber,secondnumber);
}
System.out.println(" The Dividor is " +divisor);
}
public String ReturnBinaryString(int number) {
String S="";
if (number==0)
return "0";
else if (number ==1)
return "1";
else return (S.concat(ReturnBinaryString(number/2)) +(number%2));
}
public int divisornumber(int number,int firstnumber,int secondnumber)
{
String bibaryString=ReturnBinaryString(number);
// convert the in to String
String firststring=""+firstnumber;
String secondstring=""+secondnumber;
//replace all the 0's with first number and 1's with second number
bibaryString=bibaryString.replaceAll("0", firststring);
bibaryString=bibaryString.replaceAll("1",secondstring);
//covert the number to integer back
return (Integer.parseInt(bibaryString));
}
static ArrayList<Integer> NextNumber(ArrayList<Integer> numArray)
{
ArrayList<Integer> newNum = new ArrayList<Integer>();
for(Integer i : numArray)
{
if(i != 0)
{
newNum.add(Integer.parseInt(i.toString() + "0"));
newNum.add(Integer.parseInt(i.toString() + "9"));
}
if(i == 0)
newNum.add(Integer.parseInt(i.toString() + "9"));
}
return newNum;
}
static int smallistDivisiable(int div)
{
int start =0;
ArrayList<Integer> num = new ArrayList<Integer>();
num.add(start);
while(true)
{
num = NextNumber(num);
for(int i: num)
{
if((i%div) == 0)
{
System.out.println(i);
return i;
}
}
}
}
I did it by using no strings through memoization. However it is limited and the code can be improved through better calculations of its own limits, but this is a rough idea:
bool FindMinNineZero(int g) {
if(g == 0){
printf("Num: %d\n", 0);
}
if(g == 1 || g==3){
printf("Num: %d\n", 9);
}
static const int DIGIT_LIMIT = 10;
static const int MAX_NUMS = 1000;
long mem[MAX_NUMS];
mem[0] = 0;
mem[1] = 9;
int upperBound = 2;
bool found = false;
int acc = 9;
for(int i=2; i<DIGIT_LIMIT; ++i){
acc*=10;
int phaseBound = upperBound;
for(int j=1; j<=phaseBound; ++j){
if(upperBound > MAX_NUMS) {
return false;
}
mem[upperBound] = acc + mem[j-1];
printf("new num = %d\n", mem[upperBound]);
if(mem[upperBound]%g==0) {
printf("Found: %d\n", mem[upperBound]);
found = true;
break;
}
upperBound++;
}
if(found)
break;
}
return found;
}
Elegant and fast way - to regard 9 as '1' bit and 0 - as '0' bit. In this case we just iteravely increase binary number and check that translated version (from binary '10..' to '90..') is divisable by the provided number
- alisovenko September 22, 2014