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++
#include <bits/stdc++.h>
using namespace std;
void sumAlternate( int * arr, int n)
{
int sum1 = 0, sum2 = 0;
for ( int i = 0; i < n * n; i++) {
if (i % 2 == 0)
sum1 += *(arr + i);
else
sum2 += *(arr + i);
}
cout << "Sum of alternate elements : " << sum1
<< ", " << sum2 << endl;
}
int main()
{
int mat[3][3] = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
int n = 3;
sumAlternate(&mat[0][0], n);
return 0;
}
|
Java
class GFG
{
static void sumAlternate( int [][] mat, int n)
{
int sum1 = 0 , sum2 = 0 ;
int cnt= 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int j= 0 ;j<n ;j++)
{
if (cnt % 2 == 0 )
sum1 += mat[i][j];
else
sum2 += mat[i][j];
cnt++;
}
}
System.out.println( "Sum of alternate elements : " + sum1 + ", " + sum2);
}
public static void main(String [] args)
{
int [][]mat = { { 1 , 2 , 3 },
{ 4 , 5 , 6 },
{ 7 , 8 , 9 } };
int n = 3 ;
sumAlternate(mat, n);
}
}
|
Python 3
def sumAlternate(arr, n):
sum1 = 0
sum2 = 0
i = 0
while i < n * n :
if (i % 2 = = 0 ):
sum1 + = (arr + i)
else :
sum2 + = (arr + i)
i + = 1
print ( "Sum of alternate elements : " +
str (sum1) + ", " + str (sum2))
if __name__ = = "__main__" :
mat = [[ 1 , 2 , 3 ],
[ 4 , 5 , 6 ],
[ 7 , 8 , 9 ]]
n = 3
sumAlternate(mat[ 0 ][ 0 ], n)
|
C#
using System;
class GFG
{
static void sumAlternate( int [,] mat,
int n)
{
int sum1 = 0, sum2 = 0;
int cnt = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
{
if (cnt % 2 == 0)
sum1 += mat[i, j];
else
sum2 += mat[i, j];
cnt++;
}
}
Console.WriteLine( "Sum of alternate elements : " +
sum1 + ", " + sum2);
}
public static void Main()
{
int [,] mat = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
int n = 3;
sumAlternate(mat, n);
}
}
|
Javascript
<script>
function sumAlternate(arr, n)
{
var sum1 = 0, sum2 = 0;
var cnt = 0;
for ( var i = 0; i < n ; i++) {
for ( var j = 0; j < n ; j++) {
if (cnt % 2 == 0)
sum1 += arr[i][j];
else
sum2 += arr[i][j];
cnt++;
}
}
document.write( "Sum of alternate elements : " + sum1
+ ", " + sum2 );
}
var mat = [ [ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ] ];
var n = 3;
sumAlternate(mat, n);
</script>
|
Output
Sum of alternate elements : 25, 20
Time Complexity: O(N2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...