- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- 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

- Related Articles
- Maximum value K such that array has at-least K elements that are >= K in C++
- Find smallest number K such that K % p = 0 and q % K = 0 in C++
- Find minimum x such that (x % k) * (x / k) == n in C++
- Find prime number K in an array such that (A[i] % K) is maximum in C++
- Cheapest Flights Within K Stops in C++
- Print N lines of numbers such that every pair among numbers has a GCD K
- Count the triplets such that A[i] < B[j] < C[k] in C++
- Place k elements such that minimum distance is maximized in C++
- Maximize arr[j] – arr[i] + arr[l] – arr[k], such that i < j < k < l in C++
- Find maximum value of x such that n! % (k^x) = 0 in C++
- Find minimum radius such that atleast k point lie inside the circle in C++
- Place K-knights such that they do not attack each other in C++
- Maximum subarray size, such that all subarrays of that size have sum less than k in C++
- Maximum subarray size, such that all subarrays of that size have sum less than k in C++ program
- Finding ‘k’ such that its modulus with each array element is same in C++