Check if array sum of first half is divisible by sum of other half or vice versa
Given an array arr[] of size N, the task is to check if the sum of the left subarray is divisible by the sum of the right subarray or vice-versa. Print Yes if it was, otherwise No. Here, the left subarray will contain the string from 0 to mid=(N-1)/2 and the right subarray will contain the string from mid+1 to N-1.
Example:
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
Approach: Follow the below steps, to solve this problem:
- Create two variables sumL and sumR to store the sum of the left subarray and of the right subarray respectively. Initialise both of them with 0.
- Now, run a loop from 0 to N-1 and for the iterations in the range of 0 to (N-1)/2, add the elements into sumL. And for the iterations after (N-1)/2, add the elements in sumR.
- After the loop end, check if sumL is divisible by sumR, or if sumR is divisible by sumL. If any of these conditions satisfies, then print Yes otherwise No.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
bool isDivisible( int * arr, int N)
{
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 (sumL % sumR == 0
or sumR % sumL == 0) {
return 1;
}
return 0;
}
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
class GFG {
public static boolean isDivisible( int [] arr, int N)
{
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 (sumL % sumR == 0 || sumR % sumL == 0 ) {
return true ;
}
return false ;
}
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" );
}
}
|
Python3
def isDivisible(arr, N) :
sumL = 0 ; sumR = 0 ;
for i in range (N) :
if (i < = (N - 1 ) / / 2 ) :
sumL + = arr[i];
else :
sumR + = arr[i];
if (sumL % sumR = = 0 or sumR % sumL = = 0 ) :
return 1 ;
return 0 ;
if __name__ = = "__main__" :
arr = [ 4 , 5 , 6 , 1 , 2 , 2 ];
N = len (arr);
if (isDivisible(arr, N)) :
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG {
public static bool isDivisible( int [] arr, int N)
{
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 (sumL % sumR == 0 || sumR % sumL == 0) {
return true ;
}
return false ;
}
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" );
}
}
|
Javascript
<script>
function isDivisible(arr, N) {
let sumL = 0, sumR = 0;
for (let i = 0; i < N; ++i) {
if (i <= (N - 1) / 2) {
sumL += arr[i];
}
else {
sumR += arr[i];
}
}
if (sumL % sumR == 0
|| sumR % sumL == 0) {
return 1;
}
return 0;
}
let arr = [4, 5, 6, 1, 2, 2];
let N = arr.length;
if (isDivisible(arr, N))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N), as we are using a loop to traverse N times.
Auxiliary Space: O(1), as we are not using any extra space.
Last Updated :
31 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...