1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Algorithm for checking clumps inside an array not working

  1. Jan 22, 2017 #1
    1. The problem statement, all variables and given/known data
    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

    2. Relevant 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

    3. The attempt at a solution
    Code (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
     
  2. jcsd
  3. Jan 22, 2017 #2

    Drakkith

    User Avatar

    Staff: Mentor

    Tell me, what's your iterationsLeft variable for?
     
  4. Jan 22, 2017 #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!:
    Code (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?
     
  5. Jan 22, 2017 #4

    Drakkith

    User Avatar

    Staff: Mentor

    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:
    Code (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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Algorithm for checking clumps inside an array not working
Loading...