Sum of GCDs of each row of the given matrix
Last Updated :
01 Aug, 2022
Given a matrix mat[][] of size N * N, the task is to find the sum of GCDs of all the rows of the given matrix.
Examples:
Input: arr[][] = {
{2, 4, 6, 8},
{3, 6, 9, 12},
{4, 8, 12, 16},
{5, 10, 15, 20}};
Output: 14
gcd(2, 4, 6, 8) = 2
gcd(3, 6, 9, 12) = 3
gcd(4, 8, 12, 16) = 4
gcd(5, 10, 15, 20) = 5
2 + 3 + 4 + 5 = 14
Input: arr[][] = {
{1, 1, 1, 1},
{2, 2, 2, 2},
{3, 3, 3, 3},
{4, 4, 4, 4}};
Output: 10
Approach: Find the GCD of each row of the matrix and add it to the total sum. The total sum will be the sum of GCD of all the rows.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
int sumGcd( int arr[][4], int n)
{
int sum = 0;
for ( int i = 0; i < n; i++) {
int gcdRow = arr[i][0];
for ( int j = 1; j < n; j++)
gcdRow = gcd(arr[i][j], gcdRow);
sum += gcdRow;
}
return sum;
}
int main()
{
int arr[][4] = { { 2, 4, 6, 8 },
{ 3, 6, 9, 12 },
{ 4, 8, 12, 16 },
{ 5, 10, 15, 20 } };
int size = sizeof (arr) / sizeof (arr[0]);
cout << sumGcd(arr, size);
return 0;
}
|
Java
class GFG
{
static int gcd( int a, int b)
{
if (a == 0 )
return b;
return gcd(b % a, a);
}
static int sumGcd( int arr[][], int n)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
{
int gcdRow = arr[i][ 0 ];
for ( int j = 1 ; j < n; j++)
gcdRow = gcd(arr[i][j], gcdRow);
sum += gcdRow;
}
return sum;
}
public static void main (String[] args)
{
int arr[][] = { { 2 , 4 , 6 , 8 },
{ 3 , 6 , 9 , 12 },
{ 4 , 8 , 12 , 16 },
{ 5 , 10 , 15 , 20 } };
int size = arr.length ;
System.out.println(sumGcd(arr, size));
}
}
|
Python3
def gcd(a, b):
if (a = = 0 ):
return b
return gcd(b % a, a)
def SumGcd(arr, n):
Sum = 0
for i in range (n):
gcdRow = arr[i][ 0 ]
for j in range ( 1 ,n):
gcdRow = gcd(arr[i][j], gcdRow)
Sum + = gcdRow
return Sum
arr = [ [ 2 , 4 , 6 , 8 ],
[ 3 , 6 , 9 , 12 ],
[ 4 , 8 , 12 , 16 ],
[ 5 , 10 , 15 , 20 ] ]
size = len (arr)
print (SumGcd(arr, size))
|
C#
using System;
class GFG
{
static int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int sumGcd( int [ , ]arr, int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
{
int gcdRow = arr[i, 0];
for ( int j = 1; j < n; j++)
gcdRow = gcd(arr[i, j], gcdRow);
sum += gcdRow;
}
return sum;
}
public static void Main ()
{
int [ , ] arr = new int [ 4, 4 ]{{ 2, 4, 6, 8 },
{ 3, 6, 9, 12 },
{ 4, 8, 12, 16 },
{ 5, 10, 15, 20 }};
int size = 4;
Console.WriteLine(sumGcd(arr, size));
}
}
|
PHP
<?php
function gcd( $a , $b )
{
if ( $a == 0)
return $b ;
return gcd( $b % $a , $a );
}
function sumGcd(& $arr , $n )
{
$sum = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$gcdRow = $arr [ $i ][0];
for ( $j = 1; $j < $n ; $j ++)
$gcdRow = gcd( $arr [ $i ][ $j ], $gcdRow );
$sum += $gcdRow ;
}
return $sum ;
}
$arr = array ( array ( 2, 4, 6, 8 ),
array ( 3, 6, 9, 12 ),
array (4, 8, 12, 16 ),
array ( 5, 10, 15, 20));
$size = sizeof( $arr );
echo sumGcd( $arr , $size );
return 0;
?>
|
Javascript
<script>
function gcd(a,b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
function sumGcd(arr,n)
{
let sum = 0;
for (let i = 0; i < n; i++)
{
let gcdRow = arr[i][0];
for (let j = 1; j < n; j++)
gcdRow = gcd(arr[i][j], gcdRow);
sum += gcdRow;
}
return sum;
}
let arr= [ [ 2, 4, 6, 8 ],
[ 3, 6, 9, 12 ],
[ 4, 8, 12, 16 ],
[ 5, 10, 15, 20 ] ];
let size = arr.length ;
document.write(sumGcd(arr, size));
</script>
|
Time Complexity: O(n2 log(min(a,b))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...