HW3A - Editorial

PROBLEM LINK:
Practice
Source

Author: SQU_Test

DIFFICULTY:
EASY

PREREQUISITES:
Basic programming, Observations, Even, Odd.

PROBLEM:
Ali is displeased with the current state of the order of natural numbers (natural number is positive integer number). He is determined to rearrange them. But there are too many natural numbers, so Ali decided to start with the first n. He writes down the following sequence of numbers: firstly all odd integers from 1 to n (in ascending order), then all even integers from 1 to n (also in ascending order). Help our hero to find out which number will stand at the position number k.

EXPLANATION:
In this problem we need to understand how exactly numbers from 1 to n rearrange when we write firstly all odd numbers and after them all even numbers. To find out which number stands at position k one needs to find the position where even numbers start and output either the position of the odd number from the first half of the sequence or for the even number from the second half of the sequence.

TIME COMPLEXITY:
Time complexity of the solution is O(1) as you just need to check k in relation to the start of the even numbers.

SOLUTIONS:

Setter’s Solution
n,k = map(int,input().split())

if n%2 == 0:
if k > n//2:
	print((k-n//2)*2)
else:
	print(k*2-1)
else:
if k > (n+1)//2:
	print((k-n//2-1)*2)
else:
	print(k*2-1)