Count the number of elements in an array which are divisible by k - GeeksforGeeks

23 Sep, 2021
Given an array of integers. The task is to calculate the count of a number of elements which are divisible by a given number k.
Examples: 
Input: arr[] = { 2, 6, 7, 12, 14, 18 }, k = 3
Output: 3
Numbers which are divisible by k are { 6, 12, 18 }

Input: arr[] = { 2, 6, 7, 12, 14, 18 }, k = 2
Output: 5
Method-1: Start traversing the array and check if the current element is divisible by K. If yes then increment the count. Print the count when all the elements get traversed.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to Count the number of elements// in an array which are divisible by k#include <iostream>using namespace std; // Function to count the elementsint CountTheElements(int arr[], int n, int k){    int counter = 0;     for (int i = 0; i < n; i++) {        if (arr[i] % k == 0)            counter++;    }     return counter;} // Driver codeint main(){    int arr[] = { 2, 6, 7, 12, 14, 18 };    int n = sizeof(arr) / sizeof(arr[0]);    int k = 3;     cout << CountTheElements(arr, n, k);     return 0;}
// Java program to Count the number of elements// in an array which are divisible by kimport java.util.*; class Geeks {  // Function to count the elementsstatic int CountTheElements(int arr[], int n, int k){    int counter = 0;     for (int i = 0; i < n; i++) {        if (arr[i] % k == 0)            counter++;    }     return counter;} // Driver codepublic static void main(String args[]){    int arr[] = { 2, 6, 7, 12, 14, 18 };    int n = arr.length;    int k = 3;     System.out.println(CountTheElements(arr, n, k));}} // This code is contributed by ankita_saini
# Python 3 program to Count the# number of elements in an array# which are divisible by k # Function to count the elementsdef CountTheElements(arr, n, k):    counter = 0     for i in range(0, n, 1):        if (arr[i] % k == 0):            counter = counter+1         return counter # Driver codeif __name__ == '__main__':    arr = [2, 6, 7, 12, 14, 18];    n = len(arr)    k = 3     print(CountTheElements(arr, n, k)) # This code is contributed by# Surendra_Gangwar
// C# program to Count the number of elements// in an array which are divisible by kusing System; class Geeks {  // Function to count the elementsstatic int CountTheElements(int []arr, int n, int k){    int counter = 0;     for (int i = 0; i < n; i++) {        if (arr[i] % k == 0)            counter++;    }     return counter;} // Driver codepublic static void Main(){    int []arr = { 2, 6, 7, 12, 14, 18 };    int n = arr.Length;    int k = 3;     Console.WriteLine(CountTheElements(arr, n, k));}}//This code is contributed by inder_verma..
<?php// PHP program to Count the number of elements// in an array which are divisible by k // Function to count the elementsfunction CountTheElements($arr, $n, $k){    $counter = 0;     for ($i = 0; $i < $n; $i++)    {        if ($arr[$i] % $k == 0)            $counter++;    }     return $counter;} // Driver code$arr = array( 2, 6, 7, 12, 14, 18 );$n = count($arr);$k = 3; echo CountTheElements($arr, $n, $k); // This code is contributed by inder_verma?>
<script> // Javascript program to Count the number// of elements in an array which are// divisible by k // Function to count the elementsfunction CountTheElements(arr, n, k){    let counter = 0;     for(let i = 0; i < n; i++)    {        if (arr[i] % k == 0)            counter++;    }     return counter;} // Driver codelet arr = [ 2, 6, 7, 12, 14, 18 ];let n = arr.length;let k = 3; document.write(CountTheElements(arr, n, k)); // This code is contributed by subhammahato348 </script>
3
 
Method-2: Another way to do that is to use inbuilt function – Count_if . This functions takes in pointers to beginning and ending of the container containing the elements and auxiliary function. It will return the count of elements for which the function will return true. 
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ implementation of the above approach#include <bits/stdc++.h>using namespace std;int main(){    vector<int> v{ 2, 6, 7, 12, 14, 18 };     // Count the number elements which when passed    // through the function (3rd argument) returns true    int res = count_if(v.begin(), v.end(),                       [](int i, int k = 3) { return i % k == 0; });    cout << res;     return 0;}
// Java implementation of the above approachimport java.util.*; class GFG{public static void main(String[] args){   int []v = { 2, 6, 7, 12, 14, 18 };     // Count the number elements which when passed    // through the function (3rd argument) returns true    int res = 0;    for(int i = 0; i < v.length; i++) {        if(v[i] % 3 == 0)            res++;    }     System.out.print(res); }} // This code is contributed by gauravrajput1
# Java implementation of the above approachv = [ 2, 6, 7, 12, 14, 18 ]; # Count the number elements which when passed# through the function (3rd argument) returns trueres = 0for i in range(0,len(v)):    if(v[i] % 3 == 0):        res+=1     print(res)# This code is contributed by shivanisinghss2110
// C# implementation of the above approachusing System; class GFG{public static void Main(String[] args){   int []v = { 2, 6, 7, 12, 14, 18 };     // Count the number elements which when passed    // through the function (3rd argument) returns true    int res = 0;    for(int i = 0; i < v.Length; i++) {        if(v[i] % 3 == 0)            res++;    }     Console.Write(res); }} // This code is contributed by shivanisinghss2110
<script> // JavaScript implementation of the above approachvar v = [ 2, 6, 7, 12, 14, 18 ];     // Count the number elements which when passed    // through the function (3rd argument) returns true    var res = 0;    for(var i = 0; i < v.length; i++) {        if(v[i] % 3 == 0)            res++;    }     document.write(res); // This code is contributed by shivanisinghss2110 </script>
3
 
Time Complexity: O(N) 
ankita_saini
SURENDRA_GANGWAR
inderDuMCA
subhammahato348
GauravRajput1
shivanisinghss2110
cpp-vector
Technical Scripter 2018
Arrays
School Programming
Technical Scripter
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Stack Data Structure (Introduction and Program)
Introduction to Arrays
Multidimensional Arrays in Java
Python Dictionary
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects
Interfaces in Java