
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Count Elements in Array Divisible by K in C++
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
Approach used in the below program is as follows
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.
Efficient Approach
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.
Example (naive approach)
#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; }
Output
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
Example (Efficient Approach)
#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; }
Output
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