# Minimum number of steps to convert a given matrix into Upper Hessenberg matrix

• Last Updated : 27 Apr, 2021

Given a matrix of order NxN, Find the minimum number of steps to convert given matrix into Upper Hessenberg matrix. In each step, the only operation allowed is to decrease or increase any element value by 1.
Examples:

Input : N=3
1 2 8
1 3 4
2 3 4
Output :
Decrease the element a 2 times.
Now the matrix is upper hessenberg
Input : N=4
1 2 2 3
1 3 4 2
3 3 4 2
-1 0 1 4
Output :

Attention reader! All those who say programming isn't for kids, just haven't met the right mentors yet. Join the  Demo Class for First Step to Coding Coursespecifically designed for students of class 8 to 12.

The students will get to learn more about the world of programming in these free classes which will definitely help them in making a wise career choice in the future.

Approach:

• For a matrix to be Upper Hessenberg matrix all of its elements below sub-diagonal must be equal zero, i.e Aij = 0 for all i > j+1..
• The minimum number of steps required to convert a given matrix in the upper Hessenberg matrix is equal to the sum of the absolute values of all Aij for all i > j + 1.
• The modulus value of the element is taken into account because both the increase and decrease of the element count as a single step.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of above approach``#include ``#define N 4``using` `namespace` `std;` `// Function to count steps in``// conversion of matrix into upper``// Hessenberg matrix``int` `stepsRequired(``int` `arr[][N])``{``    ``int` `result = 0;``    ``for` `(``int` `i = 0; i < N; i++) {` `        ``for` `(``int` `j = 0; j < N; j++) {` `            ``// if element is below sub-diagonal``            ``// add abs(element) into result``            ``if` `(i > j + 1)``                ``result += ``abs``(arr[i][j]);``        ``}``    ``}``    ``return` `result;``}` `// Driver code``int` `main()``{``    ``int` `arr[N][N] = { 1, 2, 3, 4,``                      ``3, 1, 0, 3,``                      ``3, 2, 1, 3,``                     ``-3, 4, 2, 1 };` `    ``// Function call``    ``cout << stepsRequired(arr);``    ``return` `0;``}`

## Java

 `// Java implementation of above approach``class` `GFG``{``    ` `    ``static` `int` `N = ``4``;``    ` `    ``// Function to count steps in``    ``// conversion of matrix into upper``    ``// Hessenberg matrix``    ``static` `int` `stepsRequired(``int` `arr[][])``    ``{``        ``int` `result = ``0``;``        ``for` `(``int` `i = ``0``; i < N; i++)``        ``{``    ` `            ``for` `(``int` `j = ``0``; j < N; j++)``            ``{``    ` `                ``// if element is below sub-diagonal``                ``// add abs(element) into result``                ``if` `(i > j + ``1``)``                    ``result += Math.abs(arr[i][j]);``            ``}``        ``}``        ``return` `result;``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `main (String[] args)``    ``{``        ` `        ``int` `arr [][] = ``new` `int` `[][] {{``1``, ``2``, ``3``, ``4``},``                        ``{``3``, ``1``, ``0``, ``3``},``                        ``{``3``, ``2``, ``1``, ``3``},``                        ``{-``3``, ``4``, ``2``, ``1` `}};``    ` `        ``// Function call``        ``System.out.println(stepsRequired(arr));``    ``}``}` `// This code is contributed by ihritik`

## Python3

 `# Python3 implementation of above approach``N ``=` `4``;` `# Function to count steps in``# conversion of matrix into upper``# Hessenberg matrix``def` `stepsRequired(arr):``    ``result ``=` `0``;``    ``for` `i ``in` `range``(N):` `        ``for` `j ``in` `range``(N):` `            ``# if element is below sub-diagonal``            ``# add abs(element) into result``            ``if` `(i > j ``+` `1``):``                ``result ``+``=` `abs``(arr[i][j]);` `    ``return` `result;` `# Driver code``arr ``=`   `[[``1``, ``2``, ``3``, ``4``],``         ``[``3``, ``1``, ``0``, ``3``],``         ``[``3``, ``2``, ``1``, ``3``],``         ``[``-``3``, ``4``, ``2``, ``1``]];` `# Function call``print``(stepsRequired(arr));` `# This code is contributed by Rajput-Ji`

## C#

 `// C# implementation of above approach``using` `System;` `class` `GFG``{``    ` `    ``static` `int` `N = 4;``    ` `    ``// Function to count steps in``    ``// conversion of matrix into upper``    ``// Hessenberg matrix``    ``static` `int` `stepsRequired(``int` `[, ] arr)``    ``{``        ``int` `result = 0;``        ``for` `(``int` `i = 0; i < N; i++)``        ``{``    ` `            ``for` `(``int` `j = 0; j < N; j++)``            ``{``    ` `                ``// if element is below sub-diagonal``                ``// add abs(element) into result``                ``if` `(i > j + 1)``                    ``result += Math.Abs(arr[i, j]);``            ``}``        ``}``        ``return` `result;``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `Main ()``    ``{``        ` `        ``int` `[ , ] arr = ``new` `int` `[, ] { {1, 2, 3, 4},``                        ``{3, 1, 0, 3},``                        ``{3, 2, 1, 3},``                        ``{-3, 4, 2, 1}};``    ` `        ``// Function call``        ``Console.WriteLine(stepsRequired(arr));``    ` `    ``}``}` `// This code is contributed by ihritik`

## Javascript

 ``
Output:
`10`

Time complexity : O(N*N)

My Personal Notes arrow_drop_up