Given an array of integers of size N, The task is to print the consecutive integers as a range.
Examples:
Input : N = 7, arr=[7, 8, 9, 15, 16, 20, 25]
Output : 7-9 15-16 20 25
Consecutive elements present are[ {7, 8, 9}, {15, 16}, {20}, {25} ]
Hence output the result as 7-9 15-16 20 25Input : N = 6, arr=[1, 2, 3, 4, 5, 6]
Output : 1-6
Approach:
The problem can be easily visualized as a variation of run length encoding problem.
- First sort the array.
- Then, start a while loop for traversing the array to check the consecutive elements. The ending of the consecutive numbers will be denoted by j-1 and start by i at any particular instance.
- Increment i by 1 if it do not falls in while loop otherwise increment it by j+1 so that it jumps to the next ith element which is out of current range.
Below is the implementation of the above approach:
C++
// C++ program to compress the array ranges #include <bits/stdc++.h> using namespace std; // Function to compress the array ranges void compressArr( int arr[], int n) { int i = 0, j = 0; sort(arr, arr + n); while (i < n) { // start iteration from the // ith array element j = i; // loop until arr[i+1] == arr[i] // and increment j while ((j + 1 < n) && (arr[j + 1] == arr[j] + 1)) { j++; } // if the program do not enter into // the above while loop this means that // (i+1)th element is not consecutive // to i th element if (i == j) { cout << arr[i] << " " ; // increment i for next iteration i++; } else { // print the consecutive range found cout << arr[i] << "-" << arr[j] << " " ; // move i jump directly to j+1 i = j + 1; } } } // Driver code int main() { int n = 7; int arr[n] = { 1, 3, 4, 5, 6, 9, 10 }; compressArr(arr, n); } |
Java
// Java program to compress the array ranges import java.util.Arrays; class GFG { // Function to compress the array ranges static void compressArr( int arr[], int n) { int i = 0 , j = 0 ; Arrays.sort(arr); while (i < n) { // start iteration from the // ith array element j = i; // loop until arr[i+1] == arr[i] // and increment j while ((j + 1 < n) && (arr[j + 1 ] == arr[j] + 1 )) { j++; } // if the program do not enter into // the above while loop this means that // (i+1)th element is not consecutive // to i th element if (i == j) { System.out.print( arr[i] + " " ); // increment i for next iteration i++; } else { // print the consecutive range found System.out.print( arr[i] + "-" + arr[j] + " " ); // move i jump directly to j+1 i = j + 1 ; } } } // Driver code public static void main (String[] args) { int n = 7 ; int arr[] = { 1 , 3 , 4 , 5 , 6 , 9 , 10 }; compressArr(arr, n); } } // This code is contributed by anuj_67.. |
Python3
# Python program to compress the array ranges # Function to compress the array ranges def compressArr(arr, n): i = 0 ; j = 0 ; arr.sort(); while (i < n): # start iteration from the # ith array element j = i; # loop until arr[i+1] == arr[i] # and increment j while ((j + 1 < n) and (arr[j + 1 ] = = arr[j] + 1 )): j + = 1 ; # if the program do not enter into # the above while loop this means that # (i+1)th element is not consecutive # to i th element if (i = = j): print (arr[i], end = " " ); # increment i for next iteration i + = 1 ; else : # print the consecutive range found print (arr[i], "-" , arr[j], end = " " ); # move i jump directly to j+1 i = j + 1 ; # Driver code n = 7 ; arr = [ 1 , 3 , 4 , 5 , 6 , 9 , 10 ]; compressArr(arr, n); # This code is contributed by PrinciRaj1992 |
C#
// C# program to compress the array ranges using System; class GFG { // Function to compress the array ranges static void compressArr( int []arr, int n) { int i = 0, j = 0; Array.Sort(arr); while (i < n) { // start iteration from the // ith array element j = i; // loop until arr[i+1] == arr[i] // and increment j while ((j + 1 < n) && (arr[j + 1] == arr[j] + 1)) { j++; } // if the program do not enter into // the above while loop this means that // (i+1)th element is not consecutive // to i th element if (i == j) { Console.Write( arr[i] + " " ); // increment i for next iteration i++; } else { // print the consecutive range found Console.Write( arr[i] + "-" + arr[j] + " " ); // move i jump directly to j+1 i = j + 1; } } } // Driver code public static void Main () { int n = 7; int []arr = { 1, 3, 4, 5, 6, 9, 10 }; compressArr(arr, n); } } // This code is contributed by anuj_67.. |
Javascript
<script> // Javascript program to compress the array ranges // Function to compress the array ranges function compressArr(arr, n) { let i = 0, j = 0; arr.sort( function (a, b){ return a - b}); while (i < n) { // start iteration from the // ith array element j = i; // loop until arr[i+1] == arr[i] // and increment j while ((j + 1 < n) && (arr[j + 1] == arr[j] + 1)) { j++; } // if the program do not enter into // the above while loop this means that // (i+1)th element is not consecutive // to i th element if (i == j) { document.write( arr[i] + " " ); // increment i for next iteration i++; } else { // print the consecutive range found document.write( arr[i] + "-" + arr[j] + " " ); // move i jump directly to j+1 i = j + 1; } } } let n = 7; let arr = [ 1, 3, 4, 5, 6, 9, 10 ]; compressArr(arr, n); </script> |
Output
1 3-6 9-10
Time complexity: O(n log n)
Auxiliary Space: O(1), no extra space is required, so it is a constant.
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!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!