Reputation: 11
I have an array, which for example contains the values 123456, which obviously contains more than 3 consecutive values.
I want a method that will return true if the array contains at least 3 consecutive values in it, thanks in advance.
for example:
972834 - return true (234)
192645 - return true (456)
etc. etc..
update! :
i have an array in java, it takes in 6 integers. for example nextTurn[], and it contains 8 4 2 5 6 5 at the moment it sorts the array - 2 4 5 5 6 8
how would i get it to return true if there are 3 consecutive numbers throughout the array?
ie so it will find 4 5 6
i would also like it to return the position of the integer in the array, so for the original array 8 4 2 5 6 5
it will return, 2 4 5 or 2 5 6
thanks for all your help guys, appreciated
Upvotes: 1
Views: 13575
Reputation: 3692
Late to the party, but here's a solution.
function checkConsecutiveExists(arr) {
for (let i = 0; i < arr.length; i++) {
if (checkNumLast(arr, arr[i]) || checkNumMid(arr, arr[i]) || checkNumFirst(arr, arr[i])) {
return true;
}
}
return false;
}
function checkNumLast(arr, num) {
return arr.includes(num - 2) && arr.includes(num - 1);
}
function checkNumMid(arr, num) {
return arr.includes(num - 1) && arr.includes(num + 1);
}
function checkNumFirst(arr, num) {
return arr.includes(num + 1) && arr.includes(num + 2);
}
console.log(checkConsecutiveExists([9, 7, 2, 8, 3, 4]));
console.log(checkConsecutiveExists([1, 9, 2, 6, 4, 50]));
It's a brute force solution, so not the most optimal.
Upvotes: 0
Reputation: 700302
The most straight forward solution would be to simply loop through the items, and check against the next two items:
bool HasConsecutive(int[] a){
for(int i = 0; i < a.Length - 2; i++) {
if (a[i + 1] == a[i] + 1 && a[i + 2] == a[i] + 2) return true;
}
return false;
}
Another solution is to loop through the items and count consecutive items:
bool HasConsecutive(int[] a){
int cnt = 1;
for (int i = 1; i < a.Length; i++) {
if (a[i] == a[i - 1] + 1) {
cnt++;
if (cnt == 3) return true;
} else {
cnt = 1;
}
}
return false;
}
Upvotes: 3
Reputation: 8586
h = new hash table
for i in array
if i + 1 in h && i + 2 in h
return i, i+1, i+2
add i to h
return no-match
Upvotes: 1
Reputation: 630
Should be tagged homework I'm assuming.
In pseudo code you are going to want something along the lines of
for int i = 0 to array.length - 2
temp = array[i]
if((array[i+1] == (temp + 1)) && (array[i+2] == (temp + 2)))
return true
else return false
edit: This is assuming you have an array of ints. If it is a string, you are going to have to use something along the lines of charAt(position) and then convert the char to a decimal number, by subtracting '0' or using a parseInteger function
Update on the misleading part
To do this, I would create an array the same length of the string, for simplicities sake
int arr[array.length];
then loop through every item in the string array, while incrementing arr at the position the number falls at
(assuming a char array, single digit numbers) for( int i = 0; i < array.length; i++ ) arr[array[i] - '0']++;
then go through arr checking for three consecutive numbers
for( int i = 0; i < arr.length - 2; i++ )
if( arr[i] >= 1 && arr[i+1] >= 1 && arr[i+2] >= 1 )
return true;
return false;
Upvotes: 1