Count the number of clumps in the given Array
Last Updated :
24 Mar, 2021
Given an array arr[] of N integers, the task is to count the number of clumps in the given array.
Clump is defined as a series of 2 or more adjacent elements of the same value.
Examples:
Input: arr[] = { 13, 15, 66, 66, 37, 8, 8, 11, 52 };
Output: 2
Explanation:
There are two clumps in the given array {66, 66} and {8, 8}.
Input: arr[] = {1, 2, 1, 4, 3, 2}
Output: 0
Explanation:
There are no clumps in the given array.
Approach: In order to solve the problem, we need to follow the following steps:
- Traverse through the array and check for any occurrence of same element on two consecutive indices.
- For any such occurrence, loop until a different number occurs.
- Increase the count of clumps by 1 only after execution of step 2. If the entire array isn’t traversed yet, repeat the above steps for the following elements.
- Print the final count of clumps after entire array traversal.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countClumps( int arr[], int N)
{
int clumps = 0;
for ( int i = 0; i < N - 1; i++) {
int flag = 0;
while (arr[i] == arr[i + 1]) {
flag = 1;
i++;
}
if (flag)
clumps++;
}
return clumps;
}
int main()
{
int arr[] = { 13, 15, 66, 66, 66, 37, 37,
8, 8, 11, 11 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << countClumps(arr, N) << '\n' ;
return 0;
}
|
Java
class Test {
static int arr[] = { 13 , 15 , 66 , 66 , 66 ,
37 , 37 , 8 , 8 , 11 , 11 };
static int countClumps()
{
int l = arr.length;
int clumps = 0 ;
for ( int i = 0 ; i < l - 1 ; i++) {
int flag = 0 ;
while (i < l - 1
&& arr[i] == arr[i + 1 ]) {
flag = 1 ;
i++;
}
if (flag == 1 )
clumps++;
}
return clumps;
}
public static void main(String[] args)
{
System.out.println(countClumps());
}
}
|
Python3
def countClumps(arr, N):
clumps = 0
i = 0
while (i < N - 1 ):
flag = 0
while (i + 1 < N and
arr[i] = = arr[i + 1 ]):
flag = 1
i + = 1
if (flag):
clumps + = 1
i + = 1
return clumps
arr = [ 13 , 15 , 66 , 66 , 66 ,
37 , 37 , 8 , 8 , 11 , 11 ]
N = len (arr)
print (countClumps(arr, N))
|
C#
using System;
class GFG{
static int []arr = { 13, 15, 66, 66, 66,
37, 37, 8, 8, 11, 11 };
static int countClumps()
{
int l = arr.Length;
int clumps = 0;
for ( int i = 0; i < l - 1; i++)
{
int flag = 0;
while (i < l - 1 && arr[i] == arr[i + 1])
{
flag = 1;
i++;
}
if (flag == 1)
clumps++;
}
return clumps;
}
public static void Main()
{
Console.WriteLine(countClumps());
}
}
|
Javascript
<script>
function countClumps(arr, N)
{
let clumps = 0;
for (let i = 0; i < N - 1; i++) {
let flag = 0;
while (arr[i] == arr[i + 1]) {
flag = 1;
i++;
}
if (flag)
clumps++;
}
return clumps;
}
let arr = [ 13, 15, 66, 66, 66, 37, 37, 8, 8, 11, 11 ];
let N = arr.length;
document.write(countClumps(arr, N));
</script>
|
Time Complexity: O(N), where N is the number of elements in the given array.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...