Array problem: I have never been so lost.

This problem is actually written in Java, but it should still be readable by anyone who has never worked with Java. The problem is as follows:

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

My solution is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
public int countClumps(int[] nums) {
  int count = 0;
  if(nums.length == 0) return count;
  
  int currentNum = nums[0];
  boolean counted = false;
  
  for(int i = 1; i < nums.length; ++i){
    if(nums[i] == currentNum && !counted){ count++; counted = true; }
    else{ currentNum = nums[i]; counted = false; }
  }
  return count;
}


This solution works for every case except where the elements of nums are ubiquitous (i.e. {1,1,1,1,1,1}) In which case it returns 2. I have searched through my code time and time again and I cannot find the cause. Please help!

EDIT: Please tell me the issue with my code rather than giving me a new solution.
Last edited on
I've rewritten your code in C++, and it correctly prints 3 without having introduced any changes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

int countClumps(int array[6]) {
	int count = 0;

	int current = array[0];
	bool counted = false;

	for (int i = 1; i < 6; ++i) {
		if (current == array[i] && !counted) {
			count++;
			counted = true;
		}
		else {
			current = array[i];
			counted = false;
		}
	}
	return count;
}

int main() {

	int array[6] = {1, 1, 1, 1, 1, 1};

	std::cout << countClumps(array) << std::endl;

	return 0;
}


EDIT* read the instructions incorrectly.
Last edited on
It should print 1, not 3.
Once you're in a clump, you need to keep advancing the index until you've consumed everything that is the same as the current number.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>

int countClumps(int* arr, int arrSize)
{
    if (arrSize <= 0) return 0;

    int clumpCount = 0;

    for (int idx = 0; idx < arrSize - 1; ++idx)
    {
        bool inClump = false;
        int currentItem = arr[idx];

        while (idx + 1 < arrSize && currentItem == arr[idx + 1])
        {
            inClump = true;
            currentItem == arr[idx + 1];
            ++idx;
        }

        if (inClump) clumpCount++;
    }

    return clumpCount;
}

int main() {

    int arr1[6] = { 1, 2, 2, 3, 4, 4 };
    std::cout << countClumps(arr1, 6) << std::endl;

    int arr2[6] = { 1, 1, 2, 1, 1 };
    std::cout << countClumps(arr2, 5) << std::endl;

    int arr3[6] = { 1, 1, 1, 1, 1 };
    std::cout << countClumps(arr3, 5) << std::endl;

    return 0;
}
2
2
1
Last edited on
Whether that actually works or not, I was actually hoping for an explanation for why what I have doesn't work. I apologize for being unclear in my original post.
It's because you turn counted back to false when you shouldn't. Any time counted is true, you turn it back to false.
Because any time counted is true, you end up in the else part (line 10) and set it back to false. The correct code in C++ is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int countClumps(int nums[6]) {
    int count = 0;

    int current = nums[0];
    bool counted = false;

    for (int i = 1; i < 6; ++i) {
        if (current == nums[i]) {
            if (!counted) {
                count++;
                counted = true;
            }
        } else {
            current = nums[i];
            counted = false;
        }
    }
    return count;
}

Topic archived. No new replies allowed.