- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print direction of moves such that you stay within the [-k, +k] boundary in C++
In this problem, we have to find a valid way to move positive direction or negative direction in such a way that we stay within a certain limit that is provided by the user.
Here, We are given a certain maximum limit K, which is the maximum value to which we can move and an array of n positive values to move. We have to return the sequence i.e. positive or negative directions to move so that it never crosses K value.
Let’s take an example to understand the topic better,
Input : K = 56 and the array is [25 , 14 , 31 , 16 , 5]. Output : positive positive negative positive positive.
Explanation
First we will check if 0 + a[0] = 0+25 = 25 < 56 yes, then we will move in positive direction.
Now, we will check if 25 + a[1] = 25+14 = 39 < 56 yes, then we will move in positive direction.
Now, we will check if 29 + a[2] = 39+31 = 70 < 56 No, then we will check if 39 - a[2] = 39 - 31 = 8 > 0, then we will move in a negative direction.
we will check if 8 + a[3] = 8+16 = 24 < 56 yes, then we will move in positive direction.
we will check if 16 + a[4] = 16 + 5 = 21 < 56 yes, then we will move in positive direction.
So, now let’s create the logic to solve this problem. We have to check if moving in a positive direction will reach the limit or not. If no, then move in a positive direction. Otherwise, check if moving in a negative direction will reach the lower limit i.e. 0. If no, then move in a negative direction. If both are Yes then return not possible.
Based on this logic the algorithm that we have to follow for creating our code is −
Algorithm
Initially set position to 0. Step 1 : for i -> 0 to n , n is the length of array. Follow step 2 - 4. Step 2 : if initial_postition + a[i] < K, initial_position + = a[i]. Print “POSITIVE”. Step 3 : else if initial_postition - a[i] > 0, initial_position - = a[i]. Print “NEGATIVE”. Step 4 : else , print “NO MORE VALID MOVES”.
Example
Now, let's create a program to show the implementation of the algorithm to solve the problem,
#include <iostream> using namespace std; void StepsTaken(int a[], int n, int k){ string res = ""; int position = 0; int steps = 1; for (int i = 0; i < n; i++) { if (position + a[i] <= k && position + a[i] >= (-k)) { position += a[i]; cout<<"POSITIVE \t"; } else if (position - a[i] >= -k && position - a[i] <= k) { position -= a[i]; cout<<"NEGATIVE \t"; } else { cout << -1; return; } } cout << res; } int main(){ int a[] = { 12 , 24 , 9 , 17 , 8}; int n = sizeof(a) / sizeof(a[0]); int k = 40; StepsTaken(a, n, k); return 0; }
Output
POSITIVE POSITIVE NEGATIVE NEGATIVE POSITIVE