Q59 PepCoding | Line Reflection
Fri Feb 03 2023 05:41:43 GMT+0000 (Coordinated Universal Time)
Saved by
@Ayush_dabas07
import java.io.*;
import java.util.*;
public class Main {
public static boolean isReflected(int[][] points) {
//find extremetis while making frequency map of points
HashMap<Long,Long> map = new HashMap<>();
long xmax = Integer.MIN_VALUE;
long xmin = Integer.MAX_VALUE;
for(int point[] : points){
long x = point[0];
long y = point[1];
xmax = Math.max(xmax , x);
xmin = Math.min(xmin , x);
//make a unique hash and store it against frequency
long hash = x * 100000000 + y; //constraint is 10^8
//we use L to convert it into long format
map.put(hash , 1L);
}
//find mirror between extremetis
long mirror = xmax + xmin;
for(int point[] : points){
long x = point[0];
long y = point[1];
//mirror point
long ximg = mirror - x;
//creating unique hash for mirror point
long hash_img = ximg * 100000000 + y;
//checking if mirror point exists
if(map.containsKey(hash_img) == false)
return false;
}
return true;
}
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int[][] points = new int[n][2];
for (int i = 0; i < points.length; i++) {
for (int j = 0; j < points[0].length; j++) {
points[i][j] = scn.nextInt();
}
}
System.out.println(isReflected(points));
}
}
content_copyCOPY
https://www.pepcoding.com/resources/data-structures-and-algorithms-in-java-levelup/hashmap-and-heaps/line-reflection-official/ojquestion
Comments