Compress the array into Ranges
Last Updated :
05 Dec, 2022
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 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++
#include <bits/stdc++.h>
using namespace std;
void compressArr( int arr[], int n)
{
int i = 0, j = 0;
sort(arr, arr + n);
while (i < n) {
j = i;
while ((j + 1 < n) &&
(arr[j + 1] == arr[j] + 1)) {
j++;
}
if (i == j) {
cout << arr[i] << " " ;
i++;
}
else {
cout << arr[i] << "-" << arr[j] << " " ;
i = j + 1;
}
}
}
int main()
{
int n = 7;
int arr[n] = { 1, 3, 4, 5, 6, 9, 10 };
compressArr(arr, n);
}
|
Java
import java.util.Arrays;
class GFG
{
static void compressArr( int arr[], int n)
{
int i = 0 , j = 0 ;
Arrays.sort(arr);
while (i < n)
{
j = i;
while ((j + 1 < n) &&
(arr[j + 1 ] == arr[j] + 1 ))
{
j++;
}
if (i == j)
{
System.out.print( arr[i] + " " );
i++;
}
else
{
System.out.print( arr[i] + "-" + arr[j] + " " );
i = j + 1 ;
}
}
}
public static void main (String[] args)
{
int n = 7 ;
int arr[] = { 1 , 3 , 4 , 5 , 6 , 9 , 10 };
compressArr(arr, n);
}
}
|
Python3
def compressArr(arr, n):
i = 0 ;
j = 0 ;
arr.sort();
while (i < n):
j = i;
while ((j + 1 < n) and
(arr[j + 1 ] = = arr[j] + 1 )):
j + = 1 ;
if (i = = j):
print (arr[i], end = " " );
i + = 1 ;
else :
print (arr[i], "-" , arr[j], end = " " );
i = j + 1 ;
n = 7 ;
arr = [ 1 , 3 , 4 , 5 , 6 , 9 , 10 ];
compressArr(arr, n);
|
C#
using System;
class GFG
{
static void compressArr( int []arr, int n)
{
int i = 0, j = 0;
Array.Sort(arr);
while (i < n)
{
j = i;
while ((j + 1 < n) &&
(arr[j + 1] == arr[j] + 1))
{
j++;
}
if (i == j)
{
Console.Write( arr[i] + " " );
i++;
}
else
{
Console.Write( arr[i] + "-" + arr[j] + " " );
i = j + 1;
}
}
}
public static void Main ()
{
int n = 7;
int []arr = { 1, 3, 4, 5, 6, 9, 10 };
compressArr(arr, n);
}
}
|
Javascript
<script>
function compressArr(arr, n)
{
let i = 0, j = 0;
arr.sort( function (a, b){ return a - b});
while (i < n)
{
j = i;
while ((j + 1 < n) && (arr[j + 1] == arr[j] + 1))
{
j++;
}
if (i == j)
{
document.write( arr[i] + " " );
i++;
}
else
{
document.write( arr[i] + "-" + arr[j] + " " );
i = j + 1;
}
}
}
let n = 7;
let arr = [ 1, 3, 4, 5, 6, 9, 10 ];
compressArr(arr, n);
</script>
|
Time complexity: O(n log n)
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...