Get possible sequence from array

I am having an Array like,

const array = [1,3,4,5,6] // array will have unique values

I want to get the possible sequence from that array in optimised way and sequence length should be greater than 3

Example: I should get [[3,4,5,6]]

examples:

[1,2,3,5,8,9,10] => output should be [[1,2,3],[8,9,10]]

I have tried Array.every but not able to figure out when sequence breaks in between.


const data = [1,2,3,5,8,9,10];
const result = data.reduce((seq, v, i, a) => {
	if (i && a[i - 1] !== v - 1) {
		seq.push([]);
	}
	seq[seq.length - 1].push(v);
	return seq;
}, [[]]).filter(({length}) => length > 1);
console.log(result);

Longest sequence of positive integers in an array. Find the longest running positive sequence in an array. Input : arr [] = {1, 2, -3, 2, 3, 4, -6, 1, 2, 3, 4, 5, -8, 5, 6} Output :Index : 7, length : 5 Input : arr [] = {-3, -6, -1, -3, -8} Output : No positive sequence detected.


Maximum consecutive numbers present in an array. Find the length of maximum number of consecutive numbers jumbled up in an array. Examples: Input : arr [] = {1, 94, 93, 1000, 5, 92, 78}; Output : 3 The largest set of consecutive elements is 92, 93, 94 Input : arr [] = {1, 5, 92, 4, 78, 6, 7}; Output : 4 The largest set of consecutive elements is 4, 5, 6, 7.


.every(someFunctionThatReturnsTrueOrFalse) returns a boolean and checks whether all elements in the array satisfy a condition or not. You want to consider multiple elements at the same time for a very specific purpose. I guess you'd have to write a custom function for this.

Something like this should do the trick:

  var array1 = [bunch of values];
   var i;
   var j;
   var optimizedArr1 = [array1[0]];
   var optimizedArr2 = [];
   for(j = 0; j < array1.length-1; j = i){
     optimizedArr2.push(array1[j]);

     for(i = j; i < array1.length; i++){
      if(array1[i+1]-array1[i] != 1){
        if(optimizedArr1.length < optimizedArr2.length)
        {
          optimizedArr1 = optimizedArr2;
        }
          optimizedArr2 = [];
          i++;
          break;

      } else {
         optimizedArr2.push(array1[i+1])
      }
     }
    }
   return optimizedArr2;

Given an array of numbers, arrange them in a way that yields the largest value. For example, if the given numbers are {54, 546, 548, 60}, the arrangement 6054854654 gives the largest value. And if the given numbers are {1, 34, 3, 98, 9, 76, 45, 4}, then the arrangement 998764543431 gives the largest value.


data =[-1,0,1,3,4,5,6,8,9,13,14];


function getSequence(dataArray){
  let results = [];
  let temp = [];
  temp.push(data[0])
  console.log(temp)
  for(let i= 0; i < data.length -1; i++){
    if (Math.abs((data[i+1] - data[i])) == 1){
       temp.push(data[i + 1])
    }
    else{
      if(temp.length >= 2){
        results.push(temp)
      }
      temp = []
      temp.push(data[i + 1])
    }

  }

   if (temp.length >= 2){
      results.push(temp)
    }

  return results;
}
console.log(getSequence(data))

This should work assuming it's given sorted data.

Given an array of size n, generate and print all possible combinations of r elements in array. For example, if input array is {1, 2, 3, 4} and r is 2, then output should be {1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4} and {3, 4}. Following are two methods to do this. We create a temporary array ‘data []’ which stores all outputs one by one.


function sequence(arr) {
  let ret = [];
  let temp = [];
  let hasNext = false;
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] + 1 === arr[i + 1]) {
      temp.push(arr[i]);
      hasNext = true;
    } else {
      if (hasNext) {
        temp.push(arr[i]);
      }
      if (temp.length > 0) {
        ret.push(temp);
        temp = [];
      }
      hasNext = false;
    }
  }
  return ret;
}

console.log(sequence([1, 3, 5, 7, 8, 9, 13, 14, 15, 16, 20]));
console.log(sequence([1, 2, 3, 5, 7, 9, 13, 14, 15, 19, 20, 21, 22, 25, 28, 29, 30, 31, 32]));

After sorting the array run a loop and keep a count and max (both initially zero). Run a loop from start to end and if the current element is not equal to the previous (element+1) then set the count to 1 else increase the count. Update max with a maximum of count and max.


Seq.windowed is like Seq.pairwise, except that instead of producing a sequence of tuples, it produces a sequence of arrays that contain copies of adjacent elements (a window) from the sequence. You specify the number of adjacent elements you want in each array. The following code example demonstrates the use of Seq.windowed.


The goal was to find all possible combinations of choosing k elements from a n-sized array (basically the Binomial coefficient) and return them. Unlike this code - here I don't want all possible combinations, just those that are k in size. Permutations inside a k-size element are not counted (i.e. (1,2,3) == (2,1,3) == etc.).


The Excel SEQUENCE function generates a list of sequential numbers in an array. The array can be one dimensional, or two-dimensional, controlled by rows and columns arguments. Start and step (increment) values are also supplied as arguments. In the example shown, the formula in B4 is: =