Given two integers **N** and **K**, the task is to find the element at the **K ^{th}** 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.

**Examples:**

Input:N = 10, K = 3

Output:5

The required sequence is 1, 3, 5, 7, 9, 2, 4, 6, 8 and 10.

Input:N = 7, K = 7

Output:6

**Approach:** It is known that the **N ^{th}** even number is given by

**2 * K**and the

**N**odd number is given by

^{th}**2 * K – 1**. But since the even numbers are written after

**(N + 1) / 2**odd numbers here. Therfore,

**K**even number is given by

^{th}**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:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the kth number ` `// from the required sequence ` `int` `kthNum(` `int` `n, ` `int` `k) ` `{ ` ` ` ` ` `// Count of odd integers ` ` ` `// in the sequence ` ` ` `int` `a = (n + 1) / 2; ` ` ` ` ` `// kth number is even ` ` ` `if` `(k > a) ` ` ` `return` `(2 * (k - a)); ` ` ` ` ` `// It is odd ` ` ` `return` `(2 * k - 1); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 7, k = 7; ` ` ` ` ` `cout << kthNum(n, k); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the kth number ` `// from the required sequence ` `static` `int` `kthNum(` `int` `n, ` `int` `k) ` `{ ` ` ` ` ` `// Count of odd integers ` ` ` `// in the sequence ` ` ` `int` `a = (n + ` `1` `) / ` `2` `; ` ` ` ` ` `// kth number is even ` ` ` `if` `(k > a) ` ` ` `return` `(` `2` `* (k - a)); ` ` ` ` ` `// It is odd ` ` ` `return` `(` `2` `* k - ` `1` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String []args) ` `{ ` ` ` `int` `n = ` `7` `, k = ` `7` `; ` ` ` ` ` `System.out.println(kthNum(n, k)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the kth number ` `# from the required sequence ` `def` `kthNum(n, k) : ` ` ` ` ` `# Count of odd integers ` ` ` `# in the sequence ` ` ` `a ` `=` `(n ` `+` `1` `) ` `/` `/` `2` `; ` ` ` ` ` `# kth number is even ` ` ` `if` `(k > a) : ` ` ` `return` `(` `2` `*` `(k ` `-` `a)); ` ` ` ` ` `# It is odd ` ` ` `return` `(` `2` `*` `k ` `-` `1` `); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `7` `; k ` `=` `7` `; ` ` ` ` ` `print` `(kthNum(n, k)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the kth number ` `// from the required sequence ` `static` `int` `kthNum(` `int` `n, ` `int` `k) ` `{ ` ` ` ` ` `// Count of odd integers ` ` ` `// in the sequence ` ` ` `int` `a = (n + 1) / 2; ` ` ` ` ` `// kth number is even ` ` ` `if` `(k > a) ` ` ` `return` `(2 * (k - a)); ` ` ` ` ` `// It is odd ` ` ` `return` `(2 * k - 1); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` ` ` `int` `n = 7, k = 7; ` ` ` ` ` `Console.WriteLine(kthNum(n, k)); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

6

**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.

## Recommended Posts:

- 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
- Print X array elements closest to the Kth smallest element in the array
- Kth smallest element from an array of intervals
- Split array into equal length subsets with maximum sum of Kth largest element of each subset
- Farthest index that can be reached from the Kth index of given array by given operations
- 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
- Find position of the given number among the numbers made of 4 and 7

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.