# cluster series

• 11-10-2012, 11:39 AM
ameerulislam
cluster series
I got a problem in hand where I can't find any solution yet.. I don't know how to explain this properly let me begin with some sample input outputs

Input: {1,1,2,3,3} => Output will be: 2
Input: {1,1,1,1,1} => Output will be: 1
Input: {1,2,1,1,1} => Output will be: 1
Input: {1,1,2,1,1,2,4,4} => Output will be: 3

So the thing is it will basically out put how many group of 2 or more same consecutive numbers in a series. I hope I'm not speaking gibberish :x:.

my code is incomplete/wrong.:(-:

Code:

``` import javax.swing.JOptionPane; public class Cluster {         public static void main(String[] args) {                 // TODO Auto-generated method stub                 String a= JOptionPane.showInputDialog("Enter Array size" );                 int size= Integer.parseInt(a);                 int [] array = new int[size];                                 for(int i=0; i<size; i++){                 a= JOptionPane.showInputDialog("Enter array elements" );                 int n= Integer.parseInt(a);                                 array[i]=n;                                 System.out.print(" "+array[i]);                 }                 int flag=0;                 for (int j=0; j<size-1; j++){                                                         if (array[j]!=array[j+1]){                                         flag=flag+1;                                         if(array[j+1]!=array[j+2]){                                                                                         }                                 }                                         } System.out.println(" Clusters "+flag);                         } }```
• 11-10-2012, 01:52 PM
JosAH
Re: cluster series
Explain in words how you would do it; no code please.

kind regards,

Jos
• 11-10-2012, 02:12 PM
ameerulislam
Re: cluster series
Quote:

Originally Posted by JosAH
Explain in words how you would do it; no code please.

kind regards,

Jos

I don't exactly know. This is why I need your help.
• 11-10-2012, 02:41 PM
JosAH
Re: cluster series
Just give it a try; make that wetware between your ears work.

kind regards,

Jos
• 11-10-2012, 04:03 PM
ameerulislam
Re: cluster series
Quote:

Originally Posted by JosAH
Just give it a try; make that wetware between your ears work.

kind regards,

Jos

This came in my exam today. I couldn't do it there then returned and tried again.. still no real idea. This is why I took the time to write here.
• 11-10-2012, 04:34 PM
JosAH
Re: cluster series
As I wrote before: just give it a try; I know it won't be perfect (otherwise you wouldn't have started this thread), but you have to show some effort instead of begging for a spoonfeeding answer.

kind regards,

Jos
• 11-10-2012, 04:52 PM
ameerulislam
Re: cluster series
Quote:

Originally Posted by JosAH
As I wrote before: just give it a try; I know it won't be perfect (otherwise you wouldn't have started this thread), but you have to show some effort instead of begging for a spoonfeeding answer.

kind regards,

Jos

"A try"? I already given several tries. Was I not clear? Never mind..
• 11-10-2012, 05:58 PM
JosAH
Re: cluster series
Ok, if you refuse to show your tries here ... never mind, goodbye and good luck with it.

kind regards,

Jos
• 11-10-2012, 06:44 PM
ameerulislam
Re: cluster series
Ok I think there has been a miss-communication here. I though u were asking to try coding it again. I reread all the posts again and realized you were asking to explain the possible solution for it. Ok here it goes.

The way I'm trying to solve this problem is this

Lets go through this input and out put

Input: {1,1,2,3,3} => Output will be: 2

first I'll start a loop to see consecutive similar numbers in that array. and add a flag every time it finds two or more consecutive numbers. It will continue to go forward until it gets a different number. So here it will start the loop and when it will see two consecutive 1's then it will increase its counter/flag by 1. Then when it will find 2 which is a different number it will see if there is more 2's after that. Here it will see that there is 3 after 2 so no need to increase the flag. Next it will compare 3 with the next array element. which it will find that there is another 3. Now it will increase the counter / flag by one more. Next when it will reach the end of the array, it will just end the operation I guess.

I apologize for the miss-communication.
• 11-10-2012, 07:09 PM
JosAH
Re: cluster series
Apologies accepted; back to the problem: suppose you start at position i in your array; also suppose at position i starts a new sequence (this is always true if i == 0). Starting at that position, move to the right until you have reached either the end of the array or the element at the new position doesn't equal the element at position i. Call this position j. The value j-i is the length of the sequence; if it is larger than one, you found a real sequence. Next set i equal to j (one element beyond the sequence) and repeat the above steps until i reaches the end of the array. In code this scenario only requires one loop and a couple of if-statements. Your example:

1,1,2,3,3 (i= 0, j= 2 --> sequence length == 2)
1,1,2,3,3 (i= 2, j= 3 --> sequence length == 1)
1,1,2,3,3 (i= 3, j= 5 --> sequence length == 2)
1,1,2,3,3,(i= 5 at the end of the array, two 'real' sequences found)

kind regards,

Jos