fork download
  1. /*
  2. Problem: Find All Points Within a Radius
  3.  
  4. Given a list of points [x, y], a fixed radius, and query centers
  5. [qx, qy], return one list of matching point indices per query.
  6.  
  7. A point matches if its distance from the query center is at most
  8. the radius. Include points on the boundary. Return indices in
  9. increasing order, keep duplicate points as separate indices,
  10. and return [] for a query with no matches.
  11.  
  12. Approach: Scan all points for each query. Compare squared distances
  13. to avoid a square root. Scanning in index order keeps the hits sorted.
  14.  
  15. TC: O(n * q)
  16. SC: O(1) auxiliary space, O(q + k) output space
  17.  
  18. n = number of points
  19. q = number of queries
  20. k = total number of matching indices across all queries
  21. */
  22.  
  23. function pointsWithinRadius(points, radius, queries) {
  24. const answer = [];
  25. const radiusSquared = radius * radius;
  26.  
  27. for (const [qx, qy] of queries) {
  28. const hits = [];
  29.  
  30. for (let i = 0; i < points.length; i++) {
  31. const [x, y] = points[i];
  32. const dx = x - qx;
  33. const dy = y - qy;
  34.  
  35. if (dx * dx + dy * dy <= radiusSquared) {
  36. hits.push(i);
  37. }
  38. }
  39.  
  40. answer.push(hits);
  41. }
  42.  
  43. return answer;
  44. }
  45.  
  46. // Test cases
  47.  
  48. function runTest(name, points, radius, queries, expected) {
  49. const actual = pointsWithinRadius(points, radius, queries);
  50. const actualText = JSON.stringify(actual);
  51. const expectedText = JSON.stringify(expected);
  52. const passed = actualText === expectedText;
  53.  
  54. console.log(`${passed ? "PASS" : "FAIL"} - ${name}`);
  55. console.log(`Expected: ${expectedText}`);
  56. console.log(`Actual: ${actualText}\n`);
  57. }
  58.  
  59. // 1. Boundary and outside the circle - imp. Dry-run this first.
  60. // Radius squared is 4. The squared distances are 2, 4, and 8.
  61. // Include indices 0 and 1. Index 2 is outside the circle.
  62. runTest(
  63. "Include the boundary and exclude the corner",
  64. [[1, 1], [2, 0], [2, 2]],
  65. 2,
  66. [[0, 0]],
  67. [[0, 1]]
  68. );
  69.  
  70. // 2. Radius zero, duplicates, and no match - imp. Dry-run this next.
  71. // Both copies of [2, 2] match the first query. The second returns [].
  72. runTest(
  73. "Radius zero and duplicates",
  74. [[2, 2], [2, 2], [5, 5]],
  75. 0,
  76. [[2, 2], [3, 3]],
  77. [[0, 1], []]
  78. );
  79.  
  80. // 3. Multiple queries and negative coordinates.
  81. // Use this if asked how the outer loop preserves query order.
  82. runTest(
  83. "Multiple queries and negative coordinates",
  84. [[0, 0], [3, 4], [6, 8], [-3, -4], [1, 1]],
  85. 5,
  86. [[0, 0], [6, 8], [10, 10]],
  87. [[0, 1, 3, 4], [1, 2], [2]]
  88. );
  89.  
  90. // 4. Original index order. Explain this in one sentence.
  91. // Index 0 is the farthest match, but it comes first in the result.
  92. runTest(
  93. "Return indices in increasing order",
  94. [[4, 0], [0, 0], [1, 0]],
  95. 4,
  96. [[0, 0]],
  97. [[0, 1, 2]]
  98. );
Success #stdin #stdout 0.03s 5616KB
stdin
Standard input is empty
stdout
PASS - Include the boundary and exclude the corner
Expected: [[0,1]]
Actual:   [[0,1]]

PASS - Radius zero and duplicates
Expected: [[0,1],[]]
Actual:   [[0,1],[]]

PASS - Multiple queries and negative coordinates
Expected: [[0,1,3,4],[1,2],[2]]
Actual:   [[0,1,3,4],[1,2],[2]]

PASS - Return indices in increasing order
Expected: [[0,1,2]]
Actual:   [[0,1,2]]