## Amazon Interview Question

SDE1s**Country:**India

Question is not well explained. No offense, but you guys are ruining this website.

Is input same as X ? squared plot centered at (0,0) => How can it be centered at 0,0 since its the corner of the grid !! Do you mean plot starts at (0,0) and it has to be square ?

If thats the case, then Grid can be converted by adding values in square :

0 1 2 0 1 2

3 4 5 => 3 8 5

6 7 8 6 7 36

This grid has 3 squares starting at 0,0. size 1x1, 2x2 and 3x3. is that what you mean ?

The input is X.

Which is the minimum number of apples that should be in the plot.

By i,j they mean points in catrtesian system. At a point i,j number of apples found is |i| +|j|.

The plots (square) would be centered at 0,0 and can be of certain half lengths 1,2,3,4 i.e., with side lengths 2,4,6,etc..

For the plot of minimum size, it would have perimiter 8 units and number of apples would be the sum of absolute value of coordinates both on the perimeter and inside the square.

As the square plot is centred around (0, 0), we can't have a plot of odd dimensions (seeing test cases, I guess that the coordinates are integral). Now when we try to get the sum of points on the boundary for dimension = 2, 4 etc, we see that the sum is 3 * (dimension) * (dimension).

From now on, let me take n as the dimension. We have to find least n such that 3 * sum of squares of even numbers till n is greater than or equal to our input. The sum of squares of first n natural numbers can be calculated using 2n(n+1)(2n+1)/3. Thus we have to find least n such that 2n(n+1)(2n+1) is less than or equal to our input (Note that the outer 3 got cancelled with the denominator). Do a binary search over n and get it!). Answer = 4 * n (we have to return perimeter).

why do we have to take sum of 3*(dimension)*(dimension)?

like for plot dimension=2, sum of apples=8, and for dimension=4, it is comes out to be 3*(4)*(4)=48, so why to take their sum?

if we take their sums, wouldn't it mean that for n=4,total apple the plot can contain=(48+8)=56, while according to me, it must be 48 only?

```
We can use binary search.
1. Double the half square side. When we reach the number of collected apples, it stops.
2. Use the binary search to get the minimal perimeter.
int CalculateApples(int x)
{
int result = 0;
for (int i = -x+1; i <= x-1; i++)
{
result += abs(i) + x;
}
result = result * 4 + (x+x)*4;
return result;
}
int GetMinimalPerimeter(int X)
{
int result;
int cur = 1;
while (true)
{
int num_apples = CalculateApples(cur);
if (num_apples == X)
{
result = 8 * cur;
return result;
}
if (num_apples > X)
{
result = 8 * cur;
break;
}
cur *= 2;
}
if (cur == 1) return result;
int left = cur / 2, right = cur;
while (left <= right)
{
int m = left + (right - left) / 2;
int num_apples = CalculateApples(m);
if (num_apples == X)
{
result = 8 * m;
return result;
}
else if (num_apples > X)
{
result = 8 * m;
right = m - 1;
}
else
{
left = m + 1;
}
}
return result;
}
```

```
#include <iostream>
using namespace std;
int findPeri(int apples) {
int sum=0;
int x[] = {-1,-1,-1,0,0,1,1,1};
int y[] = {-1,0,1,-1,1,-1,0,1};
for (int i=0;i<8;++i) {
x[i]<0?(x[i]*=-1):x[i];
y[i]<0?(y[i]*=-1):y[i];
}
int factor=1;
while(sum<apples) {
for (int i=0;i<8;++i){
sum += x[i]+y[i];
}
if(sum>=apples) break;
++factor;
for (int i=0;i<8;++i) {
x[i]*=factor; y[i]*=factor;
}
}
cout<<endl<<"Sum:"<<sum;
return factor<<3;
}
int main() {
int a; cin>>a;
int p = findPeri(a);
cout<<endl<<"Perimeter:"<<p;
return 0;
}
```

```
//"apple_req ":- no of minimum apple required
//" n" :- Order of matrix i.e. size of Garden
int perameter(int apple_req, int n){
int p = 0;
int total_apple = 0;
if(apple_req >= 4){
for(p = 1; p<=n; p++){
// pow(4,i)*(2*n-p): - will give maximum number of apples in square Garden with side of size p
if(apple_req <= pow(4,i)*(2*n-p))
break;
}
return 4*p;
}
```

#include <iostream>

#include <bits/stdc++.h>

using namespace std;

void cal_apples(int len, int &perimeter, int &num_apples)

{

int x_init= -len;

int y_init = -len;

int x_fin = len;

int y_fin = +len;

for (int i=x_init; i<=x_fin; i++)

{

for (int j = y_init; j<=y_fin; j++)

{

cout << "i : " << i << ", j : " << j << endl;

num_apples += abs(i) + abs(j);

}

}

perimeter = len*2*4;

}

int main()

{

int len = 0;

int perimeter = 0;

int num_apples = 0;

int num_min_apples = 61;

while (!(num_min_apples <= num_apples))

{

len++;

perimeter = 0;

num_apples = 0;

cal_apples(len, perimeter, num_apples);

cout << "len : " << len << endl;

}

cout << "Num of apples : " << num_apples << endl;

cout << "Perimeter : " << perimeter << endl;

return 0;

}

{{

#include <iostream>

#include <bits/stdc++.h>

using namespace std;

void cal_apples(int len, int &perimeter, int &num_apples)

{

int x_init= -len;

int y_init = -len;

int x_fin = len;

int y_fin = +len;

for (int i=x_init; i<=x_fin; i++)

{

for (int j = y_init; j<=y_fin; j++)

{

cout << "i : " << i << ", j : " << j << endl;

num_apples += abs(i) + abs(j);

}

}

perimeter = len*2*4;

}

int main()

{

int len = 0;

int perimeter = 0;

int num_apples = 0;

int num_min_apples = 61;

while (!(num_min_apples <= num_apples))

{

len++;

perimeter = 0;

num_apples = 0;

cal_apples(len, perimeter, num_apples);

cout << "len : " << len << endl;

}

cout << "Num of apples : " << num_apples << endl;

cout << "Perimeter : " << perimeter << endl;

return 0;

}

}}

#include <iostream>

#include <bits/stdc++.h>

using namespace std;

void cal_apples(int len, int &perimeter, int &num_apples)

{

int x_init= -len;

int y_init = -len;

int x_fin = len;

int y_fin = +len;

for (int i=x_init; i<=x_fin; i++)

{

for (int j = y_init; j<=y_fin; j++)

{

cout << "i : " << i << ", j : " << j << endl;

num_apples += abs(i) + abs(j);

}

}

perimeter = len*2*4;

}

int main()

{

int len = 0;

int perimeter = 0;

int num_apples = 0;

int num_min_apples = 61;

while (!(num_min_apples <= num_apples))

{

len++;

perimeter = 0;

num_apples = 0;

cal_apples(len, perimeter, num_apples);

cout << "len : " << len << endl;

}

cout << "Num of apples : " << num_apples << endl;

cout << "Perimeter : " << perimeter << endl;

return 0;

}

private static int sqaurePlotToBuy(int minApples) {

- shashi July 13, 2019double totalApplesInSquare = 0;

int unit =0;

while(minApples>totalApplesInSquare){

unit++;

totalApplesInSquare +=12*Math.pow(unit,2);

}

return unit*8;

}