Count numbers in a range that are divisible by all array elements in C++


We are provided two numbers START and END to define a range of numbers. And also an array of positive numbers Arr[]. The goal is to find all the numbers that are divisible by all elements of Arr[] and are in the range [START,END] .

Method 1 ( Naive Approach )

We will do this by traversing numbers from START to END and for each number we will check if the number is divisible by all elements of the array. If yes increment count.

Method 2 ( check divisibility by LCM of array elements )

We will find the LCM of all array elements and then check and count all numbers in range [START,END] that are fully divisible by that LCM.

Let’s understand with examples.

Input 

START=1 END=20 Arr[]= { 2, 4, 8 }

Output 

Numbers that are divisible by all array elements: 2

Explanation 

Numbers 8 and 16 are in the range that are divisible by all array elements.

Input 

START=100 END=200 Arr[]= { 230, 321, 490, 521 }

Output 

Numbers that are divisible by all array elements: 0

Explanation 

No number between 100 to 200 divisible by any array element.

Method 1 ( Naive Approach )

Approach used in the below program is as follows

  • We take an integers START and END as range variables.

  • Function divisiblebyArr(int start, int end, int arr[], int len) takes range variables and arrays and returns the count of numbers divisible by all array elements.

  • Take the initial variable count as 0 for such numbers.

  • Take variable flag as 0

  • Traverse range of numbers using for loop. i=start to i=end

  • Now for each number num=i, using while loop check if number is divisible by all array elements.

  • If all the elements fully divide num, set flag=1.

  • Outside while if flag=1 increment count

  • At the end of all loops count will have a total number which is divisible by all elements of the array.

  • Return the count as result.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int divisiblebyArr(int start, int end, int arr[], int len){
   int count = 0;
   int flag=0;
   int index=0;
   for (int i = start; i <= end; i++){
      int num = i;
      index=0;
      while(index<len){
         if(num % arr[index++] == 0)
            { flag=1; }
         else{
            flag=0;
            break;
         }
      }
      if (flag == 1)
         { count++; }
      }
   return count;
}
int main(){
   int START = 5, END = 20;
   int Arr[] = {2,4,8 };
   int len=sizeof(Arr)/sizeof(Arr[0]);
   cout <<"Numbers that are divisible by all array elements: "<< divisiblebyArr(START,END,Arr,len);
   return 0;
}

Output

If we run the above code it will generate the following output −

Numbers that are divisible by all array elements: 2

Method 2 ( LCM Approach )

Approach used in the below program is as follows

  • We take an integers START and END as range variables.

  • Function getLCM(int a, int b) takes two numbers and returns the LCM of them by finding the first number which is divisible by both using while loop.

  • Function getLCMArray(int arr[], int n) takes an array and its length as input and returns the LCM of all the elements of the array.

  • Calculate first LCM as getLCM(arr[0], arr[1]). After that consecutively find lcm of previous lcm and arr[i] by calling getLCM(lcm, arr[i]) where i=2 to i<n.

  • Function divisiblebyArr(int start, int end, int arr[], int len) takes range variables and arrays and returns the count of numbers divisible by all array elements.

  • Take the initial variable count as 0 for such numbers.

  • Take variable lcm as getLCMArray(int arr[], int len).

  • Traverse range of numbers using for loop. i=start to i=end

  • Now for each number i, check if it is divisible lcm. If true, increment count.

  • At the end of all loops count will have a total number which is divisible by all elements of the array.

  • Return the count as result.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int getLCM(int a, int b){
   int m;
   m = (a > b) ? a : b;
   while(true){
      if(m % a == 0 && m % b == 0)
         return m;
      m++;
   }
}
int getLCMArray(int arr[], int n){
   int lcm = getLCM(arr[0], arr[1]);
   for(int i = 2; i < n; i++){
      lcm = getLCM(lcm, arr[i]);
   }
   return lcm;
}
int divisiblebyArr(int start, int end, int arr[], int len){
   int count = 0;
   int flag=0;
   int lcm=getLCMArray(arr,len);
   for (int i = start; i <= end; i++){
      if(i%lcm==0)
         { count++; }
      }
   return count;
}
int main(){
   int START = 5, END = 20;
   int Arr[] = {2,4,8 };
   int len=sizeof(Arr)/sizeof(Arr[0]);
   cout <<"Numbers that are divisible by all array elements: "<< divisiblebyArr(START,END,Arr,len);
   return 0;
}

Output

If we run the above code it will generate the following output −

Numbers that are divisible by all array elements: 2

Updated on: 31-Oct-2020

475 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements