Optimal partition of an array into four parts

• Last Updated : 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++

 // CPP program to find three indices#include #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);    find_Indices(arr, n);}

Java

 // 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.

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 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

 \$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)?>

Javascript



Output:

0 1 3

Time complexity: My Personal Notes arrow_drop_up