The problem is about finding the sum of all repeating groups from an integer array as explained below and here.
Problem statement:
Say that a "clump" in an array is a series of 2 or more adjacent elements of the same value. Return the number of clumps in the given array.
countClumps({1, 2, 2, 3, 4, 4}) → 2 countClumps({1, 1, 2, 1, 1}) → 2 countClumps({1, 1, 1, 1, 1}) → 1
Conditions for solving:
- No other helper methods.
- Do not use Java.util.Arrays.copyOf or any other utility under Arrays
- Do not use collections.
Can I solve it using 1 loop with or with a better time complexity?
Any other nitpicks about my solutions are also welcome.
public int countClumps(int[] nums) {
final int len=nums.length;
int count=0;
for(int i=0;i<len;i++)
{
int j=i+1;
if(nums[i]==nums[j])
{
count++;
while((nums[i]==nums[j]))
{
if(j==len-1)
break;
j++;
}
}
i=j-1;
if(i==len-2)
break;
}
return count;
}