给定大小为N的整数数组,任务是将连续整数打印为范围。
例子:
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
方法:
该问题可以很容易地可视化为游程长度编码问题的一种变体。
- 首先对数组进行排序。
- 然后,启动一个while循环以遍历数组以检查连续的元素。在任何特定情况下,连续数字的结尾将由j-1表示,并由i开头。
- 如果i不落入循环中,则将i递增1,否则将j + 1递增,以使其跳至当前范围之外的下一个ith元素。
下面是上述方法的实现:
C++
// C++ program to compress the array ranges
#include
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..
输出:
1 3-6 9-10
时间复杂度: O(n)