Related Articles
Find Partition Line such that sum of values on left and right is equal
• Last Updated : 30 Jul, 2020

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

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

Approach:

1. 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.
2. 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.
3. 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++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;`` ` `const` `int` `MAX = 1000;`` ` `// Function that returns true if``// the required line exists``bool` `lineExists(``int` `x[], ``int` `y[],``                ``int` `v[], ``int` `n)``{`` ` `    ``// To handle negative values from x[]``    ``int` `size = (2 * MAX) + 1;``    ``long` `arr[size] = { 0 };`` ` `    ``// Update arr[] such that arr[i] contains``    ``// the sum of all v[j] such that x[j] = i``    ``// for all valid values of j``    ``for` `(``int` `i = 0; i < n; i++) {``        ``arr[x[i] + MAX] += v[i];``    ``}`` ` `    ``// Update arr[i] such that arr[i] contains``    ``// the sum of the subarray arr[0...i]``    ``// from the original array``    ``for` `(``int` `i = 1; i < size; i++)``        ``arr[i] += arr[i - 1];`` ` `    ``// If all the points add to 0 then``    ``// the line can be drawn anywhere``    ``if` `(arr[size - 1] == 0)``        ``return` `true``;`` ` `    ``// If the line is drawn touching the``    ``// leftmost possible points``    ``if` `(arr[size - 1] - arr == 0)``        ``return` `true``;`` ` `    ``for` `(``int` `i = 1; i < size - 1; i++) {`` ` `        ``// If the line is drawn just before``        ``// the current point``        ``if` `(arr[i - 1] == arr[size - 1] - arr[i - 1])``            ``return` `true``;`` ` `        ``// If the line is drawn touching``        ``// the current point``        ``if` `(arr[i - 1] == arr[size - 1] - arr[i])``            ``return` `true``;`` ` `        ``// If the line is drawn just after``        ``// the current point``        ``if` `(arr[i] == arr[size - 1] - arr[i])``            ``return` `true``;``    ``}`` ` `    ``// If the line is drawn touching the``    ``// rightmost possible points``    ``if` `(arr[size - 2] == 0)``        ``return` `true``;`` ` `    ``return` `false``;``}`` ` `// Driver code``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

 `// Java implementation of the approach``class` `GFG ``{``static` `int` `MAX = ``1000``;`` ` `// Function that returns true if``// the required line exists``static` `boolean` `lineExists(``int` `x[], ``int` `y[],``                          ``int` `v[], ``int` `n)``{`` ` `    ``// To handle negative values from x[]``    ``int` `size = (``2` `* MAX) + ``1``;``    ``long` `[]arr = ``new` `long``[size];`` ` `    ``// Update arr[] such that arr[i] contains``    ``// the sum of all v[j] such that x[j] = i``    ``// for all valid values of j``    ``for` `(``int` `i = ``0``; i < n; i++) ``    ``{``        ``arr[x[i] + MAX] += v[i];``    ``}`` ` `    ``// Update arr[i] such that arr[i] contains``    ``// the sum of the subarray arr[0...i]``    ``// from the original array``    ``for` `(``int` `i = ``1``; i < size; i++)``        ``arr[i] += arr[i - ``1``];`` ` `    ``// If all the points add to 0 then``    ``// the line can be drawn anywhere``    ``if` `(arr[size - ``1``] == ``0``)``        ``return` `true``;`` ` `    ``// If the line is drawn touching the``    ``// leftmost possible points``    ``if` `(arr[size - ``1``] - arr[``0``] == ``0``)``        ``return` `true``;`` ` `    ``for` `(``int` `i = ``1``; i < size - ``1``; i++) ``    ``{`` ` `        ``// If the line is drawn just before``        ``// the current point``        ``if` `(arr[i - ``1``] == arr[size - ``1``] - arr[i - ``1``])``            ``return` `true``;`` ` `        ``// If the line is drawn touching``        ``// the current point``        ``if` `(arr[i - ``1``] == arr[size - ``1``] - arr[i])``            ``return` `true``;`` ` `        ``// If the line is drawn just after``        ``// the current point``        ``if` `(arr[i] == arr[size - ``1``] - arr[i])``            ``return` `true``;``    ``}`` ` `    ``// If the line is drawn touching the``    ``// rightmost possible points``    ``if` `(arr[size - ``2``] == ``0``)``        ``return` `true``;`` ` `    ``return` `false``;``}`` ` `// Driver code``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"``);``}``}`` ` `// This code is contributed by 29AjayKumar`

## Python3

 `# Python3 implementation of the approach``MAX` `=` `1000``; `` ` `# Function that returns true if ``# the required line exists ``def` `lineExists(x, y, v, n) :`` ` `    ``# To handle negative values from x[] ``    ``size ``=` `(``2` `*` `MAX``) ``+` `1``; ``    ``arr ``=` `[``0``] ``*` `size ; `` ` `    ``# Update arr[] such that arr[i] contains ``    ``# the sum of all v[j] such that x[j] = i ``    ``# for all valid values of j ``    ``for` `i ``in` `range``(n) :``        ``arr[x[i] ``+` `MAX``] ``+``=` `v[i]; `` ` `    ``# Update arr[i] such that arr[i] contains ``    ``# the sum of the subarray arr[0...i] ``    ``# from the original array ``    ``for` `i ``in` `range``(``1``, size) :``        ``arr[i] ``+``=` `arr[i ``-` `1``]; `` ` `    ``# If all the points add to 0 then ``    ``# the line can be drawn anywhere ``    ``if` `(arr[size ``-` `1``] ``=``=` `0``) :``        ``return` `True``; `` ` `    ``# If the line is drawn touching the ``    ``# leftmost possible points ``    ``if` `(arr[size ``-` `1``] ``-` `arr[``0``] ``=``=` `0``) :``        ``return` `True``; `` ` `    ``for` `i ``in` `range``(``1``, size ``-` `1``) : `` ` `        ``# If the line is drawn just before ``        ``# the current point ``        ``if` `(arr[i ``-` `1``] ``=``=` `arr[size ``-` `1``] ``-` `arr[i ``-` `1``]) :``            ``return` `True``; `` ` `        ``# If the line is drawn touching ``        ``# the current point ``        ``if` `(arr[i ``-` `1``] ``=``=` `arr[size ``-` `1``] ``-` `arr[i]) :``            ``return` `True``; `` ` `        ``# If the line is drawn just after ``        ``# the current point ``        ``if` `(arr[i] ``=``=` `arr[size ``-` `1``] ``-` `arr[i]) :``            ``return` `True``; `` ` `    ``# If the line is drawn touching the ``    ``# rightmost possible points ``    ``if` `(arr[size ``-` `2``] ``=``=` `0``) :``        ``return` `True``; `` ` `    ``return` `False``; `` ` `# Driver code ``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"``); `` ` `# This code is contributed by AnkitRai01`

## C#

 `// C# implementation of the approach``using` `System;``     ` `class` `GFG ``{``static` `int` `MAX = 1000;`` ` `// Function that returns true if``// the required line exists``static` `Boolean lineExists(``int` `[]x, ``int` `[]y,``                          ``int` `[]v, ``int` `n)``{`` ` `    ``// To handle negative values from x[]``    ``int` `size = (2 * MAX) + 1;``    ``long` `[]arr = ``new` `long``[size];`` ` `    ``// Update arr[] such that arr[i] contains``    ``// the sum of all v[j] such that x[j] = i``    ``// for all valid values of j``    ``for` `(``int` `i = 0; i < n; i++) ``    ``{``        ``arr[x[i] + MAX] += v[i];``    ``}`` ` `    ``// Update arr[i] such that arr[i] contains``    ``// the sum of the subarray arr[0...i]``    ``// from the original array``    ``for` `(``int` `i = 1; i < size; i++)``        ``arr[i] += arr[i - 1];`` ` `    ``// If all the points add to 0 then``    ``// the line can be drawn anywhere``    ``if` `(arr[size - 1] == 0)``        ``return` `true``;`` ` `    ``// If the line is drawn touching the``    ``// leftmost possible points``    ``if` `(arr[size - 1] - arr == 0)``        ``return` `true``;`` ` `    ``for` `(``int` `i = 1; i < size - 1; i++) ``    ``{`` ` `        ``// If the line is drawn just before``        ``// the current point``        ``if` `(arr[i - 1] == arr[size - 1] - arr[i - 1])``            ``return` `true``;`` ` `        ``// If the line is drawn touching``        ``// the current point``        ``if` `(arr[i - 1] == arr[size - 1] - arr[i])``            ``return` `true``;`` ` `        ``// If the line is drawn just after``        ``// the current point``        ``if` `(arr[i] == arr[size - 1] - arr[i])``            ``return` `true``;``    ``}`` ` `    ``// If the line is drawn touching the``    ``// rightmost possible points``    ``if` `(arr[size - 2] == 0)``        ``return` `true``;`` ` `    ``return` `false``;``}`` ` `// Driver code``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"``);``}``}`` ` `// This code is contributed by Rajput-Ji`
Output:
```Yes
```

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.  Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live and Geeks Classes Live USA

My Personal Notes arrow_drop_up