Optimal partition of an array into four parts - GeeksforGeeks

21 Jun, 2021
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++
Java
Python3
C#
PHP
Javascript
// CPP program to find three indices#include <bits/stdc++.h>#define max 50009using 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 functionint main() {    int arr[] = {-1, 2, 3};    int n = sizeof(arr)/sizeof(arr[0]);    find_Indices(arr, n);}
// Java program to find three indicesclass 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.
# 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# program to find three indicesusing 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 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)?>
<script>// Javascript program to// find three indiceslet max = 50009; // Function to find// required indices.function find_Indices(arr, n){         let sum = new Array(); k = 0;    sum[0] = 0;         // calculating prefix sum    // from 1 to i for each i.    for (let i = 1, k = 0;        i <= n; i++)        sum[i] = sum[i - 1] +                arr[k++];         let ans = -(1000000000000000);    let index_1 = index_2 = index_3 = -1;     // iterating the loop from    // 0 to n for all possibilities.    for (let l = 0; l <= n; l++)    {        let index = 0;        let vmin = (1000000000000000);         // Here, we recalling the        // best segment to end at        // position i.        for (let 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.    document.write(index_1 + " " + index_2 +" " + index_3 + " ");} // Driver Codelet arr = new Array(-1, 2, 3);let n = arr.length;find_Indices(arr, n); // This code is contributed by// _saurabh_jaiswal</script>
Output:  
0 1 3
Time complexity:  
manishshaw1
_saurabh_jaiswal
prefix-sum
Arrays
prefix-sum
Arrays
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
Search an element in a sorted and rotated array
Queue | Set 1 (Introduction and Array Implementation)
Array of Strings in C++ (5 Different Ways to Create)
Find the Missing Number
Subset Sum Problem | DP-25