[Week Twelve] Programming Contest - Array Clumps

Welcome back to the weekly programming contests! If you are new to the contest or need a refresher don’t forget to read up on the rules and information Posted Here

This week’s challenge is to write a method (you can call it anything you want) that takes in an integer array and returns the number of “clumps” of integers in the array. For this challenge a clump will be a series of two or more adjacent elements of the same value.

Examples:

clumps([6, 9, 9, 9, 9, 9, 5, 7]) -> 1
clumps([5, 5, 5, 8, 5, 1, 5, 5]) -> 2
clumps([5, 1, 3, 5, 4, 6, 4, 8]) -> 0
clumps([7, 1, 7, 7, 7, 2, 2, 6]) -> 2
clumps([8, 1, 4, 5, 6, 1, 4, 7]) -> 0
clumps([2, 7, 8, 3, 7, 9, 9, 2]) -> 1
clumps([5, 3, 6, 3, 2, 9, 9, 2]) -> 1
clumps([2, 2, 8, 6, 9, 9, 8, 9]) -> 2
clumps([2, 4, 1, 2, 2, 9, 2, 4]) -> 1
clumps([7, 9, 9, 6, 4, 5, 5, 3]) -> 2

Also, if you like these weekly programming problems, please make some suggestions for the contest or specific challenges (in the main thread)! This takes time out of my Sunday and its not always fun! Using suggested challenges makes it a lot faster and easier for me.

Imports are allowed for this challenge!

Challenge Closes: Sunday 1/24/16

Good luck!

Er… could you clarify please?

Yoshi, i think this is what he means.

The wording was a bit unclear. If you read the part I quoted slowly, you’ll see.

that takes in a String
and returns an array containing of integers
and returns the number of “clumps” of numbers in the array.

Sorry, the thread got rolled back when the forums went offline for a bit so i didn’t notice that half of it was still stuff from the last challenge

Thanks.

Compressed (117 symbols):

public static int c(int[] a){int l=-1,s=0,n=0;for(int k:a){if(k==l)s++;else{if(s>0)n++;s=0;l=k;}}return n+(s>0?1:0);}

Full:

public static int clumps(int[] arr)
{
    int kprev=-1, curClumpSize=0, clumpNum=0;
    for (int k: arr) {
        if (k==kprev)
            curClumpSize++;
        else {
            if(curClumpSize>0)clumpNum++;
            curClumpSize=0; kprev=k;
        }
    }
    return clumpNum+(curClumpSize>0?1:0);
}

119 characters:

public static int clumps(int[]a){int v=0,l=0,c=0;for(int i:a)if(i!=v){v=i;if(l>1)c++;l=1;}else l++;return c+(l>1?1:0);}

Readable version:

public static int clumps(int[] array) {
    int clumpValue = 0, clumpLength = 0, clumpCount = 0;
    for(int value : array) {
        if (value != clumpValue) {
            clumpValue = value;
            if (clumpLength > 1) {
                c++;
            }
            l = 1;
        } else {
            l++;
        }
    return c + (l > 1 ? 1 : 0);
}

*week twelve

*week’s (since I’m posting about a correction may as well correct the grammar :P)

instead of “clumps”, I would perhaps call it “groups of the same integer value”.

Really appreciate your effort, these challenges are great! (even if I never get around to finishing a solution I always attempt it)

1 Like

In your long version, you forgot to rename c to clumpCount and, in some places l with clumpLength. :stuck_out_tongue:

EDIT: You could also save a few characters by altering the method name.

public static int c(int[]n){int t=0;for(int i=0;i<n.length-1;i++){if(!(i>0&&n[i-1]==n[i])&&n[i]==n[i+1])t++;}return t;}

119 chars

Pretty Version

public static int countClumps(int[] nums) {
    int total = 0;
    for(int i = 0; i < nums.length-1; i++) {
        if(!(i > 0 && nums[i-1] == nums[i]) && nums[i] == nums[i+1])
            total++;
    }
    return total;
}

Also i shorten @JBYoshi’s version by renaming the method :stuck_out_tongue:

public static int c(int[]a){int v=0,l=0,c=0;for(int i:a)if(i!=v){v=i;if(l>1)c++;l=1;}else l++;return c+(l>1?1:0);}

114 chars!

Having a -1 at the start of the array would cause yours to return the wrong value

108 characters:

public static int c(int[]a){int c=0,i=0,p=0;for(;++i<a.length;)if(a[i-1]==a[i]){if(p<i)c++;p=i+1;}return c;}

It compares every pair of elements next to each other. If they are equal, they are part of a clump, but it’s only counted if it’s the start of a new clump. It’s the start of a new clump if the previous 2 elements were not part of a clump. I use the variable p to store the psuedo-index of the last successful match. To know whether the previous 2 elements were part of a clump, I just compare p with the current index.

Congratulations to @eah for winning week twelve!

The next challenge will be up shortly