Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# countClumps  challenge

Posted on 2016-08-14
Medium Priority
388 Views
Last Modified: 2016-08-17
Hi,

I am working on below challenge

http://codingbat.com/prob/p193817

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

i am not clear on the description of the challenge. please advise
0
Question by:gudii9
• 4
• 4
10 Comments

LVL 38

Expert Comment

ID: 41756126
gudii9 - where are you from, what is your native language?
0

LVL 38

Accepted Solution

Gerwin Jansen, EE MVE earned 2000 total points
ID: 41756128
You have to count blocks of the same numbers.

1 1 1 2 3 3 4 5 contains 2 blocks (numbers 1 and numbers 3)
0

LVL 7

Author Comment

ID: 41756451
countClumps([1, 2, 2, 3, 4, 4]) → 2//this is clear as it has one 2's clump and one other 4's clump
countClumps([1, 1, 2, 1, 1]) → 2//this is clear
countClumps([1, 1, 1, 1, 1]) → 1//how it is 1?
is it because it has one clump of 5 1's in it?
0

LVL 38

Expert Comment

ID: 41756540
Yes, one clump of 1's
0

LVL 7

Author Comment

ID: 41758651
``````public int countClumps(int[] nums) {
int counter=0;
boolean flag=false;
for(int i=0;i<nums.length-1;i++){
if(nums[i]==nums[i+1]&&!flag){
flag=true;
counter++;
}
else if(nums[i]!=nums[i+1]){
flag=false;
}
}
return counter;
}
``````

above passes all tests.
basically checking i with i+1  and making flag to true and repeat in if loop then in else check for inequality and make flag false there
1

LVL 7

Author Comment

ID: 41759619
just thinking any other possible simple solutions/approaches for this? or any improvements, modifications, optimization to my code above?
0

LVL 38

Expert Comment

ID: 41759689
Can't think of an easier way, if you want a different way then you have to write different pseudo code first. But it works and passes the tests so you've learned some here right?
0

LVL 7

Author Comment

ID: 41760276
yes definitely
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
###### Suggested Courses
Course of the Month9 days, 18 hours left to enroll

#### 927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.