
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
#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
#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
- Related Articles
- Count numbers in range that are divisible by all of its non-zero digits in C++
- Count elements that are divisible by at-least one element in another array in C++
- Count the numbers divisible by ‘M’ in a given range in C++
- Count numbers which are divisible by all the numbers from 2 to 10 in C++
- Find an array element such that all elements are divisible by it using c++
- Count numbers in range 1 to N which are divisible by X but not by Y in C++
- Python Program to Print all Numbers in a Range Divisible by a Given Number
- Golang Program to Print all Numbers in a Range Divisible by a Given Number
- Elements to be added so that all elements of a range are present in array in C++
- Count the number of elements in an array which are divisible by k in C++
- Finding the count of numbers divisible by a number within a range using JavaScript
- Print array elements that are divisible by at-least one other in C++
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3 in C++
- How to find numbers that are divisible by a certain number for a range of values in R?
- Count all prefixes of the given binary array which are divisible by x in C++
