给定一个由N 个整数组成的数组arr[] ,任务是检查是否可以通过从数组元素对中的两个元素中重复减去任何连续数组元素对的最小值来将数组元素减少到0 。如果可能,则打印“是” 。否则,打印“否” 。
例子:
Input: arr[] = {2, 3, 3, 4, 2}
Output: Yes
Explanation:
One of the possible way is:
Select the pair of indices (0, 1) and perform the operation, which modifies the array as arr[] = {0, 1, 3, 4, 2}.
Select the pair of indices (1, 2), and perform the operation which modifies the array as arr[] = {0, 0, 2, 4, 2}.
Select the pair of indices (2, 3), and perform the operation which modifies the array as arr[] = {0, 0, 0, 2, 2}.
Select the pair of indices (3, 4), and perform the operation which modifies the array as arr[] = {0, 0, 0, 0, 0}.
Therefore, it is possible to convert the array elements to zero. So print “YES”
Input: arr[] = {243, 12, 11}
Output: No
Explanation:
It is impossible to convert every array elements to zero.
方法:根据以下观察可以解决给定的问题:
- 假设arr[] = {a, b, c, d, e, f},那么可以观察到a应该小于等于b,即a≤b满足上述条件。否则,元素a将保持大于0 。
- 现在将数组修改为 arr[] = {0, b – a, c, d, e} , b – a是最左边的元素,因此与上述相同的条件也适用于它,即b – a ≤ c 。
- 所以可以观察到,最后重复上述过程后,偶数索引元素的总和必须等于奇数索引元素的总和。
请按照以下步骤解决问题:
- 遍历范围[1, N-1]并检查是否arr[i] < arr[i – 1] ,然后打印“NO”并中断。否则,将arr[i]递减arr[ i – 1] 。
- 完成上述步骤后,如果以上情况都不满足,则检查arr[N – 1] = 0是否。如果发现是真的,则打印“YES” 。否则,打印“NO” 。
- 使用变量i在[0, N – 2]范围内遍历给定数组arr[]并执行以下操作:
- 如果arr[i] 的值小于arr[i + 1]则打印“No”,因为所有数组元素都不能减少到0 。
- 否则,将arr[i + 1]递减arr [i] 。
- 完成上述步骤后,如果以上情况都不满足并且arr[N – 1] 的值为0 ,则打印“Yes” 。否则,打印“否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if it is possible
// to convert the array
void checkPossible(int* arr, int n)
{
// Traverse the array range [1, N-1]
for (int i = 1; i < n; i++) {
// If arr[i] < arr[i-1]
if (arr[i] < arr[i - 1]) {
cout << "No\n";
return;
}
// Otherwise
else {
// Decrement arr[i] by arr[i-1]
arr[i] -= arr[i - 1];
arr[i - 1] = 0;
}
}
// If arr[n - 1] is not equal to zero
if (arr[n - 1] == 0) {
cout << "Yes\n";
}
// Otherwise
else {
cout << "No\n";
}
}
// Driver Code
int main()
{
int arr[] = { 2, 3, 3, 4, 2 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
checkPossible(arr, N);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
// Function to check if it is possible
// to convert the array
static void checkPossible(int[] arr, int n)
{
// Traverse the array range [1, N-1]
for (int i = 1; i < n; i++)
{
// If arr[i] < arr[i-1]
if (arr[i] < arr[i - 1])
{
System.out.print("No\n");
return;
}
// Otherwise
else
{
// Decrement arr[i] by arr[i-1]
arr[i] -= arr[i - 1];
arr[i - 1] = 0;
}
}
// If arr[n - 1] is not equal to zero
if (arr[n - 1] == 0)
{
System.out.print("Yes\n");
}
// Otherwise
else
{
System.out.print("No\n");
}
}
// Driver Code
public static void main(String args[])
{
int arr[] = { 2, 3, 3, 4, 2 };
int N = arr.length;
// Function Call
checkPossible(arr, N);
}
}
// This code is contributed by splevel62.
Python3
# Python program to implement
# the above approach
# Function to check if it is possible
# to convert the array
def checkPossible(arr, n):
# Traverse the array range [1, N-1]
for i in range(1, n):
# If arr[i] < arr[i-1]
if (arr[i] < arr[i - 1]):
print("No");
return;
# Otherwise
else:
# Decrement arr[i] by arr[i-1]
arr[i] -= arr[i - 1];
arr[i - 1] = 0;
# If arr[n - 1] is not equal to zero
if (arr[n - 1] == 0):
print("Yes");
# Otherwise
else:
print("No");
# Driver Code
if __name__ == '__main__':
arr = [2, 3, 3, 4, 2];
N = len(arr);
# Function Call
checkPossible(arr, N);
# This code is contributed by shikhasingrajput
C#
// C# program to implement
// the above approach
using System;
class GFG
{
// Function to check if it is possible
// to convert the array
static void checkPossible(int[] arr, int n)
{
// Traverse the array range [1, N-1]
for (int i = 1; i < n; i++)
{
// If arr[i] < arr[i-1]
if (arr[i] < arr[i - 1])
{
Console.Write("No\n");
return;
}
// Otherwise
else
{
// Decrement arr[i] by arr[i-1]
arr[i] -= arr[i - 1];
arr[i - 1] = 0;
}
}
// If arr[n - 1] is not equal to zero
if (arr[n - 1] == 0)
{
Console.Write("Yes\n");
}
// Otherwise
else
{
Console.Write("No\n");
}
}
// Driver Code
public static void Main()
{
int[] arr = { 2, 3, 3, 4, 2 };
int N = arr.Length;
// Function Call
checkPossible(arr, N);
}
}
// This code is contributed by susmitakundugoaldanga
Javascript
Yes
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live