Advertisement
Spidey2182

Splitting Bill O(sqrt(n)) per case (Java)

Jul 5th, 2023 (edited)
633
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.54 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class TestClass {
  4.     public static void main(String args[] ) throws Exception {
  5.         Scanner sc = new Scanner(System.in);
  6.        
  7.         int t = sc.nextInt();
  8.         while(t-- > 0) {
  9.             int n = sc.nextInt();
  10.            
  11.             int ans = n - 1;
  12.             for(int i = (int)Math.sqrt(n); i >= 1; i--) {
  13.                 if(n % i == 0) {
  14.                     ans = n / i - i;
  15.                     break;
  16.                 }
  17.             }
  18.  
  19.             System.out.println(ans);
  20.         }
  21.     }
  22. }
  23.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement