Sort by

recency

|

1577 Discussions

|

  • + 0 comments

    Here is problem solution in Python, Java, C++, C and Javascript - https://programmingoneonone.com/hackerrank-sherlock-and-squares-problem-solution.html

  • + 0 comments

    SOLUTION IN RUST:

    fn squares(a: i32, b: i32) -> i32 {
        let c = (a as f64).sqrt().ceil() as i32;
        let f = (b as f64).sqrt().floor() as i32;
    
        (c..=f).count() as i32
    }
    
  • + 0 comments

    Here are my c++ solutions, you can watch the explanation here : https://youtu.be/LtU0ItsvbMI

    Solution 1 O(√n)

    int squares(int a, int b) {
        int i = ceil(sqrt(a)), result = 0;
        for(; i * i <= b; i++) result++;
        return result;
    }
    

    Solution 2 O(1)

    int squares(int a, int b) {
        int i = ceil(sqrt(a)), j = floor(sqrt(b));
        return j - i + 1;
    }
    
  • + 1 comment
    def squares(a, b):
        # Write your code here
        i = 0 
        while i**2 <= a:
            i += 1
        i = i -1    
        y = i 
        
        if i**2 == a:
            counter = 1
        else:
            counter = 0
    
    while y**2 <= b:
        counter += 1
        y += 1
    return  counter -1 
    
  • + 0 comments
    def squares(a, b):
        
        # Find the smallest integer whose square is >= a
        start = math.ceil(math.sqrt(a))
        # Find the largest integer whose square is <= b
        end = math.floor(math.sqrt(b))
        # The count of perfect squares is the difference + 1
        return max(0, end - start + 1)