Dada una array bidimensional arr[][] , la tarea es verificar si es posible hacer que todos los elementos de la array sean iguales a un número k dado si, en una operación, se puede elegir cualquier elemento y la diagonal circundante los elementos pueden hacerse iguales a él.
Ejemplos:
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
Acercarse:
- La array se puede considerar como un tablero de ajedrez con casillas en blanco y negro.
- Si se elige cualquier elemento en la caja negra que sea igual al número dado, entonces todos los elementos de las cajas negras pueden hacerse iguales usando la operación dada,
- Del mismo modo, se puede comprobar por las casillas blancas. Por lo tanto, debe haber al menos un elemento igual al elemento dado en los cuadros blanco y negro.
- Entonces necesitamos iterar sobre todos los elementos usando un contador. Si el valor de la ficha es impar, se puede considerar una caja negra y para valores pares, se puede considerar una caja blanca.
A continuación se muestra la implementación del enfoque anterior.
C++
// C++ implementation of the above approach. #include <iostream> using namespace std; // Function to check if all // elements can be equal or not void checkEqualMatrix(int arr[][3], int n, int m, int k) { int c = 0, cnt1 = 0, cnt2 = 0; // Iterate over the matrix for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (c % 2 == 0) { // Update the counter for odd values // if array element at that position is k if (arr[i][j] == k) { cnt1++; } } else { // Update the counter for even values // if array element at that position is k if (arr[i][j] == k) { cnt2++; } } c = c + 1; } } // To check if there is at least one // element at both even and odd indices. if (cnt1 >= 1 && cnt2 >= 1) { cout << "Yes"; } else { cout << "No"; } } // Driver code int main() { int arr[3][3] = { { 1, 8, 3 }, { 1, 2, 2 }, { 4, 1, 9 } }; int k = 2; // Function calling checkEqualMatrix(arr, 3, 3, k); }
Java
// Java implementation of the above approach. class GFG { // Function to check if all // elements can be equal or not static void checkEqualMatrix(int arr[][], int n, int m, int k) { int c = 0, cnt1 = 0, cnt2 = 0; // Iterate over the matrix for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (c % 2 == 0) { // Update the counter for odd values // if array element at that position is k if (arr[i][j] == k) { cnt1++; } } else { // Update the counter for even values // if array element at that position is k if (arr[i][j] == k) { cnt2++; } } c = c + 1; } } // To check if there is at least one // element at both even and odd indices. if (cnt1 >= 1 && cnt2 >= 1) { System.out.println("Yes"); } else { System.out.println("No"); } } // Driver code public static void main (String[] args) { int arr[][] = { { 1, 8, 3 }, { 1, 2, 2 }, { 4, 1, 9 } }; int k = 2; // Function calling checkEqualMatrix(arr, 3, 3, k); } } // This code is contributed by AnkitRai01
Python3
# Python3 implementation of the above approach. # Function to check if all # elements can be equal or not def checkEqualMatrix(arr, n, m, k) : c = 0; cnt1 = 0; cnt2 = 0; # Iterate over the matrix for i in range(n) : for j in range(m) : if (c % 2 == 0) : # Update the counter for odd values # if array element at that position is k if (arr[i][j] == k) : cnt1 += 1; else : # Update the counter for even values # if array element at that position is k if (arr[i][j] == k) : cnt2 += 1; c = c + 1; # To check if there is at least one # element at both even and odd indices. if (cnt1 >= 1 and cnt2 >= 1) : print("Yes"); else : print("No"); # Driver code if __name__ == "__main__" : arr = [ [ 1, 8, 3 ], [ 1, 2, 2 ], [ 4, 1, 9 ] ]; k = 2; # Function calling checkEqualMatrix(arr, 3, 3, k); # This code is contributed by AnkitRai01
C#
// C# implementation of the above approach. using System; class GFG { // Function to check if all // elements can be equal or not static void checkEqualMatrix(int [,]arr, int n, int m, int k) { int c = 0, cnt1 = 0, cnt2 = 0; // Iterate over the matrix for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (c % 2 == 0) { // Update the counter for odd values // if array element at that position is k if (arr[i,j] == k) { cnt1++; } } else { // Update the counter for even values // if array element at that position is k if (arr[i,j] == k) { cnt2++; } } c = c + 1; } } // To check if there is at least one // element at both even and odd indices. if (cnt1 >= 1 && cnt2 >= 1) { Console.WriteLine("Yes"); } else { Console.WriteLine("No"); } } // Driver code public static void Main() { int [,]arr = { { 1, 8, 3 }, { 1, 2, 2 }, { 4, 1, 9 } }; int k = 2; // Function calling checkEqualMatrix(arr, 3, 3, k); } } // This code is contributed by AnkitRai01
Javascript
<script> // Javascript implementation of the above approach. // Function to check if all // elements can be equal or not function checkEqualMatrix(arr, n, m, k) { var c = 0, cnt1 = 0, cnt2 = 0; // Iterate over the matrix for (var i = 0; i < n; i++) { for (var j = 0; j < m; j++) { if (c % 2 == 0) { // Update the counter for odd values // if array element at that position is k if (arr[i][j] == k) { cnt1++; } } else { // Update the counter for even values // if array element at that position is k if (arr[i][j] == k) { cnt2++; } } c = c + 1; } } // To check if there is at least one // element at both even and odd indices. if (cnt1 >= 1 && cnt2 >= 1) { document.write( "Yes"); } else { document.write( "No"); } } // Driver code var arr = [ [ 1, 8, 3 ], [ 1, 2, 2 ], [ 4, 1, 9 ] ]; var k = 2; // Function calling checkEqualMatrix(arr, 3, 3, k); // This code is contributed by importantly. </script>
Producción:
Yes
Publicación traducida automáticamente
Artículo escrito por AmanGupta65 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA