Breaking

Friday 18 August 2017

Count of common factors

                  Count of common factors


                                    


Given a set of numbers, the program must find the count of the common factors C excluding 1.

Input Format:
First line will contain the integer value N representing how many numbers are passed as input.
Next N lines will have the numbers.

Output Format:
First line will contain the count of common factors C.Constraints:N will be from 2 to 20.

Sample Input/Output:

Example 1:
Input:
2
100
75
Output:
2
Explanation:
The common factors excluding 1 are 5,25. Hence output is 2

Example 2: 
Input:
3
10
20
30

Output:
3

Explanation:The common factors excluding 1 are 2,5,10. Hence output is 3.

Code:

  1. import java.util.*;
  2. public class Hello {
  3.     public static void main(String[] args) {
  4. Scanner sc=new Scanner(System.in);
  5. int n=sc.nextInt();
  6. int i,j,flag=0,gcd=0;
  7. int[] arr=new int[n];
  8.    for(i=0;i<n;i++)
  9.        arr[i]=sc.nextInt();
  10.  
  11.    Arrays.sort(arr);
  12.    int small=arr[n-1];
  13.    for(i=small;i>1;i--)
  14.        {
  15.            for(j=0;j<n;j++)
  16.            {
  17.                if(arr[j]%i==0)
  18.                      {  flag=1;
  19.                     // System.out.print(arr[j]+" "+i+"\n");
  20.                      }
  21.                   else
  22.                        break;
  23.            }
  24.            if(flag==1)
  25.                gcd++;
  26.           flag=0;
  27.        }
  28.    System.out.print(gcd);
  29. }
  30. }

No comments:

Post a Comment

Like