Given a 2-d array arr, the task is to check whether it is possible to make all elements of the array to equal to a given number k if, in one operation, any element can be chosen and the surrounding diagonal elements can be made equal to it.
Input: arr = 1 8 3 1 2 2 4 1 9 k = 2 Output: Yes Explanation: In first operation choose element at (2, 2) New array = 2 8 2 1 2 2 2 1 2 In second operation choose element at (2, 3) New array = 2 2 2 1 2 2 2 2 2 In third operation choose element at (1, 2) New array = 2 2 2 2 2 2 2 2 2 Input: arr = 3 1 2 3 2 1 8 6 9 7 9 9 k = 4 Output: No
- The matrix can be considered as a chessboard with black and white boxes.
- If any element in the black box is chosen which is equal to the given number, then all elements of black boxes can be made equal to it using the given operation,
- Similarly, it can be checked for the white boxes. So there need to be at least one element equal to the given element in both black and white boxes.
- So we need to iterate over all elements using a counter. If the value of the counter is odd, it can be considered a black box and for even values, it can be considered a white box.
Below is the implementation of the above approach.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find maximum array sum after making all elements same with repeated subtraction
- Minimum operations of given type to make all elements of a matrix equal
- Making elements distinct in a sorted array by minimum increments
- Making elements of two arrays same with minimum increment/decrement
- Arrange array elements such that last digit of an element is equal to first digit of the next element
- Finding the maximum square sub-matrix with all equal elements
- Largest sub-matrix with all equal elements
- Ways of filling matrix such that product of all rows and all columns are equal to unity
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Reach the numbers by making jumps of two given lengths
- Check if the array has an element which is equal to sum of all the remaining elements
- Element equal to the sum of all the remaining elements
- Count of operations to make all elements of array a equal to its min element by performing a[i] – b[i]
- Minimum elements to change so that for an index i all elements on the left are -ve and all elements on the right are +ve
- Position of Elements which are equal to sum of all Preceding elements
- Index of the elements which are equal to the sum of all succeeding elements
- Find a common element in all rows of a given row-wise sorted matrix
- Smallest element from all square submatrices of size K from a given Matrix
- Making zero array by decrementing pairs of adjacent
- Check if a given matrix can be converted to another given matrix by row and column exchanges
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : AnkitRai01