Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find Consecutive Numbers Among multiple Arrays?

I right away give an example, now suppose I have 3 arrays a,b,c such as

a = c(3,5)
b = c(6,1,8,7)
c = c(4,2,9)

I must be able to extract consecutive triplets among them i,e.,

c(1,2,3),c(4,5,6)

But this was just an example, I would be having a larger data set with even more than 10 arrays, hence must be able to find the consecutive series of length ten.

So could anyone provide an algorithm, to generally find the consecutive series of length 'n' among 'n' arrays.

I am actually doing this stuff in R, so its preferable if you give your code in R. Yet algorithm from any language is more than welcomed.

like image 330
Dwarakanath Thoppe Avatar asked Jun 24 '16 14:06

Dwarakanath Thoppe


People also ask

How do you find consecutive numbers in an array?

Method 1 (Use Sorting) 1) Sort all the elements. 2) Do a linear scan of the sorted array. If the difference between the current element and the next element is anything other than 1, then return false. If all differences are 1, then return true.

What is the formula for calculating consecutive numbers?

By using Carl Gauss's clever formula, (n / 2)(first number + last number) = sum, where n is the number of integers, we learned how to add consecutive numbers quickly. We now know that the sum of the pairs in consecutive numbers starting with the first and last numbers is equal.


1 Answers

Reorganize the data first into a list containing value and array number. Sort the list; you'd have smth like:

1-2
2-3
3-1 (i.e. " there' s a three in array 1" )
4-3
5-1
6-2
7-2
8-2
9-3

Then loop the list, check if there are actually n consecutive numbers, then check if these had different array numbers

like image 123
Jur Avatar answered Sep 20 '22 00:09

Jur