Concepts Used:

Mathematics

Difficulty Level:

Medium

Problem Statement (Simplified):

For a given array of integers print the total number of integers which divides all the array elements and are less than or equal to the first element.

See original problem statement here

Test Case:

Input:
1
5
8 16 24 32 40

Output:
4

Explanation:
Total values below or equal to 8 are [1,2,3,4,5,6,7,8],
From these numbers 1,2,4, and 8 divides whole array elements completely. 
These are total 4 numbers, so 4 is our answer.

Solving Approach:

1) We iterate from 1 to the first element (arr[0]) of the array and count all the numbers which divide all the array elements.
2) We refer the best online programming courses and print the count of total such numbers after all iterations are completed.

Example:

1) Let array A be [8,12,14,16,24,36]
2) As 8 is our first element, so we'll check for all elements from 1 to 8.
3) In this range, only 1, and 2 divides all the elements, so 2 is our answer.

Solutions:

#include <stdio.h>

int main()
{
  int test;
  scanf("%d", &test);

  while(test--){

    int n;
    scanf("%d", &n);

    int hero[n];
    for(int i=0; i<n; i++)
      scanf("%d", &hero[i]);

    int i, count=0;
    for(i = hero[0]; i>1; i--){
      if(hero[0]%i==0){
        int flag = 1;
        for(int j=1; j<n; j++){
          if(hero[j]%i!=0){
            flag = 0;
            break;
          }
        }
        if(flag)
          count++;
      }
    }
    printf("%d\n", count+1);
  }
  return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main()
{
  int test;
  cin>>test;

  while(test--){

    int n;
    scanf("%d", &n);

    int hero[n];
    for(int i=0; i<n; i++)
      cin>>hero[i];

    int i, count=0;
    for(i = hero[0]; i>1; i--){
      if(hero[0]%i==0){
        int flag = 1;
        for(int j=1; j<n; j++){
          if(hero[j]%i!=0){
            flag = 0;
            break;
          }
        }
        if(flag)
          count++;
      }
    }
    cout<<count+1<<endl;
  }
  return 0;
}
import java.util.*;
import java.io.*;

public class Main {
  public static void main(String args[]) throws IOException {

    Scanner sc = new Scanner(System.in);

    int test = sc.nextInt();

    while(test!=0){

      int n = sc.nextInt();

      int hero[] = new int[n];
      for(int i=0; i<n; i++)
        hero[i] = sc.nextInt();

      int i, count=0;
      for(i = hero[0]; i>1; i--){
        if(hero[0]%i==0){
          int flag = 1;
          for(int j=1; j<n; j++){
            if(hero[j]%i!=0){
              flag = 0;
              break;
            }
          }
          if(flag==1)
            count++;
        }
      }
      System.out.println(count+1);
      test--;
    }

  }
}
Previous post Prime Numbers
Next post FRACTION1

Leave a Reply

Your email address will not be published. Required fields are marked *