Given two integers **N** and **B**, the task is to print the maximum index a pointer, starting from **0**^{th} index can reach in an array of natural numbers(i.e., 0, 1, 2, 3, 4, 5…), say **arr[]**, in **N** steps without placing itself at index **B** at any point.

In each step, the pointer can move from the

Current Indexto aJumping Indexor can remain at theCurrent Index.

Jumping Index = Current Index + Step Number

**Examples:**

Input:N = 3, B = 2Output:6Explanation:

Step 1:

Current Index = 0

Step Number = 1

Jumping Index = 0 + 1 = 1Step 2:Current Index = 1

Step Number = 2

Jumping Index = 1 + 2 = 3Step 3:

Current Index = 3

Step Number = 3

Jumping Index = 3 + 3 = 6

Therefore, the maximum index that can be reached is 6.

Input:N = 3, B = 1Output:5Explanation:

Step 1:

Current Index = 0

Step Number = 1

Jumping Index = 0 + 1 = 1But this is bad index. So pointer remains at theCurrent Index.Step 2:

Current Index = 0

Step Number = 2

Jumping Index = 0 + 2 = 2Step 3:

Current Index = 2

Step Number = 3

Jumping Index = 2 + 3 = 5

Therefore, the maximum index that can be reached is 5.

**Naive Approach:** The simplest approach to solve the problem is to calculate the maximum index by considering two possiblities for every **Current Index**, either to move pointer by **Step Number** or by remaining at the **Current Index**, and generate all possible combinations. Finally, print the maximum index obtained.

**Time Complexity:** O(N^{3})**Auxiliary Space:** O(1)

**Efficient Approach:**

Calculate the maximum index that can be reached within the given steps. If the 0^{th} Index can be reached from the maximum index by avoiding the bad index, print the result. Otherwise, repeat the procedure by decrementing the maximum index by 1.

Below are the steps:

- Calculate the maximum index that can be reached in
**N**steps by calculating the sum of the first**N**natural numbers. - Assign the value of the calculated maximum index to
**Current Index**. - Keep decrementing
**Current Index**by**Step Number**and**Step Number**by**1**until one of them becomes negative. - After every decrement, check if the
**Current Index**is equal to**B**or not. If found to be true, revert the changes made on**Current Index**. - If
**Current Index**reaches to**0**successfully, print the current value of**maximum index**as teh answer. - Otherwise, decrement the value of the
**maximum index**by**1**and repeat from step**2**.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the maximum ` `// index the pointer can reach ` `void` `maximumIndex(` `int` `N, ` `int` `B) ` `{ ` ` ` `int` `max_index = 0; ` ` ` ` ` `// Calculate maximum possible ` ` ` `// index that can be reached ` ` ` `for` `(` `int` `i = 1; i <= N; i++) { ` ` ` ` ` `max_index += i; ` ` ` `} ` ` ` ` ` `int` `current_index = max_index, step = N; ` ` ` ` ` `while` `(1) { ` ` ` ` ` `// Check if current index and step ` ` ` `// both are greater than 0 or not ` ` ` `while` `(current_index > 0 && N > 0) { ` ` ` ` ` `// Decrement current_index by step ` ` ` `current_index -= N; ` ` ` ` ` `// Check if current index is ` ` ` `// equal to B or not ` ` ` `if` `(current_index == B) { ` ` ` ` ` `// Restore to previous index ` ` ` `current_index += N; ` ` ` `} ` ` ` ` ` `// Decrement step by one ` ` ` `N--; ` ` ` `} ` ` ` ` ` `// If it reaches the 0th index ` ` ` `if` `(current_index <= 0) { ` ` ` ` ` `// Print result ` ` ` `cout << max_index << endl; ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `// If max index fails to ` ` ` `// reach the 0th index ` ` ` `else` `{ ` ` ` ` ` `N = step; ` ` ` ` ` `// Store max_index - 1 in current index ` ` ` `current_index = max_index - 1; ` ` ` ` ` `// Decrement max index ` ` ` `max_index--; ` ` ` ` ` `// If current index is equal to B ` ` ` `if` `(current_index == B) { ` ` ` ` ` `current_index = max_index - 1; ` ` ` ` ` `/ ` ` ` `// Decrement current index ` ` ` `max_index--; ` ` ` `} ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 3, B = 2; ` ` ` `maximumIndex(N, B); ` ` ` `return` `0; ` `}` |

*chevron_right*

*filter_none*

**Output:**

6

**Time Complexity:** O(N^{2})**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings
- Count the minimum steps to reach 0 from the given integer N
- Minimize steps defined by a string required to reach the destination from a given source
- Number of steps required to reach point (x,y) from (0,0) using zig-zag way
- Find the minimum number of steps to reach M from N
- Minimize the number of steps required to reach the end of the array | Set 2
- Count ways to reach the Nth stair using multiple 1 or 2 steps and a single step 3
- Minimum steps to reach the Nth stair in jumps of perfect power of 2
- Minimum steps to reach N from 1 by multiplying each step by 2, 3, 4 or 5
- Check if it is possible to reach (x, y) from origin in exactly Z steps using only plus movements
- Minimize steps to reach K from 0 by adding 1 or doubling at each step
- Minimum steps for increasing and decreasing Array to reach either 0 or N
- Maximum money that can be withdrawn in two steps
- Find the index of the left pointer after possible moves in the array
- Minimize steps required to move all 1's in a matrix to a given index
- Total position where king can reach on a chessboard in exactly M moves
- Total position where king can reach on a chessboard in exactly M moves | Set 2
- Generate array with minimum sum which can be deleted in P steps
- Largest number N which can be reduced to 0 in K steps
- Minimum number of steps required to obtain the given Array by the given operations

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.