-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathAlice Garden.java
More file actions
57 lines (42 loc) · 1.17 KB
/
Alice Garden.java
File metadata and controls
57 lines (42 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*In a garden, there are several apple trees planted in a grid format. Each point (i, j) in the grid has | i | + | j | apples. Alice can buy a square plot centered at (0, 0). Find the minimum perimeter of the plot (1 unit having length = 1) such that she can collect at least X apples. All plants on the perimeter of the plot are also included.
Format:
Input:
X denotes the number of apples Alice has to collect.
Output:
Your function should return the minimum perimeter of the plot, which should be bought by Alice.
Code :
*/
import java.util.*;
class Main {
static int find(int apples){
int sum=0;
int x[]=new int[] {-1,-1,-1,0,0,1,1,1};
int y[]=new int[] {-1,0,1,-1,1,-1,0,1};
for(int i=0;i<8;i++)
{
x[i]=x[i]<0?(x[i]*=-1):x[i];
y[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;
}
}
return factor<<3;
}
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int apples=sc.nextInt();
System.out.println(find(apples));
}
}