Advertisement
Spidey2182

Splitting Bill O(sqrt(n)) per case (C++)

Jul 5th, 2023 (edited)
775
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.41 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     int t;
  7.     cin >> t;
  8.     while(t-- > 0) {
  9.         int n;
  10.         cin >> n;
  11.            
  12.         int ans = n - 1;
  13.         for(int i = (int)sqrt(n); i >= 1; i--) {
  14.             if(n % i == 0) {
  15.                 ans = n / i - i;
  16.                 break;
  17.             }
  18.         }
  19.        
  20.         cout << ans << '\n';
  21.     }
  22.    
  23.     return 0;
  24. }
  25.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement