Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Link = https://www.hackerrank.com/challenges/sherlock-and-squares/problem
- Solution = ( I only had to modify squares method)
- import java.io.*;
- import java.math.*;
- import java.security.*;
- import java.text.*;
- import java.util.*;
- import java.util.concurrent.*;
- import java.util.regex.*;
- public class Solution {
- static boolean isPerfect(int n)
- {
- if ((Math.sqrt(n) - Math.floor(Math.sqrt(n)))== 0)
- {
- return true;
- }
- else
- return false;
- }
- // Complete the squares function below.
- static int squares(int a, int b) {
- int n=b;
- int count=0;
- int lessn;
- while(n>a)
- {
- //count++;
- if(isPerfect(n))
- {
- count++;
- n=(int)Math.sqrt(n)-1;
- }
- else
- {
- n=(int)Math.sqrt(n);
- n=(int)Math.floor(n);
- }
- n=(int)Math.pow(n,2);
- }
- return count;
- }
- private static final Scanner scanner = new Scanner(System.in);
- public static void main(String[] args) throws IOException {
- BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
- int q = scanner.nextInt();
- scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
- for (int qItr = 0; qItr < q; qItr++) {
- String[] ab = scanner.nextLine().split(" ");
- int a = Integer.parseInt(ab[0]);
- int b = Integer.parseInt(ab[1]);
- int result = squares(a, b);
- bufferedWriter.write(String.valueOf(result));
- bufferedWriter.newLine();
- }
- bufferedWriter.close();
- scanner.close();
- }
- }
Add Comment
Please, Sign In to add comment