- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We are given with an array of positive integer numbers and an integer variable k. The task is to calculate the count of the number of elements in an array which is divisible by the given value k.

**Input** − int arr[] = {4, 2, 6, 1, 3, 8, 10, 9}, k = 2

**Output** − Count the number of elements in an array which are divisible by 2 are − 5

**Explanation** − we will divide the elements in an array by a value k and check whether the reminder is 0 or not. So, 4 is divisible by 2, 2 is divisible by 2, 6 is divisible by 2, 1 isn’t divisible by 2, 3 isn’t divisible by 2, 8 is divisible by 2, 10 is divisible by 2, 9 isn’t divisible by 2. So, there are 5 elements in an array which are completely divisible by k i.e. 2.

**Input** − int arr[] = {3, 2, 9, 15, 0, 8, 10}, k = 3

**Output** − Count the number of elements in an array which are divisible by 3 are − 3

**Explanation** − we will divide the elements in an array by a value k and check whether the reminder is 0 or not. So, 3 is divisible by 3, 2 isn’t divisible by 3, 9 is divisible by 3, 15 is divisible by 3, 0 isn’t divisible by any number, 8 isn’t divisible by 3, 10 isn’t divisible by 3. So, there are 3 elements in an array which are completely divisible by k i.e. 23

There can be multiple approaches to solve a particular problem. So firstly we will go with a naive approach.

Input an array of integer elements and an integer variable k

Calculate the length of an array and pass the data to the function for further processing.

Take a temporary variable count to store the count of elements divisible by k

Start loop FOR from 0 till the length of an array

Inside the loop, check IF arr[i] % k = 0 then increment the count by 1

Return the count

Print the result.

Input elements in a vector of integer type and take an integer variable k.

Take a temporary variable count to store the count of elements divisible by k

Set the count as the call to an inbuilt count_if() function that will take vector.begin(), vector.end() as an argument and start the traversal then return i%k if 0.

Print the result.

#include <bits/stdc++.h> using namespace std; int divisible_k(int arr[], int size, int k){ int count = 0; for(int i = 0; i<size; i++){ if(arr[i]%k==0){ count++; } } return count; } int main(){ int arr[] = {4, 2, 6, 1, 3, 8, 10, 9}; int k = 2; int size = sizeof(arr) / sizeof(arr[0]); cout<<"Count the number of elements in an array which are divisible by "<<k<<" are: "<<divisible_k(arr, size, k); return 0; }

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

Count the number of elements in an array which are divisible by 2 are: 5

#include <bits/stdc++.h> using namespace std; int main(){ vector<int> vec = {4, 2, 6, 1, 3, 8, 10, 9}; int count = count_if(vec.begin(), vec.end(), [](int i, int k = 2) { return i % k == 0; }); cout<<"Count the number of elements in an array which are divisible by k are: "<<count; return 0; }

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

Count the number of elements in an array which are divisible by 2 are: 5

- Related Questions & Answers
- Maximize the number of sum pairs which are divisible by K in C++
- Find elements of an array which are divisible by N using STL in C++
- Product of all the elements in an array divisible by a given number K in C++
- Count all prefixes of the given binary array which are divisible by x in C++
- Elements of an array that are not divisible by any element of another array in C++
- Count numbers in a range that are divisible by all array elements in C++
- Count pairs in array whose sum is divisible by K in C++
- Count elements that are divisible by at-least one element in another array in C++
- Count all pairs of an array which differ in K bits in C++
- Largest K digit number divisible by X in C++
- Count subarrays whose product is divisible by k in C++
- Count numbers which are divisible by all the numbers from 2 to 10 in C++
- JavaScript Count the number of unique elements in an array of objects by an object property?
- Count divisible pairs in an array in C++
- Find an array element such that all elements are divisible by it using c++

Advertisements