Problem:-
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.
See the sample explanation for better understanding.
Input format
- The first line contains denoting the number of test cases.
- The first line of each test case containing an integer denoting the number of moves.
Output format
Print lines. For each test case, print a single integer as described in the problem statement.
Constraints
He is initially at (0,0). He has 1 move to make, the positions where he can end up are (-1,0),(1,0) and (0,0).
=abs(-1)+abs(0)+abs(1)+abs(0)+abs(0)+abs(0)
=1+0+1+0+0+0
=2
Code:-
Here I am going to give you two solution first one is on the basis of C language and second one is on the basis of c++ language which you can submit in c++14 and c++17 also
Solution 1 ( C language):-
Solution 2 ( C++ language):-
Recommended Post:-
- Hackerearth Problems:-Data structure:-
- Very Cool numbers | Hacker earth solution
- Birthday party | Hacker earth solution
- Most frequent | hacker earth problem solution
- program to find symetric difference of two sets
- cost of balloons | Hacker earth problem solution
- Chacha o chacha | hacker earth problem solution
- jadu and dna | hacker earth solution
- Bricks game | hacker earth problem
- Anti-Palindrome strings | hacker earth solution
- connected components in the graph | hacker earth data structure
- odd one out || hacker earth problem solution
- Minimum addition | Hackerearth Practice problem
- The magical mountain | Hackerearth Practice problem
- The first overtake | Hackerearth Practice problem
- Program to find cycle in the graph
- Implementation of singly link list
- Implementation of queue by using link list
- Algorithm of quick sort
- stack by using link list
- program to find preorder post order and inorder of the binary search tree
- Minimum weight of spanning tree
- Preorder, inorder and post order traversal of the tree
0 Comments