Open In App
Related Articles

Check if sum of arr[i] / j for all possible pairs (i, j) in an array is 0 or not

Improve Article
Improve
Save Article
Save
Like Article
Like

Given an array arr[] consisting of N integers, the task is to check if the sum of all possible values of (arr[i] / j) for all pairs (i, j) such that 0 < i ? j < (N – 1) is 0 or not. If found to be true, then print “Yes”. Otherwise, print “No”.

Examples:

Input: arr[] = {1, -1, 3, -2, -1}
Output: Yes
Explanation:
For all possible pairs (i, j), such that 0 < i <= j < (N – 1), required sum = 1/1 + -1/2 + 3/3 + -2/4 + -1/5 + -1/2 + 3/3 + -2/4 + -1/5 + 3/3 + -2/4 + -1/5 + -2/ 4 + -1/5 + -1/5 = 0.

Input: arr[] = {1, 2, 3, 4, 5}
Output: No

Approach: The given problem can be solved based on the following observations:

  • For every possible value of i over the range [0, N – 1] and for every possible values of j following are the expressions:
    • j = 1: \frac{a_1}{1}  + \frac{a_2}{2} + \frac{a_3}{3} .... +\frac{a_n}{n}
    • j = 2:\frac{a_2}{2} + \frac{a_3}{3} .... +\frac{a_n}{n}
    • j = 3:\frac{a_3}{3} .... +\frac{a_n}{n} 3rd line and so on…
  • Therefore, the sum of all the above expression is given by:

=> \frac{a_1}{1} + \frac{2*a_2}{2} + \frac{3*a_3}{3} ...+ \frac{n*a_n}{n}

=> a_1 + a_2 + a_3 + ... + a_n

From the above observations, if the sum of the array is 0, then print Yes. Otherwise, print 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 sum of all
// values of (arr[i]/j) for all
// 0 < i <= j < (N - 1) is 0 or not
void check(int arr[], int N)
{
    // Stores the required sum
    int sum = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++)
        sum += arr[i];
 
    // If the sum is equal to 0
    if (sum == 0)
        cout << "Yes";
 
    // Otherwise
    else
        cout << "No";
}
 
// Driver Code
int main()
{
    int arr[] = { 1, -1, 3, -2, -1 };
    int N = sizeof(arr) / sizeof(arr[0]);
    check(arr, N);
 
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to check if sum of all
// values of (arr[i]/j) for all
// 0 < i <= j < (N - 1) is 0 or not
static void check(int arr[], int N)
{
     
    // Stores the required sum
    int sum = 0;
 
    // Traverse the array
    for(int i = 0; i < N; i++)
        sum += arr[i];
 
    // If the sum is equal to 0
    if (sum == 0)
        System.out.println("Yes");
 
    // Otherwise
    else
        System.out.println("No");
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, -1, 3, -2, -1 };
    int N = arr.length;
     
    check(arr, N);
}
}
 
// This code is contributed by Kingash


Python3




# Python3 program for the above approach
 
# Function to check if sum of all
# values of (arr[i]/j) for all
# 0 < i <= j < (N - 1) is 0 or not
def check(arr, N):
     
    # Stores the required sum
    sum = 0
 
    # Traverse the array
    for i in range(N):
        sum += arr[i]
 
    # If the sum is equal to 0
    if (sum == 0):
        print("Yes")
 
    # Otherwise
    else:
        print("No")
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 1, -1, 3, -2, -1 ]
    N = len(arr)
     
    check(arr, N)
 
# This code is contributed by mohit kumar 29


C#




// C# program for the above approach
using System;
 
class GFG {
 
    // Function to check if sum of all
    // values of (arr[i]/j) for all
    // 0 < i <= j < (N - 1) is 0 or not
    static void check(int[] arr, int N)
    {
 
        // Stores the required sum
        int sum = 0;
 
        // Traverse the array
        for (int i = 0; i < N; i++)
            sum += arr[i];
 
        // If the sum is equal to 0
        if (sum == 0)
            Console.WriteLine("Yes");
 
        // Otherwise
        else
            Console.WriteLine("No");
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        int[] arr = { 1, -1, 3, -2, -1 };
        int N = arr.Length;
 
        check(arr, N);
    }
}
 
// This code is contributed by ukasp.


Javascript




<script>
// javascript program for the above approach
    // Function to check if sum of all
    // values of (arr[i]/j) for all
    // 0 < i <= j < (N - 1) is 0 or not
    function check(arr , N) {
 
        // Stores the required sum
        var sum = 0;
 
        // Traverse the array
        for (i = 0; i < N; i++)
            sum += arr[i];
 
        // If the sum is equal to 0
        if (sum == 0)
            document.write("Yes");
 
        // Otherwise
        else
            document.write("No");
    }
 
    // Driver Code
     
        var arr = [ 1, -1, 3, -2, -1 ];
        var N = arr.length;
 
        check(arr, N);
 
// This code contributed by umadevi9616
</script>


Output: 

Yes

 

Time Complexity: O(N)
Auxiliary Space: O(1)


Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Last Updated : 29 Apr, 2021
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials