Sherlock and Squares HackerRank

Botman
ProgrammerCave
Published in
1 min readDec 19, 2019

Problem :

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

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

For example, the range is a = 24 and b = 49, inclusive. There are three square integers in the range: 25, 36 and 49.

Read full problem : Sherlock and Squares.

Solution :

Let the integer variables first_num store the first number of the range and last_num store the last number of the range.

To find number of square integers in the range [first_num, last_num], first we calculate square root of the first_num and an integer variable count stores the number of square integers in the range.

For eg. in the range [35, 70], variable sr is equal to 5 since √35 = 5.916. If square of sr is greater than or equal to the first_num and less than or equal to the last_num then the number ( sr * sr) is in the range and is a square integer.

C++ Implementation on Programmercave

Other Competitive Programming Problems and Solutions

Circular Array Rotation
Drawing Book HackerRank Challenge

Originally published at https://programmercave.com/ on December 19, 2019.

--

--