Sudo Placement[1.5] | Partition

Given an array of positive and negative numbers. The task is to find a partition point such that none of the elements of left array are in the right array. If there are multiple partitions, then find the partition at which the absolute difference between the sum of left array and sum of right array (|sumleft – sumright|) with respect to the partition point is minimum. In case of multiple points, print the first partition point from left which is (last index of left array and first index of right array)/2 . Consider 1-based indexing. The left and right array on partition must have a minimum of 1 element and maximum of n-1 elements. Print -1 if no partition is possible.

Examples:

Input: a[] = {1, 2, -1, 2, 3}
Output: 1
Left array = {1, 2, -1, 2}
Right array = {3}
Sumleft = 4, Sumright = 3
Difference = 1 which is the minimum possible

Input: a[] = {1, 2, 3, 1}
Output: -1



A naive approach will be to traverse left and right from every index and check if the partition is possible or not at that index. If the partition is possible, then check if the absolute difference between the sum of an element of left array and element of right array is less than that of the previous obtained value at the partition. After finding the partition point, greedily find the |sumleft – sumright|.

Time Complexity: O(N2)

An efficient solution will be to store the last index of every occurring element in a hash-map. Since the element values are large, direct indexing cannot be used. Create a prefix[] and suffix[] array which stores the prefix sum and suffix sum respectively. Initialize a variable count as 0. Iterate for all the element in the array. A common point of observation is, while traversing if the present element’s(Ai) last nonoccurence is not i itself, then we cannot have a partition in between i and the element’s last occurrence. While traversing store the maximum of element’s last occurrence as the partition cannot be done till then.
Once the count is i itself, we can have a partition, now if there are multiple partitions then choose the min |sumleft – sumright|.

Note : Use of map instead of unordered_map may cause TLE.

Below is the implementation of the above approach.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for SP- partition
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the partition
void partition(int a[], int n)
{
    unordered_map<long long, long long> mpp;
  
    // mark the last occurrence of every element
    for (int i = 0; i < n; i++)
        mpp[a[i]] = i;
  
    // calculate the prefix sum
    long long presum[n];
    presum[0] = a[0];
    for (int i = 1; i < n; i++)
        presum[i] = presum[i - 1] + a[i];
  
    // calculate the suffix sum
    long long sufsum[n];
    sufsum[n - 1] = a[n - 1];
    for (int i = n - 2; i >= 0; i--) {
        sufsum[i] = sufsum[i + 1] + a[i];
    }
  
    // Check if partition is possible
    bool possible = false;
  
    // Stores the absolute differnce
    long long ans = 1e18;
  
    // stores the last index till
    // which there can not be any partition
    long long count = 0;
  
    // Stores the partition
    long long index = -1;
  
    // Check if partition is possible or not
    // donot check for the last element
    // as partition is not possible
    for (int i = 0; i < n - 1; i++) {
  
        // takes an element and checks it last occurrence
        // stores the maximum of the last occurrence
        // where partition can be done
        count = max(count, mpp[a[i]]);
  
        // if partition is possible
        if (count == i) {
  
            // partition is possible
            possible = true;
  
            // stores the left array sum
            long long sumleft = presum[i];
  
            // stores the rigth array sum
            long long sumright = sufsum[i + 1];
  
            // check if the difference is minimum
            if ((abs(sumleft - sumright)) < ans) {
                ans = abs(sumleft - sumright);
                index = i + 1;
            }
        }
    }
  
    // is partition is possible or not
    if (possible)
        cout << index << ".5" << endl;
    else
        cout << -1 << endl;
}
  
// Driver Code-
int main()
{
    int a[] = { 1, 2, -1, 2, 3 };
    int n = sizeof(a) / sizeof(a[0]);
  
    partition(a, n);
    return 0;
}

chevron_right


Time Complexity: O(n) under the assumption that unordered_map search works in O(1) time.



My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

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.