Maximum index a pointer can reach in N steps by avoiding a given index B | Set 2 | Function to find the maximum index the pointer can reach ; Initialize two pointers ; Stores number of steps ; Stores sum of first N natural numbers ; Increment i with j ; Increment j with 1 ; Increment count ; If i points to B ; Break ; Print the pointer index ; Driver Code ; Given value of N & B ; Function call to find maximum index the pointer can reach
def maximumIndex ( N , B ) : NEW_LINE INDENT i , j = 0 , 1 NEW_LINE cnt = 0 NEW_LINE sum = N * ( N + 1 ) // 2 NEW_LINE flag = False NEW_LINE while ( cnt < N ) : NEW_LINE INDENT i += j NEW_LINE j += 1 NEW_LINE cnt += 1 NEW_LINE if ( i == B ) : NEW_LINE INDENT flag = True NEW_LINE break NEW_LINE DEDENT DEDENT if ( not flag ) : NEW_LINE INDENT print ( sum ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( sum - 1 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N , B = 4 , 6 NEW_LINE maximumIndex ( N , B ) NEW_LINE DEDENT