Given a square matrix mat[][] of dimension N*N, convert the elements present in both the diagonals to their respective binary representations and perform the following operations:
- For every position of bits, count the number of set bits and non-set bits in those binary representations.
- If count of set bits exceeds that of non-set bits, place 0 at that position for a new number. Otherwise, place 1 at that position.
- Finally, print the sum of the two generated numbers.
Examples:
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)<number of non-set bits(=3)
At bit position 2, number of set bits(=1)<number of non-set bits(=2)
At bit position 3, number of set bits(=1)<number of non-set bits(=2)
Therefore, after processing the primary diagonal, the number generated is (0001)2 = 1.
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
Naive Approach: The simplest approach is to traverse the matrix and store the primary diagonal elements in an array and the secondary diagonal elements in another array. Then find the sum of the numbers generated by iterating over the set bits of the elements in both the arrays.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by finding the diagonal elements using a single loop. Follow the steps below to solve the problem:
- For Principal Diagonal elements: Iterate a loop until N, where N is the number of columns, and store mat[i][i] where i is the index variable.
- For Secondary Diagonal elements: Iterate a loop until N, where N is the number of columns and store mat[i][k], where i is the index variable and K = N – 1. Decrease K until i < N.
For finding the numbers for each set of diagonal elements, perform the following steps:
- Initialize a variable, say ans as 0, to store the resultant number.
- Iterate over the range [0, 31] using variable i and perform the following:
- Initialize S and NS as 0, to store the number of set and non-set bits respectively at position i.
- Traverse the diagonal elements using variable j and if arr[j] is set at position i, increment S by 1, else increment NS by 1.
- If the value of S is greater than NS, set the bit at position i of ans.
- After completing the above steps, the value of ans is the number generated for each set of diagonal elements.
- Repeat the above steps for the other sets of diagonal elements, and print the sum of the two numbers generated.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int processDiagonal(vector< int >arr)
{
int ans = 0;
int getBit = 1;
for ( int i = 0; i < 32; i++)
{
int S = 0;
int NS = 0;
for ( auto j: arr)
{
if (getBit&j)
S += 1;
else
NS += 1;
}
if (S > NS)
ans += pow (2,i);
getBit <<= 1;
}
return ans;
}
int findSum(vector<vector< int >>mat)
{
int i = 0;
int j = 0;
vector< int > priDiag;
while (i<mat.size()){
priDiag.push_back(mat[i][j]);
i += 1;
j += 1;
}
i = 0;
j = mat.size()-1;
vector< int > secDiag;
while (i<mat.size()){
secDiag.push_back(mat[i][j]);
i += 1;
j -= 1;
}
return processDiagonal(priDiag) + processDiagonal(secDiag);
}
int main(){
vector<vector< int >>mat{{1, 2, 3},{4, 5, 6},{7, 8, 9}};
cout<<findSum(mat)<<endl;
}
|
Java
import java.util.*;
class GFG
{
static int processDiagonal(ArrayList<Integer> arr)
{
int ans = 0 ;
int getBit = 1 ;
for ( int i = 0 ; i < 32 ; i++)
{
int S = 0 ;
int NS = 0 ;
for ( int j: arr)
{
if ((getBit&j) != 0 )
S += 1 ;
else
NS += 1 ;
}
if (S > NS)
ans += Math.pow( 2 ,i);
getBit <<= 1 ;
}
return ans;
}
static int findSum( int [][] mat)
{
int i = 0 ;
int j = 0 ;
ArrayList<Integer> priDiag
= new ArrayList<Integer>();
while (i<mat.length){
priDiag.add(mat[i][j]);
i += 1 ;
j += 1 ;
}
i = 0 ;
j = mat.length - 1 ;
ArrayList<Integer> secDiag
= new ArrayList<Integer>();
while (i<mat.length){
secDiag.add(mat[i][j]);
i += 1 ;
j -= 1 ;
}
return (processDiagonal(priDiag) + processDiagonal(secDiag));
}
public static void main(String[] args)
{
int [][] mat= {{ 1 , 2 , 3 },{ 4 , 5 , 6 },{ 7 , 8 , 9 }};
System.out.print(findSum(mat));
}
}
|
Python3
def processDiagonal(arr):
ans = 0
getBit = 1
for i in range ( 32 ):
S = 0
NS = 0
for j in arr:
if getBit&j:
S + = 1
else :
NS + = 1
if S > NS:
ans + = 2 * * i
getBit << = 1
return ans
def findSum(mat):
i = 0
j = 0
priDiag = []
while i< len (mat):
priDiag.append(mat[i][j])
i + = 1
j + = 1
i = 0
j = len (mat) - 1
secDiag = []
while i< len (mat):
secDiag.append(mat[i][j])
i + = 1
j - = 1
return processDiagonal(priDiag) + processDiagonal(secDiag)
mat = [[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 7 , 8 , 9 ]]
print (findSum(mat))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int processDiagonal(List< int > arr)
{
int ans = 0;
int getBit = 1;
for ( int i = 0; i < 32; i++) {
int S = 0;
int NS = 0;
foreach ( int j in arr)
{
if ((getBit & j) != 0)
S += 1;
else
NS += 1;
}
if (S > NS)
ans += ( int )Math.Pow(2, i);
getBit <<= 1;
}
return ans;
}
static int findSum( int [, ] mat)
{
int i = 0;
int j = 0;
List< int > priDiag = new List< int >();
while (i < mat.GetLength(0)) {
priDiag.Add(mat[i, j]);
i += 1;
j += 1;
}
i = 0;
j = mat.GetLength(0) - 1;
List< int > secDiag = new List< int >();
while (i < mat.GetLength(0)) {
secDiag.Add(mat[i, j]);
i += 1;
j -= 1;
}
return (processDiagonal(priDiag)
+ processDiagonal(secDiag));
}
public static void Main( string [] args)
{
int [, ] mat
= { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
Console.Write(findSum(mat));
}
}
|
Javascript
<script>
function processDiagonal(arr)
{
let ans = 0;
let getBit = 1;
for (let i = 0; i < 32; i++)
{
let S = 0;
let NS = 0;
for (let j = 0; j < arr.length; j++)
{
if (getBit & arr[j])
S += 1;
else
NS += 1;
}
if (S > NS)
ans += Math.pow(2,i);
getBit <<= 1;
}
return ans;
}
function findSum(mat)
{
let i = 0;
let j = 0;
let priDiag = [];
while (i<mat.length){
priDiag.push(mat[i][j]);
i += 1;
j += 1;
}
i = 0;
j = mat.length - 1;
let secDiag = [];
while (i<mat.length){
secDiag.push(mat[i][j]);
i += 1;
j -= 1;
}
return processDiagonal(priDiag) + processDiagonal(secDiag);
}
let mat = [[1, 2, 3],[4, 5, 6],[7, 8, 9]];
document.write(findSum(mat));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N) because using extra space for vectors priDiag and secDiag