Balanced Neighborhood (Contest)
Balanced Neighborhood (Contest) easy Time Limit: 2 sec Memory Limit: 128000 kB
Last updated
Balanced Neighborhood (Contest) easy Time Limit: 2 sec Memory Limit: 128000 kB
Last updated
Problem Statement :
Given a 2-D array of binary integers of size NXM. The cell at ith row and jth column is denoted by (i, j).
The array is called Balanced if every cell of the array having 4 elements has a balanced neighborhood. A neighborhood is balanced if 4 neighbors can be divided into 2 groups with equal size and equal sum.
A cell at (i, j) has 4 neighbors (i-1, j), (i+1, j), (i, j-1), (i, j+1). Input The first line contains N and M. Next N lines contain M integers each.
Constraints 1 ≤ N, M ≤ 1000 0 ≤ arr[i][j] ≤ 1 Output Print "YES" if the given array is balanced, otherwise print "NO". Example Input: 3 4 0 1 0 0 1 1 0 1 0 0 1 1
Output: NO
Explanation: neighbors of (1, 1) => {1, 1, 0, 0} => 1+0 = 0+1 => balanced neighborhood neighbors of (1, 2) => {0, 1, 1, 1} => no way to divide into two groups with equal sum and equal size => unbalanced
No other cell has 4 neighbors.
link: