Thứ Bảy, 7 tháng 2, 2015

Amazon coding round question for fresher

Question

Given a number A, find the smallest number which has only 1s and 0s as its digits which divisible by the number A. For example: if the given number A is 4, the smallest number with 1s and 0s is which is divisible by 4 is 100.

My solution stratagy:

Am gona take a maximum 8 bit digit to stop my checking process(You may get result between these numbers surely).get remainder for each element and check for individual digits ,if these digits were 0 or 1 proceed further else stop algorithm.




FindNumber.java

package com.shashi.carrercup;

/**
 *
 * @author shashi
 */
public class FindNumber {
   
    public static void main(String args[])
    {
        FindNumber n=new FindNumber();
        System.out.print(n.getNumber(4));
    }
    public  int getNumber(int number)
    {
        //check for invalid condition
        if(number<0)
            return 0;
        //if not do some operations
        //let take maximum 8 bit digit
        int max=11111111;
         int current;
         int c1=0;
        for(int i=2;i<max;i++)
        {
            //take the current number
            boolean check=false;
             current=number*i;
            //copy number
            c1=current;
            while(current>0)
            {
                int rem=current%10;
                if(rem == 0 || rem == 1 )
                {
                    current=current/10;
                    if(current == 0)
                    {
                        check=true;
                        break;
                    }
                }
                else
                {
                    break;
                }
            }
            //you got first min if check is true
            if(check)
                break;
        }
         return c1;
    }  
 }
 

result;
-------------
100


 

Không có nhận xét nào:

Đăng nhận xét