# Minimum steps required to convert the matrix into lower hessenberg matrix

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

Examples:

Input: mat[][] = {
{1, 2, 8},
{1, 3, 4},
{2, 3, 4}}
Output: 8
Decrease the element a[0][2] 8 times.
Now the matrix is lower Hessenberg.

Input: mat[][] = {
{9, 2, 5, 5},
{12, 3, 4, 5},
{13, -3, 4, 2},
{-1, 10, 1, 4}}
Output: 15

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• For a matrix to be Lower Hessenberg matrix all of its elements above super-diagonal must be equal zero, i.e Aij = 0 for all j > i+1..
• The minimum number of steps required to convert a given matrix in the lower Hessenberg matrix is equal to the sum of the absolute values of all Aij for all j > i + 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 apporach ` `#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` `(j > i + 1) ` `                ``result += ``abs``(arr[i][j]); ` `        ``} ` `    ``} ` `    ``return` `result; ` `} ` ` `  `int` `main() ` `{ ` `    ``int` `arr[N][N] = { 1, 2, 3, 2, ` `                      ``3, 1, 0, 3, ` `                      ``3, 2, 1, 3, ` `                      ``-3, 4, 2, 1 }; ` ` `  `    ``cout << stepsRequired(arr); ` `    ``return` `1; ` `} `

## Java

 `// Java implementation of above apporach ` `import` `java.io.*; ` ` `  `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` `(j > i + ``1``) ` `                ``result += Math.abs(arr[i][j]); ` `        ``} ` `    ``} ` `    ``return` `result; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` `  `    ``int` `[][]arr = { {``1``, ``2``, ``3``, ``2``}, ` `                    ``{``3``, ``1``, ``0``, ``3``}, ` `                    ``{``3``, ``2``, ``1``, ``3``}, ` `                    ``{-``3``, ``4``, ``2``, ``1``} ` `                    ``}; ` ` `  `    ``System.out.println (stepsRequired(arr)); ` `} ` `} ` ` `  `// The code is contributed by ajit. `

## Python3

 `# Python3 implementation of above apporach ` `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` `(j > i ``+` `1``): ` `                ``result ``+``=` `abs``(arr[i][j]) ` `    ``return` `result ` ` `  ` `  `# Driver code ` `arr ``=` `[[``1``, ``2``, ``3``, ``2``], ` `        ``[``3``, ``1``, ``0``, ``3``], ` `        ``[``3``, ``2``, ``1``, ``3``], ` `        ``[``-``3``, ``4``, ``2``, ``1``]] ` ` `  `print``(stepsRequired(arr)) ` ` `  `# This code is contributed by mohit kumar 29  ` ` `

## C#

 `// C# implementation of above apporach ` `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` `(j > i + 1) ` `                ``result += Math.Abs(arr[i,j]); ` `        ``} ` `    ``} ` `    ``return` `result; ` `} ` ` `  `// Driver code ` `static` `public` `void` `Main () ` `{ ` `     `  `    ``int` `[,]arr = { {1, 2, 3, 2}, ` `                    ``{3, 1, 0, 3}, ` `                    ``{3, 2, 1, 3}, ` `                    ``{-3, 4, 2, 1} ` `                    ``}; ` ` `  `    ``Console.Write(stepsRequired(arr)); ` `} ` `} ` ` `  `// The code is contributed by Tushil.. `

Output:

```8
```

Time complexity: O(N*N)

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : mohit kumar 29, jit_t

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.