给定N * N维的2D矩阵arr [] [] ,任务是找到矩阵的所有四个部分的元素之和,除以对角线,而不在这四个部分中的任何一个中包含对角线元素。
例子:
Input: arr[][] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }
Output: 2 4 6 8
Explanation:
(1, 5, 9) and (3, 5, 7) are diagonals of arr[][] matrix. Therefore sum of parts are:
Top = 2
Left = 4
Right = 6
Bottom = 8
Input: arr[][] = { {1, 3, 1, 5}, {2, 2, 4, 1}, {5, 0, 2, 3}, { 1, 3, 3, 5} }
Output: 4 7 4 6
Explanation:
(1, 2, 2, 5) and (5, 4, 0, 1) are diagonals of arr[][] matrix. Therefore sum of parts are:
Top = 3 + 1 = 4
Left = 2 + 5 = 7
Right = 1 + 3 = 4
Bottom = 3 + 3 = 6
方法:
如上图所示,将大小为NxN的矩阵除以对角线后。我们观察到以下属性:
- 如果行和列的索引总和小于N – 1 ,则它属于顶部或左侧部分。
- 如果列索引大于行索引,则它属于顶部。
- 否则,它属于Left部分。
- 否则,它属于“右”部分或“下”部分。
- 如果列索引大于行索引,则它属于Right部分。
- 否则,它属于Down部分。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include "bits/stdc++.h"
using namespace std;
// Function to calculate the
// sum of all parts of matrix
void SumOfPartsOfMetrics(int* arr,
int N)
{
// To store the sum of all four
// parts of the diagonals
int top, bottom, left, right;
// Intialise respective sum
// as zero
top = bottom = right = left = 0;
// Traversing the matrix
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
// If i + j < N -1
// then it belongs to
// top or left
if (i + j < N - 1 && i != j) {
// Belongs to top
if (i < j) {
top += (arr + i * N)[j];
}
// Belongs to left
else {
left += (arr + i * N)[j];
}
}
// If i+j > N - 1 then
// it belongs to right
// or bottom
else if (i + j > N - 1 && i != j) {
// Belongs to right
if (i > j) {
bottom += (arr + i * N)[j];
}
// Belongs to bottom
else {
right += (arr + i * N)[j];
}
}
}
}
cout << top << ' ' << left
<< ' ' << right << ' '
<< bottom << endl;
}
// Driver Code
int main()
{
int N = 4;
int arr[N][N] = { { 1, 3, 1, 5 },
{ 2, 2, 4, 1 },
{ 5, 0, 2, 3 },
{ 1, 3, 3, 5 } };
// Function call to find print
// sum of al parts
SumOfPartsOfMetrics((int*)arr, N);
return 0;
}
Java
// Java program for the above approach
class GFG {
// Function to calculate the
// sum of all parts of matrix
static void SumOfPartsOfMetrics(int [][]arr,
int N)
{
// To store the sum of all four
// parts of the diagonals
// Intialise respective sum
// as zero
int top = 0, bottom = 0;
int left = 0, right = 0;
// Traversing the matrix
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
// If i + j < N -1
// then it belongs to
// top or left
if (i + j < N - 1 && i != j)
{
// Belongs to top
if (i < j)
{
top += arr[i][j];
}
// Belongs to left
else
{
left += arr[i][j];
}
}
// If i+j > N - 1 then
// it belongs to right
// or bottom
else if (i + j > N - 1 && i != j)
{
// Belongs to right
if (i > j)
{
bottom += arr[i][j];
}
// Belongs to bottom
else
{
right += arr[i][j];
}
}
}
}
System.out.println(top + " " + left + " " +
right + " " + bottom);
}
// Driver Code
public static void main (String[] args)
{
int N = 4;
int arr[][] = { { 1, 3, 1, 5 },
{ 2, 2, 4, 1 },
{ 5, 0, 2, 3 },
{ 1, 3, 3, 5 } };
// Function call to find print
// sum of al parts
SumOfPartsOfMetrics(arr, N);
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 program for the above approach
# Function to calculate the
# sum of all parts of matrix
def SumOfPartsOfMetrics(arr, N):
# To store the sum of all four
# parts of the diagonals
# Intialise respective sum
# as zero
top = bottom = right = left = 0;
# Traversing the matrix
for i in range(N):
for j in range(N):
# If i + j < N -1
# then it belongs to
# top or left
if (i + j < N - 1 and i != j):
# Belongs to top
if (i < j):
top += arr[i][j];
# Belongs to left
else:
left += arr[i][j];
# If i+j > N - 1 then
# it belongs to right
# or bottom
elif (i + j > N - 1 and i != j):
# Belongs to right
if (i > j):
bottom += arr[i][j];
# Belongs to bottom
else:
right += arr[i][j];
print(top, left, right, bottom);
# Driver Code
if __name__ == "__main__":
N = 4;
arr = [ [ 1, 3, 1, 5 ],
[ 2, 2, 4, 1 ],
[ 5, 0, 2, 3 ],
[ 1, 3, 3, 5 ] ];
# Function call to find print
# sum of al parts
SumOfPartsOfMetrics(arr, N);
# This code is contributed by AnkitRai01
C#
// C# program for the above approach
using System;
class GFG {
// Function to calculate the
// sum of all parts of matrix
static void SumOfPartsOfMetrics(int [,]arr,
int N)
{
// To store the sum of all four
// parts of the diagonals
// Intialise respective sum
// as zero
int top = 0, bottom = 0;
int left = 0, right = 0;
// Traversing the matrix
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
// If i + j < N -1
// then it belongs to
// top or left
if (i + j < N - 1 && i != j)
{
// Belongs to top
if (i < j)
{
top += arr[i, j];
}
// Belongs to left
else
{
left += arr[i, j];
}
}
// If i+j > N - 1 then
// it belongs to right
// or bottom
else if (i + j > N - 1 && i != j)
{
// Belongs to right
if (i > j)
{
bottom += arr[i, j];
}
// Belongs to bottom
else
{
right += arr[i, j];
}
}
}
}
Console.WriteLine(top + " " + left + " " +
right + " " + bottom);
}
// Driver Code
public static void Main (string[] args)
{
int N = 4;
int [,]arr = { { 1, 3, 1, 5 },
{ 2, 2, 4, 1 },
{ 5, 0, 2, 3 },
{ 1, 3, 3, 5 } };
// Function call to find print
// sum of al parts
SumOfPartsOfMetrics(arr, N);
}
}
// This code is contributed by AnkitRai01
Javascript
4 7 4 6
时间复杂度: O(N 2 )
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。