# Arranging Cupcakes Solution

Problem
On the day of the party, the Chef was over-seeing all the food arrangements as well, ensuring that every item was in its designated position. The host was satisfied with everything except the cupcakes. He noticed they were arranged neatly in the shape of a rectangle. He asks the Chef to make it as square-like as possible.

The Chef is in no mood to waste his cupcakes by transforming it into a perfect square arrangement. Instead, to fool the host, he asks you to arrange the N cupcakes as a rectangle so that the difference between the length and the width is minimized.

#### Input

The first line of the input file contains an integer T, the number of test cases. Each of the following T lines contains a single integer N denoting the number of cupcakes.

#### Output

Output T lines, each indicating the minimum possible difference between the length and the width in a rectangular arrangement of the cupcakes.

• T≤100
• N≤10^8

#### Sample Input

4
20
13
8
4

#### Sample Output

1
12
2
0
##### Explanation

Case 1: 20 cupcakes can be arranged in 6 possible ways – 1 x 20, 2 x 10, 4 x 5, 5 x 4, 10 x 2 and 20 x 1. The corresponding differences between the length and the width are 19, 8, 1, 1, 8 and 19 respectively. Hence, 1 is the answer.

#### Solution

//
//  main.cpp
//  Arranging Cupcakes
//
//  Created by Himanshu on 17/02/22.
//

#include <iostream>
#include <cmath>
using namespace std;

int main() {
int T, n, ans = 0;
cin>>T;

while (T--) {
cin>>n;

int nSqRoot = (int)sqrt(n);

for(int i=nSqRoot; i>0; i--) {
// Using the factors closest to the square root
// of n, to minimize the difference
if (n%i == 0) {
ans = (n/i)-i;
break;
}
}

cout<<ans<<endl;
}

return 0;
}


Time Complexity: O(sqrt(N)) (N is the input number)