Open In App

Check if array sum of first half is divisible by sum of other half or vice versa

Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. 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.
  2. 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.
  3. 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++




// C++ program for the above approach
 
#include <bits/stdc++.h>
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




<script>
        // JavaScript Program to implement
        // the above approach
 
        // Function to check if the sum of left subarray
        // is divisible by the sum of right or vice - versa
        function isDivisible(arr, N) {
 
            // Variables to store the sum of
            // left and right subarrays
            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 Divisible
            if (sumL % sumR == 0
                || sumR % sumL == 0) {
                return 1;
            }
 
            return 0;
        }
 
        // Driver Code
 
        let arr = [4, 5, 6, 1, 2, 2];
        let N = arr.length;
 
        if (isDivisible(arr, N))
            document.write("Yes");
        else
            document.write("No");
 
    // This code is contributed by Potta Lokesh
    </script>


Output

Yes

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
Previous
Next
Share your thoughts in the comments
Similar Reads