Skip to content

Instantly share code, notes, and snippets.

@jnhemant
Created August 31, 2020 10:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnhemant/f1919f9a5cade916add28b81a601043c to your computer and use it in GitHub Desktop.
Save jnhemant/f1919f9a5cade916add28b81a601043c to your computer and use it in GitHub Desktop.
Faulty Street Lights Coding Challenge
import java.util.*;
class Main {
public static void main(String[] args) {
int n = 8, m = 5;
int[] arr = { 0, 1, 0, 1, 0, 1, 0, 1 };
if(m == 0){
for(int i = 0; i < n; i++){
System.out.print(arr[i] + " ");
}
return;
}
for (int j = 0; j < m; j++) {
int[] res = new int[n];
if (arr[1] == 1) {
res[0] = 1;
} else {
res[0] = 0;
}
for (int i = 1; i < n - 1; i++) {
if (arr[i - 1] == arr[i + 1]) {
res[i] = 0;
} else {
res[i] = 1;
}
}
if (arr[n - 2] == 0) {
res[n - 1] = 0;
} else {
res[n - 1] = 1;
}
if (j == m - 1) {
for (int k = 0; k < n; k++) {
System.out.print(res[k] + " ");
}
}
arr = res;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment