Balanced Array | Practice | GeeksforGeeks

Given an array of even size N, task is to find minimum value that can be added to an element so that array become balanced. An array is balanced if the sum of the left half of the array elements is equal to the sum of right half. 

Example 1:
Input:
N = 4
arr[] = {1, 5, 3, 2}
Output: 1
Explanation: 
Sum of first 2 elements is 1 + 5  = 6, 
Sum of last 2 elements is 3 + 2  = 5,
To make the array balanced you can add 1.

Example 2:
Input:
N = 6
arr[] = { 1, 2, 1, 2, 1, 3 }
Output: 2
Explanation:
Sum of first 3 elements is 1 + 2 + 1 = 4, 
Sum of last three elements is 2 + 1 + 3 = 6,
To make the array balanced you can add 2.

 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function minValueToBalance() which takes the array a[] and N as inputs and returns the desired result.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
2<=N<=107
N%2==0
 
+1
harshscode6 days ago
int sum1=0,sum2=0;
           int l=0,h=n-1;
           while(l<h)
           {
               sum1+=a[l];
               sum2+=a[h];
               l++;
               h--;
           }
           return abs(sum1-sum2);
0
sumitk071 week ago
int minValueToBalance(int a[], int n)
    {
       //code here.
       int low = 0,high = n-1,suml = 0,sumr= 0;
       while(low<high){
           suml = suml + a[low];
           sumr = sumr + a[high];
           low++;
           high--;
       }
       int ans = abs(suml-sumr);
       return ans;
    }
0
mestryruturaj1 week ago
Java: Single Loop
class Solution
{
    long minValueToBalance(long a[] ,int n)
    {
        int sum = 0;
        for (int i = 0; i < n; i++) {
            sum += (i < n/2) ? a[i] : -a[i];
        }
        
        return Math.abs(sum);
    }
}
0
shubhamshrivas11 week ago
 def minValueToBalance(self,a,n):       #code here.       mid = n//2       s1,s2 =0,0       for i in range(0,mid):           s1 = s1 + a[i]       for j in range(mid,n):           s2= s2+a[j]                  return abs(s1-s2)  
0
mehraparthak01 week ago
    int minValueToBalance(int a[], int n)   {      int rightsum=0,leftsum=0;      for(int i=0;i<n/2;i++){          leftsum+=a[i];                }      for(int i=n-1;i>=n/2;i--){          rightsum+=a[i];      }      int minvalue=abs(leftsum-rightsum);      return minvalue;   }
0
mayank180919991 week ago
  int minValueToBalance(int a[], int n)
    {
       //code here.
       int sum1=0,sum2=0;
       for(int i=0;i<n/2;i++){
           sum1=sum1+a[i];
       }
       for(int i=n/2;i<n;i++){
           sum2=sum2+a[i];
       }
      return abs(sum1-sum2);
    }
0
navins3 weeks ago
Python CODE:
class Solution:   def minValueToBalance(self,a,n):       #code here.       i=0       j=n//2       su1=0       su2=0       while i<n//2:           su1+=a[i]           i+=1       while j<n:           su2+=a[j]           j+=1       return abs(su1-su2)
 
+1
alpha_hydranoid4 weeks ago
basic C++ solution
time : 0.2 sec
 
int minValueToBalance(int a[], int n)   {      //code here.      int sum1=0,sum2=0;      for(int i=0;i<n/2;i++)      {          sum1=sum1+a[i];      }      for(int j=n/2;j<n;j++)      {          sum2=sum2+a[j];      }      int diff=sum2-sum1;      int nd=0;      if(diff<0)      {          nd=(-1)*diff;      }      else      {          nd=diff;      }      return nd;   }
0
atif836141 month ago
basic java solution :
if(n%2!=0){           return 0;       }       long sum1=0, sum2=0;       int mid = (0+n)/2;       for(int i=0;i<mid;i++){           sum1 =sum1+a[i];       }       for(int j=mid; j<n;j++){           sum2 =sum2+a[j];       }       long res =(sum1-sum2);       return Math.abs(res);   }
0
hasnainraza1998hr1 month ago
C++, 0.2
int minValueToBalance(int a[], int n)   {      int firstHalf=0,secondHalf=0;      for(int i=0,j=n/2;i<n/2;i++,j++){          firstHalf += a[i];          secondHalf += a[j];      }      return abs(firstHalf-secondHalf);   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.