Skip to content
Programmingoneonone
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

  • Home
  • CS Subjects
    • IoT ? Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

HackerRank Sherlock and Squares problem solution

YASH PAL, 31 July 2024

In this HackerRank Sherlock and Squares problem, you need to complete the squares function that should return an integer representing the number of square integers in the inclusive range from a to b.

HackerRank Sherlock and Squares problem solution

Topics we are covering

Toggle
  • Problem solution in Python programming.
  • Problem solution in Java Programming.
    • Problem solution in C++ programming.
    • Problem solution in C programming.
    • Problem solution in JavaScript programming.

Problem solution in Python programming.

t = int(input())

for i in range (0,t):
	count = 0
	a,b = [int(j) for j in input().strip().split()]
	square1 = a ** (.5)
	if (square1 != int(square1)):
		a1 = int(square1) + 1
	else:
		a1 = int(square1)
	square2 = b ** (.5)
	b1 = int(square2)
	count = b1 - a1 +1
	print(count)

Problem solution in Java Programming.

import java.io.*;
import java.util.*;

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. */
        Scanner in = new Scanner(System.in);
        int square_count = 0;
        int test_cases = in.nextInt();
        int from;
        int to;
        int squareroot;
        for (int i = 0; i < test_cases; i++) {
            from = in.nextInt();
            to = in.nextInt();
            int a = (int)Math.ceil(Math.sqrt(from));
            int b = (int)Math.floor(Math.sqrt(to));
            square_count = b - a + 1;
            System.out.println(square_count);
        }
        
        in.close();
        
    }
    

}

Problem solution in C++ programming.

#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;
}

Problem solution in C programming.

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

int main() 
{
  
  //Grab the number of tests.
  int T;
  scanf( "%d", &T );
 
  //Check for SQ T times.
  for( int i = 0; i < T; i++ )
  {
    long long int A;
    scanf( "%llu", &A );
  
    long long int B;
    scanf( "%llu", &B );
    
    long long int count = 0;

     
    long long int sqA = sqrt( A );
    long long int sqB = sqrt( B );
    long double sqDoub = (long double)A;
    sqDoub = sqrt( A );
    if( sqDoub != sqA )
      count--;
    count += sqB - sqA + 1;
    printf( "%llun", count );   
  }
    return 0;
 
}

Problem solution in JavaScript programming.

function processData(input) {
    //Enter your code here
    var lines = input.split('n');
    var testCount = lines[0];
    
    for(var i=1;i<lines.length;++i){
        var test = lines[i].split(' ');
        var hit = 0;
        var min = Math.ceil(Math.sqrt(test[0]));
        var max = Math.sqrt(test[1]);
        
        for(var j = min; j <= max; ++j){
            if( (j * j)<= test[1] )
                hit += 1;
        }
        console.log(hit);
    }
} 

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

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

Algorithms coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes