Given an array arr[] of N Positive integers, the task is to find the largest prefix sum which is also the suffix sum and prefix and suffix do not overlap.
Examples:
Input: N = 5, arr = [1, 3, 2, 1, 4]
Output: 4
Explanation: consider prefix [1, 3] and suffix [4] which gives maximum
prefix sum which is also suffix sum such that prefix and suffix do not overlap.Input: N = 5, arr = [1, 3, 1, 1, 4]
Output: 5
Approach: The problem can be solved using the two-pointer technique.
Use two pointers from both ends of array and keep maintaining sum of prefix and suffix, keep moving pointers till they overlap.
Follow the steps to solve the problem:
- Declare and initialize two variables i = 0 and j = N – 1.
- Declare and initialize two variables to store prefix and suffix sum, prefix = 0, suffix = 0.
- Declare and initialize a variable result to keep the maximum possible prefix sum, result = 0.
- while i is less than or equal to
- If prefix sum is less than suffix sum add array element at the ith index to prefix sum and increment value of i.
- Else add array element at the jth index to suffix sum and decrement value of j.
- If both of them are equal update the result variable with prefix sum.
- Print value of the result.
Below is the implementation of the above approach.
C++
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // Function to find the largest prefix // whose sum is same as the suffix sum // such that they don't overlap int maxPrefixSum( int N, int * arr) { // Pointers pointing first and // last index of array int i = 0, j = N - 1; // Variables to store prefix and suffixSum int prefixSum = 0, suffixSum = 0; // Variable to store result that is // maximum possible prefix sum int result = 0; // While prefix and suffix // do not overlap while (i <= j) { // If prefix sum is less than suffix sum // add array element at the ith index to // prefix sum and increment value of i. if (prefixSum < suffixSum) { prefixSum += arr[i]; i++; } // Else add array element at the jth // index to suffix sum and decrement // value of j else { suffixSum += arr[j]; j--; } // If both of them are equal update // result variable with prefix sum. if (prefixSum == suffixSum) result = prefixSum; } return result; } // Driver code int main() { int arr[] = { 1, 3, 1, 1, 4 }; int N = sizeof (arr) / sizeof (arr[0]); // Function Call cout << maxPrefixSum(N, arr); return 0; } |
Java
// Java code to implement the approach import java.io.*; class GFG { // Function to find the largest prefix // whose sum is same as the suffix sum // such that they don't overlap public static int maxPrefixSum( int N, int arr[]) { // Pointers pointing first and // last index of array int i = 0 , j = N - 1 ; // Variables to store prefix and suffixSum int prefixSum = 0 , suffixSum = 0 ; // Variable to store result that is // maximum possible prefix sum int result = 0 ; // While prefix and suffix // do not overlap while (i <= j) { // If prefix sum is less than suffix sum // add array element at the ith index to // prefix sum and increment value of i. if (prefixSum < suffixSum) { prefixSum += arr[i]; i++; } // Else add array element at the jth // index to suffix sum and decrement // value of j else { suffixSum += arr[j]; j--; } // If both of them are equal update // result variable with prefix sum. if (prefixSum == suffixSum) result = prefixSum; } return result; } // Driver Code public static void main(String[] args) { int arr[] = { 1 , 3 , 1 , 1 , 4 }; int N = arr.length; // Function Call System.out.print(maxPrefixSum(N, arr)); } } // This code is contributed by Rohit Pradhan |
Python3
# Python code for the above approach import math # Function to find the largest prefix # whose sum is same as the suffix sum # such that they don't overlap def maxPrefixSum(N, arr): # Pointers pointing first and # last index of array i = 0 j = N - 1 ; # Variables to store prefix and suffixSum prefixSum = 0 suffixSum = 0 ; # Variable to store result that is # maximum possible prefix sum result = 0 ; # While prefix and suffix # do not overlap while i < = j: # If prefix sum is less than suffix sum # add array element at the ith index to # prefix sum and increment value of i. if (prefixSum < suffixSum): prefixSum + = arr[i]; i = i + 1 ; # Else add array element at the jth # index to suffix sum and decrement # value of j else : suffixSum + = arr[j]; j = j - 1 ; # If both of them are equal update # result variable with prefix sum. if (prefixSum = = suffixSum): result = prefixSum; return result; # Driver code arr = [ 1 , 3 , 1 , 1 , 4 ]; N = len (arr); # Function Call print (maxPrefixSum(N, arr)); # This code is contributed by Potta Lokesh |
C#
// C# code to implement the approach using System; public class GFG{ // Function to find the largest prefix // whose sum is same as the suffix sum // such that they don't overlap public static int maxPrefixSum( int N, int [] arr) { // Pointers pointing first and // last index of array int i = 0, j = N - 1; // Variables to store prefix and suffixSum int prefixSum = 0, suffixSum = 0; // Variable to store result that is // maximum possible prefix sum int result = 0; // While prefix and suffix // do not overlap while (i <= j) { // If prefix sum is less than suffix sum // add array element at the ith index to // prefix sum and increment value of i. if (prefixSum < suffixSum) { prefixSum += arr[i]; i++; } // Else add array element at the jth // index to suffix sum and decrement // value of j else { suffixSum += arr[j]; j--; } // If both of them are equal update // result variable with prefix sum. if (prefixSum == suffixSum) result = prefixSum; } return result; } static public void Main (){ int [] arr = { 1, 3, 1, 1, 4 }; int N = arr.Length; // Function Call Console.Write(maxPrefixSum(N, arr)); } } // This code is contributed by lokeshmvs21. |
Javascript
<script> // JavaScript implementation of the approach // Function to find the largest prefix // whose sum is same as the suffix sum // such that they don't overlap function maxPrefixSum(N, arr) { // Pointers pointing first and // last index of array let i = 0, j = N - 1; // Variables to store prefix and suffixSum let prefixSum = 0, suffixSum = 0; // Variable to store result that is // maximum possible prefix sum let result = 0; // While prefix and suffix // do not overlap while (i <= j) { // If prefix sum is less than suffix sum // add array element at the ith index to // prefix sum and increment value of i. if (prefixSum < suffixSum) { prefixSum += arr[i]; i++; } // Else add array element at the jth // index to suffix sum and decrement // value of j else { suffixSum += arr[j]; j--; } // If both of them are equal update // result variable with prefix sum. if (prefixSum == suffixSum) result = prefixSum; } return result; } // Driver code let arr = [1, 3, 1, 1, 4]; let N = arr.length; // Function Call document.write(maxPrefixSum(N, arr)); // This code is contributed by sanjoy_62. </script> |
5
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!