Dada una array 2D de orden M * N, la tarea es encontrar la suma de los elementos de la array.
Ejemplos:
Entrada: array[2][2] = {{1, 2}, {3, 4}};
Salida: 10Entrada: array[4][4] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16} };
Salida: 136
Acercarse:
La suma de cada elemento de la array 2D se puede calcular recorriendo la array y sumando los elementos.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to find sum of // elements in 2D array #include <iostream> using namespace std; // Get the size m and n #define M 4 #define N 4 // Function to calculate sum // of elements in 2d array int sum(int arr[M][N]) { int i, j; int sum = 0; // Finding the sum for (i = 0; i < M; ++i) { for (j = 0; j < N; ++j) { // Add the element sum = sum + arr[i][j]; } } return sum; } // Driver code int main() { int i, j; int arr[M][N]; // Get the matrix elements int x = 1; for (i = 0; i < M; i++) for (j = 0; j < N; j++) arr[i][j] = x++; // Get sum cout << sum(arr); return 0; }
Java
// Java code for the above approach import java.io.*; class GFG { // Get the size m and n static int M = 4; static int N = 4; // Function to calculate sum // of elements in 2d array static int sum(int arr[][]) { int i, j; int sum = 0; // Finding the sum for (i = 0; i < M; ++i) { for (j = 0; j < N; ++j) { // Add the element sum = sum + arr[i][j]; } } return sum; } public static void main (String[] args) { int i, j; int arr[][]= new int[M][N]; // Get the matrix elements int x = 1; for (i = 0; i < M; i++) for (j = 0; j < N; j++) arr[i][j] = x++; // Get sum System.out.println(sum(arr)); } } // This code is contributed by Potta Lokesh
Python3
# phython program to find sum of # elements in 2D array # Get the size m and n M = 4 N = 4 # Function to calculate sum # of elements in 2d array def sum(arr): sum = 0 # Finding the sum for i in range(M): for j in range(N): # Add the element sum = sum + arr[i][j] return sum # Driver code arr = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] # Get the matrix elements x = 1 for i in range(M): for j in range(N): arr[i][j] = x x += 1 # Get sum print(sum(arr)) # This code is contributed by ninja_hattori
C#
using System; class GFG { // Get the size m and n static int M = 4; static int N = 4; // Function to calculate sum // of elements in 2d array static int sum(int [,]arr) { int i, j; int sum = 0; // Finding the sum for (i = 0; i < M; ++i) { for (j = 0; j < N; ++j) { // Add the element sum = sum + arr[i,j]; } } return sum; } static public void Main (String[] args){ int i, j; int [,]arr= new int[M,N]; // Get the matrix elements int x = 1; for (i = 0; i < M; i++) for (j = 0; j < N; j++) arr[i,j] = x++; // Get sum Console.WriteLine(sum(arr)); // Code } } // This code is contributed by Kritima Gupta
Javascript
<script> // JavaScript program to find sum of // elements in 2D array // Get the size m and n const M = 4; const N = 4; // Function to calculate sum // of elements in 2d array const sum = (arr) => { let i, j; let sum = 0; // Finding the sum for (i = 0; i < M; ++i) { for (j = 0; j < N; ++j) { // Add the element sum = sum + arr[i][j]; } } return sum; } // Driver code let i, j; let arr = new Array(M).fill(0).map(() => new Array(N).fill(0)); // Get the matrix elements let x = 1; for (i = 0; i < M; i++) for (j = 0; j < N; j++) arr[i][j] = x++; // Get sum document.write(sum(arr)); // This code is contributed by rakeshsahni. </script>
136
Complejidad temporal: O(M*N)
Espacio auxiliar: O(1)
Otro método: usando STL. Llamar a la función incorporada para la suma de elementos de una array en STL. Usamos la función de acumulación (primero, último, suma) para devolver la suma de la array 1D.
A continuación se muestra la implementación de la idea.
C++
// C++ program to find sum of // elements in 2D array #include <iostream> #include <numeric> using namespace std; // Get the size m and n #define M 4 #define N 4 // Function to calculate sum // of elements in 2d array int sum(int arr[M][N]) { int i, j; int sum = 0; // Finding the sum for (i = 0; i < M; ++i) { int ans = 0; sum += accumulate(arr[i], arr[i] + N, ans); } return sum; } // Driver code int main() { int i, j; int arr[M][N]; // Get the matrix elements int x = 1; for (i = 0; i < M; i++) for (j = 0; j < N; j++) arr[i][j] = x++; // Get sum cout << sum(arr); return 0; }
Python
# phython program to find sum of # elements in 2D array # Get the size m and n M = 4 N = 4 # Function to calculate sum # of elements in 2d array def Findsum(arr): ans = 0 # Finding the sum for i in range(M): ans += int(sum(arr[i])) return ans # Driver code arr = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] # Get the matrix elements x = 1 for i in range(M): for j in range(N): arr[i][j] = x x += 1 # Get sum print(Findsum(arr)) # This code is contributed by Sam_snehil
Javascript
// JavaScript program to find sum of // elements in 2D array // Get the size m and n const M = 4; const N = 4; // Function to calculate sum // of elements in 2d array const sum = (arr) => { let i, j; let sum = 0; // Finding the sum for (i = 0; i < M; ++i) { sum = sum + arr[i].reduce(function(accumulator, currentValue){ return accumulator + currentValue;}, 0); } return sum; } // Driver code let i, j; let arr = new Array(M).fill(0).map(() => new Array(N).fill(0)); // Get the matrix elements let x = 1; for (i = 0; i < M; i++) for (j = 0; j < N; j++) arr[i][j] = x++; // Get sum document.write(sum(arr)); // This code is contributed by rakeshsahni.
136
Complejidad temporal: O(n*m) (donde n = nº de filas y m = nº de columna)
Espacio Auxiliar: O(1)