Compress the array into Ranges
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 visualised 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 whle 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: # prthe 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.. |
1 3-6 9-10
Time complexity: O(n)
Recommended Posts:
- Products of ranges in an array
- Search an element in given N ranges
- Find the missing elements from 1 to M in given N ranges | Set-2
- Queries on probability of even or odd number in given ranges
- Maximum occurred integer in n ranges
- Find k-th smallest element in given n ranges
- Maximum occurred integer in n ranges | Set-2
- Find the missing elements from 1 to M in given N ranges
- Queries to check if a number lies in N ranges of L-R
- Find the kth element in the series generated by the given N ranges
- Find a range that covers all the elements of given N ranges
- Find original array from encrypted array (An array of sums of other elements)
- Find an element in array such that sum of left array is equal to sum of right array
- Check whether an array can be fit into another array rearranging the elements in the array
- Minimum cost to reach end of array array when a maximum jump of K index is allowed
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.



