A robot's initial position is and it can only move along X-axis. It has moves to make and in each move, it will select one of the following options:
- Go to from
- Go to from
- Remain at its current position
Your task is to calculate for all reachable .
Note: Here, 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