给定一个由不同的正数组成的数组,任务是从该数组计算子集(或子序列)的数量,以使每个子集包含连续的数字。
例子:
Input : arr[] = {100, 56, 5, 6, 102, 58,
101, 57, 7, 103, 59}
Output : 3
{5, 6, 7}, { 56, 57, 58, 59}, {100, 101, 102, 103}
are 3 subset in which numbers are consecutive.
Input : arr[] = {10, 100, 105}
Output : 3
{10}, {100} and {105} are 3 subset in which
numbers are consecutive.
这个想法是对数组进行排序并遍历排序后的数组以计算此类子集的数量。要计算这些子集的数量,我们需要计算连续的数字,以使它们之间的差不等于1。
以下是查找包含连续数字的子集的数量的算法:
1. Sort the array arr[ ] and count = 1.
2. Traverse the sorted array and for each element arr[i].
If arr[i] + 1 != arr[i+1],
then increment the count by one.
3. Return the count.
以下是此方法的实现:
C++
// C++ program to find number of subset containing
// consecutive numbers
#include
using namespace std;
// Returns count of subsets with consecutive numbers
int numofsubset(int arr[], int n)
{
// Sort the array so that elements which are
// consecutive in nature became consecutive
// in the array.
sort(arr, arr + n);
int count = 1; // Initialize result
for (int i = 0; i < n - 1; i++) {
// Check if there is beginning of another
// subset of consecutive number
if (arr[i] + 1 != arr[i + 1])
count++;
}
return count;
}
// Driven Program
int main()
{
int arr[] = { 100, 56, 5, 6, 102, 58, 101,
57, 7, 103, 59 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << numofsubset(arr, n) << endl;
return 0;
}
Java
// Java program to find number of subset
// containing consecutive numbers
import java.util.*;
class GFG {
// Returns count of subsets with consecutive numbers
static int numofsubset(int arr[], int n)
{
// Sort the array so that elements
// which are consecutive in nature
// became consecutive in the array.
Arrays.sort(arr);
// Initialize result
int count = 1;
for (int i = 0; i < n - 1; i++) {
// Check if there is beginning
// of another subset of
// consecutive number
if (arr[i] + 1 != arr[i + 1])
count++;
}
return count;
}
// Driven Program
public static void main(String[] args)
{
int arr[] = { 100, 56, 5, 6, 102, 58, 101,
57, 7, 103, 59 };
int n = arr.length;
System.out.println(numofsubset(arr, n));
}
}
// This code is contributed by prerna saini.
Python
# Python program to find number of subset containing
# consecutive numbers
def numofsubset(arr, n):
# Sort the array so that elements which are consecutive
# in nature became consecutive in the array.
x = sorted(arr)
count = 1
for i in range(0, n-1):
# Check if there is beginning of another subset of
# consecutive number
if (x[i] + 1 != x[i + 1]):
count = count + 1
return count
# Driven Program
arr = [ 100, 56, 5, 6, 102, 58, 101, 57, 7, 103, 59 ]
n = len(arr)
print numofsubset(arr, n)
# This code is contributed by Afzal Ansari.
C#
// C# program to find number of subset
// containing consecutive numbers
using System;
class GFG {
// Returns count of subsets with
// consecutive numbers
static int numofsubset(int[] arr, int n)
{
// Sort the array so that elements
// which are consecutive in nature
// became consecutive in the array.
Array.Sort(arr);
// Initialize result
int count = 1;
for (int i = 0; i < n - 1; i++) {
// Check if there is beginning
// of another subset of
// consecutive number
if (arr[i] + 1 != arr[i + 1])
count++;
}
return count;
}
// Driven Program
public static void Main()
{
int[] arr = { 100, 56, 5, 6, 102, 58, 101,
57, 7, 103, 59 };
int n = arr.Length;
Console.WriteLine(numofsubset(arr, n));
}
}
// This code is contributed by vt_m.
PHP
输出:
3
时间复杂度: O(nlogn)