Given the coordinates of three points a, b, and c, find the coordinates of the fourth point d such that abcd forms a parallelogram.
Since three different parallelograms can be formed using the given three points, return the lexicographically smallest possible coordinates of d.
A point P(x1, y1) is lexicographically smaller than Q(x2, y2) if:
x1 < x2, or
x1 == x2 and y1 < y2.
Return the coordinates of the required point with a precision of 6 decimal places.
Note: The given three points are guaranteed to be non-collinear.
Examples:
Input: a = (3, 2), b = (3, 4), c = (2, 2) Output: [2.000000, 0.000000] Explanation: There are two options for point d : (2, 4) and (2, 0) such that abcd forms a parallelogram. Since (2, 0) is lexicographically smaller than (2, 4). Hence, (2, 0) is the answer.
Input: a = (1, 1), b = (2, 3), c = (4, 2) Output: [3.000000, 0.000000] Explanation: There are three possible coordinates for point d: (5, 4) (3, 0) (-1, 4) Among these, (3, 0) is the lexicographically smallest valid point. Hence, the answer is (3.000000, 0.000000).
Using the Diagonal Midpoint Property - O(1) Time and O(1) Space
The diagonals of a parallelogram bisect each other, so their midpoints are always the same. By treating each of the three given points as the vertex opposite the missing point, we can calculate three possible fourth vertices. We then choose the lexicographically smallest one.
Suppose, AC and BD are diagonals of a parallelogram, and diagonals of a parallelogram bisect each other. So their midpoints must be equal:
\frac{A+C}{2} = \frac{B+D}{2}
Multiply both sides by 2, we get -> A + C = B + D
Rearranging, we get -> D = A + C - B
That's where the formula comes from.
Use the property that the diagonals of a parallelogram bisect each other.
Find the first possible point by considering A and D as opposite vertices: D1 = B + C − A
Find the second possible point by considering B and D as opposite vertices: D2 = A + C − B
Find the third possible point by considering C and D as opposite vertices: D3 = A + B − C
Compare them lexicographically: first compare x, then y if x is equal.
Return the lexicographically smallest candidate.
C++
#includeusingnamespacestd;vector<double>findPoint(vector<int>&a,vector<int>&b,vector<int>&c){// If A and D are opposite vertices,// diagonals AD and BC have the same midpoint.// Therefore:// A + D = B + C// D = B + C - Apair<double,double>d1={b[0]+c[0]-a[0],b[1]+c[1]-a[1]};// If B and D are opposite vertices:// B + D = A + C// D = A + C - Bpair<double,double>d2={a[0]+c[0]-b[0],a[1]+c[1]-b[1]};// If C and D are opposite vertices:// C + D = A + B// D = A + B - Cpair<double,double>d3={a[0]+b[0]-c[0],a[1]+b[1]-c[1]};// pair comparison in C++ is lexicographical.// Hence, min() directly gives the required point.pair<double,double>ans=min({d1,d2,d3});return{ans.first,ans.second};}intmain(){vector<int>a={3,2};vector<int>b={3,4};vector<int>c={2,2};vector<double>ans=findPoint(a,b,c);cout<<fixed<<setprecision(6);cout<<ans[0]<<" "<<ans[1]<<"\n";return0;}
Java
importjava.util.*;classGFG{publicstaticList<Double>findPoint(int[]a,int[]b,int[]c){// If A and D are opposite vertices:// A + D = B + C// D = B + C - Adouble[]d1={b[0]+c[0]-a[0],b[1]+c[1]-a[1]};// If B and D are opposite vertices:// B + D = A + C// D = A + C - Bdouble[]d2={a[0]+c[0]-b[0],a[1]+c[1]-b[1]};// If C and D are opposite vertices:// C + D = A + B// D = A + B - Cdouble[]d3={a[0]+b[0]-c[0],a[1]+b[1]-c[1]};// Find the lexicographically smallest point.double[]ans=d1;if(d2[0]<ans[0]||(d2[0]==ans[0]&&d2[1]<ans[1])){ans=d2;}if(d3[0]<ans[0]||(d3[0]==ans[0]&&d3[1]<ans[1])){ans=d3;}// Return the answer as List.returnArrays.asList(ans[0],ans[1]);}publicstaticvoidmain(String[]args){int[]a={3,2};int[]b={3,4};int[]c={2,2};List<Double>ans=findPoint(a,b,c);System.out.printf("%.6f %.6f%n",ans.get(0),ans.get(1));}}
Python
deffindPoint(a,b,c):# If A and D are opposite vertices:# A + D = B + C# D = B + C - Ad1=[b[0]+c[0]-a[0],b[1]+c[1]-a[1]]# If B and D are opposite vertices:# B + D = A + C# D = A + C - Bd2=[a[0]+c[0]-b[0],a[1]+c[1]-b[1]]# If C and D are opposite vertices:# C + D = A + B# D = A + B - Cd3=[a[0]+b[0]-c[0],a[1]+b[1]-c[1]]# Python compares lists lexicographically,# so min() directly gives the required point.ans=min(d1,d2,d3)return[float(ans[0]),float(ans[1])]# Driver Codeif__name__=="__main__":a=[3,2]b=[3,4]c=[2,2]ans=findPoint(a,b,c)print(f"{ans[0]:.6f}{ans[1]:.6f}")
C#
usingSystem;usingSystem.Collections.Generic;classGFG{staticList<double>findPoint(int[]a,int[]b,int[]c){// If A and D are opposite vertices:// A + D = B + C// D = B + C - Adouble[]d1={b[0]+c[0]-a[0],b[1]+c[1]-a[1]};// If B and D are opposite vertices:// B + D = A + C// D = A + C - Bdouble[]d2={a[0]+c[0]-b[0],a[1]+c[1]-b[1]};// If C and D are opposite vertices:// C + D = A + B// D = A + B - Cdouble[]d3={a[0]+b[0]-c[0],a[1]+b[1]-c[1]};// Find the lexicographically smallest point.double[]ans=d1;if(d2[0]<ans[0]||(d2[0]==ans[0]&&d2[1]<ans[1])){ans=d2;}if(d3[0]<ans[0]||(d3[0]==ans[0]&&d3[1]<ans[1])){ans=d3;}// Return the answer as List.returnnewList<double>{ans[0],ans[1]};}staticvoidMain(){int[]a={3,2};int[]b={3,4};int[]c={2,2};List<double>ans=findPoint(a,b,c);Console.WriteLine($"{ans[0]:F6} {ans[1]:F6}");}}
JavaScript
functionfindPoint(a,b,c){// If A and D are opposite vertices:// A + D = B + C// D = B + C - Aconstd1=[b[0]+c[0]-a[0],b[1]+c[1]-a[1]];// If B and D are opposite vertices:// B + D = A + C// D = A + C - Bconstd2=[a[0]+c[0]-b[0],a[1]+c[1]-b[1]];// If C and D are opposite vertices:// C + D = A + B// D = A + B - Cconstd3=[a[0]+b[0]-c[0],a[1]+b[1]-c[1]];// Find the lexicographically smallest point.constpoints=[d1,d2,d3];letans=points[0];for(leti=1;i<points.length;i++){// Compare x-coordinates first.// If x is equal, compare y-coordinates.if(points[i][0]<ans[0]||(points[i][0]===ans[0]&&points[i][1]<ans[1])){ans=points[i];}}returnans;}// Driver Codeconsta=[3,2];constb=[3,4];constc=[2,2];constans=findPoint(a,b,c);console.log(`${ans[0].toFixed(6)}${ans[1].toFixed(6)}`);