📌  相关文章
📜  通过对角矩阵元素中的设置位和未设置位的计数获得的数字总和

📅  最后修改于: 2021-05-25 08:59:54             🧑  作者: Mango

给定尺寸为N * N的方阵mat [] [] ,将两个对角线中存在的元素转换为它们各自的二进制表示形式,并执行以下操作:

  • 对于位的每个位置,请在这些二进制表示形式中计数设置位和未设置位的数量。
  • 如果设置的位数超过未设置的位数,则在该位置放置0以获得新的数字。否则,将1放置在该位置。
  • 最后,打印两个生成的数字之和。

例子:

天真的方法:最简单的方法是遍历矩阵并将主要对角线元素存储在一个数组中,将辅助对角线元素存储在另一个数组中。然后找到两个数组中元素的设置位进行迭代生成的数字总和。
时间复杂度: O(N 2 )
辅助空间: O(1)

高效方法:可以通过使用单个循环找到对角线元素来优化上述方法。请按照以下步骤解决问题:

  • 对于主要对角元素:循环直到N ,其中N是列数,并存储mat [i] [i] ,其中i是索引变量。
  • 对于次要对角线元素:循环直到N ,其中N是列数,并存储mat [i] [k] ,其中i是索引变量,并且K = N – 1 。减小K直到i

要查找每组对角线元素的数字,请执行以下步骤:

  • 初始化一个变量,例如ans0 ,以存储结果数。
  • 使用变量i遍历[ 0,31 ]范围并执行以下操作:
    • SNS初始化为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.


输出:
8

时间复杂度: O(N)
辅助空间: O(1)