HackerRank 'Sherlock and Squares' Solution

by Srikant Padala on April 30, 2016, 3:51 pm


Explanation


  • Find the next integer value of square root of a. Call this res.
  • while the square of res is there in the interval, increment count and res.

Sherlock and Squares Problem Statement

Video

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

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int t, a, b, count;
    cin >> t;
    while(t--) {
        cin >> a >> b;
        int res = ceil(sqrt(a));
        count = 0;
        while(res*res <= b) {
            count++;
            res++;
        }
        cout << count << endl;
    }
    return 0;
}

Coming Soon.