
- 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 the number of elements in an array which are 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
- Related Articles
- Product of all the elements in an array divisible by a given number K in C++
- Find elements of an array which are divisible by N using STL in C++
- Maximize the number of sum pairs which are divisible by K 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 all prefixes of the given binary array which are divisible by x in C++
- Count elements that are divisible by at-least one element in another array in C++
- JavaScript Count the number of unique elements in an array of objects by an object property?
- Elements of an array that are not divisible by any element of another array in C++
- Count number of elements in an array with MongoDB?
- Program to count number of trailing zeros of minimum number x which is divisible by all values from 1 to k in Python
- Count subarrays whose product is divisible by k in C++
- Count divisible pairs in an array in C++
- Count all pairs of an array which differ in K bits in C++
- Count number of even and odd elements in an array in C++
