KCE_0005-Editorial

Titanic
Inception

Setter : harijey
Tester : rohinth_076
Editorialist: rohinth_076

DIFFICULTY:

Simple

PREREQUISITES:

None

PROBLEM:

The new Titanic Ship sets sails from London to New York in December. Since it is winter icebergs in the North Atlantic Ocean pose a great threat to the ship. Since it is a new era and with modern GPS it is easier to spot the location of icebergs very easily. The captain of the ship is given the Cartesian coordinates of all icebergs. They want to find the number of icebergs in around N nautical mile radius from his ship. You being his assistant he seeks out your help. Help the captain to figure out the number of icebergs around his ship. You are given the location of the ship and the N nautical miles he wants to find and the cartesian coordinate of every iceberg in the North Atlantic Ocean.

Input:

2 3
2
5
1 3
3 3
5 3
2 2
2 5

Output:

4

EXPLANATION:

  • From the above picture you understand the concepts.
  • We need to find how many icebergs are in the ship radius.
  • Now just find the distance between the ship and each iceberg. If the distance is less than or equal to the radius then increase the count.

Formula :

Math.sqrt(Math.pow(x1-x2,2) +Math.pow(y1-y2,2));

TIME COMPLEXITY:

O(m) for each test case. m means a number of icebergs.

SOLUTION:

import java.io.*;
import java.util.*;
class Solution{
	static Scanner fs  = new Scanner(System.in);
	static PrintWriter out = new PrintWriter(System.out);

	public static void main(String[] args) {
	    
	    int x = fs.nextInt(),y = fs.nextInt();
       int n = fs.nextInt(),m = fs.nextInt();
       int ans = 0;
       for(int i=1;i<=m;i++){
           int x1 = fs.nextInt(),y1 = fs.nextInt();
           if(Math.sqrt((Math.pow(x1-x,2) + Math.pow(y1-y,2))) <= n){
               ans++;
           }
       }
       out.println(ans);
       	out.flush();	
	}
}