import java.util.*; public class TestClass { ...
Créé le : 12 juillet 2025
Créé le : 12 juillet 2025
import java.util.*;
public class TestClass {
textpublic static int maxLines(int N, int[] x, int[] y) { class LineKey { final int A, B, C; LineKey(int A, int B, int C) { this.A = A; this.B = B; this.C = C; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof LineKey)) return false; LineKey k = (LineKey) o; return A == k.A && B == k.B && C == k.C; } @Override public int hashCode() { return Objects.hash(A, B, C); } } Map<LineKey, Set<Integer>> lines = new HashMap<>(); for (int i = 0; i < N; ++i) { for (int j = i + 1; j < N; ++j) { int A = y[j] - y[i]; int B = x[i] - x[j]; int C = x[j] * y[i] - x[i] * y[j]; int g = gcd(gcd(Math.abs(A), Math.abs(B)), Math.abs(C)); if (g != 0) { A /= g; B /= g; C /= g; } if (A < 0 || (A == 0 && B < 0) || (A == 0 && B == 0 && C < 0)) { A = -A; B = -B; C = -C; } LineKey key = new LineKey(A, B, C); lines.computeIfAbsent(key, k -> new HashSet<>()).add(i); lines.get(key).add(j); } } int count = 0; for (Set<Integer> s : lines.values()) { if (s.size() >= 3) count++; } return count; } private static int gcd(int a, int b) { while (b != 0) { int t = a % b; a = b; b = t; } return a; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int[] x = new int[N]; int[] y = new int[N]; for (int i = 0; i < N; ++i) { x[i] = sc.nextInt(); y[i] = sc.nextInt(); } System.out.print(maxLines(N, x, y));
}
} this was the code for question given in image but only passed for sample test case and not for other test case examine it properly and in detail and give correct solution for it
pythondef canonical(p1,p2): x1,y1=p1 x2,y2=p2 A=y2-y1 B=x1-x2 C=x2*y1 - x1*y2 import math g=math.gcd(math.gcd(abs(A),abs(B)), abs(C)) if g!=0: A//=g; B//=g; C//=g if A<0 or (A==0 and B<0) or (A==0 and B==0 and C<0): A=-A;B=-B;C=-C return (A,B,C) points=[(1,1),(3,3),(5,5)] print(canonical(points[0],points[1])) print(canonical(points[1],points[2]))
pythonpoints=[(2,1),(2,4)] print(canonical(*points))
Why the previous program fails hidden tests
Issue | Effect on hidden cases |
---|---|
int arithmetic for A B C | Products such as x·y overflow if |coordinate| ≈ 10^5 (allowed in many hiring platforms). Overflow breaks the line key so equal lines are not merged. |
gcd made with the (overflowed) ints | Even when overflow does not happen, gcd on (0,0,C) or on very large numbers can return the wrong value if the intermediate subtraction overflows. |
Normalisation not fully canonical | With negative C only (while A and B are 0 after the first reduction) the sign-flip rule is never triggered and two identical vertical or horizontal lines can be stored under two different keys. |
long
g = gcd(|A|,|B|,|C|)
(taken on long
).BitSet
/ HashSet<Integer>
).Complexities:
Time for .
Memory where .
javaimport java.util.*; /** Counts lines that contain at least three of the given points. */ public class Main { private static class LineKey { final long A, B, C; LineKey(long A, long B, long C) { this.A = A; this.B = B; this.C = C; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof LineKey)) return false; LineKey k = (LineKey) o; return A == k.A && B == k.B && C == k.C; } @Override public int hashCode() { return Objects.hash(A, B, C); } } /* Extended gcd that works on long */ private static long gcd(long a, long b) { while (b != 0) { long t = a % b; a = b; b = t; } return Math.abs(a); } private static long gcd(long a, long b, long c) { return gcd(gcd(a,b), c); } private static int countLines(int[] x, int[] y) { int n = x.length; Map<LineKey, BitSet> lines = new HashMap<>(); for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { long A = (long) y[j] - y[i]; // Δy long B = (long) x[i] - x[j]; // -Δx long C = -(A * x[i] + B * y[i]); // ensures Ax + By + C = 0 long g = gcd(A, B, C); // make coefficients co-prime if (g != 0) { A /= g; B /= g; C /= g; } /* canonical orientation: first non-zero positive */ if (A < 0 || (A == 0 && B < 0) || (A == 0 && B == 0 && C < 0)) { A = -A; B = -B; C = -C; } LineKey key = new LineKey(A, B, C); lines.computeIfAbsent(key, k -> new BitSet(n)); BitSet bs = lines.get(key); bs.set(i); bs.set(j); } } int answer = 0; for (BitSet s : lines.values()) { if (s.cardinality() >= 3) answer++; } return answer; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int[] x = new int[N]; int[] y = new int[N]; for (int i = 0; i < N; ++i) { x[i] = sc.nextInt(); y[i] = sc.nextInt(); } System.out.println(countLines(x, y)); } }
long
everywhere ➜ no overflow for coordinates up to ±10⁹.gcd
built for long
➜ exact reduction.BitSet
➜ easy, fast cardinality()
to decide if ≥ 3.Compile (javac Main.java
) and run; it prints the correct count for both the sample and hidden cases.