Open In App

Final direction after visiting every cell of Matrix starting from (0, 0)

Given a 2D grid of size N x M. The task is to find the final direction after visiting every cell under given conditions. 
 

Examples: 
 



Input: N = 3, M = 3 
Output: Right 
Explanation: Below is the final position after traversing the grid 
 



Input: N = 3, M = 1 
Output: Down 
 

 

Approach: For the above problem statement we have to observe the following: 
 

Therefore on the basis of above observations, there can be only 4 cases for 4 directions: 
 

  1. If n > m and m is even, final direction will be Up.
  2. If n > m and m is odd, final direction will be Down.
  3. If n <= m and n is even, final direction will be Left.
  4. If n <= m and n is odd, final direction will be Right.

Below is the implementation of the above approach:
 




// C++ program to find the direction
// when stopped moving
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the direction
// when stopped moving
void findDirection(int n, int m)
{
    if (n > m) {
        if (m % 2 == 0)
            printf("Up\n");
        else
            printf("Down\n");
    }
    else {
        if (n % 2 == 0)
            printf("Left\n");
        else
            printf("Right\n");
    }
}
 
// Driver Code
int main()
{
    // Given size of NxM grid
    int n = 3, m = 3;
 
    // Function Call
    findDirection(n, m);
    return 0;
}




// Java program to find the direction
// when stopped moving
class GFG{
 
// Function to find the direction
// when stopped moving
static void findDirection(int n, int m)
{
    if (n > m)
    {
        if (m % 2 == 0)
            System.out.print("Up\n");
        else
            System.out.print("Down\n");
    }
    else
    {
        if (n % 2 == 0)
            System.out.print("Left\n");
        else
            System.out.print("Right\n");
    }
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given size of NxM grid
    int n = 3, m = 3;
 
    // Function Call
    findDirection(n, m);
}
}
 
// This code is contributed by shubham




# Python3 program to find the direction
# when stopped moving
 
# Function to find the direction
# when stopped moving
def findDirection(n, m):
 
    if (n > m):
        if (m % 2 == 0):
            print("Up\n");
        else:
            print("Down\n");
     
    else:
        if (n % 2 == 0):
            print("Left\n");
        else:
            print("Right\n");
     
# Driver Code
 
# Given size of NxM grid
n = 3; m = 3;
 
# Function Call
findDirection(n, m);
 
# This code is contributed by Code_Mech




// C# program to find the direction
// when stopped moving
using System;
class GFG{
 
// Function to find the direction
// when stopped moving
static void findDirection(int n, int m)
{
    if (n > m)
    {
        if (m % 2 == 0)
            Console.Write("Up\n");
        else
            Console.Write("Down\n");
    }
    else
    {
        if (n % 2 == 0)
            Console.Write("Left\n");
        else
            Console.Write("Right\n");
    }
}
 
// Driver code
public static void Main()
{
     
    // Given size of NxM grid
    int n = 3, m = 3;
 
    // Function Call
    findDirection(n, m);
}
}
 
// This code is contributed by Code_Mech




<script>
// Javascript program to find the direction
// when stopped moving
 
// Function to find the direction
// when stopped moving
function findDirection(n, m)
{
    if (n > m) {
        if (m % 2 == 0)
            document.write("Up<br>");
        else
            document.write("Down<br>");
    }
    else {
        if (n % 2 == 0)
            document.write("Left<br>");
        else
            document.write("Right<br>");
    }
}
 
// Driver Code
// Given size of NxM grid
let n = 3, m = 3;
 
// Function Call
findDirection(n, m);
 
// This code is contributed by rishavmahato348.
</script>

Output: 
Right

 

Time Complexity: O(1) 
Auxiliary Space: O(1)
 


Article Tags :