Check if sum of arr[i] / j for all possible pairs (i, j) in an array is 0 or not
Last Updated :
29 Apr, 2021
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>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Find the sum of all possible pairs in an array of N elements Given an array arr[] of N integers, the task is to find the sum of all the pairs possible from the given array. Note that, (arr[i], arr[i]) is also considered as a valid pair.(arr[i], arr[j]) and (arr[j], arr[i]) are considered as two different pairs.Examples: Input: arr[] = {1, 2} Output: 12 All va
7 min read
Count of pairs (i, j) in the array such that arr[i] is a factor of arr[j] Given an array of integers arr, the task is to calculate the number of pairs (i, j) where i < j such that arr[j] % arr[i] = 0.Examples: Input: arr[] = {1, 2, 3, 4, 5} Output: 5Input: arr[] = {1, 1, 2, 2, 3, 3} Output: 11 Approach 1: Iterate over all pairs of the array and keep incrementing the co
9 min read
Count number of pairs (i, j) from an array such that arr[i] * j = arr[j] * i Given an array arr[] of size N, the task is to count the number of pairs (i, j) possible from the array such that arr[j] * i = arr[i] * j, where 1 ⤠i < j ⤠N. Examples: Input: arr[] = {1, 3, 5, 6, 5}Output: 2Explanation: Pair (1, 5) satisfies the condition, since arr[1] * 5 = arr[5] * 1.Pair (2,
9 min read
Sum of Bitwise And of all pairs in a given array Given an array "arr[0..n-1]" of integers, calculate sum of "arr[i] & arr[j]" for all the pairs in the given where i < j. Here & is bitwise AND operator. Expected time complexity is O(n). Examples : Input: arr[] = {5, 10, 15} Output: 15 Required Value = (5 & 10) + (5 & 15) + (10
13 min read
Sum of Bitwise OR of all pairs in a given array Given an array "arr[0..n-1]" of integers. The task is to calculate the sum of Bitwise OR of all pairs, i.e. calculate the sum of "arr[i] | arr[j]" for all the pairs in the given array where i < j. Here '|' is a bitwise OR operator. The expected time complexity is O(n). Examples: Input: arr[] = {5
13 min read