Minimum number of steps required to reach origin from a given point
Last Updated :
29 Jan, 2022
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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the minimum moves
// required to reach origin from (a, b)
void findMinMoves(int a, int b)
{
// Stores the minimum number of moves
int ans = 0;
// Check if the absolute
// difference is 1 or 0
if (a == b || abs(a - b) == 1) {
ans = a + b;
}
else {
// Store the minimum of a, b
int k = min(a, b);
// Store the maximum of a, b
int j = max(a, b);
ans = 2 * k + 2 * (j - k) - 1;
}
// Print the answer
cout << ans;
}
// Driver Code
int main()
{
// Given co-ordinates
int a = 3, b = 5;
// Function Call
findMinMoves(a, b);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG
{
// Function to find the minimum moves
// required to reach origin from (a, b)
static void findMinMoves(int a, int b)
{
// Stores the minimum number of moves
int ans = 0;
// Check if the absolute
// difference is 1 or 0
if (a == b || Math.abs(a - b) == 1)
{
ans = a + b;
}
else
{
// Store the minimum of a, b
int k = Math.min(a, b);
// Store the maximum of a, b
int j = Math.max(a, b);
ans = 2 * k + 2 * (j - k) - 1;
}
// Print the answer
System.out.print(ans);
}
// Driver Code
public static void main (String[] args)
{
// Given co-ordinates
int a = 3, b = 5;
// Function Call
findMinMoves(a, b);
}
}
// This code is contributed by Dharanendra L V.
Python3
# Python3 program for the above approach
# function to find the minimum moves
# required to reach origin from (a, b)
def findMinMoves(a, b):
# Stores the minimum number of moves
ans = 0
# Check if the absolute
# difference is 1 or 0
if (a == b or abs(a - b) == 1):
ans = a + b
else:
# Store the minimum of a, b
k = min(a, b)
# Store the maximum of a, b
j = max(a, b)
ans = 2 * k + 2 * (j - k) - 1
# Print the answer
print (ans)
# Driver Code
if __name__ == '__main__':
# Given co-ordinates
a,b = 3, 5
# Function Call
findMinMoves(a, b)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the minimum moves
// required to reach origin from (a, b)
static void findMinMoves(int a, int b)
{
// Stores the minimum number of moves
int ans = 0;
// Check if the absolute
// difference is 1 or 0
if (a == b || Math.Abs(a - b) == 1)
{
ans = a + b;
}
else
{
// Store the minimum of a, b
int k = Math.Min(a, b);
// Store the maximum of a, b
int j = Math.Max(a, b);
ans = 2 * k + 2 * (j - k) - 1;
}
// Print the answer
Console.Write(ans);
}
// Driver Code
public static void Main()
{
// Given co-ordinates
int a = 3, b = 5;
// Function Call
findMinMoves(a, b);
}
}
// This code is contributed by chitranayal.
JavaScript
<script>
// JavaScript program to implement the above approach
// Function to find the minimum moves
// required to reach origin from (a, b)
function findMinMoves(a, b)
{
// Stores the minimum number of moves
let ans = 0;
// Check if the absolute
// difference is 1 or 0
if (a == b || Math.abs(a - b) == 1) {
ans = a + b;
}
else {
// Store the minimum of a, b
let k = Math.min(a, b);
// Store the maximum of a, b
let j = Math.max(a, b);
ans = 2 * k + 2 * (j - k) - 1;
}
// Print the answer
document.write(ans);
}
// Driver Code
// Given co-ordinates
let a = 3, b = 5;
// Function Call
findMinMoves(a, b);
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Number of jump required of given length to reach a point of form (d, 0) from origin in 2D plane
Given three positive integers a, b and d. You are currently at origin (0, 0) on infinite 2D coordinate plane. You are allowed to jump on any point in the 2D plane at euclidean distance either equal to a or b from your current position. The task is to find the minimum number of jump required to reach
7 min read
Minimum number of stops from given path
There are many points in two-dimensional space which need to be visited in a specific sequence. Path from one point to other is always chosen as shortest path and path segments are always aligned with grid lines. Now we are given the path which is chosen for visiting the points, we need to tell the
7 min read
Minimum number of lines needed to cross to reach at origin
Given N number of integers in sorted format, where each integer Ai for all(1 ? i ? N), denotes the join of two points (0, Ai) and (Ai, 0) and forms a line by joining these two points, also given Q number of coordinates in form of (X, Y) in the first Quadrant. Return the minimum number of lines neede
15+ min read
Minimum number of steps required to place all 1s at a single index
Given a binary array A[] of size N, where all 1s can be moved to its adjacent position, the task is to print an array res[] of size N, where res[i] contains the minimum number of steps required to move all the 1s at the ith index. Examples: Input: A[] = {1, 0, 1, 0}Output: {2, 2, 2, 4}Explanation: F
9 min read
Minimize the number of steps required to reach the end of the array | Set 2
Given an integer array arr[] of length N consisting of positive integers, the task is to minimize the number of steps required to reach the arr[N - 1] starting from arr[0]. At a given step if we are at index i we can go to index i - arr[i] or i + arr[i] given we have not visited those indexes before
7 min read
Find the minimum number of steps to reach M from N
Given two integers N and M. The task is to find the minimum number of steps to reach M from N by performing given operations. Multiply a number x by 2. So, x becomes 2*x.Subtract one from the number x. So, x becomes x-1. Examples: Input : N = 4, M = 6 Output : 2 Explanation : Perform operation numbe
5 min read
Number of steps required to reach point (x,y) from (0,0) using zig-zag way
Given a coordinate (x, y). The task is to calculate the number of steps required to reach point (x, y) from (0, 0) using zig-zag way and you cannot travel in straight line for more than 1 unit. Also, start moving along Y axis.For example we can reach the Point denoted by red color in the respective
4 min read
Find time required to reach point N from point 0 according to given rules
Given two positive integers X and Y and N number of points arranged in a line, the task is to find the time required to reach point N from point 0 according to the following rules: Every point has one barrier that closes after every Y minutes and remains closed for the next Y minutes.On reaching a p
7 min read
Minimum number of operations required to return to the main folder
Given an array of strings arr[] representing the changed folder operations(Unix-style) performed on the file system. Initially, the file system opens in the main folder. The task is to find the minimum count of operations of the following three types to return to the main folder: "../": Moves to the
6 min read
Maximize jump size to reach all given points from given starting point
Given an array, arr[] consisting of N integers representing coordinates on a number line and an integer S. Find the maximum size of jump required to visit all the coordinates at least once if the starting position is S. Also, Jumping in both directions is permitted. Example: Input: arr[]={1, 7, 3, 9
5 min read