Given two integers N and K, the task is to find the element at the Kth position if all odd numbers from 1 to N are written down in increasing order followed by all the even numbers from 1 to N in increasing order.
Input: N = 10, K = 3
The required sequence is 1, 3, 5, 7, 9, 2, 4, 6, 8 and 10.
Input: N = 7, K = 7
Approach: It is known that the Nth even number is given by 2 * K and the Nth odd number is given by 2 * K – 1. But since the even numbers are written after (N + 1) / 2 odd numbers here. Therfore, Kth even number is given by 2 * (K – (N + 1) / 2) and the odd numbers will remain the same as 2 * K – 1
Below is the implementation of the above approach:
Time Complexity: 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.
- Find element position in given monotonic sequence
- Find the value at kth position in the generated array
- Multiplication table till N rows where every Kth row is table of K upto Kth term
- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Set 2
- Find the Kth smallest element in the sorted generated array
- Kth Smallest Element of a Matrix of given dimensions filled with product of indices
- Find the position of the last removed element from the array
- Convert an unbalanced bracket sequence to a balanced sequence
- Find the Kth node in the DFS traversal of a given subtree in a Tree
- Kth array element after M replacements of array elements by XOR of adjacent pairs
- Find position of the given number among the numbers made of 4 and 7
- Find the position of box which occupies the given ball
- Find the position of the given Prime Number
- Find position of given term in a series formed with only digits 4 and 7 allowed
- Count Derangements (Permutation such that no element appears in its original position)
- Shuffle the position of each Array element by swapping adjacent elements
- Count of right shifts for each array element to be in its sorted position
- Find kth smallest number in range [1, n] when all the odd numbers are deleted
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.