📜  数组中可以形成的最小连续序列数

📅  最后修改于: 2021-10-26 06:59:48             🧑  作者: Mango

给定一个整数数组。任务是找到可以使用数组元素形成的最小连续序列数。
例子:

Input: arr[] = { -3, -2, -1, 0, 2 }
Output: 2
Consecutive sequences are (-3, -2, -1, 0), (2).

Input: arr[] = { 3, 4, 0, 2, 6, 5, 10 }
Output: 3
Consecutive sequences are (0), {2, 3, 4, 5, 6} and {10}

方法:

  • 对数组进行排序。
  • 迭代数组,并检查当前元素是否仅比下一个元素小 1。
  • 如果是,则将计数增加 1。
  • 返回连续序列的最终计数。

以下是上述方法的实现:

C++
// C++ program find the minimum number of consecutive
// sequences in an array
#include 
using namespace std;
 
int countSequences(int arr[], int n)
{
    int count = 1;
 
    sort(arr, arr + n);
 
    for (int i = 0; i < n - 1; i++)
        if (arr[i] + 1 != arr[i + 1])
            count++;
 
    return count;
}
 
// Driver program
int main()
{
    int arr[] = { 1, 7, 3, 5, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // function call to print required answer
    cout << countSequences(arr, n);
    return 0;
}


Java
// Java  program find the minimum number of consecutive
// sequences in an array
 
import java.util.Arrays;
import java.io.*;
 
class GFG {
     
static int countSequences(int arr[], int n)
{
    int count = 1;
 
    Arrays.sort(arr);
 
    for (int i = 0; i < n - 1; i++)
        if (arr[i] + 1 != arr[i + 1])
            count++;
 
    return count;
}
 
// Driver program
    public static void main (String[] args) {
 
    int arr[] = { 1, 7, 3, 5, 10 };
    int n = arr.length;
    // function call to print required answer
    System.out.println( countSequences(arr, n));
 
    }
//This code is contributed by ajit.   
}


Python3
# Python3 program find the minimum number of consecutive
# sequences in an array
 
def countSequences(arr, n) :
    count = 1
 
    arr.sort()
 
    for i in range( n -1) :
        if (arr[i] + 1 != arr[i + 1]) :
            count += 1
 
    return count
  
 
# Driver program
if __name__ == "__main__" :
 
    arr = [ 1, 7, 3, 5, 10 ]
    n = len(arr)
 
    # function call to print required answer
    print(countSequences(arr, n))
 
# This code is contributed by Ryuga


C#
// C# program find the minimum number of consecutive
// sequences in an array
 using System;
class GFG {
      
static int countSequences(int []arr, int n)
{
    int count = 1;
  
    Array.Sort(arr);
  
    for (int i = 0; i < n - 1; i++)
        if (arr[i] + 1 != arr[i + 1])
            count++;
  
    return count;
}
  
// Driver program
    static public void Main (String []args) {
  
    int []arr = { 1, 7, 3, 5, 10 };
    int n = arr.Length;
    // function call to print required answer
    Console.WriteLine( countSequences(arr, n));
  
    }
}
//This code is contributed by Arnab Kundu


PHP


Javascript


输出:

5

时间复杂度: O(n log n),其中 n 是数组的大小。

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程