Dado un número entero N , la tarea es construir una array de tamaño N 2 usando números enteros positivos y negativos y excluyendo 0 , tal que la suma de la array sea igual a la suma de la diagonal de la array.
Ejemplos:
Entrada: N = 2
Salida:
1 -2
2 4
Explicación:
Suma diagonal = (1 + 4) = 5
Suma matricial = (1 – 2 + 2 + 4) = 5Entrada: N = 5
Salida:
1 2 3 5 10
3 1 4 -9 1
-19 6 1 5 -8
4 -7 2 1 12
-17 1 1 1 1
Explicación:
Suma diagonal = (1 + 1 + 1 + 1 + 1) = 5
Suma de arrays = 5
Enfoque:
El enfoque para resolver el problema es atravesar todos los índices de la array e imprimir un elemento positivo (digamos y ) en las N posiciones diagonales y distribuir equitativamente un entero positivo y negativo de un solo valor (digamos x y -x ) en el restantes N 2 – N posiciones.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to construct matrix with // diagonal sum equal to matrix sum void constructmatrix(int N) { bool check = true; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { // If diagonal position if (i == j) { cout << 1 << " "; } else if (check) { // Positive element cout << 2 << " "; check = false; } else { // Negative element cout << -2 << " "; check = true; } } cout << endl; } } // Driver Code int main() { int N = 5; constructmatrix(5); return 0; }
Java
// Java program to implement // the above approach public class Main { // Function to construct matrix with // diagonal sum equal to matrix sum public static void constructmatrix(int N) { boolean check = true; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { // If diagonal position if (i == j) { System.out.print("1 "); } else if (check) { // Positive element System.out.print("2 "); check = false; } else { // Negative element System.out.print("-2 "); check = true; } } System.out.println(); } } // Driver Code public static void main(String[] args) { int N = 5; constructmatrix(5); } }
Python3
# Python3 program to implement # the above approach # Function to construct matrix with # diagonal sum equal to matrix sum def constructmatrix(N): check = bool(True) for i in range(N): for j in range(N): # If diagonal position if (i == j): print(1, end = " ") elif (check): # Positive element print(2, end = " ") check = bool(False) else: # Negative element print(-2, end = " ") check = bool(True) print() # Driver code N = 5 constructmatrix(5) # This code is contributed by divyeshrabadiya07
C#
// C# program to implement // the above approach using System; class GFG{ // Function to construct matrix with // diagonal sum equal to matrix sum public static void constructmatrix(int N) { bool check = true; for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { // If diagonal position if (i == j) { Console.Write("1 "); } else if (check) { // Positive element Console.Write("2 "); check = false; } else { // Negative element Console.Write("-2 "); check = true; } } Console.WriteLine(); } } // Driver Code static public void Main () { int N = 5; constructmatrix(N); } } // This code is contributed by piyush3010
Javascript
<script> // Javascript program to implement // the above approach // Function to construct matrix with // diagonal sum equal to matrix sum function constructmatrix(N) { let check = true; for (let i = 0; i < N; i++) { for (let j = 0; j < N; j++) { // If diagonal position if (i == j) { document.write("1 "); } else if (check) { // Positive element document.write("2 "); check = false; } else { // Negative element document.write("-2 "); check = true; } } document.write("<br/>"); } } // Driver Code let N = 5; constructmatrix(5); </script>
1 2 -2 2 -2 2 1 -2 2 -2 2 -2 1 2 -2 2 -2 2 1 -2 2 -2 2 -2 1
Complejidad de Tiempo: O(N 2 )
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por divyeshrabadiya07 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA