检查前半部分的数组总和是否可以被另一半的总和整除,反之亦然
给定一个大小为N的数组arr[] ,任务是检查左子数组的总和是否可以被右子数组的总和整除,反之亦然。如果是则打印Yes ,否则打印No 。这里,左子数组将包含从0到mid=(N-1)/2的字符串,右子数组将包含从mid+1到N-1的字符串。
例子:
Input: arr[] = [1, 2, 3, 4, 5]
Output: No
Explanation:
Sum of left subarray: 1+2+3=6
Sum of right subarray: 4+5=9
So, the sum of neither of them is divisible by the other one.
Input: arr[] = [4, 5, 6, 1, 2, 2]
Output: Yes
Explanation:
Sum of left subarray: 4+5+6=15
Sum of right subarray: 1+2+2=5
So, the sum of left subarray is divisible by the sum of right subarray
方法:按照以下步骤,解决这个问题:
- 创建两个变量sumL和sumR分别存储左子数组和右子数组的和。用0初始化它们。
- 现在,从0到N-1运行一个循环,对于0到(N-1)/2范围内的迭代,将元素添加到sumL中。对于(N-1)/2之后的迭代,添加sumR中的元素。
- 循环结束后,检查 sumL 是否可以被sumR整除,或者sumR是否可以被sumL整除。如果满足这些条件中的任何一个,则打印Yes否则No 。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if the sum of left subarray
// is divisible by the sum of right or vice - versa
bool isDivisible(int* arr, int N)
{
// Variables to store the sum of
// left and right subarrays
int sumL = 0, sumR = 0;
for (int i = 0; i < N; ++i) {
if (i <= (N - 1) / 2) {
sumL += arr[i];
}
else {
sumR += arr[i];
}
}
// If Divisible
if (sumL % sumR == 0
or sumR % sumL == 0) {
return 1;
}
return 0;
}
// Driver Code
int main()
{
int arr[] = { 4, 5, 6, 1, 2, 2 };
int N = sizeof(arr) / sizeof(int);
if (isDivisible(arr, N))
cout << "Yes";
else
cout << "No";
}
Java
// Java program for the above approach
class GFG {
// Function to check if the sum of left subarray
// is divisible by the sum of right or vice - versa
public static boolean isDivisible(int[] arr, int N)
{
// Variables to store the sum of
// left and right subarrays
int sumL = 0, sumR = 0;
for (int i = 0; i < N; ++i) {
if (i <= (N - 1) / 2) {
sumL += arr[i];
} else {
sumR += arr[i];
}
}
// If Divisible
if (sumL % sumR == 0 || sumR % sumL == 0) {
return true;
}
return false;
}
// Driver Code
public static void main(String args[]) {
int[] arr = { 4, 5, 6, 1, 2, 2 };
int N = arr.length;
if (isDivisible(arr, N))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Saurabh Jaiswal
Python3
# Python3 program for the above approach
# Function to check if the sum of left subarray
# is divisible by the sum of right or vice - versa
def isDivisible(arr, N) :
# Variables to store the sum of
# left and right subarrays
sumL = 0; sumR = 0;
for i in range(N) :
if (i <= (N - 1) // 2) :
sumL += arr[i];
else :
sumR += arr[i];
# If Divisible
if (sumL % sumR == 0 or sumR % sumL == 0) :
return 1;
return 0;
# Driver Code
if __name__ == "__main__" :
arr = [ 4, 5, 6, 1, 2, 2 ];
N = len(arr);
if (isDivisible(arr, N)) :
print("Yes");
else :
print("No");
# This code is contributed by AnkThon
C#
// C# program for the above approach
using System;
class GFG {
// Function to check if the sum of left subarray
// is divisible by the sum of right or vice - versa
public static bool isDivisible(int[] arr, int N)
{
// Variables to store the sum of
// left and right subarrays
int sumL = 0, sumR = 0;
for (int i = 0; i < N; ++i) {
if (i <= (N - 1) / 2) {
sumL += arr[i];
} else {
sumR += arr[i];
}
}
// If Divisible
if (sumL % sumR == 0 || sumR % sumL == 0) {
return true;
}
return false;
}
// Driver Code
public static void Main() {
int[] arr = { 4, 5, 6, 1, 2, 2 };
int N = arr.Length;
if (isDivisible(arr, N))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by gfgking.
Javascript
输出
Yes
时间复杂度: O(N)
辅助空间: O(1)