Minimum number of steps required to reach origin from a given point
Given two integers A and B representing coordinates of a point in the first quadrant, the task is to find the minimum number of steps required to reach the origin. All possible moves from a point (i, j) are (i – 1, j), (i, j – 1) or (i, j) (staying at the same position).
Note: It is not allowed to move in the same direction twice in a row.
Examples:
Input: A = 4, B = 0
Output: 7
Explanation:
Below are the movements from the given points to origin:
(4, 0) ? (3, 0) ? (3, 0)(stays) ? (2, 0) ? (2, 0)(stays) ? (1, 0) ? (1, 0)(stays) ? (0, 0).
Hence, 7 moves are required to reach origin.
Input: A = 3, B = 5
Output: 9
Explanation:
Below are the movements from the given points to origin:
(3, 5) ? (3, 4) ? (2, 4) ? (2, 3) ? (1, 3) ? (1, 2) ? (0, 2) ? (0, 1) ? (0, 1)(stays) ? (0, 0).
Hence, 9 moves are required to reach origin.
Naive Approach: The simplest approach is to recursion. The idea is to recursively consider all possible moves from each point and for each of them, calculate the minimum number of steps required to reach the origin.
Time Complexity: O(3max(A, B))
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is based on the observation that if the absolute difference between the x and y coordinates is 1 or 0, then the minimum number of steps required to reach the origin is (a + b). Otherwise, it takes (2 * abs(a – b) – 1) moves to reach (k, k), where k is the minimum of a, b.
Therefore, the minimum number of steps required to reach origin from (a, b) is equal to = (Steps required to reach (k, k) + Steps required to reach (0, 0) from (k, k)) = (2 * abs(a – b) – 1) + (2 * k)
Follow the steps below to solve the problem:
- Initialize a variable, ans, which stores the minimum number of steps required to reach origin from (a, b).
- If the absolute difference of a and b is 1 or 0, then update ans to (a + b).
- Otherwise:
- Find the minimum of a, b, and store it in a variable k.
- Using the formula, update ans = (2 * abs(a – b) – 1) + (2 * k).
- After completing the above steps, print the value of ans as the result.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findMinMoves( int a, int b)
{
int ans = 0;
if (a == b || abs (a - b) == 1) {
ans = a + b;
}
else {
int k = min(a, b);
int j = max(a, b);
ans = 2 * k + 2 * (j - k) - 1;
}
cout << ans;
}
int main()
{
int a = 3, b = 5;
findMinMoves(a, b);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void findMinMoves( int a, int b)
{
int ans = 0 ;
if (a == b || Math.abs(a - b) == 1 )
{
ans = a + b;
}
else
{
int k = Math.min(a, b);
int j = Math.max(a, b);
ans = 2 * k + 2 * (j - k) - 1 ;
}
System.out.print(ans);
}
public static void main (String[] args)
{
int a = 3 , b = 5 ;
findMinMoves(a, b);
}
}
|
Python3
def findMinMoves(a, b):
ans = 0
if (a = = b or abs (a - b) = = 1 ):
ans = a + b
else :
k = min (a, b)
j = max (a, b)
ans = 2 * k + 2 * (j - k) - 1
print (ans)
if __name__ = = '__main__' :
a,b = 3 , 5
findMinMoves(a, b)
|
C#
using System;
class GFG
{
static void findMinMoves( int a, int b)
{
int ans = 0;
if (a == b || Math.Abs(a - b) == 1)
{
ans = a + b;
}
else
{
int k = Math.Min(a, b);
int j = Math.Max(a, b);
ans = 2 * k + 2 * (j - k) - 1;
}
Console.Write(ans);
}
public static void Main()
{
int a = 3, b = 5;
findMinMoves(a, b);
}
}
|
Javascript
<script>
function findMinMoves(a, b)
{
let ans = 0;
if (a == b || Math.abs(a - b) == 1) {
ans = a + b;
}
else {
let k = Math.min(a, b);
let j = Math.max(a, b);
ans = 2 * k + 2 * (j - k) - 1;
}
document.write(ans);
}
let a = 3, b = 5;
findMinMoves(a, b);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
29 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...