-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPotFlowers.java
More file actions
64 lines (56 loc) · 2.15 KB
/
Copy pathPotFlowers.java
File metadata and controls
64 lines (56 loc) · 2.15 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
58
59
60
61
62
63
64
public class PotFlowers {
public static boolean canPlaceFlowers(int[] flowerbed, int flowerPlot) {
if (flowerPlot>flowerbed.length){
return false;
}
if(flowerbed.length == 1 && flowerPlot ==1 && flowerbed[0] == 0){
return true;
}
if(flowerbed.length == 1 && flowerPlot ==1 && flowerbed[0] == 1){
return false;
}
else {
int nextElement =0;
int previousElement = 0;
for (int j=0; j<flowerbed.length && flowerPlot >=1;j++){
if (j==0 && flowerbed[j] == 0){
nextElement = j+1;
if (flowerbed[nextElement] == 0){
System.out.println("Place Flowers");
flowerbed[j]=1;
flowerPlot--;
}
} else if (j==flowerbed.length -1 && flowerbed[j] == 0) {
previousElement = j-1;
if (flowerbed[previousElement] == 0){
System.out.println("Place Flower");
flowerbed[j]=1;
flowerPlot--;
}
}
else {
if ((j !=0 && j != flowerbed.length -1) && flowerbed[j]==0){
nextElement = j +1;
previousElement = j-1;
if (flowerbed[nextElement]==0 && flowerbed[previousElement]==0){
System.out.println("Place Flower");
flowerbed[j]=1;
flowerPlot--;
}
}
}
}
}
if (flowerPlot == 0){
return true;
}
else {
return false;
}
}
public static void main(String[] args){
int[] flowers = {0,0,1,0,0};
int plotFlower = 1;
System.out.println(canPlaceFlowers(flowers, plotFlower));
}
}