# Find all ranges of consecutive numbers from Array

Given a sorted array arr[] consisting of N integers without any duplicates, the task is to find the ranges of consecutive numbers from that array.

Examples:

Input: arr[] = {1, 2, 3, 6, 7}
Output: 1->3, 6->7
Explanation:
There are two ranges of consecutive number from that array.
Range 1 = 1 -> 3
Range 2 = 6 -> 7

Input: arr[] = {-1, 0, 1, 2, 5, 6, 8}
Output: -1->2, 5->6, 8
Explanation:
There are three ranges of consecutive number from that array.
Range 1 = -1 -> 2
Range 2 = 5 -> 6
Range 3 = 8

Approach: The idea is to traverse the array from the initial position and for every element in the array, check the difference between the current element and the previous element.

• If the difference between the current element and the previous element is 1 then we just increment the length variable. We use the length variable to build the range “A -> B”. Since only the range is required, we don’t need to store all the elements between A and B. We just only need to know the length of this range.
• If the difference between the current element and the previous element is doesn’t equal to 1, we build the range between the first element of the range and the current previous element as the last range.

Below is the implementation of the above approach:

 `// Java program to find the ranges of ` `// consecutive numbers from array ` ` `  `import` `java.util.*; ` ` `  `class` `GFG { ` ` `  `    ``// Function to find consecutive ranges ` `    ``static` `List ` `    ``consecutiveRanges(``int``[] a) ` `    ``{ ` `        ``int` `length = ``1``; ` `        ``List list ` `            ``= ``new` `ArrayList(); ` ` `  `        ``// If the array is empty, ` `        ``// return the list ` `        ``if` `(a.length == ``0``) { ` `            ``return` `list; ` `        ``} ` ` `  `        ``// Traverse the array from first position ` `        ``for` `(``int` `i = ``1``; i <= a.length; i++) { ` ` `  `            ``// Check the difference between the ` `            ``// current and the previous elements ` `            ``// If the difference doesn't equal to 1 ` `            ``// just increment the length variable. ` `            ``if` `(i == a.length ` `                ``|| a[i] - a[i - ``1``] != ``1``) { ` ` `  `                ``// If the range contains ` `                ``// only one element. ` `                ``// add it into the list. ` `                ``if` `(length == ``1``) { ` `                    ``list.add( ` `                        ``String.valueOf(a[i - length])); ` `                ``} ` `                ``else` `{ ` ` `  `                    ``// Build the range between the first ` `                    ``// element of the range and the ` `                    ``// current previous element as the ` `                    ``// last range. ` `                    ``list.add(a[i - length] ` `                             ``+ ``" -> "` `+ a[i - ``1``]); ` `                ``} ` ` `  `                ``// After finding the first range ` `                ``// initialize the length by 1 to ` `                ``// build the next range. ` `                ``length = ``1``; ` `            ``} ` `            ``else` `{ ` `                ``length++; ` `            ``} ` `        ``} ` ` `  `        ``return` `list; ` `    ``} ` ` `  `    ``// Driver Code. ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` ` `  `        ``// Test Case 1: ` `        ``int``[] arr1 = { ``1``, ``2``, ``3``, ``6``, ``7` `}; ` `        ``System.out.print(consecutiveRanges(arr1)); ` `        ``System.out.println(); ` ` `  `        ``// Test Case 2: ` `        ``int``[] arr2 = { -``1``, ``0``, ``1``, ``2``, ``5``, ``6``, ``8` `}; ` `        ``System.out.print(consecutiveRanges(arr2)); ` `        ``System.out.println(); ` ` `  `        ``// Test Case 3: ` `        ``int``[] arr3 = { -``1``, ``3``, ``4``, ``5``, ``20``, ``21``, ``25` `}; ` `        ``System.out.print(consecutiveRanges(arr3)); ` `    ``} ` `} `

Output:

```[1 -> 3, 6 -> 7]
[-1 -> 2, 5 -> 6, 8]
[-1, 3 -> 5, 20 -> 21, 25]
```

Time Complexity: O(N), where N is the length of the array.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up 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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.