# Solution to Leetcode’s Valid Perfect Square

## THE NAIVE APPROACH

`public static boolean isPerfectSquareWithBruteForce(int num) {    if (num == 1) return true;    for (int i = 1; i < num; i++) {        if (i*i == num) return true;    }    return false;}`
`public static boolean isPerfectSquareWithHalfNumber(int num) {    if (num ==  1 || num ==  0) return true;    for (int i = 1; i < num/2; i++ ) {        if (i*i == num) return true;    }    return false;}`

## A FASTER SOLUTION

`public static boolean isPerfectSquareWithBinarySearch(int num) {    double start = 1;    double end = num/2;    if (num == 1) return true;    while (start <= end) {        double mid = Math.floor((end - start)/2) + start;        if (mid * mid ==  num) {            return true;        }else if (mid * mid < num) {            start = mid + 1;        }else {            end = mid - 1;        }    }    return false;}`

--

--

--

## More from Chunks

Love podcasts or audiobooks? Learn on the go with our new app.