# 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 (|sum_{left} – sum_{right}|) 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 **|sum _{left} – sum_{right}|**.

**Time Complexity:** O(N^{2})

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(A_{i}) 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 |sum_{left} – sum_{right}|.

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

Below is the implementation of the above approach.

`// 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 difference ` ` ` `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*

*filter_none*

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

## Recommended Posts:

- Partition negative and positive without comparison with 0
- Check if it possible to partition in k subarrays with equal sum
- Maximum average sum partition of an array
- Partition the array into three equal sum segments
- Optimal partition of an array into four parts
- Find a partition point in array
- Partition an array such into maximum increasing segments
- 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
- Equal sum array partition excluding a given element
- Print equal sum sets of array (Partition problem) | Set 1
- Partition into two subarrays of lengths k and (N - k) such that the difference of sums is maximum
- Print equal sum sets of array (Partition Problem) | Set 2
- Find Partition Line such that sum of values on left and right is equal
- Partition the array in K segments such that bitwise AND of individual segment sum is maximized

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.