# HackerRank Sherlock and Squares Solution

Hello Programmers, In this post, you will learn how to solve HackerRank Sherlock and Squares Solution. This problem is a part of the HackerRank Algorithms Series.

One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions. We are going to solve the HackerRank Algorithms problems using C, CPP, JAVA, PYTHON, JavaScript & SCALA Programming Languages.

You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C ProgrammingHackerRank C++ ProgrammingHackerRank Java Programming, HackerRank Python ProgrammingHackerRank Linux ShellHackerRank SQL Programming, and HackerRank 10 days of Javascript.

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology, Data StructuresRDBMS ProgramsJava Programs Solutions,  Fiverr Skills Test answersGoogle Course AnswersLinkedin Assessment, and Coursera Quiz Answers.

## HackerRank Sherlock and Squares

Watson likes to challenge Sherlock’s math ability. He will provide a starting and ending value that describe a range of integers, inclusive of the endpoints. Sherlock must determine the number of square integers within that range.

Note: A square integer is an integer which is the square of an integer, e.g. 1, 4, 9, 16, 25.

Example
a = 24
b = 49

There are three square integers in the range: 25, 36 and 49. Return 3.

Function Description

Complete the squares function in the editor below. It should return an integer representing the number of square integers in the inclusive range from a to b.

squares has the following parameter(s):

• int a: the lower range boundary
• int b: the upper range boundary

Returns

• int: the number of square integers in the range

Input Format

The first line contains q, the number of test cases.
Each of the next q lines contains two space-separated integers, a and b, the starting and ending integers in the ranges.

Constraints

• 1 <= q <= 100
• 1 <= a <= b <= 109

Sample Input

2
3 9
17 24

Sample Output

2
0

Explanation

Test Case #00: In range [3, 9]4 and 9 are the two square integers.
Test Case #01: In range [17, 24], there are no square integers.

## HackerRank Sherlock and Squares Solution

### Sherlock and Squares Solution in C

```#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
int t;
int a,b,sa,sb;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&a,&b);
sa = sqrt(a), sb = sqrt(b);
if(sa*sa == a)sa--;
printf("%d\n",sb - sa);
}
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}```

### Sherlock and Squares Solution in Cpp

```#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int n,m;
cin>>n;

while (cin>>n>>m)
{
cout<<(int)(sqrt(m)+0.0000001)-(int)(sqrt(n-1)+0.0000001)<<endl;
}
return 0;
}```

### Sherlock and Squares Solution in Java

```//package contest;

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
try{
int num = Integer.parseInt(line);
for(int i = 0; i < num; i++){
int a = Integer.parseInt(inp[0]);
int b = Integer.parseInt(inp[1]);
int count = 0;
for(int j = 1; j*j <= b; j++){
if(j*j >= a){
count++;
}
}
System.out.println(count);
}

} catch(Exception e) {

}

}
}```

### Sherlock and Squares Solution in Python

```from math import sqrt
T = int(input())
for i in range(T):
count = 0
inp = [int(a) for a in input().split()]
c = inp[0]
while int(sqrt(c)) != sqrt(c) and c<inp[1]:
c+=1
while c <= inp[1] and int(sqrt(c)) == sqrt(c):
count += 1
c += 2*sqrt(c)+1
print(count)```

### Sherlock and Squares Solution using JavaScript

```function processData(input) {
lines = input.trim().split("\n");

for(var i=1;i<lines.length && lines[i].length > 0;i++){

start = parseInt(lines[i].trim().split(" ")[0]);
end = parseInt(lines[i].trim().split(" ")[1]);
squares =0;

min = Math.sqrt(start);

if(min != Math.floor(min)){
min = Math.floor(min) + 1;
}

max = Math.sqrt(end);
if(max != Math.floor(max)){
max = Math.floor(max);
}

if(max < min){
console.log("0");
} else {

console.log((max-min+1));
}

}
}

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});

process.stdin.on("end", function () {
processData(_input);
});```

### Sherlock and Squares Solution in Scala

```object Solution {

def main(args: Array[String]) {
for(i <- 1 to t) println(f(readLine.split(" ")));
}

def f(n:Array[String]):Long = {
return Math.sqrt(n(1).toInt).toLong - Math.sqrt(n(0).toInt-1).toLong;
}
}```

### Sherlock and Squares Solution in Pascal

```program PSherlockandSquares;
Uses math;
var T,A,B,i:Longint;
begin
for i:=1 to T do
begin
WriteLn(floor(sqrt(B))-ceil(sqrt(A))+1);
end;
end.```

Disclaimer: This problem (Sherlock and Squares) is generated by HackerRank but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purposes.

## FAQ:

1. How do you solve the first question in HackerRank?

If you want to solve the first question of Hackerrank then you have to decide which programing language you want to practice i.e C programming, Cpp Programing, or Java programming then you have to start with the first program HELLO WORLD.

2. How do I find my HackerRank ID?

You will receive an email from HackerRank to confirm your access to the ID. Once you have confirmed your email, the entry will show up as verified on the settings page. You will also have an option to “Make primary”. Click on that option. Read more

3. Does HackerRank detect cheating?

yes, HackerRank uses a powerful tool to detect plagiarism in the candidates’ submitted code. The Test report of a candidate highlights any plagiarized portions in the submitted code and helps evaluators to verify the integrity of answers provided in the Test.

4. Does HackerRank use camera?

No for coding practice Hackerrank does not use camera but for companies’ interviews code submission time Hackerrank uses the camera.

5. Should I put HackerRank certificate on resume?

These certificates are useless, and you should not put them on your resume. The experience you gained from getting them is not useless. Use it to build a portfolio, and link to it on your resume.

6. Can I retake HackerRank test?

The company which sent you the HackerRank Test invite owns your Test submissions and results. It’s their discretion to permit a reattempt for a particular Test. If you wish to retake the test, we recommend that you contact the concerned recruiter who invited you to the Test and request a re-invite.

7. What is HackerRank?

HackerRank is a tech company that focuses on competitive programming challenges for both consumers and businesses. Developers compete by writing programs according to provided specifications. Wikipedi

Finally, we are now, in the end, I just want to conclude some important message for you

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.