Robotic moves

A robot's initial position is (0,0) and it can only move along X-axis. It has N moves to make and in each move, it will select one of the following options:

  1. Go to (X1,0) from (X,0)
  2. Go to (X+1,0) from (X,0)
  3. Remain at its current position

Your task is to calculate (abs(X)+abs(Y)) for all reachable (XY).

Note: Here, abs denotes the absolute value.

Example

Input: n = 1
Output: 2

Approach

Java


public class RoboticMoves {
    public static void main(String args[]) throws Exception {
      
        int n = 1;
        long ans = ((long) n) * (n + 1);
        System.out.println(ans);
    }

}

C++

#include <bits/stdc++.h>
using namespace std;

long roboticMoves(long n)
{
    long ans = n * (n + 1);
    return ans;
}
int main()
{
    long n = 1;
    cout << roboticMoves(n<< "\n";

    return 0;
}


No comments:

Post a Comment