📌  相关文章
📜  所有大小为K的子数组中的最小公共元素

📅  最后修改于: 2021-05-04 14:55:44             🧑  作者: Mango

给定一个由n个不同的整数和一个正整数K组成的数组arr [] ,任务是找到大小为K的所有子数组中出现的最小元素。如果不存在这样的元素,则打印“ -1”

例子:

天真的方法:想法是生成给定大小为K的数组的所有可能的子数组,并在形成的所有子数组中找到公共元素。之后,找到通用元素,然后在其中打印最小的元素。如果在所有子数组中都没有找到共同的元素,则打印“ -1”

时间复杂度: O(N 2 )
辅助空间: O(1)

高效的方法:首先检查所有子数组中公共元素的条件,如果存在该元素,则该条件应在给定数组的[N – K,K]范围内。以下是我们找不到任何此类最小元素的条件:

  • 如果N为奇数且K≥(N +1)/ 2
  • 如果N为偶数且K≥((N + 1)/ 2)+1

如果上述条件不满足,则最小元素位于[N – K,K]范围内。因此,在此范围内迭代给定数组,并在其中打印最小元素的值。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the minimum common
// among all the subarray of size K
// from the given array arr[]
void minCommonElementInSubarrays(
    int arr[], int N, int K)
{
    int c;
 
    // If N is odd then update
    // C as K >= (N + 1)/2
    if ((N + 1) % 2 == 0) {
        c = (N + 1) / 2;
    }
 
    // If N is even then update
    // C as K >= (N + 1)/2 + 1
    else {
        c = (N + 1) / 2 + 1;
    }
 
    // If K < C, return "=1"
    if (K < c) {
        cout << -1;
    }
 
    // Otherwise
    else {
 
        // Initialize result variable
        int ar = INT_MAX;
 
        // Find minimum element
        for (int i = N - K; i < K; i++) {
            ar = min(arr[i], ar);
        }
 
        // Print the minimum value
        cout << ar;
    }
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 1, 2, 3, 4, 5 };
 
    // Given K
    int K = 4;
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    minCommonElementInSubarrays(arr, N, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
   
// Function to find the minimum common
// among all the subarray of size K
// from the given array arr[]
static void minCommonElementInSubarrays(int arr[],
                                        int N, int K)
{
    int c;
   
    // If N is odd then update
    // C as K >= (N + 1)/2
    if ((N + 1) % 2 == 0)
    {
        c = (N + 1) / 2;
    }
   
    // If N is even then update
    // C as K >= (N + 1)/2 + 1
    else
    {
        c = (N + 1) / 2 + 1;
    }
   
    // If K < C, return "=1"
    if (K < c)
    {
        System.out.print(-1);
    }
   
    // Otherwise
    else
    {
         
        // Initialize result variable
        int ar = Integer.MAX_VALUE;
   
        // Find minimum element
        for(int i = N - K; i < K; i++)
        {
            ar = Math.min(arr[i], ar);
        }
   
        // Print the minimum value
        System.out.print(ar);
    }
}
   
// Driver Code
public static void main (String[] args)
{
     
    // Given array arr[]
    int arr[] = { 1, 2, 3, 4, 5 };
   
    // Given K
    int K = 4;
   
    int N = arr.length;
   
    // Function call
    minCommonElementInSubarrays(arr, N, K);
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program for the above approach
import sys
 
# Function to find the minimum common
# among all the subarray of size K
# from the given array arr[]
def minCommonElementInSubarrays(arr, N, K):
     
    c = 0
 
    # If N is odd then update
    # C as K >= (N + 1)/2
    if ((N + 1) % 2 == 0):
        c = (N + 1) // 2
 
    # If N is even then update
    # C as K >= (N + 1)/2 + 1
    else:
        c = (N + 1) / 2 + 1
 
    # If K < C, return "=1"
    if (K < c):
        print(-1)
 
    # Otherwise
    else:
         
        # Initialize result variable
        ar = sys.maxsize
 
        # Find minimum element
        for i in range(N - K, K):
            ar = min(arr[i], ar)
 
        # Print the minimum value
        print(ar)
 
# Driver Code
if __name__ == '__main__':
     
    # Given array arr[]
    arr = [ 1, 2, 3, 4, 5 ]
 
    # Given K
    K = 4
 
    N = len(arr)
 
    # Function call
    minCommonElementInSubarrays(arr, N, K)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG{
   
// Function to find the minimum common
// among all the subarray of size K
// from the given array arr[]
static void minCommonElementInSubarrays(int[] arr,
                                        int N, int K)
{
    int c;
   
    // If N is odd then update
    // C as K >= (N + 1)/2
    if ((N + 1) % 2 == 0)
    {
        c = (N + 1) / 2;
    }
   
    // If N is even then update
    // C as K >= (N + 1)/2 + 1
    else
    {
        c = (N + 1) / 2 + 1;
    }
   
    // If K < C, return "=1"
    if (K < c)
    {
        Console.Write(-1);
    }
   
    // Otherwise
    else
    {
         
        // Initialize result variable
        int ar = Int32.MaxValue;
   
        // Find minimum element
        for(int i = N - K; i < K; i++)
        {
            ar = Math.Min(arr[i], ar);
        }
         
        // Print the minimum value
        Console.Write(ar);
    }
}
   
// Driver Code
public static void Main ()
{
     
    // Given array arr[]
    int[] arr = { 1, 2, 3, 4, 5 };
   
    // Given K
    int K = 4;
   
    int N = arr.Length;
   
    // Function call
    minCommonElementInSubarrays(arr, N, K);
}
}
 
// This code is contributed by sanjoy_62


输出:
2



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