Sunday, October 26, 2025
HomeData Modelling & AIMinimum number of consecutive sequences that can be formed in an array

Minimum number of consecutive sequences that can be formed in an array

Given an array of integers. The task is to find the minimum number of consecutive sequences that can be formed using the elements of the array. 
Examples: 
 

Input: arr[] = { -3, -2, -1, 0, 2 }
Output: 2
Consecutive sequences are (-3, -2, -1, 0), (2).

Input: arr[] = { 3, 4, 0, 2, 6, 5, 10 }
Output: 3
Consecutive sequences are (0), {2, 3, 4, 5, 6} and {10}

 

Approach: 

  • Sort the array.
  • Iterate the array, and check if current element is just 1 smaller than the next element. 
     
  • If it is then increment the count by 1.
  • Return the final count of consecutive sequences.

Below is the implementation of above approach : 
 

C++




// C++ program find the minimum number of consecutive
// sequences in an array
#include <bits/stdc++.h>
using namespace std;
 
int countSequences(int arr[], int n)
{
    int count = 1;
 
    sort(arr, arr + n);
 
    for (int i = 0; i < n - 1; i++)
        if (arr[i] + 1 != arr[i + 1])
            count++;
 
    return count;
}
 
// Driver program
int main()
{
    int arr[] = { 1, 7, 3, 5, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // function call to print required answer
    cout << countSequences(arr, n);
    return 0;
}


Java




// Java  program find the minimum number of consecutive
// sequences in an array
 
import java.util.Arrays;
import java.io.*;
 
class GFG {
     
static int countSequences(int arr[], int n)
{
    int count = 1;
 
    Arrays.sort(arr);
 
    for (int i = 0; i < n - 1; i++)
        if (arr[i] + 1 != arr[i + 1])
            count++;
 
    return count;
}
 
// Driver program
    public static void main (String[] args) {
 
    int arr[] = { 1, 7, 3, 5, 10 };
    int n = arr.length;
    // function call to print required answer
    System.out.println( countSequences(arr, n));
 
    }
//This code is contributed by ajit.   
}


Python3




# Python3 program find the minimum number of consecutive
# sequences in an array
 
def countSequences(arr, n) :
    count = 1
 
    arr.sort()
 
    for i in range( n -1) :
        if (arr[i] + 1 != arr[i + 1]) :
            count += 1
 
    return count
  
 
# Driver program
if __name__ == "__main__" :
 
    arr = [ 1, 7, 3, 5, 10 ]
    n = len(arr)
 
    # function call to print required answer
    print(countSequences(arr, n))
 
# This code is contributed by Ryuga


C#




// C# program find the minimum number of consecutive
// sequences in an array
 using System;
class GFG {
      
static int countSequences(int []arr, int n)
{
    int count = 1;
  
    Array.Sort(arr);
  
    for (int i = 0; i < n - 1; i++)
        if (arr[i] + 1 != arr[i + 1])
            count++;
  
    return count;
}
  
// Driver program
    static public void Main (String []args) {
  
    int []arr = { 1, 7, 3, 5, 10 };
    int n = arr.Length;
    // function call to print required answer
    Console.WriteLine( countSequences(arr, n));
  
    }
}
//This code is contributed by Arnab Kundu  


PHP




<?php
// PHP program find the minimum number
// of consecutive sequences in an array
 
function countSequences($arr, $n)
{
    $count = 1;
 
    sort($arr);
 
    for ($i = 0; $i < $n - 1; $i++)
        if ($arr[$i] + 1 != $arr[$i + 1])
            $count++;
 
    return $count;
}
 
// Driver Code
$arr = array( 1, 7, 3, 5, 10 );
$n = count($arr);
 
// function call to print required answer
echo countSequences($arr, $n);
 
// This code is contributed by inder_verma
?>


Javascript




<script>
 
    // Javascript program find the
    // minimum number of consecutive
    // sequences in an array
     
    function countSequences(arr, n)
    {
        let count = 1;
 
        arr.sort(function(a, b){return a - b});
 
        for (let i = 0; i < n - 1; i++)
            if (arr[i] + 1 != arr[i + 1])
                count++;
 
        return count;
    }
     
    let arr = [ 1, 7, 3, 5, 10 ];
    let n = arr.length;
     
    // function call to print required answer
    document.write(countSequences(arr, n));
     
</script>


Output: 

5

 

Time Complexity: O(n log n), where n is the size of the array.

Auxiliary Space: O(1)
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS