Reputation: 21
hi guys i want to ask something if i want to check whether the array has a 4 consecutive elements or not like this {1,2,4,4,4,4,3,5}
it will return true
but if the array is {1,2,4,4,4,2,4,2}
return false
PLEASE help in Java :)} that's my code:
public class arraysearch {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr [] ={1,2,4,4,4,4,5,7,10};
int start = arr[0];
int count = 0;
int sum=0;
for(int i = 1;i<arr.length-1;i++)
{
if(start==4)
{
count++;
}
else
{
//count=0;
for(int j=i+1;j<arr.length-1;j++){
if(arr[i]==arr[j]&&arr[j]==4)
{
count++;
//continue;
}
}
}
}
if(count == 4)
{
System.out.println("it has 4 elements");
}
else
System.out.println("it hasn't 4 elements");
}
}
Upvotes: 2
Views: 5428
Reputation: 3625
Your code makes no differentiation between consecutive elements and those separated. what you can do (with a slight modification) is reset the count.
count = 1;
for(int i=0; i<arr.size-1; i++){
if(arr[i+1] == arr[i]){
count++;
}
else{
count=1;
}
if(count == 4){
return true;
}
}
This code iterates through checking if the next element is the same as the current element, and if so, adds one to the count. If not, the count is reset for the next chain. This will work for any series of numbers, and will always store the longest, so I reccomend checking if it contains a series of AT LEAST 4 elements.
Upvotes: 1
Reputation: 11041
An unique solution, for reference:
int arr [] ={1,2,4,4,4,4,5,7,10};
for(int o : arr)
buffer.append(",").append(o);
boolean match = buffer.toString().matches("(\\d+)(?:,\\1){3}");
System.out.println("It "+(match ? "has" : "hasn't")+ " 4 elements");
You can reuse the pattern to make it more efficient:
// Class
private static final Pattern CHAIN=Pattern.compile("(\\d+)(?:,\\1){3}");
// Method
int arr [] ={1,2,4,4,4,4,5,7,10};
for(int o : arr)
buffer.append(",").append(o);
System.out.println("It "+(CHAIN.matcher(buffer).matches() ? "has" : "hasn't")+ " 4 elements");
Let me explain this magic a bit here:
(\d+)(?:,\1){3}
is a regular expression. It matches when a sequence of digits appears four times in a row seperated by commas. Here's how it looks like when broken down:
> Capturing Group "\d+" one or more number digits
| > Non- Capturing Group: ",\1" a comma and a captured group
| | > The second group matches 3 times
| | |
(\d+)(?:,\1){3}
Upvotes: 3
Reputation: 12939
Well, your code seems kinda messed up. You ask if your code has 4 consecutive elements, but it seems like in your code you are testing if it has 4 consecutive 4
s. Suppose your array has at least 1 element:
int[] arr = new int[]{1, 2, 4, 4, 4, 2, 4, 2};
//current consecutive sector
int currentLength = 1;
int currentValue = arr[0];
//longest consecutive sector so far
int maxLength = 1;
int maxValue = currentValue;
for (int i = 1; i < arr.length; i++) {
if (currentValue == arr[i]) {
//hooray, current consecutive sector is getting larger
currentLength++;
} else {
//oh no, current consecutive sector ends and starts a new one
if (currentLength > maxLength) {
maxLength = currentLength;
maxValue = currentValue;
}
currentLength = 1;
currentValue = arr[i];
}
}
//finally, check if the last sector isn't the longest
if (currentLength > maxLength) {
maxLength = currentLength;
maxValue = currentValue;
}
System.out.format("Longest consecutive sector contained %d times number %d.\n",
maxLength, maxValue);
Upvotes: 0
Reputation: 403
Try this :
public class SumDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[] = {
1, 2, 4, 4, 4, 4, 5, 7, 10
};
int count = 0;
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] != arr[i + 1])
continue;
else
count++;
}
if (count >= 3) { // three comparision found true, means the array has 4
// simliar continuous elements
System.out.println("it has " + count + "elements");
} else
System.out.println("it has " + count + "elements");
}
}
This solution will not work if it has more than one element that is continuous. e.g. {1,2,2,4,4,4,4,5,7,10}
Upvotes: 0