方阵两对角线上的行公共元素

给定一个方阵,找出同一行中相同的数,以及主对角线和次对角线中相同的数。 例如:

null
Input : 1 2 1        4 5 2        0 5 1Output : 2Primary diagonal is 1 5 1Secondary diagonal is 1 5 0Two elements (1 and 5) match in two diagonals and same.Input : 1 0 0        0 1 0        0 0 1Output : 1Primary diagonal is 1 1 1Secondary diagonal is 0 1 0Only one element is same.

我们可以在O(n)时间、O(1)空间和一次遍历中实现这一点。我们可以在主对角线的第i行找到当前元素mat[i][i],在次对角线的第i行找到元素mat[i][n-i-1]。

C++

// CPP program to find common elements in
// two diagonals.
#include <iostream>
#define MAX 100
using namespace std;
// Returns count of row wise same
// elements in two diagonals of
// mat[n][n]
int countCommon( int mat[][MAX], int n)
{
int res = 0;
for ( int i=0;i<n;i++)
if (mat[i][i] == mat[i][n-i-1])
res++;
return res;
}
// Driver Code
int main()
{
int mat[][MAX] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
cout << countCommon(mat, 3);
return 0;
}


JAVA

// Java program to find common
// elements in two diagonals.
import java.io.*;
class GFG
{
int MAX = 100 ;
// Returns count of row wise same elements
// in two diagonals of mat[n][n]
static int countCommon( int mat[][], int n)
{
int res = 0 ;
for ( int i = 0 ; i < n; i++)
if (mat[i][i] == mat[i][n - i - 1 ])
res++;
return res;
}
// Driver Code
public static void main(String args[]) throws IOException
{
int mat[][] = {{ 1 , 2 , 3 },
{ 4 , 5 , 6 },
{ 7 , 8 , 9 }};
System.out.println(countCommon(mat, 3 ));
}
}
// This code is contributed by Anshika Goyal.


Python3

# Python3 program to find common
# elements in two diagonals.
Max = 100
# Returns count of row wise same
# elements in two diagonals of
# mat[n][n]
def countCommon(mat, n):
res = 0
for i in range (n):
if mat[i][i] = = mat[i][n - i - 1 ] :
res = res + 1
return res
# Driver Code
mat = [[ 1 , 2 , 3 ],
[ 4 , 5 , 6 ],
[ 7 , 8 , 9 ]]
print (countCommon(mat, 3 ))
# This code is contributed by Anant Agarwal.


C#

// C# program to find common
// elements in two diagonals.
using System;
class GFG {
// Returns count of row wise same
// elements in two diagonals of
// mat[n][n]
static int countCommon( int [,]mat, int n)
{
int res = 0;
for ( int i = 0; i < n; i++)
if (mat[i,i] == mat[i,n - i - 1])
res++;
return res;
}
// Driver Code
public static void Main()
{
int [,]mat = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
Console.WriteLine(countCommon(mat, 3));
}
}
// This code is contributed by vt_m.


PHP

<?php
// PHP program to find common
// elements in two diagonals.
$MAX = 100;
// Returns count of row wise
// same elements in two
// diagonals of mat[n][n]
function countCommon( $mat , $n )
{
global $MAX ;
$res = 0;
for ( $i = 0; $i < $n ; $i ++)
if ( $mat [ $i ][ $i ] == $mat [ $i ][ $n - $i - 1])
$res ++;
return $res ;
}
// Driver Code
$mat = array ( array (1, 2, 3),
array (4, 5, 6),
array (7, 8, 9));
echo countCommon( $mat , 3);
// This code is contributed by aj_36
?>


Javascript

<script>
// Javascript program to find common elements in
// two diagonals.
let MAX = 100;
// Returns count of row wise same
// elements in two diagonals of
// mat[n][n]
function countCommon(mat, n)
{
let res = 0;
for (let i = 0; i < n; i++)
if (mat[i][i] == mat[i][n - i - 1])
res++;
return res;
}
// Driver Code
let mat = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]];
document.write(countCommon(mat, 3));
// This code is contributed by subham348.
</script>


输出:

  1  

本文由 希曼舒兰扬 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享