给定一个维度为N*N 的方阵mat[][] ,将两条对角线中存在的元素转换为它们各自的二进制表示,并执行以下操作:
- 对于位的每个位置,计算这些二进制表示中设置位和非设置位的数量。
- 如果设置位的计数超过未设置位的计数,则在该位置放置0以获取新数字。否则,在该位置放置1 。
- 最后,打印两个生成数字的总和。
例子:
Input: mat[][] = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Output: 8
Explanation:
For the primary diagonal, the binary representation of each element is:
1 = (0001)2
5 = (0101)2
9 = (1001)2
At bit position 0, number of set bits(=3)>number of non-set bits(=0)
At bit position 1, number of set bits(=0)
For the secondary diagonal, the binary representation of each element is:
3 = (011)2
5 = (101)2
7 = (111)2
At bit position 0, number of set bits(=3)>number of non-set bits(=0)
At bit position 1, number of set bits(=2)>number of non-set bits(=1)
At bit position 2, number of set bits(=2)>number of non-set bits(=1)
Therefore, after processing the primary diagonal, the number generated is (111)2 = 7.
Hence, the required sum = 1 + 7 = 8.
Input: mat[][] = [[2, 3], [3, 9]]
Output: 3
朴素的方法:最简单的方法是遍历矩阵并将主对角线元素存储在一个数组中,将次对角线元素存储在另一个数组中。然后找到通过迭代两个数组中元素的集合位生成的数字的总和。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效的方法:可以通过使用单个循环查找对角线元素来优化上述方法。请按照以下步骤解决问题:
- 对于主对角线元素:迭代循环直到N ,其中N是列数,并存储mat[i][i] ,其中i是索引变量。
- 对于 Secondary Diagonal 元素:迭代循环直到N ,其中N是列数并存储mat[i][k] ,其中i是索引变量, K = N – 1 。减少K直到i < N 。
要查找每组对角元素的数字,请执行以下步骤:
- 初始化一个变量,比如ans为0 ,以存储结果数字。
- 使用变量i迭代范围[0, 31]并执行以下操作:
- 将S和NS初始化为0 ,分别在位置i存储设置和未设置位的数量。
- 使用变量j遍历对角线元素,如果arr[j]设置在位置i ,则将S增加1 ,否则将NS增加1 。
- 如果S的值大于NS ,则在ans 的位置i设置位。
- 完成上述步骤后, ans的值就是为每组对角线元素生成的数量。
- 对其他对角元素集重复上述步骤,并打印生成的两个数字的总和。
下面是上述方法的实现:
C++
// CPP program for the above approach
#include
using namespace std;
// Functino to find the number after
// processing the diagonal elements
int processDiagonal(vectorarr)
{
// Store the required number
int ans = 0;
int getBit = 1;
// Checking for each position
for (int i = 0; i < 32; i++)
{
// Store the number of set bits
// & non-set bits at position i
int S = 0;
int NS = 0;
// Traverse the diagonal elements
for(auto j: arr)
{
// Update count of S if current
// element is set at position i
if (getBit&j)
S += 1;
// Else update NS
else
NS += 1;
}
// If number of set bits is >
// number of non-set bits, add
// set bits value to the ans
if(S > NS)
ans += pow(2,i);
getBit <<= 1;
}
// Return the answer
return ans;
}
// Function to find the sum of the
// numbers generated after processing
// both the diagonals of the matrix
int findSum(vector>mat)
{
int i = 0;
int j = 0;
// Store the primary diagonal elements
vector priDiag;
while(i secDiag;
while(i>mat{{1, 2, 3},{4, 5, 6},{7, 8, 9}};
// Function Call
cout<
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Functino to find the number after
// processing the diagonal elements
static int processDiagonal(ArrayList arr)
{
// Store the required number
int ans = 0;
int getBit = 1;
// Checking for each position
for (int i = 0; i < 32; i++)
{
// Store the number of set bits
// & non-set bits at position i
int S = 0;
int NS = 0;
// Traverse the diagonal elements
for(int j: arr)
{
// Update count of S if current
// element is set at position i
if ((getBit&j) != 0)
S += 1;
// Else update NS
else
NS += 1;
}
// If number of set bits is >
// number of non-set bits, add
// set bits value to the ans
if(S > NS)
ans += Math.pow(2,i);
getBit <<= 1;
}
// Return the answer
return ans;
}
// Function to find the sum of the
// numbers generated after processing
// both the diagonals of the matrix
static int findSum(int[][] mat)
{
int i = 0;
int j = 0;
// Store the primary diagonal elements
ArrayList priDiag
= new ArrayList();
while(i secDiag
= new ArrayList();
while(i
Python3
# Python program for the above approach
# Functino to find the number after
# processing the diagonal elements
def processDiagonal(arr):
# Store the required number
ans = 0
getBit = 1
# Checking for each position
for i in range(32):
# Store the number of set bits
# & non-set bits at position i
S = 0
NS = 0
# Traverse the diagonal elements
for j in arr:
# Update count of S if current
# element is set at position i
if getBit&j:
S += 1
# Else update NS
else:
NS += 1
# If number of set bits is >
# number of non-set bits, add
# set bits value to the ans
if S > NS:
ans += 2**i
getBit <<= 1
# Return the answer
return ans
# Function to find the sum of the
# numbers generated after processing
# both the diagonals of the matrix
def findSum(mat):
i = 0
j = 0
# Store the primary diagonal elements
priDiag = []
while i
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
// Functino to find the number after
// processing the diagonal elements
static int processDiagonal(List arr)
{
// Store the required number
int ans = 0;
int getBit = 1;
// Checking for each position
for (int i = 0; i < 32; i++) {
// Store the number of set bits
// & non-set bits at position i
int S = 0;
int NS = 0;
// Traverse the diagonal elements
foreach(int j in arr)
{
// Update count of S if current
// element is set at position i
if ((getBit & j) != 0)
S += 1;
// Else update NS
else
NS += 1;
}
// If number of set bits is >
// number of non-set bits, add
// set bits value to the ans
if (S > NS)
ans += (int)Math.Pow(2, i);
getBit <<= 1;
}
// Return the answer
return ans;
}
// Function to find the sum of the
// numbers generated after processing
// both the diagonals of the matrix
static int findSum(int[, ] mat)
{
int i = 0;
int j = 0;
// Store the primary diagonal elements
List priDiag = new List();
while (i < mat.GetLength(0)) {
priDiag.Add(mat[i, j]);
i += 1;
j += 1;
}
i = 0;
j = mat.GetLength(0) - 1;
// Store the secondary diagonal elements
List secDiag = new List();
while (i < mat.GetLength(0)) {
secDiag.Add(mat[i, j]);
i += 1;
j -= 1;
}
// Function Call to get the required
// numbers and return their sum
return (processDiagonal(priDiag)
+ processDiagonal(secDiag));
}
// Driver Code
public static void Main(string[] args)
{
int[, ] mat
= { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
// Function Call
Console.Write(findSum(mat));
}
}
// This code is contributed by ukasp.
Javascript
8
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live