Find Partition Line such that sum of values on left and right is equal
Last Updated :
27 Feb, 2022
Consider n points on the Cartesian Coordinate Plane. Let point (Xi, Yi) is given a value Vi. A line parallel to y-axis is said to be a good partition line if sum of the point values on its left is equal to the sum of the point values on its right. Note that if a point lies on the partition line then it is not considered on either side of the line. The task is to print Yes if a good partition line exist else print No.
Example:
Input: X[] = {-3, 5, 8}, Y[] = {8, 7, 9}, V[] = {8, 2, 10}
Output: Yes
x = c where 5 < c < 8 is a good partition
line as sum of values on both sides is 10.
Input: X[] = {-2, 5, 8, 9}, Y[] = {7, 7, 9, 8}, V[] = {6, 2, 1, 8}
Output: Yes
Approach:
- Count the value at each x-coordinate. This means that if there are multiple points with the same x-coordinate then they will be treated as a single point and their values will be added.
- Start from the minimum x-coordinate and check whether one of the following conditions is satisfied at Xi:
- Sum of the values till i – 1 is equal to sum of the values from i + 1 till n.
- Sum of the values till i is equal to sum of the values from i + 1 till n.
- Sum of the values till i – 1 is equal to sum of the values from i till n.
- If any of the above condition is satisfied for any given point then the line exists.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000;
bool lineExists( int x[], int y[],
int v[], int n)
{
int size = (2 * MAX) + 1;
long arr[size] = { 0 };
for ( int i = 0; i < n; i++) {
arr[x[i] + MAX] += v[i];
}
for ( int i = 1; i < size; i++)
arr[i] += arr[i - 1];
if (arr[size - 1] == 0)
return true ;
if (arr[size - 1] - arr[0] == 0)
return true ;
for ( int i = 1; i < size - 1; i++) {
if (arr[i - 1] == arr[size - 1] - arr[i - 1])
return true ;
if (arr[i - 1] == arr[size - 1] - arr[i])
return true ;
if (arr[i] == arr[size - 1] - arr[i])
return true ;
}
if (arr[size - 2] == 0)
return true ;
return false ;
}
int main()
{
int x[] = { -3, 5, 8 };
int y[] = { 8, 7, 9 };
int v[] = { 8, 2, 10 };
int n = sizeof (x) / sizeof ( int );
if (lineExists(x, y, v, n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG
{
static int MAX = 1000 ;
static boolean lineExists( int x[], int y[],
int v[], int n)
{
int size = ( 2 * MAX) + 1 ;
long []arr = new long [size];
for ( int i = 0 ; i < n; i++)
{
arr[x[i] + MAX] += v[i];
}
for ( int i = 1 ; i < size; i++)
arr[i] += arr[i - 1 ];
if (arr[size - 1 ] == 0 )
return true ;
if (arr[size - 1 ] - arr[ 0 ] == 0 )
return true ;
for ( int i = 1 ; i < size - 1 ; i++)
{
if (arr[i - 1 ] == arr[size - 1 ] - arr[i - 1 ])
return true ;
if (arr[i - 1 ] == arr[size - 1 ] - arr[i])
return true ;
if (arr[i] == arr[size - 1 ] - arr[i])
return true ;
}
if (arr[size - 2 ] == 0 )
return true ;
return false ;
}
public static void main(String []args)
{
int x[] = { - 3 , 5 , 8 };
int y[] = { 8 , 7 , 9 };
int v[] = { 8 , 2 , 10 };
int n = x.length;
if (lineExists(x, y, v, n))
System.out.printf( "Yes" );
else
System.out.printf( "No" );
}
}
|
Python3
MAX = 1000 ;
def lineExists(x, y, v, n) :
size = ( 2 * MAX ) + 1 ;
arr = [ 0 ] * size ;
for i in range (n) :
arr[x[i] + MAX ] + = v[i];
for i in range ( 1 , size) :
arr[i] + = arr[i - 1 ];
if (arr[size - 1 ] = = 0 ) :
return True ;
if (arr[size - 1 ] - arr[ 0 ] = = 0 ) :
return True ;
for i in range ( 1 , size - 1 ) :
if (arr[i - 1 ] = = arr[size - 1 ] - arr[i - 1 ]) :
return True ;
if (arr[i - 1 ] = = arr[size - 1 ] - arr[i]) :
return True ;
if (arr[i] = = arr[size - 1 ] - arr[i]) :
return True ;
if (arr[size - 2 ] = = 0 ) :
return True ;
return False ;
if __name__ = = "__main__" :
x = [ - 3 , 5 , 8 ];
y = [ 8 , 7 , 9 ];
v = [ 8 , 2 , 10 ];
n = len (x);
if (lineExists(x, y, v, n)) :
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG
{
static int MAX = 1000;
static Boolean lineExists( int []x, int []y,
int []v, int n)
{
int size = (2 * MAX) + 1;
long []arr = new long [size];
for ( int i = 0; i < n; i++)
{
arr[x[i] + MAX] += v[i];
}
for ( int i = 1; i < size; i++)
arr[i] += arr[i - 1];
if (arr[size - 1] == 0)
return true ;
if (arr[size - 1] - arr[0] == 0)
return true ;
for ( int i = 1; i < size - 1; i++)
{
if (arr[i - 1] == arr[size - 1] - arr[i - 1])
return true ;
if (arr[i - 1] == arr[size - 1] - arr[i])
return true ;
if (arr[i] == arr[size - 1] - arr[i])
return true ;
}
if (arr[size - 2] == 0)
return true ;
return false ;
}
public static void Main(String []args)
{
int []x = { -3, 5, 8 };
int []y = { 8, 7, 9 };
int []v = { 8, 2, 10 };
int n = x.Length;
if (lineExists(x, y, v, n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
const MAX = 1000;
function lineExists(x, y, v, n)
{
let size = (2 * MAX) + 1;
let arr = new Array(size).fill(0);
for (let i = 0; i < n; i++) {
arr[x[i] + MAX] += v[i];
}
for (let i = 1; i < size; i++)
arr[i] += arr[i - 1];
if (arr[size - 1] == 0)
return true ;
if (arr[size - 1] - arr[0] == 0)
return true ;
for (let i = 1; i < size - 1; i++) {
if (arr[i - 1] == arr[size - 1] - arr[i - 1])
return true ;
if (arr[i - 1] == arr[size - 1] - arr[i])
return true ;
if (arr[i] == arr[size - 1] - arr[i])
return true ;
}
if (arr[size - 2] == 0)
return true ;
return false ;
}
let x = [-3, 5, 8];
let y = [8, 7, 9]
let v = [8, 2, 10];
let n = x.length;
if (lineExists(x, y, v, n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity : O(N) , where N is equal to size = (2*MAX) + 1
Auxiliary Space : O(N) , where N is equal to size = (2*MAX) + 1
Share your thoughts in the comments
Please Login to comment...