Maximum equilibrium sum in an array - GeeksforGeeks

CoursesFor Working ProfessionalsLIVEDSA Live ClassesSystem DesignJava Backend DevelopmentFull Stack LIVEExplore MoreSelf-PacedDSA- Self PacedSDE TheoryMust-Do Coding QuestionsExplore MoreFor StudentsLIVECompetitive ProgrammingData Structures with C++Data ScienceExplore MoreSelf-PacedDSA- Self PacedCIPJAVA / Python / C++Explore MoreSchool CoursesSchool GuidePython ProgrammingLearn To Make AppsExplore moreAll Courses
For Working ProfessionalsLIVEDSA Live ClassesSystem DesignJava Backend DevelopmentFull Stack LIVEExplore MoreSelf-PacedDSA- Self PacedSDE TheoryMust-Do Coding QuestionsExplore More
LIVEDSA Live ClassesSystem DesignJava Backend DevelopmentFull Stack LIVEExplore More
DSA Live Classes
System Design
Java Backend Development
Full Stack LIVE
Explore More
Self-PacedDSA- Self PacedSDE TheoryMust-Do Coding QuestionsExplore More
DSA- Self Paced
SDE Theory
Must-Do Coding Questions
Explore More
For StudentsLIVECompetitive ProgrammingData Structures with C++Data ScienceExplore MoreSelf-PacedDSA- Self PacedCIPJAVA / Python / C++Explore More
LIVECompetitive ProgrammingData Structures with C++Data ScienceExplore More
Competitive Programming
Data Structures with C++
Data Science
Explore More
Self-PacedDSA- Self PacedCIPJAVA / Python / C++Explore More
DSA- Self Paced
CIP
JAVA / Python / C++
Explore More
School CoursesSchool GuidePython ProgrammingLearn To Make AppsExplore more
School Guide
Python Programming
Learn To Make Apps
Explore more
All Courses
TutorialsAlgorithmsAnalysis of AlgorithmsAsymptotic AnalysisWorst, Average and Best CasesAsymptotic NotationsLittle o and little omega notationsLower and Upper Bound TheoryAnalysis of LoopsSolving RecurrencesAmortized AnalysisWhat does 'Space Complexity' mean ?Pseudo-polynomial AlgorithmsPolynomial Time Approximation SchemeA Time Complexity QuestionSearching AlgorithmsSorting AlgorithmsGraph AlgorithmsPattern SearchingGeometric AlgorithmsMathematicalBitwise AlgorithmsRandomized AlgorithmsGreedy AlgorithmsDynamic ProgrammingDivide and ConquerBacktrackingBranch and BoundAll AlgorithmsData StructuresArraysLinked ListStackQueueBinary TreeBinary Search TreeHeapHashingGraphAdvanced Data StructureMatrixStringsAll Data StructuresInterview CornerCompany PreparationTop TopicsPractice Company QuestionsInterview ExperiencesExperienced InterviewsInternship InterviewsCompetititve ProgrammingDesign PatternsSystem Design TutorialMultiple Choice QuizzesLanguagesCC++JavaPythonC#JavaScriptjQuerySQLPHPScalaPerlGo LanguageHTMLCSSKotlinML & Data ScienceMachine LearningData ScienceCS SubjectsMathematicsOperating SystemDBMSComputer NetworksComputer Organization and ArchitectureTheory of ComputationCompiler DesignDigital LogicSoftware EngineeringGATEGATE Computer Science NotesLast Minute NotesGATE CS Solved PapersGATE CS Original Papers and Official KeysGATE 2021 DatesGATE CS 2021 SyllabusImportant Topics for GATE CSWeb TechnologiesHTMLCSSJavaScriptAngularJSReactJSNodeJSBootstrapjQueryPHPSoftware DesignsSoftware Design PatternsSystem Design TutorialSchool LearningSchool ProgrammingMathematicsNumber SystemAlgebraTrigonometryStatisticsProbabilityGeometryMensurationCalculusMaths Notes (Class 8-12)Class 8 NotesClass 9 NotesClass 10 NotesClass 11 NotesClass 12 NotesNCERT SolutionsClass 8 Maths SolutionClass 9 Maths SolutionClass 10 Maths SolutionClass 11 Maths SolutionClass 12 Maths SolutionRD Sharma SolutionsClass 8 Maths SolutionClass 9 Maths SolutionClass 10 Maths SolutionClass 11 Maths SolutionClass 12 Maths SolutionPhysics Notes (Class 8-11)Class 8 NotesClass 9 NotesClass 10 NotesClass 11 NotesCS Exams/PSUsISROISRO CS Original Papers and Official KeysISRO CS Solved PapersISRO CS Syllabus for Scientist/Engineer ExamUGC NETUGC NET CS Notes Paper IIUGC NET CS Notes Paper IIIUGC NET CS Solved PapersStudentCampus Ambassador ProgramSchool Ambassador ProgramProjectGeek of the MonthCampus Geek of the MonthPlacement CourseCompetititve ProgrammingTestimonialsStudent ChapterGeek on the TopInternshipCareers
AlgorithmsAnalysis of AlgorithmsAsymptotic AnalysisWorst, Average and Best CasesAsymptotic NotationsLittle o and little omega notationsLower and Upper Bound TheoryAnalysis of LoopsSolving RecurrencesAmortized AnalysisWhat does 'Space Complexity' mean ?Pseudo-polynomial AlgorithmsPolynomial Time Approximation SchemeA Time Complexity QuestionSearching AlgorithmsSorting AlgorithmsGraph AlgorithmsPattern SearchingGeometric AlgorithmsMathematicalBitwise AlgorithmsRandomized AlgorithmsGreedy AlgorithmsDynamic ProgrammingDivide and ConquerBacktrackingBranch and BoundAll Algorithms
Analysis of AlgorithmsAsymptotic AnalysisWorst, Average and Best CasesAsymptotic NotationsLittle o and little omega notationsLower and Upper Bound TheoryAnalysis of LoopsSolving RecurrencesAmortized AnalysisWhat does 'Space Complexity' mean ?Pseudo-polynomial AlgorithmsPolynomial Time Approximation SchemeA Time Complexity Question
Asymptotic Analysis
Worst, Average and Best Cases
Asymptotic Notations
Little o and little omega notations
Lower and Upper Bound Theory
Analysis of Loops
Solving Recurrences
Amortized Analysis
What does 'Space Complexity' mean ?
Pseudo-polynomial Algorithms
Polynomial Time Approximation Scheme
A Time Complexity Question
Searching Algorithms
Sorting Algorithms
Graph Algorithms
Pattern Searching
Geometric Algorithms
Mathematical
Bitwise Algorithms
Randomized Algorithms
Greedy Algorithms
Dynamic Programming
Divide and Conquer
Backtracking
Branch and Bound
All Algorithms
Data StructuresArraysLinked ListStackQueueBinary TreeBinary Search TreeHeapHashingGraphAdvanced Data StructureMatrixStringsAll Data Structures
Arrays
Linked List
Stack
Queue
Binary Tree
Binary Search Tree
Heap
Hashing
Graph
Advanced Data Structure
Matrix
Strings
All Data Structures
Interview CornerCompany PreparationTop TopicsPractice Company QuestionsInterview ExperiencesExperienced InterviewsInternship InterviewsCompetititve ProgrammingDesign PatternsSystem Design TutorialMultiple Choice Quizzes
Company Preparation
Top Topics
Practice Company Questions
Interview Experiences
Experienced Interviews
Internship Interviews
Competititve Programming
Design Patterns
System Design Tutorial
Multiple Choice Quizzes
LanguagesCC++JavaPythonC#JavaScriptjQuerySQLPHPScalaPerlGo LanguageHTMLCSSKotlin
C
C++
Java
Python
C#
JavaScript
jQuery
SQL
PHP
Scala
Perl
Go Language
HTML
CSS
Kotlin
ML & Data ScienceMachine LearningData Science
Machine Learning
Data Science
CS SubjectsMathematicsOperating SystemDBMSComputer NetworksComputer Organization and ArchitectureTheory of ComputationCompiler DesignDigital LogicSoftware Engineering
Mathematics
Operating System
DBMS
Computer Networks
Computer Organization and Architecture
Theory of Computation
Compiler Design
Digital Logic
Software Engineering
GATEGATE Computer Science NotesLast Minute NotesGATE CS Solved PapersGATE CS Original Papers and Official KeysGATE 2021 DatesGATE CS 2021 SyllabusImportant Topics for GATE CS
GATE Computer Science Notes
Last Minute Notes
GATE CS Solved Papers
GATE CS Original Papers and Official Keys
GATE 2021 Dates
GATE CS 2021 Syllabus
Important Topics for GATE CS
Web TechnologiesHTMLCSSJavaScriptAngularJSReactJSNodeJSBootstrapjQueryPHP
HTML
CSS
JavaScript
AngularJS
ReactJS
NodeJS
Bootstrap
jQuery
PHP
Software DesignsSoftware Design PatternsSystem Design Tutorial
Software Design Patterns
System Design Tutorial
School LearningSchool ProgrammingMathematicsNumber SystemAlgebraTrigonometryStatisticsProbabilityGeometryMensurationCalculusMaths Notes (Class 8-12)Class 8 NotesClass 9 NotesClass 10 NotesClass 11 NotesClass 12 NotesNCERT SolutionsClass 8 Maths SolutionClass 9 Maths SolutionClass 10 Maths SolutionClass 11 Maths SolutionClass 12 Maths SolutionRD Sharma SolutionsClass 8 Maths SolutionClass 9 Maths SolutionClass 10 Maths SolutionClass 11 Maths SolutionClass 12 Maths SolutionPhysics Notes (Class 8-11)Class 8 NotesClass 9 NotesClass 10 NotesClass 11 Notes
School Programming
MathematicsNumber SystemAlgebraTrigonometryStatisticsProbabilityGeometryMensurationCalculus
Number System
Algebra
Trigonometry
Statistics
Probability
Geometry
Mensuration
Calculus
Maths Notes (Class 8-12)Class 8 NotesClass 9 NotesClass 10 NotesClass 11 NotesClass 12 Notes
Class 8 Notes
Class 9 Notes
Class 10 Notes
Class 11 Notes
Class 12 Notes
NCERT SolutionsClass 8 Maths SolutionClass 9 Maths SolutionClass 10 Maths SolutionClass 11 Maths SolutionClass 12 Maths Solution
Class 8 Maths Solution
Class 9 Maths Solution
Class 10 Maths Solution
Class 11 Maths Solution
Class 12 Maths Solution
RD Sharma SolutionsClass 8 Maths SolutionClass 9 Maths SolutionClass 10 Maths SolutionClass 11 Maths SolutionClass 12 Maths Solution
Class 8 Maths Solution
Class 9 Maths Solution
Class 10 Maths Solution
Class 11 Maths Solution
Class 12 Maths Solution
Physics Notes (Class 8-11)Class 8 NotesClass 9 NotesClass 10 NotesClass 11 Notes
Class 8 Notes
Class 9 Notes
Class 10 Notes
Class 11 Notes
CS Exams/PSUsISROISRO CS Original Papers and Official KeysISRO CS Solved PapersISRO CS Syllabus for Scientist/Engineer ExamUGC NETUGC NET CS Notes Paper IIUGC NET CS Notes Paper IIIUGC NET CS Solved Papers
ISROISRO CS Original Papers and Official KeysISRO CS Solved PapersISRO CS Syllabus for Scientist/Engineer Exam
ISRO CS Original Papers and Official Keys
ISRO CS Solved Papers
ISRO CS Syllabus for Scientist/Engineer Exam
UGC NETUGC NET CS Notes Paper IIUGC NET CS Notes Paper IIIUGC NET CS Solved Papers
UGC NET CS Notes Paper II
UGC NET CS Notes Paper III
UGC NET CS Solved Papers
StudentCampus Ambassador ProgramSchool Ambassador ProgramProjectGeek of the MonthCampus Geek of the MonthPlacement CourseCompetititve ProgrammingTestimonialsStudent ChapterGeek on the TopInternshipCareers
Campus Ambassador Program
School Ambassador Program
Project
Geek of the Month
Campus Geek of the Month
Placement Course
Competititve Programming
Testimonials
Student Chapter
Geek on the Top
Internship
Careers
JobsApply for JobsPost a JobJOB-A-THON
Apply for Jobs
Post a Job
JOB-A-THON
PracticeAll DSA ProblemsProblem of the DayInterview Series: Weekly ContestsBi-Wizard Coding: School ContestsContests and EventsPractice SDE SheetCurated DSA ListsTop 50 Array ProblemsTop 50 String ProblemsTop 50 Tree ProblemsTop 50 Graph ProblemsTop 50 DP Problems
All DSA Problems
Problem of the Day
Interview Series: Weekly Contests
Bi-Wizard Coding: School Contests
Contests and Events
Practice SDE Sheet
Curated DSA ListsTop 50 Array ProblemsTop 50 String ProblemsTop 50 Tree ProblemsTop 50 Graph ProblemsTop 50 DP Problems
Top 50 Array Problems
Top 50 String Problems
Top 50 Tree Problems
Top 50 Graph Problems
Top 50 DP Problems




WriteCome write articles for us and get featuredPracticeLearn and code with the best industry expertsPremiumGet access to ad-free content, doubt assistance and more!JobsCome and find your dream job with usGeeks DigestQuizzesGeeks CampusGblog ArticlesIDECampus Mantri
Geeks Digest
Quizzes
Geeks Campus
Gblog Articles
IDE
Campus Mantri
Sign In
Sign In
Home
Saved Videos
Courses


For Working Professionals
                        



LIVE
                        


DSA Live Classes

System Design

Java Backend Development

Full Stack LIVE

Explore More


Self-Paced
                        


DSA- Self Paced

SDE Theory

Must-Do Coding Questions

Explore More


For Students
                        



LIVE
                        


Competitive Programming

Data Structures with C++

Data Science

Explore More


Self-Paced
                        


DSA- Self Paced

CIP

JAVA / Python / C++

Explore More


School Courses
                        


School Guide

Python Programming

Learn To Make Apps

Explore more


Algorithms
                        


Searching Algorithms

Sorting Algorithms

Graph Algorithms

Pattern Searching

Geometric Algorithms

Mathematical

Bitwise Algorithms

Randomized Algorithms

Greedy Algorithms

Dynamic Programming

Divide and Conquer

Backtracking

Branch and Bound

All Algorithms


Analysis of Algorithms
                        


Asymptotic Analysis

Worst, Average and Best Cases

Asymptotic Notations

Little o and little omega notations

Lower and Upper Bound Theory

Analysis of Loops

Solving Recurrences

Amortized Analysis

What does 'Space Complexity' mean ?

Pseudo-polynomial Algorithms

Polynomial Time Approximation Scheme

A Time Complexity Question


Data Structures
                        


Arrays

Linked List

Stack

Queue

Binary Tree

Binary Search Tree

Heap

Hashing

Graph

Advanced Data Structure

Matrix

Strings

All Data Structures


Interview Corner
                        


Company Preparation

Top Topics

Practice Company Questions

Interview Experiences

Experienced Interviews

Internship Interviews

Competititve Programming

Design Patterns

System Design Tutorial

Multiple Choice Quizzes


Languages
                        


C

C++

Java

Python

C#

JavaScript

jQuery

SQL

PHP

Scala

Perl

Go Language

HTML

CSS

Kotlin


ML & Data Science
                        


Machine Learning

Data Science


CS Subjects
                        


Mathematics

Operating System

DBMS

Computer Networks

Computer Organization and Architecture

Theory of Computation

Compiler Design

Digital Logic

Software Engineering


GATE
                        


GATE Computer Science Notes

Last Minute Notes

GATE CS Solved Papers

GATE CS Original Papers and Official Keys

GATE 2021 Dates

GATE CS 2021 Syllabus

Important Topics for GATE CS


Web Technologies
                        


HTML

CSS

JavaScript

AngularJS

ReactJS

NodeJS

Bootstrap

jQuery

PHP


Software Designs
                        


Software Design Patterns

System Design Tutorial


School Learning
                        


School Programming


Mathematics
                        


Number System

Algebra

Trigonometry

Statistics

Probability

Geometry

Mensuration

Calculus


Maths Notes (Class 8-12)
                        


Class 8 Notes

Class 9 Notes

Class 10 Notes

Class 11 Notes

Class 12 Notes


NCERT Solutions
                        


Class 8 Maths Solution

Class 9 Maths Solution

Class 10 Maths Solution

Class 11 Maths Solution

Class 12 Maths Solution


RD Sharma Solutions
                        


Class 8 Maths Solution

Class 9 Maths Solution

Class 10 Maths Solution

Class 11 Maths Solution

Class 12 Maths Solution


Physics Notes (Class 8-11)
                        


Class 8 Notes

Class 9 Notes

Class 10 Notes

Class 11 Notes


CS Exams/PSUs
                        



ISRO
                        


ISRO CS Original Papers and Official Keys

ISRO CS Solved Papers

ISRO CS Syllabus for Scientist/Engineer Exam


UGC NET
                        


UGC NET CS Notes Paper II

UGC NET CS Notes Paper III

UGC NET CS Solved Papers


Student
                        


Campus Ambassador Program

School Ambassador Program

Project

Geek of the Month

Campus Geek of the Month

Placement Course

Competititve Programming

Testimonials

Student Chapter

Geek on the Top

Internship

Careers


Curated DSA Lists
                        


Top 50 Array Problems

Top 50 String Problems

Top 50 Tree Problems

Top 50 Graph Problems

Top 50 DP Problems


Tutorials
                        



Jobs
                        


Apply for Jobs

Post a Job

JOB-A-THON


Practice
                        


All DSA Problems

Problem of the Day

Interview Series: Weekly Contests

Bi-Wizard Coding: School Contests

Contests and Events

Practice SDE Sheet


For Working Professionals
                        



LIVE
                        


DSA Live Classes

System Design

Java Backend Development

Full Stack LIVE

Explore More


DSA Live Classes


System Design


Java Backend Development


Full Stack LIVE


Explore More


Self-Paced
                        


DSA- Self Paced

SDE Theory

Must-Do Coding Questions

Explore More


DSA- Self Paced


SDE Theory


Must-Do Coding Questions


Explore More


For Students
                        



LIVE
                        


Competitive Programming

Data Structures with C++

Data Science

Explore More


Competitive Programming


Data Structures with C++


Data Science


Explore More


Self-Paced
                        


DSA- Self Paced

CIP

JAVA / Python / C++

Explore More


DSA- Self Paced


CIP


JAVA / Python / C++


Explore More


School Courses
                        


School Guide

Python Programming

Learn To Make Apps

Explore more


School Guide


Python Programming


Learn To Make Apps


Explore more


Algorithms
                        


Searching Algorithms

Sorting Algorithms

Graph Algorithms

Pattern Searching

Geometric Algorithms

Mathematical

Bitwise Algorithms

Randomized Algorithms

Greedy Algorithms

Dynamic Programming

Divide and Conquer

Backtracking

Branch and Bound

All Algorithms


Searching Algorithms


Sorting Algorithms


Graph Algorithms


Pattern Searching


Geometric Algorithms


Mathematical


Bitwise Algorithms


Randomized Algorithms


Greedy Algorithms


Dynamic Programming


Divide and Conquer


Backtracking


Branch and Bound


All Algorithms


Analysis of Algorithms
                        


Asymptotic Analysis

Worst, Average and Best Cases

Asymptotic Notations

Little o and little omega notations

Lower and Upper Bound Theory

Analysis of Loops

Solving Recurrences

Amortized Analysis

What does 'Space Complexity' mean ?

Pseudo-polynomial Algorithms

Polynomial Time Approximation Scheme

A Time Complexity Question


Asymptotic Analysis


Worst, Average and Best Cases


Asymptotic Notations


Little o and little omega notations


Lower and Upper Bound Theory


Analysis of Loops


Solving Recurrences


Amortized Analysis


What does 'Space Complexity' mean ?


Pseudo-polynomial Algorithms


Polynomial Time Approximation Scheme


A Time Complexity Question


Data Structures
                        


Arrays

Linked List

Stack

Queue

Binary Tree

Binary Search Tree

Heap

Hashing

Graph

Advanced Data Structure

Matrix

Strings

All Data Structures


Arrays


Linked List


Stack


Queue


Binary Tree


Binary Search Tree


Heap


Hashing


Graph


Advanced Data Structure


Matrix


Strings


All Data Structures


Interview Corner
                        


Company Preparation

Top Topics

Practice Company Questions

Interview Experiences

Experienced Interviews

Internship Interviews

Competititve Programming

Design Patterns

System Design Tutorial

Multiple Choice Quizzes


Company Preparation


Top Topics


Practice Company Questions


Interview Experiences


Experienced Interviews


Internship Interviews


Competititve Programming


Design Patterns


System Design Tutorial


Multiple Choice Quizzes


Languages
                        


C

C++

Java

Python

C#

JavaScript

jQuery

SQL

PHP

Scala

Perl

Go Language

HTML

CSS

Kotlin


C


C++


Java


Python


C#


JavaScript


jQuery


SQL


PHP


Scala


Perl


Go Language


HTML


CSS


Kotlin


ML & Data Science
                        


Machine Learning

Data Science


Machine Learning


Data Science


CS Subjects
                        


Mathematics

Operating System

DBMS

Computer Networks

Computer Organization and Architecture

Theory of Computation

Compiler Design

Digital Logic

Software Engineering


Mathematics


Operating System


DBMS


Computer Networks


Computer Organization and Architecture


Theory of Computation


Compiler Design


Digital Logic


Software Engineering


GATE
                        


GATE Computer Science Notes

Last Minute Notes

GATE CS Solved Papers

GATE CS Original Papers and Official Keys

GATE 2021 Dates

GATE CS 2021 Syllabus

Important Topics for GATE CS


GATE Computer Science Notes


Last Minute Notes


GATE CS Solved Papers


GATE CS Original Papers and Official Keys


GATE 2021 Dates


GATE CS 2021 Syllabus


Important Topics for GATE CS


Web Technologies
                        


HTML

CSS

JavaScript

AngularJS

ReactJS

NodeJS

Bootstrap

jQuery

PHP


HTML


CSS


JavaScript


AngularJS


ReactJS


NodeJS


Bootstrap


jQuery


PHP


Software Designs
                        


Software Design Patterns

System Design Tutorial


Software Design Patterns


System Design Tutorial


School Learning
                        


School Programming


School Programming


Mathematics
                        


Number System

Algebra

Trigonometry

Statistics

Probability

Geometry

Mensuration

Calculus


Number System


Algebra


Trigonometry


Statistics


Probability


Geometry


Mensuration


Calculus


Maths Notes (Class 8-12)
                        


Class 8 Notes

Class 9 Notes

Class 10 Notes

Class 11 Notes

Class 12 Notes


Class 8 Notes


Class 9 Notes


Class 10 Notes


Class 11 Notes


Class 12 Notes


NCERT Solutions
                        


Class 8 Maths Solution

Class 9 Maths Solution

Class 10 Maths Solution

Class 11 Maths Solution

Class 12 Maths Solution


Class 8 Maths Solution


Class 9 Maths Solution


Class 10 Maths Solution


Class 11 Maths Solution


Class 12 Maths Solution


RD Sharma Solutions
                        


Class 8 Maths Solution

Class 9 Maths Solution

Class 10 Maths Solution

Class 11 Maths Solution

Class 12 Maths Solution


Class 8 Maths Solution


Class 9 Maths Solution


Class 10 Maths Solution


Class 11 Maths Solution


Class 12 Maths Solution


Physics Notes (Class 8-11)
                        


Class 8 Notes

Class 9 Notes

Class 10 Notes

Class 11 Notes


Class 8 Notes


Class 9 Notes


Class 10 Notes


Class 11 Notes


CS Exams/PSUs
                        



ISRO
                        


ISRO CS Original Papers and Official Keys

ISRO CS Solved Papers

ISRO CS Syllabus for Scientist/Engineer Exam


ISRO CS Original Papers and Official Keys


ISRO CS Solved Papers


ISRO CS Syllabus for Scientist/Engineer Exam


UGC NET
                        


UGC NET CS Notes Paper II

UGC NET CS Notes Paper III

UGC NET CS Solved Papers


UGC NET CS Notes Paper II


UGC NET CS Notes Paper III


UGC NET CS Solved Papers


Student
                        


Campus Ambassador Program

School Ambassador Program

Project

Geek of the Month

Campus Geek of the Month

Placement Course

Competititve Programming

Testimonials

Student Chapter

Geek on the Top

Internship

Careers


Campus Ambassador Program


School Ambassador Program


Project


Geek of the Month


Campus Geek of the Month


Placement Course


Competititve Programming


Testimonials


Student Chapter


Geek on the Top


Internship


Careers


Curated DSA Lists
                        


Top 50 Array Problems

Top 50 String Problems

Top 50 Tree Problems

Top 50 Graph Problems

Top 50 DP Problems


Top 50 Array Problems


Top 50 String Problems


Top 50 Tree Problems


Top 50 Graph Problems


Top 50 DP Problems


Tutorials
                        



Jobs
                        


Apply for Jobs

Post a Job

JOB-A-THON


Apply for Jobs


Post a Job


JOB-A-THON


Practice
                        


All DSA Problems

Problem of the Day

Interview Series: Weekly Contests

Bi-Wizard Coding: School Contests

Contests and Events

Practice SDE Sheet


All DSA Problems


Problem of the Day


Interview Series: Weekly Contests


Bi-Wizard Coding: School Contests


Contests and Events


Practice SDE Sheet

GBlog
Puzzles
What's New ?
Array
Matrix
Strings
Hashing
Linked List
Stack
Queue
Binary Tree
Binary Search Tree
Heap
Graph
Searching
Sorting
Divide & Conquer
Mathematical
Geometric
Bitwise
Greedy
Backtracking
Branch and Bound
Dynamic Programming
Pattern Searching
Randomized
Maximum equilibrium sum in an array
Equilibrium index of an array
Persistent Systems Interview Experience (On-Campus)
Persistent Systems interview experience (on campus)
Persistent Systems Interview – Round I and II (On Campus)
Persistent Pune Interview Questions
Persistent Systems Interview Experience | Set 3 (On-Campus Kolkata 2017)
Persistent Systems Interview Experience
Persistent Systems Pool Campusing Interview Experience-2019
Persistent Interview Experience
Persistent Systems Interview Experience | On-Campus
Persistent Systems Interview Experience | On-Campus
Persistent Systems Interview Experience
Persistent Systems Limited Interview Experience
Persistent System Interview experience ( On Campus Drive July-August 2019)
Persistent Systems Limited Interview Experience (Pool Campus Drive for B.E. Freshers 2019-20 batch – Goa)
Persistent Systems Interview Experience (On-Campus 2021 Batch)
Persistent Systems Interview Experience (On-Campus Drive)
Adobe Interview Experience
Adobe Systems Online Test (On-Campus Internship)
Find an element in array such that sum of left array is equal to sum of right array
Arrays in Java
Write a program to reverse an array or string
Largest Sum Contiguous Subarray
Program for array rotation
Arrays in C/C++
Stack Data Structure (Introduction and Program)
Top 50 Array Coding Problems for Interviews
Maximum and minimum of an array using minimum number of comparisons
Linear Search
Maximum equilibrium sum in an array
Equilibrium index of an array
Persistent Systems Interview Experience (On-Campus)
Persistent Systems interview experience (on campus)
Persistent Systems Interview – Round I and II (On Campus)
Persistent Pune Interview Questions
Persistent Systems Interview Experience | Set 3 (On-Campus Kolkata 2017)
Persistent Systems Interview Experience
Persistent Systems Pool Campusing Interview Experience-2019
Persistent Interview Experience
Persistent Systems Interview Experience | On-Campus
Persistent Systems Interview Experience | On-Campus
Persistent Systems Interview Experience
Persistent Systems Limited Interview Experience
Persistent System Interview experience ( On Campus Drive July-August 2019)
Persistent Systems Limited Interview Experience (Pool Campus Drive for B.E. Freshers 2019-20 batch – Goa)
Persistent Systems Interview Experience (On-Campus 2021 Batch)
Persistent Systems Interview Experience (On-Campus Drive)
Adobe Interview Experience
Adobe Systems Online Test (On-Campus Internship)
Find an element in array such that sum of left array is equal to sum of right array
Arrays in Java
Write a program to reverse an array or string
Largest Sum Contiguous Subarray
Program for array rotation
Arrays in C/C++
Stack Data Structure (Introduction and Program)
Top 50 Array Coding Problems for Interviews
Maximum and minimum of an array using minimum number of comparisons
Linear Search
Difficulty Level :
Easy
Given an array arr[]. Find the maximum value of prefix sum which is also suffix sum for index i in arr[].
Examples : 
Input : arr[] = {-1, 2, 3, 0, 3, 2, -1}
Output : 4
Prefix sum of arr[0..3] = 
            Suffix sum of arr[3..6]

Input : arr[] = {-2, 5, 3, 1, 2, 6, -4, 2}
Output : 7
Prefix sum of arr[0..3] = 
              Suffix sum of arr[3..7]
A Simple Solution is to one by one check the given condition (prefix sum equal to suffix sum) for every element and returns the element that satisfies the given condition with maximum value. 
C++
Java
Python3
C#
PHP
Javascript
// CPP program to find// maximum equilibrium sum.#include <bits/stdc++.h>using namespace std; // Function to find// maximum equilibrium sum.int findMaxSum(int arr[], int n){    int res = INT_MIN;    for (int i = 0; i < n; i++)    {    int prefix_sum = arr[i];    for (int j = 0; j < i; j++)        prefix_sum += arr[j];     int suffix_sum = arr[i];    for (int j = n - 1; j > i; j--)        suffix_sum += arr[j];     if (prefix_sum == suffix_sum)        res = max(res, prefix_sum);    }    return res;} // Driver Codeint main(){    int arr[] = {-2, 5, 3, 1,                  2, 6, -4, 2 };    int n = sizeof(arr) / sizeof(arr[0]);    cout << findMaxSum(arr, n);    return 0;}
// java program to find maximum// equilibrium sum.import java.io.*; class GFG {         // Function to find maximum    // equilibrium sum.    static int findMaxSum(int []arr, int n)    {        int res = Integer.MIN_VALUE;                 for (int i = 0; i < n; i++)        {            int prefix_sum = arr[i];                         for (int j = 0; j < i; j++)                prefix_sum += arr[j];                     int suffix_sum = arr[i];                         for (int j = n - 1; j > i; j--)                suffix_sum += arr[j];                     if (prefix_sum == suffix_sum)                res = Math.max(res, prefix_sum);        }                 return res;    }         // Driver Code    public static void main (String[] args)    {        int arr[] = {-2, 5, 3, 1, 2, 6, -4, 2 };        int n = arr.length;        System.out.println(findMaxSum(arr, n));    }} // This code is contributed by anuj_67.
# Python 3 program to find maximum# equilibrium sum.import sys # Function to find maximum equilibrium sum.def findMaxSum(arr, n):    res = -sys.maxsize - 1    for i in range(n):        prefix_sum = arr[i]        for j in range(i):            prefix_sum += arr[j]         suffix_sum = arr[i]        j = n - 1        while(j > i):            suffix_sum += arr[j]            j -= 1        if (prefix_sum == suffix_sum):            res = max(res, prefix_sum)     return res # Driver Codeif __name__ == '__main__':    arr = [-2, 5, 3, 1, 2, 6, -4, 2]    n = len(arr)    print(findMaxSum(arr, n)) # This code is contributed by# Surendra_Gangwar
// C# program to find maximum// equilibrium sum.using System; class GFG {         // Function to find maximum    // equilibrium sum.    static int findMaxSum(int []arr, int n)    {        int res = int.MinValue;                 for (int i = 0; i < n; i++)        {            int prefix_sum = arr[i];                         for (int j = 0; j < i; j++)                prefix_sum += arr[j];                     int suffix_sum = arr[i];                         for (int j = n - 1; j > i; j--)                suffix_sum += arr[j];                     if (prefix_sum == suffix_sum)                res = Math.Max(res, prefix_sum);        }                 return res;    }         // Driver Code    public static void Main ()    {        int []arr = {-2, 5, 3, 1, 2, 6, -4, 2 };        int n = arr.Length;        Console.WriteLine(findMaxSum(arr, n));    }} // This code is contributed by anuj_67.
<?php// PHP program to find// maximum equilibrium sum. // Function to find// maximum equilibrium sum.function findMaxSum( $arr, $n){    $res = PHP_INT_MIN;    for ( $i = 0; $i < $n; $i++)    {    $prefix_sum = $arr[$i];    for ( $j = 0; $j < $i; $j++)        $prefix_sum += $arr[$j];     $suffix_sum = $arr[$i];    for ( $j = $n - 1; $j > $i; $j--)        $suffix_sum += $arr[$j];     if ($prefix_sum == $suffix_sum)        $res = max($res, $prefix_sum);    }    return $res;} // Driver Code$arr = array(-2, 5, 3, 1,              2, 6, -4, 2 );$n = count($arr);echo findMaxSum($arr, $n); // This code is contributed by anuj_67.?>
<script> // Javascript program to find// maximum equilibrium sum. // Function to find// maximum equilibrium sum.function findMaxSum(arr, n){    var res = -1000000000;    for (var i = 0; i < n; i++)    {    var prefix_sum = arr[i];    for (var j = 0; j < i; j++)        prefix_sum += arr[j];     var suffix_sum = arr[i];    for (var j = n - 1; j > i; j--)        suffix_sum += arr[j];     if (prefix_sum == suffix_sum)        res = Math.max(res, prefix_sum);    }    return res;} // Driver Codevar arr = [-2, 5, 3, 1,              2, 6, -4, 2 ];var n = arr.length;document.write( findMaxSum(arr, n)); </script>
7
 
Time Complexity: O(n2) Auxiliary Space: O(n)
A Better Approach is to traverse the array and store prefix sum for each index in array presum[], in which presum[i] stores sum of subarray arr[0..i]. Do another traversal of the array and store suffix sum in another array suffsum[], in which suffsum[i] stores sum of subarray arr[i..n-1]. After this for each index check if presum[i] is equal to suffsum[i] and if they are equal then compare their value with the overall maximum so far. 
C++
Java
Python3
C#
PHP
Javascript
// CPP program to find// maximum equilibrium sum.#include <bits/stdc++.h>using namespace std; // Function to find maximum// equilibrium sum.int findMaxSum(int arr[], int n){    // Array to store prefix sum.    int preSum[n];     // Array to store suffix sum.    int suffSum[n];     // Variable to store maximum sum.    int ans = INT_MIN;     // Calculate prefix sum.    preSum[0] = arr[0];    for (int i = 1; i < n; i++)        preSum[i] = preSum[i - 1] + arr[i];     // Calculate suffix sum and compare    // it with prefix sum. Update ans    // accordingly.    suffSum[n - 1] = arr[n - 1];    if (preSum[n - 1] == suffSum[n - 1])        ans = max(ans, preSum[n - 1]);             for (int i = n - 2; i >= 0; i--)    {        suffSum[i] = suffSum[i + 1] + arr[i];        if (suffSum[i] == preSum[i])            ans = max(ans, preSum[i]);        }     return ans;} // Driver Codeint main(){    int arr[] = { -2, 5, 3, 1,                   2, 6, -4, 2 };    int n = sizeof(arr) / sizeof(arr[0]);    cout << findMaxSum(arr, n);    return 0;}
// Java program to find maximum equilibrium sum.import java.io.*; public class GFG {          // Function to find maximum    // equilibrium sum.    static int findMaxSum(int []arr, int n)    {                 // Array to store prefix sum.        int []preSum = new int[n];             // Array to store suffix sum.        int []suffSum = new int[n];             // Variable to store maximum sum.        int ans = Integer.MIN_VALUE;             // Calculate prefix sum.        preSum[0] = arr[0];        for (int i = 1; i < n; i++)            preSum[i] = preSum[i - 1] + arr[i];             // Calculate suffix sum and compare        // it with prefix sum. Update ans        // accordingly.        suffSum[n - 1] = arr[n - 1];                 if (preSum[n - 1] == suffSum[n - 1])            ans = Math.max(ans, preSum[n - 1]);                     for (int i = n - 2; i >= 0; i--)        {            suffSum[i] = suffSum[i + 1] + arr[i];                         if (suffSum[i] == preSum[i])                ans = Math.max(ans, preSum[i]);        }             return ans;    }         // Driver Code    static public void main (String[] args)    {        int []arr = { -2, 5, 3, 1, 2, 6, -4, 2 };        int n = arr.length;                 System.out.println( findMaxSum(arr, n));    }} // This code is contributed by anuj_67
# Python3 program to find# maximum equilibrium sum. # Function to find maximum# equilibrium sum.def findMaxSum(arr, n):     # Array to store prefix sum.    preSum = [0 for i in range(n)]     # Array to store suffix sum.    suffSum = [0 for i in range(n)]     # Variable to store maximum sum.    ans = -10000000     # Calculate prefix sum.    preSum[0] = arr[0]         for i in range(1, n):             preSum[i] = preSum[i - 1] + arr[i]     # Calculate suffix sum and compare    # it with prefix sum. Update ans    # accordingly.    suffSum[n - 1] = arr[n - 1]    if (preSum[n - 1] == suffSum[n - 1]):        ans = max(ans, preSum[n - 1])          for i in range(n - 2, -1, -1):        suffSum[i] = suffSum[i + 1] + arr[i]        if (suffSum[i] == preSum[i]):            ans = max(ans, preSum[i])         return ans # Driver Codeif __name__=='__main__':     arr = [-2, 5, 3, 1,2, 6, -4, 2]    n = len(arr)    print(findMaxSum(arr, n))     # This code i contributed by pratham76
// C# program to find maximum equilibrium sum.using System; public class GFG {          // Function to find maximum    // equilibrium sum.    static int findMaxSum(int []arr, int n)    {                 // Array to store prefix sum.        int []preSum = new int[n];             // Array to store suffix sum.        int []suffSum = new int[n];             // Variable to store maximum sum.        int ans = int.MinValue;             // Calculate prefix sum.        preSum[0] = arr[0];        for (int i = 1; i < n; i++)            preSum[i] = preSum[i - 1] + arr[i];             // Calculate suffix sum and compare        // it with prefix sum. Update ans        // accordingly.        suffSum[n - 1] = arr[n - 1];                 if (preSum[n - 1] == suffSum[n - 1])            ans = Math.Max(ans, preSum[n - 1]);                     for (int i = n - 2; i >= 0; i--)        {            suffSum[i] = suffSum[i + 1] + arr[i];                         if (suffSum[i] == preSum[i])                ans = Math.Max(ans, preSum[i]);        }             return ans;    }         // Driver Code    static public void Main ()    {        int []arr = { -2, 5, 3, 1, 2, 6, -4, 2 };        int n = arr.Length;                 Console.WriteLine( findMaxSum(arr, n));    }} // This code is contributed by anuj_67
<?php// PHP program to find maximum equilibrium sum. // Function to find maximum equilibrium sum.function findMaxSum($arr, $n){    // Array to store prefix sum.    $preSum[$n] = array();     // Array to store suffix sum.    $suffSum[$n] = array();     // Variable to store maximum sum.    $ans = PHP_INT_MIN;     // Calculate prefix sum.    $preSum[0] = $arr[0];    for ($i = 1; $i < $n; $i++)        $preSum[$i] = $preSum[$i - 1] + $arr[$i];     // Calculate suffix sum and compare    // it with prefix sum. Update ans    // accordingly.    $suffSum[$n - 1] = $arr[$n - 1];    if ($preSum[$n - 1] == $suffSum[$n - 1])        $ans = max($ans, $preSum[$n - 1]);             for ($i = $n - 2; $i >= 0; $i--)    {        $suffSum[$i] = $suffSum[$i + 1] + $arr[$i];        if ($suffSum[$i] == $preSum[$i])            $ans = max($ans, $preSum[$i]);    }     return $ans;} // Driver Code$arr = array( -2, 5, 3, 1, 2, 6, -4, 2 );$n = sizeof($arr);echo findMaxSum($arr, $n); // This code is contributed by ajit.?>
<script> // Javascript program to find// maximum equilibrium sum. // Function to find maximum// equilibrium sum.function findMaxSum(arr, n){          // Array to store prefix sum.    let preSum = new Array(n);    preSum.fill(0);      // Array to store suffix sum.    let suffSum = new Array(n);    suffSum.fill(0);      // Variable to store maximum sum.    let ans = Number.MIN_VALUE;      // Calculate prefix sum.    preSum[0] = arr[0];    for(let i = 1; i < n; i++)        preSum[i] = preSum[i - 1] + arr[i];      // Calculate suffix sum and compare    // it with prefix sum. Update ans    // accordingly.    suffSum[n - 1] = arr[n - 1];          if (preSum[n - 1] == suffSum[n - 1])        ans = Math.max(ans, preSum[n - 1]);              for(let i = n - 2; i >= 0; i--)    {        suffSum[i] = suffSum[i + 1] + arr[i];                  if (suffSum[i] == preSum[i])            ans = Math.max(ans, preSum[i]);    }    return ans;} // Driver codelet arr = [ -2, 5, 3, 1, 2, 6, -4, 2 ];let n = arr.length; document.write(findMaxSum(arr, n)); // This code is contributed by rameshtravel07 </script>
7
 
Time Complexity: O(n) Auxiliary Space: O(n)
Further Optimization : We can avoid the use of extra space by first computing the total sum, then using it to find the current prefix and suffix sums. 
C++
Java
Python3
C#
Javascript
// CPP program to find// maximum equilibrium sum.#include <bits/stdc++.h>using namespace std; // Function to find// maximum equilibrium sum.int findMaxSum(int arr[], int n){    int sum = accumulate(arr, arr + n, 0);    int prefix_sum = 0, res = INT_MIN;    for (int i = 0; i < n; i++)    {    prefix_sum += arr[i];    if (prefix_sum == sum)        res = max(res, prefix_sum);    sum -= arr[i];    }    return res;} // Driver Codeint main(){    int arr[] = { -2, 5, 3, 1,                   2, 6, -4, 2 };    int n = sizeof(arr) / sizeof(arr[0]);    cout << findMaxSum(arr, n);    return 0;}
// Java program to find maximum equilibrium// sum.import java.lang.Math.*;import java.util.stream.*; class GFG {         // Function to find maximum equilibrium    // sum.    static int findMaxSum(int arr[], int n)    {        int sum = IntStream.of(arr).sum();        int prefix_sum = 0,        res = Integer.MIN_VALUE;                 for (int i = 0; i < n; i++)        {            prefix_sum += arr[i];                         if (prefix_sum == sum)                res = Math.max(res, prefix_sum);            sum -= arr[i];        }                 return res;    }         // Driver Code    public static void main(String[] args)    {        int arr[] = { -2, 5, 3, 1,                    2, 6, -4, 2 };        int n = arr.length;        System.out.print(findMaxSum(arr, n));    }} // This code is contributed by Smitha.
# Python3 program to find# maximum equilibrium sum.import sys # Function to find# maximum equilibrium sum.def findMaxSum(arr,n):         ss = sum(arr)    prefix_sum = 0    res = -sys.maxsize         for i in range(n):        prefix_sum += arr[i]                 if prefix_sum == ss:            res = max(res, prefix_sum);                     ss -= arr[i];             return res  # Driver code  if __name__=="__main__":         arr = [ -2, 5, 3, 1,             2, 6, -4, 2 ]    n = len(arr)         print(findMaxSum(arr, n)) # This code is contributed by rutvik_56
// C# program to find maximum equilibrium sum.using System.Linq;using System; class GFG {         static int Add(int x, int y) {        return x + y;    }         // Function to find maximum equilibrium    // sum.    static int findMaxSum(int []arr, int n)    {        int sum = arr.Aggregate(func:Add);        int prefix_sum = 0,        res = int.MinValue;                 for (int i = 0; i < n; i++)        {            prefix_sum += arr[i];                         if (prefix_sum == sum)                res = Math.Max(res, prefix_sum);            sum -= arr[i];        }                 return res;    }         // Driver Code    public static void Main()    {        int []arr = { -2, 5, 3, 1,                    2, 6, -4, 2 };        int n = arr.Length;        Console.Write(findMaxSum(arr, n));    }} // This code is contributed by Smitha.
<script>   // javascript program to find// maximum equilibrium sum. // Function to find// maximum equilibrium sum.function findMaxSum(arr,n){    let sum = 0;  for(let i=0; i < n; i++)  {     sum = sum + arr[i];  }    let prefix_sum = 0, res = Number.MIN_VALUE;    for (let i = 0; i < n; i++)    {    prefix_sum += arr[i];    if (prefix_sum == sum)        res = Math.max(res, prefix_sum);    sum -= arr[i];    }    return res;} // Driver Code     let arr = [ -2, 5, 3, 1,                2, 6, -4, 2 ];    let n = arr.length;    document.write(findMaxSum(arr, n));         // This code is contributed by vaibhavrabadiya117.</script>
7
 
Time Complexity: O(n) Auxiliary Space: O(1) 
 
vt_m
Smitha Dinesh Semwal
jit_t
SURENDRA_GANGWAR
rutvik_56
pratham76
rrrtnx
rameshtravel07
vaibhavrabadiya117
akshaysingh98088
surindertarika1234
prefix-sum
Arrays
Searching
prefix-sum
Arrays
Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Arrays
Multidimensional Arrays in Java
Linked List vs Array
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Python | Using 2D arrays/lists the right way
Binary Search
Search an element in a sorted and rotated array
Find the Missing Number
K'th Smallest/Largest Element in Unsorted Array | Set 1
Program to find largest element in an array