Sum of alternate elements of a N x N matrix
Last Updated :
12 Oct, 2022
Given an NxN matrix. The task is to find the sum of the alternate elements of the given matrix.
For example, in a 2 x 2 matrix the alternate elements are { A[0][0], A[1, 1] } and { A[1][0], A[0][1] }.
Examples:
Input: mat[][] = { { 1, 2},
{ 3, 4} }
Output : Sum of alternate elements : 5, 5
Explanation: The alternate elements are {1, 4}
and {2, 3} so their sum are 5, 5.
Input : mat[][] = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } }
Output : Sum of alternate elements :25, 20
The idea is to traverse the matrix and also keep a counter. We will add the elements whose counter value is even in one variable and with odd counter value in other and finally print the two sums on complete traversal of the matrix.
Below is the implementation of the above approach:
C++
// C++ program to print sum of alternate
// elements of a N x N matrix
#include <bits/stdc++.h>
using namespace std;
// Function to find the sum of alternate
// elements of a matrix
void sumAlternate(int* arr, int n)
{
int sum1 = 0, sum2 = 0;
// check the alternate elements
for (int i = 0; i < n * n; i++) {
// count the elements at even places
if (i % 2 == 0)
sum1 += *(arr + i);
else // count the elements at odd places
sum2 += *(arr + i);
}
cout << "Sum of alternate elements : " << sum1
<< ", " << sum2 << endl;
}
// Driver code
int main()
{
int mat[3][3] = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
int n = 3;
// find the sum of alternate elements
sumAlternate(&mat[0][0], n);
return 0;
}
Java
// Java program to print sum of alternate
// elements of a N x N matrix
class GFG
{
// Function to find the sum of alternate
// elements of a matrix
static void sumAlternate(int [][] mat, int n)
{
int sum1 = 0, sum2 = 0;
int cnt=0;
// check the alternate elements
for (int i = 0; i < n; i++) {
for(int j=0;j<n ;j++)
{
if (cnt % 2 == 0)
sum1 += mat[i][j];
else // count the elements at odd places
sum2 += mat[i][j];
cnt++;
}
}
System.out.println("Sum of alternate elements : " + sum1 + ", " + sum2);
}
// Driver code
public static void main(String [] args)
{
int [][]mat = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
int n = 3;
// find the sum of alternate elements
sumAlternate(mat, n);
}
}
// This code is contributed by ihritik
Python 3
# Python 3 program to print sum of
# alternate elements of a N x N matrix
# Function to find the sum of
# alternate elements of a matrix
def sumAlternate(arr, n):
sum1 = 0
sum2 = 0
# check the alternate elements
i = 0
while i < n * n :
# count the elements at
# even places
if (i % 2 == 0):
sum1 += (arr + i)
else: # count the elements
# at odd places
sum2 += (arr + i)
i += 1
print("Sum of alternate elements : " +
str(sum1) + ", " + str(sum2))
# Driver code
if __name__ == "__main__":
mat = [[ 1, 2, 3 ],
[4, 5, 6 ],
[7, 8, 9 ]]
n = 3
# find the sum of alternate elements
sumAlternate(mat[0][0], n)
# This code is contributed
# by ChitraNayal
C#
// C# program to print sum of alternate
// elements of a N x N matrix
using System;
class GFG
{
// Function to find the sum of
// alternate elements of a matrix
static void sumAlternate(int[,] mat,
int n)
{
int sum1 = 0, sum2 = 0;
int cnt = 0;
// check the alternate elements
for (int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if (cnt % 2 == 0)
sum1 += mat[i, j];
else // count the elements
// at odd places
sum2 += mat[i, j];
cnt++;
}
}
Console.WriteLine("Sum of alternate elements : " +
sum1 + ", " + sum2);
}
// Driver code
public static void Main()
{
int[,] mat = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
int n = 3;
// find the sum of alternate elements
sumAlternate(mat, n);
}
}
// This code is contributed
// by Akanksha Rai(Abby_akku)
JavaScript
<script>
// Javascript program to print sum of alternate
// elements of a N x N matrix
// Function to find the sum of alternate
// elements of a matrix
function sumAlternate(arr, n)
{
var sum1 = 0, sum2 = 0;
var cnt = 0;
// check the alternate elements
for (var i = 0; i < n ; i++) {
for (var j = 0; j < n ; j++) {
// count the elements at even places
if (cnt % 2 == 0)
sum1 += arr[i][j];
else // count the elements at odd places
sum2 += arr[i][j];
cnt++;
}
}
document.write( "Sum of alternate elements : " + sum1
+ ", " + sum2 );
}
// Driver code
var mat = [ [ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ] ];
var n = 3;
// find the sum of alternate elements
sumAlternate(mat, n);
</script>
OutputSum of alternate elements : 25, 20
Time Complexity: O(N2)
Auxiliary Space: O(1)
Similar Reads
Find sum of all Matrix elements Given a matrix mat[][], the task is to find the sum of all the elements of the matrix. Examples: Input: mat[][] = {{1, 2, 3}, {4, 5, 6}}Output: 21Explanation: Here sum of all element = 1 + 2 + 3 + 4 + 5 + 6 = 21 Input: mat[][] = {{4, 5, 3, 2}, {9, 5, 6, 2}, {1, 5, 3, 5}}Output: 50Explanation: Here s
5 min read
Sum of all odd frequency elements in a Matrix Given a NxM matrix of integers containing duplicate elements. The task is to find the sum of all odd occurring elements in the given matrix. That is the sum of all such elements whose frequency is odd in the matrix. Examples: Input : mat[] = {{1, 1, 2}, {2, 3, 3}, {4, 5, 3}} Output : 18 The odd occu
5 min read
Sum of Matrix where each element is sum of row and column number Given two numbers M and N denoting the number of rows and columns of a matrix A[] where A[i][j] is the sum of i and j (indices follow 1 based indexing), the task is to find the sum of elements of the matrix. Examples: Input: M = 3, N = 3Output: 36Explanation: A[]: {{2, 3, 4}, {3, 4, 5}, {4, 5, 6}}.
14 min read
Maximum sum of elements from each row in the matrix Given a matrix, find the maximum sum we can have by selecting just one element from every row. Condition is element selected from nth row must be strictly greater than element from (n-1)th row, else no element must be taken from row. Print the sum if possible else print -1. Examples : Input : 1 2 31
7 min read
Efficiently compute sums of diagonals of a matrix Given a 2D square matrix, find the sum of elements in Principal and Secondary diagonals. For example, consider the following 4 X 4 input matrix. A00 A01 A02 A03 A10 A11 A12 A13 A20 A21 A22 A23 A30 A31 A32 A33 The primary diagonal is formed by the elements A00, A11, A22, A33. Condition for Principal
10 min read