Find all indices of Array having prefix sum greater than suffix sum
Last Updated :
26 Apr, 2023
Given an array arr[] of N integers, the task is to divide find all the indices such that prefix sum (i.e. sum of elements in range [0, i)) is greater than the suffix sum (elements in the range [i, N-1])
Examples:
Input: arr = [10, -3, 4, 6]
Output: [1, 3]
Explanation: Consider index 1. Prefix sum = 10, suffix sum = 7, i.e. (10 > 7)
Consider index 3. Prefix sum = 11, suffix sum = 6, i.e(11 > 6)
Input: arr = [-2, -3, -4, 10]
Output: []
Explanation: There is no index such that prefix sum is greater than suffix sum.
Naive Approach: The basic idea is to consider each index and calculate the prefix and suffix sum for that index. If the prefix sum is greater than the suffix sum, then insert this index into the answer.
Algorithm:
- Initialize an empty list named ‘ans’ to store the valid indices.
- Iterate through each index i from 0 to n-2, where n is the size of the input array ‘arr’.
- Calculate the left sum and right sum of the input array ‘arr’.
- The left sum is the sum of all elements from the 0th index to the ith index inclusive.
- The right sum is the sum of all elements from the (i+1)th index to the (n-1)th index inclusive.
- Check if the left sum is greater than the right sum, then append the index i+1 to the ‘ans’ list.
- Return the ‘ans’ list containing all valid indices.
Note: The algorithm assumes that the input array ‘arr’ contains at least two elements. If the size of the input array is less than 2, the function will return an empty list.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > solve(vector< int >& arr)
{
vector< int > ans;
int size = arr.size();
for ( int idx = 0; idx < size - 1; ++idx) {
int left_sum = 0;
int right_sum = 0;
for ( int left_idx = 0; left_idx < idx + 1;
++left_idx) {
left_sum += arr[left_idx];
}
for ( int right_idx = idx + 1; right_idx < size;
++right_idx) {
right_sum += arr[right_idx];
}
if (left_sum > right_sum)
ans.push_back(idx + 1);
}
return ans;
}
int main()
{
vector< int > arr = { 10, -3, 4, 6 };
vector< int > ans = solve(arr);
for ( auto x : ans)
cout << x << " " ;
return 0;
}
|
Java
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
class GFG {
public static ArrayList<Integer>solve(ArrayList<Integer>arr)
{
ArrayList<Integer>ans = new ArrayList<Integer>();
int size = arr.size();
for ( int idx = 0 ; idx < size - 1 ; ++idx) {
int left_sum = 0 ;
int right_sum = 0 ;
for ( int left_idx = 0 ; left_idx < idx + 1 ;++left_idx) {
left_sum += arr.get(left_idx);
}
for ( int right_idx = idx + 1 ; right_idx < size;
++right_idx) {
right_sum += arr.get(right_idx);
}
if (left_sum > right_sum)
ans.add(idx + 1 );
}
return ans;
}
public static void main (String[] args) {
ArrayList<Integer>arr = new ArrayList<Integer>(Arrays.asList( 10 , - 3 , 4 , 6 ));
ArrayList<Integer>ans = solve(arr);
for ( int x : ans){
System.out.print(x);
System.out.print( " " );
}
}
}
|
Python3
class Solution:
def solve( self , arr):
ans = []
size = len (arr)
for idx in range (size - 1 ):
left_sum = 0
right_sum = 0
for left_idx in range (idx + 1 ):
left_sum + = arr[left_idx]
for right_idx in range (idx + 1 ,
size, 1 ):
right_sum + = arr[right_idx]
if (left_sum > right_sum):
ans.append(idx + 1 )
return (ans)
if __name__ = = '__main__' :
obj = Solution()
arr = [ 10 , - 3 , 4 , 6 ]
ans = obj.solve(arr)
for x in ans:
print (x, end = " " )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static List< int > solve(List< int > arr)
{
List< int > ans = new List< int >();
int size = arr.Count;
for ( int idx = 0 ; idx < size - 1 ; ++idx) {
int left_sum = 0;
int right_sum = 0;
for ( int left_idx = 0 ; left_idx < idx + 1 ; ++left_idx) {
left_sum += arr[left_idx];
}
for ( int right_idx = idx + 1 ; right_idx < size ; ++right_idx) {
right_sum += arr[right_idx];
}
if (left_sum > right_sum){
ans.Add(idx+1);
}
}
return ans;
}
public static void Main( string [] args)
{
List< int > arr = new List< int >{
10, -3, 4, 6
};
List< int > ans = solve(arr);
foreach ( var x in ans){
Console.Write(x + " " );
}
}
}
|
Javascript
<script>
function solve(arr){
let ans = []
let size = arr.length
for (let idx = 0; idx < size - 1; idx++){
let left_sum = 0
let right_sum = 0
for (let left_idx = 0; left_idx < idx + 1; left_idx++)
left_sum += arr[left_idx]
for (let right_idx = idx + 1; right_idx < size; right_idx++)
right_sum += arr[right_idx]
if (left_sum > right_sum)
ans.push(idx + 1)
}
return ans
}
let arr = [10, -3, 4, 6]
let ans = solve(arr)
for (let x of ans)
document.write(x, " " )
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The idea is to calculate the overall sum of the given array and traverse over the array and keep calculating the prefix sum into some variable and suffix sum can be calculate by using formula suffix sum = totalSum – prefix sum. Then check if the prefix sum is equals to the suffix sum or not. If it is equal then keep storing into some data structure for result.
Follow the steps below to implement the above idea:
- Calculate the overall sum in total_sum variable.
- Iterate over the given array arr and following the steps below:
- Keep adding the current element into left_sum variable for prefix sum.
- Calculate suffix sum using formula right_sum= total_sum- left_sum.
- Check if the left_sumis greater than the suffixSum:
- If greater, then store the current index into ansarray.
- Return the ans array.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > solve(vector< int >& arr)
{
vector< int > ans;
int size = arr.size();
int left_sum = 0;
int total_sum = 0;
for ( auto dt : arr)
total_sum += dt;
for ( int idx = 0; idx < size - 1; ++idx) {
left_sum += arr[idx];
int right_sum = total_sum - left_sum;
if (left_sum > right_sum)
ans.push_back(idx + 1);
}
return (ans);
}
int main()
{
vector< int > arr = { 10, -3, 4, 6 };
vector< int > ans = solve(arr);
for ( auto x : ans)
cout << x << " " ;
return 0;
}
|
Java
import java.util.*;
class GFG {
static List<Integer> solve( int arr[])
{
List<Integer> ans = new ArrayList<Integer>();
int size = arr.length;
int left_sum = 0 ;
int total_sum = 0 ;
for ( int i = 0 ; i < size; i++){
total_sum += arr[i];
}
for ( int idx = 0 ; idx < size - 1 ; ++idx) {
left_sum += arr[idx];
int right_sum = total_sum - left_sum;
if (left_sum > right_sum)
ans.add(idx + 1 );
}
return ans;
}
public static void main (String[] args) {
int arr[] = { 10 , - 3 , 4 , 6 };
List<Integer> ans = solve(arr);
for (Integer x : ans)
System.out.print(x + " " );
}
}
|
Python3
class Solution:
def solve( self , arr):
ans = []
size = len (arr)
left_sum = 0
total_sum = sum (arr)
for idx in range (size - 1 ):
left_sum + = arr[idx]
right_sum = total_sum - left_sum
if (left_sum > right_sum):
ans.append(idx + 1 )
return (ans)
if __name__ = = '__main__' :
obj = Solution()
arr = [ 10 , - 3 , 4 , 6 ]
ans = obj.solve(arr)
for x in ans:
print (x, end = " " )
|
C#
using System;
using System.Collections;
class GFG {
static ArrayList solve( int [] arr)
{
ArrayList ans = new ArrayList();
int size = arr.Length;
int left_sum = 0;
int total_sum = 0;
for ( int i = 0; i < size; i++) {
total_sum += arr[i];
}
for ( int idx = 0; idx < size - 1; ++idx) {
left_sum += arr[idx];
int right_sum = total_sum - left_sum;
if (left_sum > right_sum)
ans.Add(idx + 1);
}
return ans;
}
public static void Main()
{
int [] arr = { 10, -3, 4, 6 };
ArrayList ans = solve(arr);
foreach ( int x in ans) Console.Write(x + " " );
}
}
|
Javascript
<script>
function solve(arr)
{
let ans = new Array();
let size = arr.length;
let left_sum = 0;
let total_sum = 0;
for (let i = 0; i < size; i++){
total_sum += arr[i];
}
for (let idx = 0; idx < size - 1; ++idx) {
left_sum += arr[idx];
let right_sum = total_sum - left_sum;
if (left_sum > right_sum)
ans.push(idx + 1);
}
return ans;
}
let arr = [10, -3, 4, 6 ];
let ans = solve(arr);
for (let x in ans){
document.write(ans[x] + " " );
}
</script>
|
Time Complexity: O(N), Where N is the size of the given array
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...