Given two integers N and B, the task is to print the maximum index a pointer, starting from 0th 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 Index to a Jumping Index or can remain at the Current Index.
Jumping Index = Current Index + Step Number
Examples:
Input: N = 3, B = 2
Output: 6
Explanation:

Step 1:
Current Index = 0
Step Number = 1
Jumping Index = 0 + 1 = 1
Step 2:Current Index = 1
Step Number = 2
Jumping Index = 1 + 2 = 3
Step 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 = 1
Output: 5
Explanation:

Step 1:
Current Index = 0
Step Number = 1
Jumping Index = 0 + 1 = 1But this is bad index. So pointer remains at the Current Index.
Step 2:
Current Index = 0
Step Number = 2
Jumping Index = 0 + 2 = 2
Step 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 possibilities for every Current Index, either to move the pointer by Step Number or by remaining at the Current Index, and generate all possible combinations. Finally, print the maximum index obtained.
Algorithm
Take two integers N and B as input.
Define a recursive function that takes four arguments
Inside the function:
a. If stepNumber is equal to N, check if currentIndex is equal to B.
If it is, return -1, otherwise, return currentIndex.
b. If currentIndex is equal to B, return -1.
c. Otherwise, consider three possibilities for the next step:
move the pointer to the left (currentIndex - 1)
stay at the current index (currentIndex)
move the pointer to the right (currentIndex + 1).
return the maximum valid index among the three possibilities.
Call the function with currentIndex = 0, stepNumber = 0, N, and B as arguments.
Print the maximum index obtained.
Implementation
C++
#include <iostream>
#include <cmath> // for pow function
using namespace std;
int max_reachable_index( int N, int B) {
int max_index = 0;
for ( int i = 0; i < N; i++) {
if (i != B) {
max_index += pow (2, N-i-1);
}
}
return max_index;
}
int main() {
int N = 3;
int B = 2;
cout << max_reachable_index(N, B) << endl;
return 0;
}
|
Java
import java.lang.Math;
public class Main {
public static int max_reachable_index( int N, int B) {
int max_index = 0 ;
for ( int i = 0 ; i < N; i++) {
if (i != B) {
max_index += Math.pow( 2 , N-i- 1 );
}
}
return max_index;
}
public static void main(String[] args) {
int N = 3 ;
int B = 2 ;
System.out.println(max_reachable_index(N, B));
}
}
|
C#
using System;
public class Program {
public static int max_reachable_index( int N, int B) {
int max_index = 0;
for ( int i = 0; i < N; i++) {
if (i != B) {
max_index += ( int )Math.Pow(2, N-i-1);
}
}
return max_index;
}
public static void Main() {
int N = 3;
int B = 2;
Console.WriteLine(max_reachable_index(N, B));
}
}
|
Python
import math
def max_reachable_index(N, B):
max_index = 0
for i in range (N):
if i ! = B:
max_index + = math. pow ( 2 , N - i - 1 )
return int (max_index)
N = 3
B = 2
print (max_reachable_index(N, B))
|
Javascript
function max_reachable_index(N, B) {
let max_index = 0;
for (let i = 0; i < N; i++) {
if (i !== B) {
max_index += Math.pow(2, N - i - 1);
}
}
return max_index;
}
const N = 3;
const B = 2;
console.log(max_reachable_index(N, B));
|
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach:
Calculate the maximum index that can be reached within the given steps. If the 0th 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 the 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 the Current Index.
- If the Current Index reaches 0 successfully, print the current value of the maximum index as the 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++
#include <bits/stdc++.h>
using namespace std;
void maximumIndex( int N, int B)
{
int max_index = 0;
for ( int i = 1; i <= N; i++) {
max_index += i;
}
int current_index = max_index, step = N;
while (1) {
while (current_index > 0 && N > 0) {
current_index -= N;
if (current_index == B) {
current_index += N;
}
N--;
}
if (current_index <= 0) {
cout << max_index << endl;
break ;
}
else {
N = step;
current_index = max_index - 1;
max_index--;
if (current_index == B) {
current_index = max_index - 1;
max_index--;
}
}
}
}
int main()
{
int N = 3, B = 2;
maximumIndex(N, B);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void maximumIndex( int N,
int B)
{
int max_index = 0 ;
for ( int i = 1 ; i <= N; i++)
{
max_index += i;
}
int current_index = max_index,
step = N;
while ( true )
{
while (current_index > 0 &&
N > 0 )
{
current_index -= N;
if (current_index == B)
{
current_index += N;
}
N--;
}
if (current_index <= 0 )
{
System.out.print(max_index + "\n" );
break ;
}
else
{
N = step;
current_index = max_index - 1 ;
max_index--;
if (current_index == B)
{
current_index = max_index - 1 ;
max_index--;
}
}
}
}
public static void main(String[] args)
{
int N = 3 , B = 2 ;
maximumIndex(N, B);
}
}
|
Python3
def maximumIndex(N, B):
max_index = 0
for i in range ( 1 , N + 1 ):
max_index + = i
current_index = max_index
step = N
while ( 1 ):
while (current_index > 0 and N > 0 ):
current_index - = N
if (current_index = = B):
current_index + = N
N - = 1
if (current_index < = 0 ):
print (max_index)
break
else :
N = step
current_index = max_index - 1
max_index - = 1
if (current_index = = B):
current_index = max_index - 1
max_index - = 1
if __name__ = = '__main__' :
N = 3
B = 2
maximumIndex(N, B)
|
C#
using System;
class GFG{
static void maximumIndex( int N,
int B)
{
int max_index = 0;
for ( int i = 1; i <= N; i++)
{
max_index += i;
}
int current_index = max_index,
step = N;
while ( true )
{
while (current_index > 0 &&
N > 0)
{
current_index -= N;
if (current_index == B)
{
current_index += N;
}
N--;
}
if (current_index <= 0)
{
Console.Write(max_index + " " );
break ;
}
else
{
N = step;
current_index = max_index - 1;
max_index--;
if (current_index == B)
{
current_index = max_index - 1;
max_index--;
}
}
}
}
public static void Main (String[] args)
{
int N = 3, B = 2;
maximumIndex(N, B);
}
}
|
Javascript
<script>
function maximumIndex( N, B)
{
var max_index = 0;
for ( var i = 1; i <= N; i++) {
max_index += i;
}
var current_index = max_index, step = N;
while (1) {
while (current_index > 0 && N > 0) {
current_index -= N;
if (current_index == B) {
current_index += N;
}
N--;
}
if (current_index <= 0) {
document.write(max_index + "<br>" );;
break ;
}
else {
N = step;
current_index = max_index - 1;
max_index--;
if (current_index == B) {
current_index = max_index - 1;
max_index--;
}
}
}
}
var N = 3, B = 2;
maximumIndex(N, B);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)