给定一个由N 个整数组成的数组arr[] 和一个正整数K ,任务是检查给定数组是否可以通过重复删除一对中存在的两个元素中较大的元素来减少到单个元素,其绝对差最大为 K 。如果数组可以简化为单个元素,则打印“Yes” 。否则,打印“否” 。
例子:
Input: arr[] = {2, 1, 1, 3}, K = 1
Output: Yes
Explanation:
Operation 1: Select the pair {arr[0], arr[3]} ( = (2, 3), as | 3 – 2 | ≤ 1. Now, remove 3 from the array. The array modifies to {2, 1, 1}.
Operation 2: Select the pair {arr[0], arr[1]} ( = (2, 1), as | 2 – 1 | ≤ 1. Now, remove 2 from the array. The array modifies to {1, 1}.
Operation 3: Remove 1 from the array. The array modifies to {1}.
Therefore, the last remaining array element is 1.
Input: arr[] = {1, 4, 3, 6}, K = 1
Output: No
方法:可以使用贪心方法解决给定的问题。这个想法是在每个可能的移动中删除具有最大值的元素。按照给定的步骤解决问题:
- 按降序对给定的数组arr[]进行排序。
- 在索引[0, N – 2]的范围内遍历数组arr[ ] 。如果arr[i]和arr[i + 1]的绝对值大于K ,则打印“No”并跳出循环。
- 如果循环完全执行,则打印“Yes” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if an array can be
// reduced to single element by removing
// maximum element among any chosen pairs
void canReduceArray(int arr[], int N, int K)
{
// Sort the array in descending order
sort(arr, arr + N, greater());
// Traverse the array
for (int i = 0; i < N - 1; i++) {
// If the absolute difference
// of 2 consecutive array
// elements is greater than K
if (arr[i] - arr[i + 1] > K) {
cout << "No";
return;
}
}
// If the array can be reduced
// to a single element
cout << "Yes";
}
// Driver Code
int main()
{
int arr[] = { 2, 1, 1, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 1;
// Function Call to check
// if an array can be reduced
// to a single element
canReduceArray(arr, N, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to check if an array can be
// reduced to single element by removing
// maximum element among any chosen pairs
static void canReduceArray(int arr[], int N, int K)
{
// Sort the array in descending order
Arrays.sort(arr);
int b[] = new int[N];
int j = N;
for (int i = 0; i < N; i++) {
b[j - 1] = arr[i];
j = j - 1;
}
// Traverse the array
for (int i = 0; i < N - 1; i++) {
// If the absolute difference
// of 2 consecutive array
// elements is greater than K
if (arr[i] - arr[i + 1] > K) {
System.out.print("No");
return;
}
}
// If the array can be reduced
// to a single element
System.out.print("Yes");
}
// Driven Code
public static void main(String[] args)
{
int arr[] = { 2, 1, 1, 3 };
int N = arr.length;
int K = 1;
// Function Call to check
// if an array can be reduced
// to a single element
canReduceArray(arr, N, K);
}
}
// This code is contributed by splevel62
Python3
# Python3 program for the above approach
# Function to check if an array can be
# reduced to single element by removing
# maximum element among any chosen pairs
def canReduceArray(arr, N, K):
# Sort the array in descending order
arr = sorted(arr)
# Traverse the array
for i in range(N - 1):
# If the absolute difference
# of 2 consecutive array
# elements is greater than K
if (arr[i] - arr[i + 1] > K):
print ("No")
return
# If the array can be reduced
# to a single element
print ("Yes")
# Driver Code
if __name__ == '__main__':
arr = [2, 1, 1, 3]
N = len(arr)
K = 1
# Function Call to check
# if an array can be reduced
# to a single element
canReduceArray(arr, N, K)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Function to check if an array can be
// reduced to single element by removing
// maximum element among any chosen pairs
static void canReduceArray(int[] arr, int N,
int K)
{
// Sort the array in descending order
Array.Sort(arr);
int[] b = new int[N];
int j = N;
for(int i = 0; i < N; i++)
{
b[j - 1] = arr[i];
j = j - 1;
}
// Traverse the array
for(int i = 0; i < N - 1; i++)
{
// If the absolute difference
// of 2 consecutive array
// elements is greater than K
if (arr[i] - arr[i + 1] > K)
{
Console.WriteLine("No");
return;
}
}
// If the array can be reduced
// to a single element
Console.WriteLine("Yes");
}
// Driver Code
public static void Main(String []args)
{
int[] arr = { 2, 1, 1, 3 };
int N = arr.Length;
int K = 1;
// Function Call to check
// if an array can be reduced
// to a single element
canReduceArray(arr, N, K);
}
}
// This code is contributed by souravghosh0416
Javascript
Yes
时间复杂度: O(N * log N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live