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 25



Input : 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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..

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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..

chevron_right


Output:

1 3-6 9-10

Time complexity: O(n)



My Personal Notes arrow_drop_up

Competitive Programmer, Full Stack Developer, Technical Content Writer, Machine Learner

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.



Improved By : vt_m, princiraj1992, nidhi_biet