New position
You are given two integers n and k. Assume numbers from 1 to n are arranged in a way that - all odd numbers (in ascending order) comes first and then comes to all even numbers (also in ascending order).
You need to find and return the integer which is at position k (numbering of positions start from 1).
Input Format :
Output Format :
Constraints :
1 <= k <= n <= 10^12
Sample Input:
Sample Output:
Sample Output Explanation :
Numbers from 1 to 7 are arranged as - (1, 3, 5, 7, 2, 4, 6) and number at position 4 is 7. So, the output is 7.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solve(n,k): | |
pos=n//2 | |
if n%2!=0: | |
pos=pos+1 | |
if k<=pos: | |
return 1+(k-1)*2 | |
else: | |
k=k-pos | |
return 2+(k-1)*2 | |
n=int(input()) | |
k=int(input()) | |
print(solve(n,k)) |
Comments
Post a Comment
Please give us your valuable feedback