Algorithm for checking clumps inside an array not working

In summary, the goal is to return the number of clumps in a given array, where a "clump" is defined as a series of 2 or more adjacent elements of the same value. The solution involves using two for loops to iterate through the array and check for clumps. One for loop sets the current number to compare, while the other checks for adjacent elements of the same value. The iterationsLeft variable was meant to prevent the loop from going out of bounds, but it did not work effectively. A simpler solution involves checking if adjacent elements are equal right after starting the outer for loop.
  • #1
Arnoldjavs3
191
3

Homework 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

Homework Equations


The logic is that it'll set a number to currentNum in the first for loop, and then run another for loop(with how many iterations that it must run so it doesn't go out of bounds) to check if there are clumps. If the number nums[x] = nums, then it'll notice that it's found a clump. This will increment the ctr by 1 and set the boolean to true. Then the loop will find the index of which the clump ends and set i = x, that index. Hopefully this makes sense verbally and more so in my code

The Attempt at a Solution


Java:
public int countClumps(int[] nums) {
  int length = nums.length;
  boolean clumpFound = false;
  int clumpCtr = 0;
  int currentNum = 0;
  int iterationsLeft = 0;
 
  for(int i = 0;i<length;i++) {
    currentNum = nums[i];
    iterationsLeft = length-i;
 
      for(int x = i+1; x<iterationsLeft;x++) {
        if (nums[x]==currentNum && clumpFound == false) {
          clumpCtr++;
          clumpFound = true;
        }
        else if(nums[x]!=currentNum &&  clumpFound == true) {
          clumpFound = false;
          i=x;
          break;
        }
        else if(nums[x]!=currentNum && clumpFound == false) {
          break;
        }
     
      }
 
  }
  return clumpCtr;
}

I'm having a really hard time visualizing this in my head as to what's going wrong here.
Here are the tests it fails/passes:
http://prntscr.com/dz1x1f
 
Physics news on Phys.org
  • #2
Tell me, what's your iterationsLeft variable for?
 
  • #3
It was meant to prevent it from going out of bounds by only having it go through the second for loop as many times as it could(but clearly it does not achieve this, now I notice)

Here is the new code that works!:
Java:
public int countClumps(int[] nums) {
  int length = nums.length;
  boolean clumpFound = false;
  int clumpCtr = 0;
  int currentNum = 0;
  int iterationsLeft = 0;

  for(int i = 0;i<length;i++) {
    currentNum = nums[i];
   

      for(int x = i+1; x<length;x++) {
        if (nums[x]==currentNum && clumpFound == false) {
          clumpCtr++;
          clumpFound = true;
        }
        else if(nums[x]!=currentNum &&  clumpFound == true) {
          clumpFound = false;
          i=x-1;
          break;
        }
        else if(nums[x]!=currentNum && clumpFound == false) {
          break;
        }
 
      }

  }
  return clumpCtr;
}

That one question was enough to make me realize... lol
Edit: Do you think me having to create two for loops makes this solution too complicated? I'm relatively new to computer science, I'd say I'm blazing through challenges but my solutions are definitely not simple. I think simple solutions that achieve the same end are better?
 
  • Like
Likes Drakkith
  • #4
One thing to note: If you check to see if nums[ i ] == nums[i+1] right after you start your outer for loop, I think you can greatly simplify things. I haven't checked to see if this works, but you can try it if you'd like:
Java:
for (int i = 0; i < length-1; i++)
     if (nums[i] == nums[i+1]){
          clumpCtr++;
          for (int x = i+1; nums[x] == nums[x+1] && x < length-1; x++){
               i = x+1;
     }
}

I put a length - 1 in the for loops to make sure you can never try to check beyond the array.
 
  • Like
Likes Arnoldjavs3

1. Why is my algorithm for checking clumps inside an array not working?

There could be several reasons why your algorithm is not working. It could be due to a logical error in your code, incorrect implementation of the algorithm, or incorrect input data. It is important to carefully review your code and debug any errors to ensure the algorithm is functioning correctly.

2. How do I know if my algorithm for checking clumps inside an array is correct?

To ensure the accuracy of your algorithm, you can test it with different input data and compare the results with the expected output. You can also consult with other experts in the field or refer to existing algorithms for checking clumps to verify the correctness of your approach.

3. Can the size of the array affect the performance of the clump checking algorithm?

Yes, the size of the array can have an impact on the performance of the algorithm. If the array is too large, it may take longer for the algorithm to check for clumps. It is important to consider the time complexity of the algorithm and optimize it for better performance.

4. Are there any alternative algorithms for checking clumps inside an array?

Yes, there are several alternative algorithms for checking clumps inside an array. Some algorithms may be more efficient or have a different approach than others. It is important to research and compare different algorithms to find the most suitable one for your specific needs.

5. How can I improve the efficiency of my algorithm for checking clumps inside an array?

There are several ways to improve the efficiency of your algorithm. You can optimize the code by reducing unnecessary operations, using data structures or algorithms with better time complexity, or parallelizing the algorithm to utilize multiple processors. It is also important to analyze and identify any bottlenecks in the algorithm and find ways to optimize them.

Similar threads

  • Engineering and Comp Sci Homework Help
3
Replies
80
Views
8K
  • Programming and Computer Science
Replies
34
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
985
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Back
Top