Search This Blog

Sunday, April 27, 2014

Algorithms - Math - Find Digits

Find Digits

The following is the solution to Hacker Rank problem Find Digits using Java.  For solutions to other Hacker Rank Problem visit my page HackerRank, alternatively try searching for the problem in my blog.

The Approach I'm following is given below:
  1. The digit 1 always divides the number so we can increase the count to 1 always.
  2. All other digits should exactly divide the number, so it should have a remainder 0. So if remainder is zero increasing the count and 
  3. also we should note that the digit should not be zero.
Score: 5/5
import java.util.Scanner;

/**
 * @author Arun.G
 *
 */

public class Solution
    {
    public static int findDigits(int num)
        {
        int res=0;
        int number = num;
        String numbers = String.valueOf(num);
      
       
        for(int i=0;i<numbers.length();i++)
            {
            int digit = Integer.parseInt(Character.toString(numbers.charAt(i)));
            if(digit==1)
                res++;
            else if(digit>0 && number%digit==0)
                res++;
        }
        return res;
    }
    public static void main(String[] args)
        {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for(int i=0;i<T;i++)
            {
            int num = sc.nextInt();
            System.out.println(findDigits(num));
              
        }
        sc.close();
    }
}


your Comments are welcome !

No comments:

Post a Comment

Labels