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++
#include <iostream>
using namespace std;
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;
}
int 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
import java.util.*;
class Geeks {
static 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;
}
public 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));
}
}
|
Python3
def CountTheElements(arr, n, k):
counter = 0
for i in range ( 0 , n, 1 ):
if (arr[i] % k = = 0 ):
counter = counter + 1
return counter
if __name__ = = '__main__' :
arr = [ 2 , 6 , 7 , 12 , 14 , 18 ];
n = len (arr)
k = 3
print (CountTheElements(arr, n, k))
|
C#
using System;
class Geeks {
static 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;
}
public static void Main()
{
int []arr = { 2, 6, 7, 12, 14, 18 };
int n = arr.Length;
int k = 3;
Console.WriteLine(CountTheElements(arr, n, k));
}
}
|
PHP
<?php
function CountTheElements( $arr , $n , $k )
{
$counter = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $arr [ $i ] % $k == 0)
$counter ++;
}
return $counter ;
}
$arr = array ( 2, 6, 7, 12, 14, 18 );
$n = count ( $arr );
$k = 3;
echo CountTheElements( $arr , $n , $k );
?>
|
Javascript
<script>
function CountTheElements(arr, n, k)
{
let counter = 0;
for (let i = 0; i < n; i++)
{
if (arr[i] % k == 0)
counter++;
}
return counter;
}
let arr = [ 2, 6, 7, 12, 14, 18 ];
let n = arr.length;
let k = 3;
document.write(CountTheElements(arr, n, k));
</script>
|
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(1)
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++
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector< int > v{ 2, 6, 7, 12, 14, 18 };
int res = count_if(v.begin(), v.end(),
[]( int i, int k = 3) { return i % k == 0; });
cout << res;
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static void main(String[] args)
{
int []v = { 2 , 6 , 7 , 12 , 14 , 18 };
int res = 0 ;
for ( int i = 0 ; i < v.length; i++) {
if (v[i] % 3 == 0 )
res++;
}
System.out.print(res);
}
}
|
Python3
v = [ 2 , 6 , 7 , 12 , 14 , 18 ];
res = 0
for i in range ( 0 , len (v)):
if (v[i] % 3 = = 0 ):
res + = 1
print (res)
|
C#
using System;
class GFG
{
public static void Main(String[] args)
{
int []v = { 2, 6, 7, 12, 14, 18 };
int res = 0;
for ( int i = 0; i < v.Length; i++) {
if (v[i] % 3 == 0)
res++;
}
Console.Write(res);
}
}
|
Javascript
<script>
var v = [ 2, 6, 7, 12, 14, 18 ];
var res = 0;
for ( var i = 0; i < v.length; i++) {
if (v[i] % 3 == 0)
res++;
}
document.write(res);
</script>
|
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(1)
Please suggest if someone has a better solution which is more efficient in terms of space and time.
This article is contributed by Aarti_Rathi.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!