Related Articles

# Maximum strength in a Matrix after performing specified operations

• Difficulty Level : Expert
• Last Updated : 07 Jun, 2021

Given a matrix of N * M containing {0, 1, #}. The task is to find the maximum value of strength based on following rules:

1. Initial strength is zero.
2. If you encounter a 0, Strength decreases by 2.
3. If you encounter a 1, Strength increases by 5.
4. If you encounter a #, Jumps to the start of a new row without losing any strength.

Note: You have to traverse every row of the matrix in top-down order from left to right.
Example:

```Input:
{{1, 0, 1, 0},
{0, #, 0, 0},
{1, 1, 0, 0},
{0, #, 1, 0}}

Output: 14
Explanation:
Here you starts with strength S = 0.

For the first row {1, 0, 1, 0}:
After {1} -> S = S + 5 = 5
After {0} -> S = S - 2 = 3
After {1} -> S = S + 5 = 8
After {0} -> S = S - 2 = 6

For the Second row {0, #, 0, 0}:
After {0} -> S = S - 2 = 4

For the Third row {1, 1, 0, 0}:
After {1} -> S = S + 5 = 9
After {1} -> S = S + 5 = 14
After {0} -> S = S - 2 = 12
After {0} -> S = S - 2 = 10

For the Fourth row {0, #, 1, 0}:
After {0} -> S = S - 2 = 8

So, The maximum value of S is 14 ```

Approach:

1. Traverse the matrix mat[][] from i = [0, N], j = [0, M] and check:

```If mat[i][j] = 0 then, S = S - 2.
If mat[i][j] = 1 then, S = S + 5.
1.
2. At every step store maximum value of strength till now and Print the strength at the end.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function return the Maximum``// value of the strength``void` `MaxStrength(``char` `mat,``                 ``int` `n, ``int` `m)``{``    ``int` `S = 0;``    ``int` `ans = 0;` `    ``for` `(``int` `i = 0; i < n; i++) {``        ``for` `(``int` `j = 0; j < m; j++) {` `            ``char` `Curr = mat[i][j];` `            ``// If current element``            ``// is 1``            ``if` `(Curr == ``'1'``) {``                ``S += 5;``            ``}``            ``// If current element``            ``// is 0``            ``if` `(Curr == ``'0'``) {``                ``S -= 2;``            ``}``            ``// If current element``            ``// is '#'``            ``if` `(Curr == ``'#'``) {``                ``break``;``            ``}` `            ``// Store the value of``            ``// maximum strength``            ``// till now``            ``ans = max(ans, S);``        ``}``    ``}` `    ``cout << ans;` `    ``return``;``}` `// Driver code``int` `main()``{``    ``int` `N = 4;``    ``int` `M = 4;``    ``char` `Mat{ { ``'1'``, ``'0'``, ``'1'``, ``'0'` `},``                        ``{ ``'0'``, ``'#'``, ``'0'``, ``'0'` `},``                        ``{ ``'1'``, ``'1'``, ``'0'``, ``'0'` `},``                        ``{ ``'0'``, ``'#'``, ``'1'``, ``'0'` `} };` `    ``MaxStrength(Mat, N, M);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.io.*;` `class` `GFG{` `// Function return the maximum``// value of the strength``static` `void` `MaxStrength(``char``[][] mat,``                        ``int` `n, ``int` `m)``{``    ``int` `S = ``0``;``    ``int` `ans = ``0``;` `    ``for``(``int` `i = ``0``; i < n; i++)``    ``{``       ``for``(``int` `j = ``0``; j < m; j++)``       ``{``          ``char` `Curr = mat[i][j];``          ` `          ``// If current element``          ``// is 1``          ``if` `(Curr == ``'1'``)``          ``{``              ``S += ``5``;``          ``}``          ` `          ``// If current element``          ``// is 0``          ``if` `(Curr == ``'0'``)``          ``{``              ``S -= ``2``;``          ``}``          ` `          ``// If current element``          ``// is '#'``          ``if` `(Curr == ``'#'``)``          ``{``              ``break``;``          ``}``          ` `          ``// Store the value of``          ``// maximum strength``          ``// till now``          ``ans = Math.max(ans, S);``       ``}``    ``}``    ``System.out.println(ans);``    ``return``;``}` `// Driver code``public` `static` `void` `main (String[] args)``{``    ``int` `N = ``4``;``    ``int` `M = ``4``;``    ``char``[][] Mat = { { ``'1'``, ``'0'``, ``'1'``, ``'0'` `},``                     ``{ ``'0'``, ``'#'``, ``'0'``, ``'0'` `},``                     ``{ ``'1'``, ``'1'``, ``'0'``, ``'0'` `},``                     ``{ ``'0'``, ``'#'``, ``'1'``, ``'0'` `} };` `    ``MaxStrength(Mat, N, M);``}``}` `// This code is contributed by shubhamsingh10`

## Python3

 `# python3 program for the above approach` `# Function return the Maximum``# value of the strength``def` `MaxStrength(mat, n, m):``    ``S ``=` `0``    ``ans ``=` `0` `    ``for` `i ``in` `range``(n):``        ``for` `j ``in` `range``(m):``            ``Curr ``=` `mat[i][j]` `            ``# If current element``            ``# is 1``            ``if` `(Curr ``=``=` `'1'``):``                ``S ``+``=` `5``                ` `            ``# If current element``            ``# is 0``            ``if` `(Curr ``=``=` `'0'``):``                ``S ``-``=` `2``                ` `            ``# If current element``            ``# is '#'``            ``if` `(Curr ``=``=` `'#'``):``                ``break` `            ``# Store the value of``            ``# maximum strength``            ``# till now``            ``ans ``=` `max``(ans, S)` `    ``print``(ans)``    ``return` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``N ``=` `4``;``    ``M ``=` `4``;``    ``Mat ``=` `[ [``'1'``, ``'0'``, ``'1'``, ``'0'``],``            ``[``'0'``, ``'#'``, ``'0'``, ``'0'``],``            ``[``'1'``, ``'1'``, ``'0'``, ``'0'``],``            ``[``'0'``, ``'#'``, ``'1'``, ``'0'``] ]``            ` `    ``MaxStrength(Mat, N, M)` `# This code is contributed by Samarth`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{` `// Function return the maximum``// value of the strength``static` `void` `MaxStrength(``char``[,] mat,``                        ``int` `n, ``int` `m)``{``    ``int` `S = 0;``    ``int` `ans = 0;` `    ``for``(``int` `i = 0; i < n; i++)``    ``{``       ``for``(``int` `j = 0; j < m; j++)``       ``{``          ``char` `Curr = mat[i, j];``          ` `          ``// If current element``          ``// is 1``          ``if` `(Curr == ``'1'``)``          ``{``              ``S += 5;``          ``}``          ` `          ``// If current element``          ``// is 0``          ``if` `(Curr == ``'0'``)``          ``{``              ``S -= 2;``          ``}``          ` `          ``// If current element``          ``// is '#'``          ``if` `(Curr == ``'#'``)``          ``{``              ``break``;``          ``}``          ` `          ``// Store the value of``          ``// maximum strength``          ``// till now``          ``ans = Math.Max(ans, S);``       ``}``    ``}``    ``Console.WriteLine(ans);``    ``return``;``}` `// Driver code``public` `static` `void` `Main(String[] args)``{``    ``int` `N = 4;``    ``int` `M = 4;``    ``char``[,] Mat = { { ``'1'``, ``'0'``, ``'1'``, ``'0'` `},``                    ``{ ``'0'``, ``'#'``, ``'0'``, ``'0'` `},``                    ``{ ``'1'``, ``'1'``, ``'0'``, ``'0'` `},``                    ``{ ``'0'``, ``'#'``, ``'1'``, ``'0'` `} };` `    ``MaxStrength(Mat, N, M);``}``}` `// This code is contributed by sapnasingh4991`

## Javascript

 ``
Output:
`14`

Attention reader! Don’t stop learning now. Join the First-Step-to-DSA Course for Class 9 to 12 students , specifically designed to introduce data structures and algorithms to the class 9 to 12 students

My Personal Notes arrow_drop_up