给定一个由N 个不同整数和一个正整数K组成的数组arr[] ,任务是找到出现在所有大小为K 的子数组中的最小元素。如果不存在这样的元素,则打印“-1” 。
例子:
Input: arr[] = {1, 2, 3, 4, 5}, K = 4
Output: 2
Explanation:
The subarrays of size 4 are {1, 2, 3, 4} and {2, 3, 4, 5}. The common elements in the above subarrays are {2, 3, 4}.
The minimum of the above common element is 2.
Input: arr[] = {1, 2, 3, 4, 5}, K = 2
Output: -1
Explanation:
The subarrays of size 2 are {1, 2}, {2, 3}, {3, 4}, {4, 5}. Since there is no common element, print -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
Javascript
2
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live