Sum of numbers obtained by the count of set and non | Functino to find the number after processing the diagonal elements ; Store the required number ; Checking for each position ; Store the number of set bits & non - set bits at position i ; Traverse the diagonal elements ; Update count of S if current element is set at position i ; Else update NS ; If number of set bits is > number of non - set bits , add set bits value to the ans ; Return the answer ; Function to find the sum of the numbers generated after processing both the diagonals of the matrix ; Store the primary diagonal elements ; Store the secondary diagonal elements ; Function Call to get the required numbers and return their sum ; Driver Code ; Function Call
def processDiagonal ( arr ) : NEW_LINE INDENT ans = 0 NEW_LINE getBit = 1 NEW_LINE for i in range ( 32 ) : NEW_LINE INDENT S = 0 NEW_LINE NS = 0 NEW_LINE for j in arr : NEW_LINE if getBit & j : NEW_LINE INDENT S += 1 NEW_LINE DEDENT else : NEW_LINE INDENT NS += 1 NEW_LINE DEDENT if S > NS : NEW_LINE ans += 2 ** i NEW_LINE getBit <<= 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT def findSum ( mat ) : NEW_LINE INDENT i = 0 NEW_LINE j = 0 NEW_LINE priDiag = [ ] NEW_LINE while i < len ( mat ) : NEW_LINE INDENT priDiag . append ( mat [ i ] [ j ] ) NEW_LINE i += 1 NEW_LINE j += 1 NEW_LINE DEDENT i = 0 NEW_LINE j = len ( mat ) - 1 NEW_LINE secDiag = [ ] NEW_LINE while i < len ( mat ) : NEW_LINE INDENT secDiag . append ( mat [ i ] [ j ] ) NEW_LINE i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT return processDiagonal ( priDiag ) + processDiagonal ( secDiag ) NEW_LINE DEDENT mat = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] NEW_LINE print ( findSum ( mat ) ) NEW_LINE