# 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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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.

## C++

 `// C++ program for SP- partition ` `#include ` `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 right 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; ` `} `

## Java

 `// Java program for SP- partition  ` `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` ` `  `    ``// Function to find the partition ` `    ``static` `void` `partition(``int` `a[], ``int` `n)  ` `    ``{ ` `        ``Map mpp = ``new` `HashMap<>(); ` ` `  `        ``// mark the last occurrence of ` `        ``// every element ` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `            ``mpp.put(a[i], i); ` ` `  `        ``// calculate the prefix sum ` `        ``long``[] presum = ``new` `long``[n]; ` `        ``presum[``0``] = a[``0``]; ` `        ``for` `(``int` `i = ``1``; i < n; i++) ` `            ``presum[i] = presum[i - ``1``] + a[i]; ` ` `  `        ``// calculate the suffix sum ` `        ``long``[] sufsum = ``new` `long``[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 ` `        ``boolean` `possible = ``false``; ` ` `  `        ``// Stores the absolute difference ` `        ``long` `ans = (``long``) 1e18; ` ` `  `        ``// stores the last index till ` `        ``// which there can not be any partition ` `        ``long` `count = ``0``; ` ` `  `        ``// Stores the partition ` `        ``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 its ` `            ``// last occurrence, stores the maximum  ` `            ``// of the last occurrence where ` `            ``// partition can be done ` `            ``count = Math.max(count, mpp.get(a[i])); ` ` `  `            ``// if partition is possible ` `            ``if` `(count == i) ` `            ``{ ` ` `  `                ``// partition is possible ` `                ``possible = ``true``; ` ` `  `                ``// stores the left array sum ` `                ``long` `sumleft = presum[i]; ` ` `  `                ``// stores the right array sum ` `                ``long` `sumright = sufsum[i + ``1``]; ` ` `  `                ``// check if the difference is minimum ` `                ``if` `((Math.abs(sumleft - sumright)) < ans)  ` `                ``{ ` `                    ``ans = Math.abs(sumleft - sumright); ` `                    ``index = i + ``1``; ` `                ``} ` `            ``} ` `        ``} ` ` `  `        ``// is partition is possible or not ` `        ``if` `(possible) ` `            ``System.out.print(index + ``".5"` `+ ``"\n"``); ` `        ``else` `            ``System.out.print(-``1` `+ ``"\n"``); ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``int` `a[] = { ``1``, ``2``, -``1``, ``2``, ``3` `}; ` `        ``int` `n = a.length; ` ` `  `        ``partition(a, n); ` `    ``} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

## Python3

 `# Python program for SP- partition ` ` `  `# Function to find the partition ` `def` `partition(a: ``list``, n: ``int``): ` `    ``mpp ``=` `dict``() ` ` `  `    ``# mark the last occurrence of every element ` `    ``for` `i ``in` `range``(n): ` `        ``mpp[a[i]] ``=` `i ` ` `  `    ``# calculate the prefix sum ` `    ``preSum ``=` `[``0``] ``*` `n ` `    ``preSum[``0``] ``=` `a[``0``] ` `    ``for` `i ``in` `range``(``1``, n): ` `        ``preSum[i] ``=` `preSum[i ``-` `1``] ``+` `a[i] ` ` `  `    ``# calculate the suffix sum ` `    ``sufSum ``=` `[``0``] ``*` `n ` `    ``sufSum[n ``-` `1``] ``=` `a[n ``-` `1``] ` `    ``for` `i ``in` `range``(n ``-` `2``, ``-``1``, ``-``1``): ` `        ``sufSum[i] ``=` `sufSum[i ``+` `1``] ``+` `a[i] ` ` `  `    ``# Check if partition is possible ` `    ``possible ``=` `False` ` `  `    ``# Stores the absolute difference ` `    ``ans ``=` `int``(``1e18``) ` ` `  `    ``# stores the last index till ` `    ``# which there can not be any partition ` `    ``count ``=` `0` ` `  `    ``# Stores the partition ` `    ``index ``=` `-``1` ` `  `    ``# Check if partition is possible or not ` `    ``# donot check for the last element ` `    ``# as partition is not possible ` `    ``for` `i ``in` `range``(n ``-` `1``): ` ` `  `        ``# 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 ` `            ``sumleft ``=` `preSum[i] ` ` `  `            ``# stores the right array sum ` `            ``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: ` `        ``print``(``"%d.5"` `%` `index) ` `    ``else``: ` `        ``print``(``"-1"``) ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"``: ` ` `  `    ``a ``=` `[``1``, ``2``, ``-``1``, ``2``, ``3``] ` `    ``n ``=` `len``(a) ` ` `  `    ``partition(a, n) ` ` `  `# This code is contributed by ` `# sanjeev2552 `

## C#

 `// C# program for SP- partition  ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG  ` `{ ` ` `  `    ``// Function to find the partition ` `    ``static` `void` `partition(``int` `[]a, ``int` `n)  ` `    ``{ ` `        ``Dictionary<``int``,  ` `                   ``int``> mpp = ``new` `Dictionary<``int``,  ` `                                             ``int``>(); ` ` `  `        ``// mark the last occurrence of ` `        ``// every element ` `        ``for` `(``int` `i = 0; i < n; i++) ` `            ``if``(mpp.ContainsKey(a[i])) ` `                ``mpp[a[i]] = i; ` `            ``else` `                ``mpp.Add(a[i], i); ` ` `  `        ``// calculate the prefix sum ` `        ``long``[] presum = ``new` `long``[n]; ` `        ``presum[0] = a[0]; ` `        ``for` `(``int` `i = 1; i < n; i++) ` `            ``presum[i] = presum[i - 1] + a[i]; ` ` `  `        ``// calculate the suffix sum ` `        ``long``[] sufsum = ``new` `long``[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` `ans = (``long``) 1e18; ` ` `  `        ``// stores the last index till which  ` `        ``// there can not be any partition ` `        ``long` `count = 0; ` ` `  `        ``// Stores the partition ` `        ``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 its ` `            ``// last occurrence, stores the maximum  ` `            ``// of the last occurrence where ` `            ``// partition can be done ` `            ``count = Math.Max(count, mpp[a[i]]); ` ` `  `            ``// if partition is possible ` `            ``if` `(count == i) ` `            ``{ ` ` `  `                ``// partition is possible ` `                ``possible = ``true``; ` ` `  `                ``// stores the left array sum ` `                ``long` `sumleft = presum[i]; ` ` `  `                ``// stores the right array sum ` `                ``long` `sumright = sufsum[i + 1]; ` ` `  `                ``// check if the difference is minimum ` `                ``if` `((Math.Abs(sumleft -  ` `                              ``sumright)) < ans)  ` `                ``{ ` `                    ``ans = Math.Abs(sumleft - sumright); ` `                    ``index = i + 1; ` `                ``} ` `            ``} ` `        ``} ` ` `  `        ``// is partition is possible or not ` `        ``if` `(possible) ` `            ``Console.Write(index + ``".5"` `+ ``"\n"``); ` `        ``else` `            ``Console.Write(-1 + ``"\n"``); ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `Main(String[] args)  ` `    ``{ ` `        ``int` `[]a = { 1, 2, -1, 2, 3 }; ` `        ``int` `n = a.Length; ` ` `  `        ``partition(a, n); ` `    ``} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

Output:

```4.5
```

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.