Sort by

recency

|

1579 Discussions

|

  • + 0 comments

    Easiest Javascript solution without any loop:

    function squares(a, b) {
        let sqrtA=Math.ceil(Math.sqrt(a));
        let sqrtB=Math.floor(Math.sqrt(b));
        
        return (sqrtB-sqrtA)+1;
    
    }
    
  • + 0 comments
    public static int squares(int start, int end) {
        int startVal = (int)Math.sqrt(start);
        int endVal = (int)Math.sqrt(end);
        int sqrtcount = 0;
        sqrtcount = (int)IntStream.rangeClosed(startVal, endVal).
        filter(a -> {
            int srt = a*a;
            return srt >= start && srt <=end;
        }).count();
        return sqrtcount;
    }
    
  • + 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;
    }