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.
C++
#include <bits/stdc++.h>
using namespace std;
void partition( int a[], int n)
{
unordered_map< long long , long long > mpp;
for ( int i = 0; i < n; i++)
mpp[a[i]] = i;
long long presum[n];
presum[0] = a[0];
for ( int i = 1; i < n; i++)
presum[i] = presum[i - 1] + a[i];
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];
}
bool possible = false ;
long long ans = 1e18;
long long count = 0;
long long index = -1;
for ( int i = 0; i < n - 1; i++) {
count = max(count, mpp[a[i]]);
if (count == i) {
possible = true ;
long long sumleft = presum[i];
long long sumright = sufsum[i + 1];
if (( abs (sumleft - sumright)) < ans) {
ans = abs (sumleft - sumright);
index = i + 1;
}
}
}
if (possible)
cout << index << ".5" << endl;
else
cout << -1 << endl;
}
int main()
{
int a[] = { 1, 2, -1, 2, 3 };
int n = sizeof (a) / sizeof (a[0]);
partition(a, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void partition( int a[], int n)
{
Map<Integer,
Integer> mpp = new HashMap<>();
for ( int i = 0 ; i < n; i++)
mpp.put(a[i], i);
long [] presum = new long [n];
presum[ 0 ] = a[ 0 ];
for ( int i = 1 ; i < n; i++)
presum[i] = presum[i - 1 ] + a[i];
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];
}
boolean possible = false ;
long ans = ( long ) 1e18;
long count = 0 ;
long index = - 1 ;
for ( int i = 0 ; i < n - 1 ; i++)
{
count = Math.max(count, mpp.get(a[i]));
if (count == i)
{
possible = true ;
long sumleft = presum[i];
long sumright = sufsum[i + 1 ];
if ((Math.abs(sumleft - sumright)) < ans)
{
ans = Math.abs(sumleft - sumright);
index = i + 1 ;
}
}
}
if (possible)
System.out.print(index + ".5" + "\n" );
else
System.out.print(- 1 + "\n" );
}
public static void main(String[] args)
{
int a[] = { 1 , 2 , - 1 , 2 , 3 };
int n = a.length;
partition(a, n);
}
}
|
Python3
def partition(a: list , n: int ):
mpp = dict ()
for i in range (n):
mpp[a[i]] = i
preSum = [ 0 ] * n
preSum[ 0 ] = a[ 0 ]
for i in range ( 1 , n):
preSum[i] = preSum[i - 1 ] + a[i]
sufSum = [ 0 ] * n
sufSum[n - 1 ] = a[n - 1 ]
for i in range (n - 2 , - 1 , - 1 ):
sufSum[i] = sufSum[i + 1 ] + a[i]
possible = False
ans = int ( 1e18 )
count = 0
index = - 1
for i in range (n - 1 ):
count = max (count, mpp[a[i]])
if count = = i:
possible = True
sumleft = preSum[i]
sumright = sufSum[i + 1 ]
if abs (sumleft - sumright) < ans:
ans = abs (sumleft - sumright)
index = i + 1
if possible:
print ( "%d.5" % index)
else :
print ( "-1" )
if __name__ = = "__main__" :
a = [ 1 , 2 , - 1 , 2 , 3 ]
n = len (a)
partition(a, n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void partition( int []a, int n)
{
Dictionary< int ,
int > mpp = new Dictionary< int ,
int >();
for ( int i = 0; i < n; i++)
if (mpp.ContainsKey(a[i]))
mpp[a[i]] = i;
else
mpp.Add(a[i], i);
long [] presum = new long [n];
presum[0] = a[0];
for ( int i = 1; i < n; i++)
presum[i] = presum[i - 1] + a[i];
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];
}
bool possible = false ;
long ans = ( long ) 1e18;
long count = 0;
long index = -1;
for ( int i = 0; i < n - 1; i++)
{
count = Math.Max(count, mpp[a[i]]);
if (count == i)
{
possible = true ;
long sumleft = presum[i];
long sumright = sufsum[i + 1];
if ((Math.Abs(sumleft -
sumright)) < ans)
{
ans = Math.Abs(sumleft - sumright);
index = i + 1;
}
}
}
if (possible)
Console.Write(index + ".5" + "\n" );
else
Console.Write(-1 + "\n" );
}
public static void Main(String[] args)
{
int []a = { 1, 2, -1, 2, 3 };
int n = a.Length;
partition(a, n);
}
}
|
Javascript
<script>
function partition(a, n) {
let mpp = new Map();
for (let i = 0; i < n; i++)
mpp.set(a[i], i);
let presum = new Array(n);
presum[0] = a[0];
for (let i = 1; i < n; i++)
presum[i] = presum[i - 1] + a[i];
let sufsum = new Array(n);
sufsum[n - 1] = a[n - 1];
for (let i = n - 2; i >= 0; i--) {
sufsum[i] = sufsum[i + 1] + a[i];
}
let possible = false ;
let ans = Number.MAX_SAFE_INTEGER;
let count = 0;
let index = -1;
for (let i = 0; i < n - 1; i++) {
count = Math.max(count, mpp.get(a[i]));
if (count == i) {
possible = true ;
let sumleft = presum[i];
let sumright = sufsum[i + 1];
if ((Math.abs(sumleft - sumright)) < ans) {
ans = Math.abs(sumleft - sumright);
index = i + 1;
}
}
}
if (possible)
document.write(index + ".5" + "<br>" );
else
document.write(-1 + "<br>" );
}
let a = [1, 2, -1, 2, 3];
let n = a.length;
partition(a, n);
</script>
|
Time Complexity: O(n) under the assumption that unordered_map search works in O(1) time.
Auxiliary Space: O(n)
Last Updated :
03 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...