Longest equilibrium Subarray of given Array
Last Updated :
23 Feb, 2023
Given an integer array arr of size N[], the task is to find the longest equilibrium subarray i.e. a subarray such that the prefix sum of the remaining array is the same as the suffix sum.
Examples:
Input: N = 3, arr[] = {10, 20, 10}
Output: 1
Explanation: The longest subarray is {20}. The remaining prefix is {10} and suffix is {10}.
Therefore only one element in the subarray.
Input: N = 6, arr[] = {2, 1, 4, 2, 4, 1}
Output: 0
Explanation: The longest subarray is of size 0.
The prefix is {2, 1, 4} and suffix is {2, 4, 1} and both has some 7.
Input: N = 5, arr[] = {1, 2, 4, 8, 16}
Output: -1
Approach: This problem can be solved with two pointer technique based on the following idea:
Traverse from both the end. If the sum of prefix is less then increment the front pointer, otherwise, do the opposite. In this way we will get the minimum number of elements in prefix and suffix. So the subarray will have the maximum length because the remaining array has minimum elements.
Follow the steps mentioned below to implement the idea:
- Let i be the left pointer initially at 0, and j be the right pointer initially at N-1.
- First, we will check if the sum of all elements is 0 or not. If 0 then the whole array will be the subarray.
- Initialize two variable prefixSum = 0 and suffixSum = 0.
- Traverse array from till i not equal to j.
- If prefixSum <= suffixSum, then add arr[i] in prefixSum. And increment i by one.
- Else check that if prefixSum > suffixSum, then add arr[i] in suffixSum. And decrement j by one.
- Now check that if prefixSum equal to suffixSum then return the difference between i and j.
- Otherwise, return -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int isEqual( int n, int arr[])
{
if (n == 1) {
return -1;
}
int sum=0;
for ( int i=0;i<n;i++)
{
sum+=arr[i];
}
if (sum==0)
return n;
int prefixSum = 0, suffixSum = 0;
int i = 0, j = n - 1;
int ans = -1;
while (i <= j) {
if (prefixSum <= suffixSum) {
prefixSum += arr[i];
i++;
}
else if (prefixSum > suffixSum) {
suffixSum += arr[j];
j--;
}
if (prefixSum == suffixSum) {
return j - i + 1;
}
}
if (prefixSum == suffixSum)
return 0;
return -1;
}
int main()
{
int N = 3;
int arr[] = { 10, 20, 10 };
cout << isEqual(N, arr) << "\n" ;
return 0;
}
|
Java
import java.util.*;
public class GFG {
static int isEqual( int n, int [] arr)
{
if (n == 1 ) {
return - 1 ;
}
int sum= 0 ;
for ( int i= 0 ;i<n;i++)
{
sum+=arr[i];
}
if (sum== 0 )
return n;
int prefixSum = 0 , suffixSum = 0 ;
int i = 0 , j = n - 1 ;
int ans = - 1 ;
while (i <= j) {
if (prefixSum <= suffixSum) {
prefixSum += arr[i];
i++;
}
else if (prefixSum > suffixSum) {
suffixSum += arr[j];
j--;
}
if (prefixSum == suffixSum) {
return j - i + 1 ;
}
}
if (prefixSum == suffixSum)
return 0 ;
return - 1 ;
}
public static void main(String args[])
{
int N = 3 ;
int [] arr = { 10 , 20 , 10 };
System.out.println(isEqual(N, arr));
}
}
|
Python3
def isEqual(n, arr):
if (n = = 1 ):
return - 1
Sum = 0
for i in range (n):
Sum = Sum + arr[i]
if ( Sum = = 0 ):
return n
prefixSum,suffixSum = 0 , 0
i,j = 0 ,n - 1
ans = - 1
while (i < = j):
if (prefixSum < = suffixSum):
prefixSum + = arr[i]
i + = 1
elif (prefixSum > suffixSum):
suffixSum + = arr[j]
j - = 1
if (prefixSum = = suffixSum):
return j - i + 1
if (prefixSum = = suffixSum):
return 0
return - 1
N = 3
arr = [ 10 , 20 , 10 ]
print (isEqual(N, arr))
|
C#
using System;
class GFG {
static int isEqual( int n, int [] arr)
{
if (n == 1) {
return -1;
}
int sum=0;
int k;
for (k=0;k<n;k++)
{
sum+=arr[k];
}
if (sum==0)
return n;
int prefixSum = 0, suffixSum = 0;
int i = 0, j = n - 1;
while (i <= j) {
if (prefixSum <= suffixSum) {
prefixSum += arr[i];
i++;
}
else if (prefixSum > suffixSum) {
suffixSum += arr[j];
j--;
}
if (prefixSum == suffixSum) {
return j - i + 1;
}
}
if (prefixSum == suffixSum)
return 0;
return -1;
}
public static void Main()
{
int N = 3;
int [] arr = { 10, 20, 10 };
Console.WriteLine(isEqual(N, arr));
}
}
|
Javascript
<script>
function isEqual(n, arr)
{
if (n == 1) {
return -1;
}
let sum=0;
for (let i=0;i<n;i++)
{
sum+=arr[i];
}
if (sum==0)
return n;
let prefixSum = 0, suffixSum = 0;
let i = 0, j = n - 1;
let ans = -1;
while (i <= j) {
if (prefixSum <= suffixSum) {
prefixSum += arr[i];
i++;
}
else if (prefixSum > suffixSum) {
suffixSum += arr[j];
j--;
}
if (prefixSum == suffixSum) {
return j - i + 1;
}
}
if (prefixSum == suffixSum)
return 0;
return -1;
}
let N = 3;
let arr = [10, 20, 10];
document.write(isEqual(N, arr) + "<br>" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...