Solved

countClumps  challenge

Posted on 2016-08-14
10
89 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
Comment
Question by:gudii9
  • 4
  • 4
10 Comments
 
LVL 37

Expert Comment

by:Gerwin Jansen
Comment Utility
gudii9 - where are you from, what is your native language?
0
 
LVL 37

Accepted Solution

by:
Gerwin Jansen earned 500 total points
Comment Utility
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

by:gudii9
Comment Utility
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 37

Expert Comment

by:Gerwin Jansen
Comment Utility
Yes, one clump of 1's
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 7

Author Comment

by:gudii9
Comment Utility
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;
}

Open in new window


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

by:gudii9
Comment Utility
just thinking any other possible simple solutions/approaches for this? or any improvements, modifications, optimization to my code above?
0
 
LVL 37

Expert Comment

by:Gerwin Jansen
Comment Utility
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

by:gudii9
Comment Utility
yes definitely
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

A short article about problems I had with the new location API and permissions in Marshmallow
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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

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

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

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now