📌  相关文章
📜  至少一个元素重复 K 次的最小子数组的长度

📅  最后修改于: 2022-05-13 01:56:10.180000             🧑  作者: Mango

至少一个元素重复 K 次的最小子数组的长度

给定一个长度为N的数组arr[]和一个整数K 。任务是找到子数组的最小长度,使得子数组的至少一个元素重复 在那个子数组中正好K次。如果不存在这样的子数组,则打印-1

例子:

方法:在这个问题中,观察到当子数组中恰好有一个元素具有K频率时,将达到最小长度,这意味着子数组看起来像[X . . . X]其中X是数组 arr 的一个元素。现在,请按照以下步骤解决此问题:

  1. 创建一个对数组,使得数字(即arr[i] )是第一个元素,其索引(即i )是第二个元素。
  2. 对该数组进行排序。
  3. 现在,创建一个变量mn来存储答案并使用INT_MAX对其进行初始化。
  4. 现在,从i = 0i = (N – K)遍历数组,并在每次迭代中:
    • 如果i(i+K-1)处的元素相等,则使mn等于mn中的最小值以及以下索引之间的差。
  5. 返回mn作为最终答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to return the minimum length of
// subarray having an element exactly K times
int minLengthKRepetitions(int* arr, int& N,
                          int& K)
{
    pair indices[N];
    int mn = INT_MAX, i;
 
    for (i = 0; i < N; i++) {
        indices[i].first = arr[i];
        indices[i].second = i;
    }
 
    sort(indices, indices + N);
    for (i = 0; i <= N - K; i++) {
        if (indices[i].first == indices[i + K - 1].first)
            mn = min(mn, indices[i + K - 1].second
                             - indices[i].second + 1);
    }
 
    return (mn == INT_MAX) ? -1 : mn;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 2, 2, 1 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int K = 3;
     
    cout << minLengthKRepetitions(arr, N, K);
    return 0;
}


Java
// Java code to implement the above approach
import java.util.*;
 
class GFG {
 
  // Function to return the minimum length of
  // subarray having an element exactly K times
  public static int minLengthKRepetitions(int[] arr, int N, int K)
  {
    int[][] indices = new int[N][2] ;
    int mn = Integer.MAX_VALUE, i;
 
    for (i = 0; i < N; i++) {
      indices[i][0] = arr[i];
      indices[i][1] = i;
    }
 
    //Arrays.sort(indices);
    for (i = 0; i <= N - K; i++) {
      if (indices[i][0] == indices[i + K - 1][0])
        mn = Math.min(mn, indices[i + K - 1][1]
                      - indices[i][1] + 1);
    }
 
    return (mn == Integer.MAX_VALUE) ? -1 : mn;
  }
 
  // Driver code
  public static void main (String[] args)
  {
    int[] arr = { 1, 2, 2, 2, 1 };
    int N = arr.length;
    int K = 3;
 
    System.out.println(minLengthKRepetitions(arr, N, K));
  }
}
 
// This code is contributed by Shubham Singh


Python3
# Python program for the above approach
 
# Function to return the minimum length of
# subarray having an element exactly K times
def minLengthKRepetitions(arr, N, K):
     
    indices = [[0,0] for i in range(N)]
    mn = 2**32
     
    for i in range(N):
        indices[i][0] = arr[i]
        indices[i][1] = i
     
    indices.sort()
    for i in range(N - K + 1):
        if (indices[i][0] == indices[i + K - 1][0]):
            mn = min(mn, indices[i + K - 1][1] - indices[i][1] + 1)
             
    return -1 if(mn == 2**32) else mn
 
# Driver code
arr = [1, 2, 2, 2, 1]
N = len(arr)
K = 3
 
print(minLengthKRepetitions(arr, N, K))
 
# This code is contributed by Shubham Singh


C#
// C# code to implement the above approach
using System;
public class GFG
{
 
  // Function to return the minimum length of
  // subarray having an element exactly K times
  public static int minLengthKRepetitions(int[] arr, int N, int K)
  {
    int[,] indices = new int[N,2] ;
    int mn = Int32.MaxValue, i;
 
    for (i = 0; i < N; i++) {
      indices[i, 0] = arr[i];
      indices[i, 1] = i;
    }
 
    //Arrays.sort(indices);
    for (i = 0; i <= N - K; i++) {
      if (indices[i,0] == indices[i + K - 1,0])
        mn = Math.Min(mn, indices[i + K - 1,1]
                      - indices[i,1] + 1);
    }
 
    return (mn == Int32.MaxValue) ? -1 : mn;
  }
 
  // Driver code
  static public void Main ()
  {
    int[] arr = { 1, 2, 2, 2, 1 };
    int N = arr.Length;
    int K = 3;
 
    Console.Write(minLengthKRepetitions(arr, N, K));
  }
}
 
// This code is contributed by Shubham Singh


Javascript


输出
3

时间复杂度: O(N * log N)
辅助空间: O(N)