Advertisement
STANAANDREY

check perfect sq

Jul 11th, 2022
886
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.45 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. bool checkPerfSq(int x) {
  5.     int st = 1, dr = x;
  6.     while (st <= dr) {
  7.         int mid = (st + dr) / 2;
  8.         if (mid * mid == x) {
  9.             return true;
  10.         }
  11.         if (mid * mid < x) {
  12.             st = mid + 1;
  13.         } else {
  14.             dr = mid - 1;
  15.         }
  16.     }
  17.     return false;
  18. }
  19.  
  20. int main() {
  21.     int x;
  22.     cin >> x;
  23.     cout << checkPerfSq(x) << endl;
  24.     return 0;
  25. }
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement