Amazon Interview Question
InternsCountry: India
Interview Type: Written Test
Below is a recursive approach which will print the number of 2's from 0-n. Please execute the code and correct me if I am wrong
public class Count_2{
static int count_2 =0;
public static void main(String[] args){
int num=20;
for(int i=0; i<=num; i++){
count_2 = count2_recur(i);
}
System.out.println("Count of 2 is:"+count_2);
}
public static int count2_recur(int num){
if(num == 2 ){ count_2++;}
else{
int a = num % 10;
if(a==2){
count_2++;
}
num = num/10;
if(num > 0 ){
count2_recur(num);
}
else{
return count_2;
}
}
return count_2;
}
}
Im not sure what the WAP means for this question. Are you just trying to count how many times the number can be divided by 2? That seems too easy
How about this?
You can prepare a Known list till the max range,
1-10 has 1 occurrence
10-100 has (1*10 + 9) 19
100-1000 has (19*10 + 99) 289
1000-10000 (289*10 + 999) 3889 and so on
n=43
#Will have a limitation of n upto 99999
known_range={1:0,10:1,100:19,1000:289,10000:3889}
base=1
numoftwos=0
while(n>1):
val=n%10
for i in range(0,val):
numoftwos += known_range[base]
if i==2:
numoftwos += base
base *= 10
n = n/10
print "n has %d of 2's"%numoftwos
Use a recursion algorithm
int recurse(int number){
if(number < 2){
return 0;
}
if(number > 2 && number < 12){
return 1;
}
int n = 0;
int temp = number;
int power = 1;
while(temp > 0){
temp = temp/10;
power = power * 10;
}
power = power /10;
int quotient = number/power;
if(quotient < 2 ){
return quotient * recurse(power - 1) + recurse(number%power);
}
if(quotient == 2){
return quotient * recurse(power - 1) + number%power + 1;
}
if(quotient > 2){
return (quotient-1)*recurse(power - 1) + (power) + recurse(number%power);
}
}
// i think its a cheap method...but it'll work efficiently...
#include<iostream.h>
void find(int b)
{ int i,j,r,c=0;
for(i=0;i<=b;i++)
{ j=i;
while(1)
{ r=j%10; j=j/10;
if(r==2) c++;
if(j/10==0 & j%10==2)
{ c++; break ; }
else if(j/10==0) break;
}
}
cout<<endl<<"The number of twos are: "; cout<<c;
}
int main()
{ int b; char z;
while(1)
{ cout<<"Enter the no.: "; cin>>b; find(b); cout<<endl;
cout<<endl<<"Do u want 2 find for another no ?(y/n) : "; cin>>z;
if(z=='y' || z=='Y')
{}
else
break ;
}
return(0);
}
import java.util.Scanner;
public class NumtoAlpha {
public NumtoAlpha() {
}
public static void main (String[] args) {
int ipn,n,t,s,r=0;
String c;
char ch;
String str,rev;
str="";
rev="";
Scanner in= new Scanner(System.in);
ipn=in.nextInt();
n=ipn;
while(n>0)
{
t=(n-1)%26;
n=(n-1)/26;
t=65+t;
ch=(char)t;
str= str+ch;
}
s=str.length();
for(int i=s-1;i>=0;i--)
{
rev=rev+str.charAt(i);
}
System.out.print(rev);
}
}
f(513) = 5 * f(99) + f(13) + 100 {Remainder: 13, 100 for 200 - 299}
- Adnan Ahmad August 01, 2013f(279) = 2 * f(99) + f(79) + 79 + 1 {Remainder: 79, 1 for 200}
public static int count2sR(int n) {
// Base case
if (n == 0) return 0;
// 513 into 5 * 100 + 13. [Power = 100; First = 5; Remainder = 13]
int power = 1;
while (10 * power < n) power *= 10;
int first = n / power;
int remainder = n % power;
// Counts 2s from first digit
int nTwosFirst = 0;
if (first > 2) nTwosFirst += power;
else if (first == 2) nTwosFirst += remainder + 1;
// Count 2s from all other digits
int nTwosOther = first * count2sR(power - 1) + count2sR(remainder);
return nTwosFirst + nTwosOther;
}