Created
August 31, 2020 10:01
-
-
Save jnhemant/f1919f9a5cade916add28b81a601043c to your computer and use it in GitHub Desktop.
Faulty Street Lights Coding Challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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