-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCanPlaceFlowers.java
More file actions
21 lines (20 loc) · 845 Bytes
/
CanPlaceFlowers.java
File metadata and controls
21 lines (20 loc) · 845 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class CanPlaceFlowers {
public static void main(String[] args) {
int[] flowerbed1 = {1,0,0,0,1};
int n1 = 1;
int[] flowerbed2 = {1,0,0,0,1};
int n2 = 1;
System.out.println("For test case 1, flowers can be placed in flowerbed is - " + CanPlaceFlowers(flowerbed1, n1));
System.out.println("For test case 1, flowers can be placed in flowerbed is - " + CanPlaceFlowers(flowerbed2, n2));
}
public static boolean CanPlaceFlowers(int[] flowerbed, int n) {
for (int i = 0; i < flowerbed.length; i++) {
if((flowerbed[i] == 0) && (i == 0 || flowerbed[i + 1] == 0) && (i == flowerbed.length - 1 || flowerbed[i - 1] == 0)){
flowerbed[i] = 1;
n --;
i ++;
}
}
return n <= 0;
}
}