Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on DP
  • Practice DP
  • MCQs on DP
  • Tutorial on Dynamic Programming
  • Optimal Substructure
  • Overlapping Subproblem
  • Memoization
  • Tabulation
  • Tabulation vs Memoization
  • 0/1 Knapsack
  • Unbounded Knapsack
  • Subset Sum
  • LCS
  • LIS
  • Coin Change
  • Word Break
  • Egg Dropping Puzzle
  • Matrix Chain Multiplication
  • Palindrome Partitioning
  • DP on Arrays
  • DP with Bitmasking
  • Digit DP
  • DP on Trees
  • DP on Graph
Open In App
Next Article:
Find maximum path length in a binary matrix
Next article icon

Maximum decimal value path in a binary matrix

Last Updated : 24 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given binary square matrix [n*n]. Find maximum integer value in a path from top left to bottom right. We compute integer value using bits of traversed path. We start at index [0,0] and end at index [n-1][n-1]. from index [i, j], we can move [i, j+1] or [i+1, j]. 

Examples: 

Input : mat[][] = {{1, 1, 0, 1},                    {0, 1, 1, 0},                    {1, 0, 0, 1},                    {1, 0, 1, 1}} Output : 111 Explanation :  Path :   (0,0) -> (0,1) -> (1,1) -> (1,2) ->          (2,2) -> (3,2) ->(3,3)   Decimal value : 1*(2^0) + 1*(2^1) + 1*(2^2) + 1*(2^3) +                  0*(2^4) + 1*(2^5) + 1*(2^6) = 111

The above problem can be recursively defined as below:  

// p indicates power of 2, initially  p = i = j = 0 MaxDecimalValue(mat, i, j, p)      // If i or j is our of boundary    If i >= n || j >= n         return 0     // Compute rest of matrix find maximum decimal value     result  max(MaxDecimalValue(mat, i, j+1, p+1),                 MaxDecimalValue(mat, i+1, j, p+1))     If mat[i][j] == 1         return power(2, p) + result    Else       return result 

Below is the implementation of above recursive algorithm. 

C++




   
// C++ program to find maximum decimal value path in
// binary matrix
#include<bits/stdc++.h>
using namespace std;
 
#define N 4
 
// Returns maximum decimal value in binary matrix.
// Here p indicate power of 2
long long int maxDecimalValue(int mat[][N], int i, int j,
                                                   int p)
{
    // Out of matrix boundary
    if (i >= N || j >= N )
        return 0;
 
    int result = max(maxDecimalValue(mat, i, j+1, p+1),
                     maxDecimalValue(mat, i+1, j, p+1));
 
    // If current matrix value is 1 then return result +
    // power(2, p) else result
    if (mat[i][j] == 1)
        return pow(2, p) + result;
    else
        return result;
}
 
//Driver program
int main()
{
    int mat[][4] = {{ 1 ,1 ,0 ,1 },
        { 0 ,1 ,1 ,0 },
        { 1 ,0 ,0 ,1 },
        { 1 ,0 ,1 ,1 },
    };
 
    cout << maxDecimalValue(mat, 0, 0, 0) << endl;
    return 0;
}
 
 

Java




// Java program to find maximum decimal value path in
// binary matrix
 
class GFG {
 
    static final int N = 4;
 
// Returns maximum decimal value in binary matrix.
// Here p indicate power of 2
    static int maxDecimalValue(int mat[][], int i, int j,
            int p) {
        // Out of matrix boundary
        if (i >= N || j >= N) {
            return 0;
        }
 
        int result = Math.max(maxDecimalValue(mat, i, j + 1, p + 1),
                maxDecimalValue(mat, i + 1, j, p + 1));
 
        // If current matrix value is 1 then return result +
        // power(2, p) else result
        if (mat[i][j] == 1) {
            return (int) (Math.pow(2, p) + result);
        } else {
            return result;
        }
    }
 
// Driver program
    public static void main(String[] args) {
        int mat[][] = {{1, 1, 0, 1},
        {0, 1, 1, 0},
        {1, 0, 0, 1},
        {1, 0, 1, 1},};
 
        System.out.println(maxDecimalValue(mat, 0, 0, 0));
    }
}
//this code contributed by Rajput-Ji
 
 

Python3




# Python3 program to find maximum decimal
# value path in binary matrix
N =4
 
# Returns maximum decimal value in binary
# matrix. Here p indicate power of 2
def maxDecimalValue(mat, i, j, p):
 
    # Out of matrix boundary
    if i >= N or j >= N:
        return 0
         
    result = max(
        maxDecimalValue(mat, i, j+1, p+1),
        maxDecimalValue(mat, i+1, j, p+1))
 
    # If current matrix value is 1 then
    # return result + power(2, p) else
    # result
    if mat[i][j] == 1:
        return pow(2, p) + result
    else:
        return result
 
 
# Driver Program
mat = [ [1, 1, 0, 1],
        [0, 1, 1, 0],
        [1, 0, 0, 1],
        [1, 0, 1, 1] ]
 
print(maxDecimalValue(mat, 0, 0, 0))
 
# This code is contributed by Shrikant13.
 
 

C#




// C# program to find maximum decimal value path in
// binary matrix
 
using System;
class GFG {
 
    static int N = 4;
 
// Returns maximum decimal value in binary matrix.
// Here p indicate power of 2
    static int maxDecimalValue(int[,] mat, int i,
                                int j,int p)
    {
        // Out of matrix boundary
        if (i >= N || j >= N) {
            return 0;
        }
 
        int result = Math.Max(maxDecimalValue(mat, i, j + 1, p + 1),
                    maxDecimalValue(mat, i + 1, j, p + 1));
 
        // If current matrix value is 1 then return result +
        // power(2, p) else result
        if (mat[i,j] == 1)
        {
            return (int) (Math.Pow(2, p) + result);
        } else
        {
            return result;
        }
    }
 
// Driver program
    public static void Main() {
        int[,] mat = {{1, 1, 0, 1},
        {0, 1, 1, 0},
        {1, 0, 0, 1},
        {1, 0, 1, 1},};
 
        Console.Write(maxDecimalValue(mat, 0, 0, 0));
    }
}
// This code is contributed by Ita_c.
 
 

PHP




<?php
// PHP program to find maximum
// decimal value path in binary
// matrix
 
// Returns maximum decimal value
// in binary matrix. Here p
// indicate power of 2
function maxDecimalValue($mat, $i,
                          $j, $p)
{
    $N=4;
     
    // Out of matrix boundary
    if ($i >= $N || $j >= $N )
        return 0;
 
    $result = max(maxDecimalValue($mat, $i,
                            $j + 1, $p + 1),
                  maxDecimalValue($mat, $i + 1,
                                $j, $p + 1));
 
    // If current matrix value
    // is 1 then return result +
    // power(2, p) else result
    if ($mat[$i][$j] == 1)
        return pow(2, $p) + $result;
    else
        return $result;
}
 
    // Driver Code
    $mat = array(array(1 ,1 ,0 ,1),
                 array(0 ,1 ,1 ,0),
                 array(1 ,0 ,0 ,1),
                 array(1 ,0 ,1 ,1));
 
    echo maxDecimalValue($mat, 0, 0, 0) ;
     
// This code is contributed by nitin mittal.
?>
 
 

Javascript




<script>
 
// JavaScript program to find maximum
// decimal value path in binary matrix
let N = 4;
   
// Returns maximum decimal value in
// binary matrix.Here p indicate power of 2
function maxDecimalValue(mat, i, j, p)
{
     
    // Out of matrix boundary
    if (i >= N || j >= N)
    {
        return 0;
    }
 
    let result = Math.max(maxDecimalValue(mat, i, j + 1,
                                                  p + 1),
                          maxDecimalValue(mat, i + 1, j,
                                               p + 1));
                                                
    // If current matrix value is 1 then
    // return result + power(2, p) else result
    if (mat[i][j] == 1)
    {
        return (Math.pow(2, p) + result);
    }
    else
    {
        return result;
    }
}
 
// Driver Code
let mat = [ [ 1, 1, 0, 1 ],
            [ 0, 1, 1, 0 ],
            [ 1, 0, 0, 1 ],
            [ 1, 0, 1, 1 ] ];
 
document.write(maxDecimalValue(mat, 0, 0, 0));
 
// This code is contributed by souravghosh0416            
                          
</script>
 
 

Output: 

111

The time complexity of above recursive solution is exponential. 

Space Complexity:O(1),since no extra space required.

 Here matrix [3][3]                            (2 2)           /        \      (1 2)          (2 1)     /     \        /     \  (0 2)   (1 1)   (1 1)   (2 1)  /  \    /   \    /  \    / \  .    .  .    .    .   .   .  . .    .  .    .    .   .   .  . and so no 

If we see recursion tree of above recursive solution, we can observe overlapping sub-problems. Since the problem has overlapping subproblems, we can solve it efficiently using Dynamic Programming. Below is Dynamic Programming based solution. 

Below is the implementation of above problem using Dynamic Programming  

C++




// C++ program to find Maximum decimal value Path in
// Binary matrix
#include<bits/stdc++.h>
using namespace std;
#define N 4
 
// Returns maximum decimal value in binary matrix.
// Here p indicate power of 2
long long int MaximumDecimalValue(int mat[][N], int n)
{
    int dp[n][n];
    memset(dp, 0, sizeof(dp));
    if (mat[0][0] == 1)
        dp[0][0] = 1 ; // 1*(2^0)
 
    // Compute binary stream of first row of matrix
    // and store result in dp[0][i]
    for (int i=1; i<n; i++)
    {
        // indicate 1*(2^i) + result of previous
        if (mat[0][i] == 1)
            dp[0][i] = dp[0][i-1] + pow(2, i);
 
        // indicate 0*(2^i) + result of previous
        else
            dp[0][i] = dp[0][i-1];
    }
 
    // Compute binary stream of first column of matrix
    // and store result in dp[i][0]
    for (int i = 1 ; i <n ; i++ )
    {
        // indicate 1*(2^i) + result of previous
        if (mat[i][0] == 1)
            dp[i][0] = dp[i-1][0] + pow(2, i);
 
        // indicate 0*(2^i) + result of previous
        else
            dp[i][0] = dp[i-1][0];
    }
 
    // Traversal rest Binary matrix and Compute maximum
    // decimal value
    for (int i=1 ; i < n ; i++ )
    {
        for (int j=1 ; j < n ; j++ )
        {
            // Here (i+j) indicate the current power of
            // 2 in path that is 2^(i+j)
            if (mat[i][j] == 1)
                dp[i][j] = max(dp[i][j-1], dp[i-1][j]) +
                                             pow(2, i+j);
            else
                dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
        }
     }
 
    // Return maximum decimal value in binary matrix
    return dp[n-1][n-1];
}
 
// Driver program
int main()
{
    int mat[][4] = {{ 1 ,1 ,0 ,1 },
        { 0 ,1 ,1 ,0 },
        { 1 ,0 ,0 ,1 },
        { 1 ,0 ,1 ,1 },
    };
    cout << MaximumDecimalValue(mat, 4) << endl;
    return 0;
}
 
 

Java




// Java program to find Maximum decimal value Path in
// Binary matrix
public class GFG {
 
    final static int N = 4;
// Returns maximum decimal value in binary matrix.
// Here p indicate power of 2
 
    static int MaximumDecimalValue(int mat[][], int n) {
        int dp[][] = new int[n][n];
        if (mat[0][0] == 1) {
            dp[0][0] = 1; // 1*(2^0)
        }
        // Compute binary stream of first row of matrix
        // and store result in dp[0][i]
        for (int i = 1; i < n; i++) {
            // indicate 1*(2^i) + result of previous
            if (mat[0][i] == 1) {
                dp[0][i] = (int) (dp[0][i - 1] + Math.pow(2, i));
            } // indicate 0*(2^i) + result of previous
            else {
                dp[0][i] = dp[0][i - 1];
            }
        }
 
        // Compute binary stream of first column of matrix
        // and store result in dp[i][0]
        for (int i = 1; i < n; i++) {
            // indicate 1*(2^i) + result of previous
            if (mat[i][0] == 1) {
                dp[i][0] = (int) (dp[i - 1][0] + Math.pow(2, i));
            } // indicate 0*(2^i) + result of previous
            else {
                dp[i][0] = dp[i - 1][0];
            }
        }
 
        // Traversal rest Binary matrix and Compute maximum
        // decimal value
        for (int i = 1; i < n; i++) {
            for (int j = 1; j < n; j++) {
                // Here (i+j) indicate the current power of
                // 2 in path that is 2^(i+j)
                if (mat[i][j] == 1) {
                    dp[i][j] = (int) (Math.max(dp[i][j - 1], dp[i - 1][j])
                            + Math.pow(2, i + j));
                } else {
                    dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j]);
                }
            }
        }
 
        // Return maximum decimal value in binary matrix
        return dp[n - 1][n - 1];
    }
 
// Driver program
    public static void main(String[] args) {
 
        int mat[][] = {{1, 1, 0, 1},
        {0, 1, 1, 0},
        {1, 0, 0, 1},
        {1, 0, 1, 1},};
        System.out.println(MaximumDecimalValue(mat, 4));
    }
}
/*This code is contributed by Rajput-Ji*/
 
 

Python3




# Python3 program to find Maximum decimal
# value Path in
# Binary matrix
 
N=4
 
# Returns maximum decimal value in binary matrix.
# Here p indicate power of 2
def MaximumDecimalValue(mat, n):
    dp=[[0 for i in range(n)] for i in range(n)]
    if (mat[0][0] == 1):
        dp[0][0] = 1     # 1*(2^0)
         
    # Compute binary stream of first row of matrix
    # and store result in dp[0][i]
    for i in range(1,n):
         
        # indicate 1*(2^i) + result of previous
        if (mat[0][i] == 1):
            dp[0][i] = dp[0][i-1] + 2**i
             
        # indicate 0*(2^i) + result of previous
        else:
            dp[0][i] = dp[0][i-1]
             
    # Compute binary stream of first column of matrix
    # and store result in dp[i][0]
    for i in range(1,n):
         
        # indicate 1*(2^i) + result of previous
        if (mat[i][0] == 1):
            dp[i][0] = dp[i-1][0] + 2**i
             
        # indicate 0*(2^i) + result of previous
    else:
        dp[i][0] = dp[i-1][0]
         
    # Traversal rest Binary matrix and Compute maximum
    # decimal value
    for i in range(1,n):
        for j in range(1,n):
             
            # Here (i+j) indicate the current power of
            # 2 in path that is 2^(i+j)
            if (mat[i][j] == 1):
                dp[i][j] = max(dp[i][j-1], dp[i-1][j])+(2**(i+j))
            else:
                dp[i][j] = max(dp[i][j-1], dp[i-1][j])
                 
    # Return maximum decimal value in binary matrix
    return dp[n-1][n-1]
 
# Driver program
if __name__=='__main__':
     
    mat = [[ 1 ,1 ,0 ,1 ],
          [ 0 ,1 ,1 ,0 ],
          [ 1 ,0 ,0 ,1 ],
          [ 1 ,0 ,1 ,1 ]]
           
    print (MaximumDecimalValue(mat, 4))
 
#this code is contributed by sahilshelangia
 
 

C#




// C# program to find Maximum decimal value Path in
// Binary matrix
using System;
public class GFG {
  
    readonly static int N = 4;
// Returns maximum decimal value in binary matrix.
// Here p indicate power of 2
  
    static int MaximumDecimalValue(int [,]mat, int n) {
        int [,]dp = new int[n,n];
        if (mat[0,0] == 1) {
            dp[0,0] = 1; // 1*(2^0)
        }
        // Compute binary stream of first row of matrix
        // and store result in dp[0,i]
        for (int i = 1; i < n; i++) {
            // indicate 1*(2^i) + result of previous
            if (mat[0,i] == 1) {
                dp[0,i] = (int) (dp[0,i - 1] + Math.Pow(2, i));
            } // indicate 0*(2^i) + result of previous
            else {
                dp[0,i] = dp[0,i - 1];
            }
        }
  
        // Compute binary stream of first column of matrix
        // and store result in dp[i,0]
        for (int i = 1; i < n; i++) {
            // indicate 1*(2^i) + result of previous
            if (mat[i,0] == 1) {
                dp[i,0] = (int) (dp[i - 1,0] + Math.Pow(2, i));
            } // indicate 0*(2^i) + result of previous
            else {
                dp[i,0] = dp[i - 1,0];
            }
        }
  
        // Traversal rest Binary matrix and Compute maximum
        // decimal value
        for (int i = 1; i < n; i++) {
            for (int j = 1; j < n; j++) {
                // Here (i+j) indicate the current power of
                // 2 in path that is 2^(i+j)
                if (mat[i,j] == 1) {
                    dp[i,j] = (int) (Math.Max(dp[i,j - 1], dp[i - 1,j])
                            + Math.Pow(2, i + j));
                } else {
                    dp[i,j] = Math.Max(dp[i,j - 1], dp[i - 1,j]);
                }
            }
        }
  
        // Return maximum decimal value in binary matrix
        return dp[n - 1,n - 1];
    }
  
// Driver program
    public static void Main() {
  
        int [,]mat = {{1, 1, 0, 1},
        {0, 1, 1, 0},
        {1, 0, 0, 1},
        {1, 0, 1, 1},};
        Console.Write(MaximumDecimalValue(mat, 4));
    }
}
 
// This code is contributed by Rajput-Ji
 
 

Javascript




<script>
// Javascript program to find Maximum decimal value Path in
// Binary matrix
     
    let N = 4;
    // Returns maximum decimal value in binary matrix.
    // Here p indicate power of 2
    function MaximumDecimalValue(mat,n)
    {
        let dp=new Array(n);
        for(let i = 0; i < n; i++)
        {
            dp[i] = new Array(n);
            for(let j = 0; j < n; j++)
            {
                dp[i][j] = 0;
            }
        }
         
        if (mat[0][0] == 1) {
            dp[0][0] = 1; // 1*(2^0)
        }
        // Compute binary stream of first row of matrix
        // and store result in dp[0][i]
        for (let i = 1; i < n; i++)
        {
         
            // indicate 1*(2^i) + result of previous
            if (mat[0][i] == 1)
            {
                dp[0][i] = dp[0][i - 1] + Math.pow(2, i);
            }
             
            // indicate 0*(2^i) + result of previous
            else
            {
                dp[0][i] = dp[0][i - 1];
            }
        }
  
        // Compute binary stream of first column of matrix
        // and store result in dp[i][0]
        for (let i = 1; i < n; i++)
        {
         
            // indicate 1*(2^i) + result of previous
            if (mat[i][0] == 1)
            {
                dp[i][0] = Math.floor(dp[i - 1][0] + Math.pow(2, i));
            }
             
            // indicate 0*(2^i) + result of previous
            else
            {
                dp[i][0] = dp[i - 1][0];
            }
        }
  
        // Traversal rest Binary matrix and Compute maximum
        // decimal value
        for (let i = 1; i < n; i++)
        {
            for (let j = 1; j < n; j++)
            {
             
                // Here (i+j) indicate the current power of
                // 2 in path that is 2^(i+j)
                if (mat[i][j] == 1)
                {
                    dp[i][j] = Math.floor(Math.max(dp[i][j - 1], dp[i - 1][j])
                            + Math.pow(2, i + j));
                }
                else
                {
                    dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j]);
                }
            }
        }
  
        // Return maximum decimal value in binary matrix
        return dp[n - 1][n - 1];
    }
     
    // Driver program
    let mat = [[ 1 ,1 ,0 ,1 ],
          [ 0 ,1 ,1 ,0 ],
          [ 1 ,0 ,0 ,1 ],
          [ 1 ,0 ,1 ,1 ]];
    document.write(MaximumDecimalValue(mat, 4))
     
    // This code is contributed by rag2127.
</script>
 
 

Output: 

111

Time Complexity : O(n2) 
Auxiliary space : O(n2)

 



Next Article
Find maximum path length in a binary matrix

N

Nishant_Singh (Pintu)
Improve
Article Tags :
  • DSA
  • Dynamic Programming
  • Matrix
  • binary-representation
Practice Tags :
  • Dynamic Programming
  • Matrix

Similar Reads

  • Find maximum path length in a binary matrix
    Given a square matrix mat every element of which is either 0 or 1. A value 1 means connected and 0 means not connected. The task is to find the largest length of a path in the matrix after changing atmost one 0 to 1. A path is a 4-directionally connected group of 1s. Examples: Input: mat[][] = {{1,
    9 min read
  • Maximum sum path in a Matrix
    Given an n*m matrix, the task is to find the maximum sum of elements of cells starting from the cell (0, 0) to cell (n-1, m-1). However, the allowed moves are right, downwards or diagonally right, i.e, from location (i, j) next move can be (i+1, j), or, (i, j+1), or (i+1, j+1). Find the maximum sum
    15+ min read
  • Paths from entry to exit in matrix and maximum path sum
    Given a maze which is a N * N grid grid[][]. Every cell of the maze contains either the number 1, 2 or 3 which defines the moves as: If grid[i][j] = 1 then the only valid move is grid[i][j + 1].If grid[i][j] = 2 then the only valid move is grid[i + 1][j].If grid[i][j] = 3 then the valid moves are gr
    15 min read
  • Maximum Path Sum in a Binary Tree
    Given a binary tree, the task is to find the maximum path sum. The path may start and end at any node in the tree. Example: Input: Output: 42Explanation: Max path sum is represented using green colour nodes in the above binary tree. Input: Output: 31Explanation: Max path sum is represented using gre
    9 min read
  • Maximum path sum in matrix
    Given a matrix of size n * m. Find the maximum path sum in the matrix. The maximum path is the sum of all elements from the first row to the last row where you are allowed to move only down or diagonally to left or right. You can start from any element in the first row. Examples: Input: mat[][] = 10
    6 min read
  • Pair with maximum difference in a Matrix
    Given a NxM matrix with N rows and M columns of positive integers. The task is to find the pair with the maximum difference in the given matrix. Note: Pairs at positions (a, b) and (b, a) are considered equivalent. Examples: Input : mat[N][M] = {{1, 2, 3, 4}, {25, 6, 7, 8}, {9, 10, 11, 12}, {13, 14,
    5 min read
  • Find the maximum sum path in the given Matrix
    Consider an infinite matrix. The cells of the matrix are filled with natural numbers from the cell (1, 1) in the direction of top right to bottom left diagonally. Then the task is to output the Maximum path sum between two points (X1, Y1) and (X2, Y2). Matrix filling process is as follows: Examples:
    7 min read
  • Find pair of rows in a binary matrix that has maximum bit difference
    Given a Binary Matrix. The task is to find the pair of row in the Binary matrix that has maximum bit difference Examples: Input: mat[][] = {{1, 1, 1, 1}, {1, 1, 0, 1}, {0, 0, 0, 0}}; Output : (1, 3) Bit difference between row numbers 1 and 3 is maximum with value 4. Bit difference between 1 and 2 is
    15 min read
  • Maximum sum path in a matrix from top to bottom
    Consider a n*n matrix. Suppose each cell in the matrix has a value assigned. We can go from each cell in row i to a diagonally higher cell in row i+1 only [i.e from cell(i, j) to cell(i+1, j-1) and cell(i+1, j+1) only]. Find the path from the top row to the bottom row following the aforementioned co
    15 min read
  • Exit Point in a Binary Matrix
    Given a binary matrix of size N x M, you enter the matrix at cell (0, 0) in the left to the right direction. Whenever encountering a 0 retain in the same direction if encountered a 1 change direction to the right of the current direction and change that 1 value to 0, find out exit point from the Mat
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences