Optimal partition of an array into four parts
Given an array of n non-negative integers. Choose three indices i.e. (0 <= index_1 <= index_ 2<= index_3 <= n) from the array to make four subsets such that the term sum(0, index_1) – sum(index_1, index_2) + sum(index_2, index_3) – sum(index_3, n) is maximum possible.
Here, two indices say l and r means, the sum(l, r) will be the sum of all numbers of subset on positions from l to r non-inclusive (l-th element is not counted, r-th element is counted). For example, if arr = {-5, 3, 9, 4}, then sum(0, 1) = -5, sum(0, 2) = -2, sum(1, 4) = 16 and sum(i, i) = 0 for each i from 0 to 4. For indices l and r holds 0 <= l <= r <= n. Indices in array are numbered from 0.
Examples:
Input : arr = {-1, 2, 3}
Output : 0 1 3
Here, sum(0, 0) = 0
sum(0, 1) = -1
sum(1, 3) = 2 + 3 = 5
sum(3, 3) = 0
Therefore , sum(0, 0) - sum(0, 1) + sum(1, 3) - sum(3, 3) = 4
which is maximum.
Input : arr = {0, 0, -1, 0}
Output : 0 0 0
Here, sum(0, 0) - sum(0, 0) + sum(0, 0) - sum(0, 0) = 0
which is maximum possible.
Imagine the same task but without the first term in sum. As the sum of the array is fixed, the best second segment should be the one with the greatest sum. This can be solved in O(n) with prefix sum. When recalling the best segment to end at position i, take minimal prefix sum from 0 to i inclusive (from the whole sum you want to subtract the lowest number).
Now let’s just iterate over all possible ends of the first segment and solve the task above on the array without this segment.
C++
// CPP program to find three indices #include <bits/stdc++.h> #define max 50009 using namespace std; // Function to find required indices. void find_Indices(int arr[], int n){ int sum[max], k; int index_1, index_2, index_3, index; // calculating prefix sum from // 1 to i for each i. for (int i = 1, k = 0; i <= n; i++) sum[i] = sum[i-1] + arr[k++]; long long ans = -(1e15); index_1 = index_2 = index_3 = -1; // iterating the loop from 0 to n // for all possibilities. for (int l = 0; l <= n; l++) { int index = 0; long long vmin = (1e15); // Here, we recalling the best // segment to end at position i. for (int r = l; r <= n; r++) { // taking the minimal prefix // sum from 0 to i inclusive. if (sum[r] < vmin) { vmin = sum[r]; index = r; } // calculating the indices. if (sum[l] + sum[r] - vmin > ans) { ans = sum[l] + sum[r] - vmin; index_1 = l; index_2 = index; index_3 = r; } } } // Required indices. printf("%d %d %d", index_1, index_2, index_3); } // Driver function int main() { int arr[] = {-1, 2, 3}; int n = sizeof(arr)/sizeof(arr[0]); find_Indices(arr, n); } |
Java
// Java program to find three indices class GFG { static final int max = 50009; // Function to find required indices. static void find_Indices(int arr[], int n) { int sum[] = new int[max]; int index_1, index_2, index_3, index; int k, i; // calculating prefix sum from // 1 to i for each i. for (i = 1, k = 0; i <= n; i++) sum[i] = sum[i - 1] + arr[k++]; double ans = -(1e15); index_1 = index_2 = index_3 = -1; // iterating the loop from 0 to n // for all possibilities. for (int l = 0; l <= n; l++) { index = 0; double vmin = (1e15); // Here, we recalling the best // segment to end at position i. for (int r = l; r <= n; r++) { // taking the minimal prefix // sum from 0 to i inclusive. if (sum[r] < vmin) { vmin = sum[r]; index = r; } // calculating the indices. if (sum[l] + sum[r] - vmin > ans) { ans = sum[l] + sum[r] - vmin; index_1 = l; index_2 = index; index_3 = r; } } } // Required indices. System.out.print(index_1 + " " + index_2 + " " + index_3); } // Driver function. public static void main(String[] args) { int arr[] = { -1, 2, 3 }; int n = arr.length; find_Indices(arr, n); } } // This code is contributed by Anant Agarwal. |
Python3
# Python program to # find three indices max = 50009 # Function to find # required indices. def find_Indices(arr, n): sum=[0 for i in range(max)] # calculating prefix sum from # 1 to i for each i. k=0 for i in range(1,n+1): sum[i] = sum[i-1] + arr[k]; k+=1 ans = -(1e15) index_1 = index_2 = index_3 = -1 # iterating the loop from 0 to n # for all possibilities. for l in range(n+1): index = 0 vmin = (1e15) # Here, we recalling the best # segment to end at position i. for r in range(l,n+1): # taking the minimal prefix # sum from 0 to i inclusive. if (sum[r] < vmin): vmin = sum[r] index = r # calculating the indices. if (sum[l] + sum[r] - vmin > ans): ans = sum[l] + sum[r] - vmin index_1 = l index_2 = index index_3 = r # Required indices. print(index_1," ", index_2," ", index_3) # Driver function arr = [-1, 2, 3] n = len(arr) find_Indices(arr, n) # This code is contributed # by Anant Agarwal. |
C#
// C# program to find three indices using System; class GFG { static int max = 50009; // Function to find required indices. static void find_Indices(int []arr, int n) { int []sum = new int[max]; int index_1, index_2, index_3, index; int k, i; // calculating prefix sum from // 1 to i for each i. for (i = 1, k = 0; i <= n; i++) sum[i] = sum[i - 1] + arr[k++]; double ans = -(1e15); index_1 = index_2 = index_3 = -1; // iterating the loop from 0 to n // for all possibilities. for (int l = 0; l <= n; l++) { index = 0; double vmin = (1e15); // Here, we recalling the best // segment to end at position i. for (int r = l; r <= n; r++) { // taking the minimal prefix // sum from 0 to i inclusive. if (sum[r] < vmin) { vmin = sum[r]; index = r; } // calculating the indices. if (sum[l] + sum[r] - vmin > ans) { ans = sum[l] + sum[r] - vmin; index_1 = l; index_2 = index; index_3 = r; } } } // Required indices. Console.WriteLine(index_1 + " " + index_2 + " " + index_3); } // Driver function. public static void Main() { int []arr = { -1, 2, 3 }; int n = arr.Length; find_Indices(arr, n); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to // find three indices $max = 50009; // Function to find // required indices. function find_Indices($arr, $n) { global $max; $sum = array(); $k = 0; $sum[0] = 0; // calculating prefix sum // from 1 to i for each i. for ($i = 1, $k = 0; $i <= $n; $i++) $sum[$i] = $sum[$i - 1] + $arr[$k++]; $ans = -(1000000000000000); $index_1 = $index_2 = $index_3 = -1; // iterating the loop from // 0 to n for all possibilities. for ($l = 0; $l <= $n; $l++) { $index = 0; $vmin = (1000000000000000); // Here, we recalling the // best segment to end at // position i. for ($r = $l; $r <= $n; $r++) { // taking the minimal prefix // sum from 0 to i inclusive. if ($sum[$r] < $vmin) { $vmin = $sum[$r]; $index = $r; } // calculating the indices. if ($sum[$l] + $sum[$r] - $vmin > $ans) { $ans = $sum[$l] + $sum[$r] - $vmin; $index_1 = $l; $index_2 = $index; $index_3 = $r; } } } // Required indices. echo($index_1." ".$index_2. " ".$index_3." "); } // Driver Code $arr = array(-1, 2, 3); $n = count($arr); find_Indices($arr, $n); // This code is contributed by // Manish Shaw(manishshaw1) ?> |
Output:
0 1 3
Time complexity: ![]()
Recommended Posts:
- Split the array into equal sum parts according to given conditions
- Divide binary array into three equal parts with same value
- Check if an array of 1s and 2s can be divided into 2 parts with equal sum
- Find a partition point in array
- Maximum average sum partition of an array
- Partition the array into three equal sum segments
- Equal sum array partition excluding a given element
- Minimum toggles to partition a binary array so that it has first 0s then 1s
- Find a partition point in array to maximize its xor sum
- Partition an array such into maximum increasing segments
- Print equal sum sets of array (Partition problem) | Set 1
- Print equal sum sets of array (Partition Problem) | Set 2
- Count the number of ways to divide an array into three contiguous parts having equal sum
- Partition the array in K segments such that bitwise AND of individual segment sum is maximized
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : manishshaw1



