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; ` `} ` |

*chevron_right*

*filter_none*

## 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. ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

5

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Sum of minimum element of all sub-sequences of a sorted array
- Minimum sum of two numbers formed from digits of an array
- Minimum sum of two numbers formed from digits of an array
- Sum of minimum difference between consecutive elements of an array
- Minimum array insertions required to make consecutive difference <= K
- Find Kth number from sorted array formed by multiplying any two numbers in the array
- Find the Largest Cube formed by Deleting minimum Digits from a number
- Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity
- Count minimum number of subsets (or subsequences) with consecutive numbers
- Greatest number that can be formed from a pair in a given Array
- Largest number in given Array formed by repeatedly combining two same elements
- Minimum number of operations to convert array A to array B by adding an integer into a subarray
- Insert minimum number in array so that sum of array becomes prime
- Smallest number dividing minimum number of elements in the array | Set 2
- Smallest number dividing minimum number of elements in the Array
- Minimum number of swaps required to sort an array of first N number
- Build original array from the given sub-sequences
- Count the Arithmetic sequences in the Array of size at least 3
- Strings formed from given characters without any consecutive repeating characters

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.