给定NxN阶的2D数组arr [] [] ,任务是找到给定arr [] []的对角线和边界元素中所有元素的总和。
例子:
Input: arr[][] = { {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4} }
Output: 40
Explanation:
The Sum of elements on the boundary is 1 + 2 + 3 + 4 + 4 + 4 + 4 + 3 + 2 + 1 + 1 + 1 = 30.
The Sum of elements on the diagonals which do not intersect with the boundary elements is 2 + 3 + 2 + 3 = 10.
Therefore the required sum is 30 + 10 = 40.
Input: arr[][] = { {1, 2, 3}, {1, 2, 3}, {1, 2, 3}}
Output: 18
Explanation:
The Sum of elements on the boundary is 1 + 2 + 3 + 3 + 3 + 2 + 1 + 1 = 16.
The Sum of elements on the diagonals which do not intersect with the boundary elements is 2.
Therefore the required sum is 16 + 2 = 18.
方法:
- 遍历给定的2D数组有两个循环,一个循环用于行(例如i ),另一个循环用于列(例如j )。
- 如果i等于j且(i + j)等于(列的大小– 1),则该元素对给定2D数组的对角线有贡献。
- 如果( i或j等于0 )或( i或j等于第– 1列的大小),则该元素将成为给定2D数组的边界元素。
- 满足以上两个条件的所有元素的总和即为所需的总和。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include "bits/stdc++.h"
using namespace std;
const int N = 4;
// Function to find the sum of all diagonal
// and Boundary elements
void diagonalBoundarySum(int arr[N][N])
{
int requiredSum = 0;
// Traverse arr[][]
// Loop from i to N-1 for rows
for (int i = 0; i < N; i++) {
// Loop from j = N-1 for columns
for (int j = 0; j < N; j++) {
// Condition for diagonal
// elements
if (i == j || (i + j) == N - 1) {
requiredSum += arr[i][j];
}
// Condition for Boundary
// elements
else if (i == 0 || j == 0
|| i == N - 1
|| j == N - 1) {
requiredSum += arr[i][j];
}
}
}
// Print the final Sum
cout << requiredSum << endl;
}
// Driver Code
int main()
{
int arr[][4] = { { 1, 2, 3, 4 },
{ 1, 2, 3, 4 },
{ 1, 2, 3, 4 },
{ 1, 2, 3, 4 } };
diagonalBoundarySum(arr);
return 0;
}
Java
// Java implementation of the above approach
import java.util.*;
class GFG{
public static int N = 4;
// Function to find the sum of all diagonal
// and Boundary elements
static void diagonalBoundarySum(int arr[][]){
int requiredSum = 0;
// Traverse arr[][]
// Loop from i to N-1 for rows
for (int i = 0; i < N; i++) {
// Loop from j = N-1 for columns
for (int j = 0; j < N; j++) {
// Condition for diagonal
// elements
if (i == j || (i + j) == N - 1) {
requiredSum += arr[i][j];
}
// Condition for Boundary
// elements
else if (i == 0 || j == 0 || i == N - 1|| j == N - 1) {
requiredSum += arr[i][j];
}
}
}
// Print the final Sum
System.out.println(requiredSum);
}
// Driver Code
public static void main(String args[])
{
int arr[][] = { { 1, 2, 3, 4 },{ 1, 2, 3, 4 },
{ 1, 2, 3, 4 },{ 1, 2, 3, 4 } };
diagonalBoundarySum(arr);
}
}
// This code is contributed by AbhiThakur
Python3
# Python implementation of the above approach
N = 4;
# Function to find the sum of all diagonal
# and Boundary elements
def diagonalBoundarySum(arr):
requiredSum = 0;
# Traverse arr
# Loop from i to N-1 for rows
for i in range(N):
# Loop from j = N-1 for columns
for j in range(N):
# Condition for diagonal
# elements
if (i == j or (i + j) == N - 1):
requiredSum += arr[i][j];
# Condition for Boundary
# elements
elif(i == 0 or j == 0 or i == N - 1 or j == N - 1):
requiredSum += arr[i][j];
# Prthe final Sum
print(requiredSum);
# Driver Code
if __name__ == '__main__':
arr = [[ 1, 2, 3, 4 ],
[ 1, 2, 3, 4 ],
[ 1, 2, 3, 4 ],
[ 1, 2, 3, 4 ]];
diagonalBoundarySum(arr);
# This code is contributed by 29AjayKumar
C#
// C# implementation of the above approach
using System;
class GFG
{
public static int N = 4;
// Function to find the sum of all diagonal
// and Boundary elements
static void diagonalBoundarySum(int[, ] arr){
int requiredSum = 0;
// Traverse arr[][]
// Loop from i to N-1 for rows
for (int i = 0; i < N; i++) {
// Loop from j = N-1 for columns
for (int j = 0; j < N; j++) {
// Condition for diagonal
// elements
if (i == j || (i + j) == N - 1) {
requiredSum += arr[i,j];
}
// Condition for Boundary
// elements
else if (i == 0 || j == 0 || i == N - 1|| j == N - 1) {
requiredSum += arr[i,j];
}
}
}
// Print the final Sum
Console.WriteLine(requiredSum);
}
// Driver Code
public static void Main()
{
int[, ] arr = { { 1, 2, 3, 4 },{ 1, 2, 3, 4 },{ 1, 2, 3, 4 },{ 1, 2, 3, 4 } };
diagonalBoundarySum(arr);
}
}
// This code is contributed by abhaysingh290895
Javascript
40