Related Articles
k-th number in the Odd-Even sequence
• Difficulty Level : Medium
• Last Updated : 20 Nov, 2019

Given two numbers n and k, find the k-th number in the Odd-Even sequence made of n. The Odd-Even sequence contains first contains all odd numbers from 1 to n then all even numbers in set 1 to n.

```Examples :
Input :  n = 5, k = 3
Output : 5
In this example, the Odd-Even  is
{1, 3, 5, 2, 4}.
The third number in sequence is 5.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive Approach :

The first approach is simply make a Odd-Even sequence and then find k-th element in it.

## C++

 `// CPP program to find k-th  ` `// element in the Odd-Even  ` `// sequence. ` `#include ` `using` `namespace` `std; ` ` `  `int` `findK(``int` `n, ``int` `k) ` `{ ` `    ``vector<``long``> a; ` `     `  `    ``// insert all the odd  ` `    ``// numbers from 1 to n.  ` `    ``for` `(``int` `i = 1; i < n; i++)  ` `        ``if` `(i % 2 == 1) ` `            ``a.push_back(i); ` `     `  `    ``// insert all the even  ` `    ``// numbers from 1 to n. ` `    ``for` `(``int` `i = 1; i < n; i++)  ` `        ``if` `(i % 2 == 0) ` `            ``a.push_back(i); ` `     `  `    ``return` `(a[k - 1]); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``long` `n = 10, k = 3; ` `    ``cout << findK(n, k) << endl; ` `    ``return` `0; ` `} `

## Java

 `// Java program to find k-th  ` `// element in the Odd-Even  ` `// sequence.  ` `import` `java.util.*; ` ` `  `class` `GFG{ ` `static` `int` `findK(``int` `n, ``int` `k)  ` `{  ` `    ``ArrayList a = ``new` `ArrayList(n); ` `     `  `    ``// insert all the odd  ` `    ``// numbers from 1 to n.  ` `    ``for` `(``int` `i = ``1``; i < n; i++)  ` `        ``if` `(i % ``2` `== ``1``)  ` `            ``a.add(i);  ` `     `  `    ``// insert all the even  ` `    ``// numbers from 1 to n.  ` `    ``for` `(``int` `i = ``1``; i < n; i++)  ` `        ``if` `(i % ``2` `== ``0``)  ` `            ``a.add(i);  ` `     `  `    ``return` `(a.get(k - ``1``));  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `main(String[] args)  ` `{  ` `    ``int` `n = ``10``, k = ``3``;  ` `    ``System.out.println(findK(n, k));  ` `}  ` `} ` `// This code is contributed by mits `

## Python3

 `# Python3 code to find  ` `# k-th element in the ` `# Odd-Even sequence. ` ` `  `def` `findK (n, k ): ` `    ``a ``=` `list``() ` `     `  `    ``# insert all the odd  ` `    ``# numbers from 1 to n. ` `    ``i ``=` `1` `    ``while` `i < n: ` `        ``a.append(i) ` `        ``i ``=` `i ``+` `2` `     `  `    ``# insert all the even  ` `    ``# numbers from 1 to n. ` `    ``i ``=` `2` `    ``while` `i < n: ` `        ``a.append(i) ` `        ``i ``=` `i ``+` `2` `         `  `    ``return` `(a[k ``-` `1``]) ` ` `  `# Driver code ` `n ``=` `10` `k ``=` `3` `print``(findK(n, k)) ` ` `  `# This code is contributed  ` `# by "Sharad_Bhardwaj". `

## C#

 `// C# program to find k-th  ` `// element in the Odd-Even  ` `// sequence.  ` `using` `System;  ` `using` `System.Collections;  ` ` `  `class` `GFG{ ` `static` `int` `findK(``int` `n, ``int` `k)  ` `{  ` `    ``ArrayList a = ``new` `ArrayList(n); ` `     `  `    ``// insert all the odd  ` `    ``// numbers from 1 to n.  ` `    ``for` `(``int` `i = 1; i < n; i++)  ` `        ``if` `(i % 2 == 1)  ` `            ``a.Add(i);  ` `     `  `    ``// insert all the even  ` `    ``// numbers from 1 to n.  ` `    ``for` `(``int` `i = 1; i < n; i++)  ` `        ``if` `(i % 2 == 0)  ` `            ``a.Add(i);  ` `     `  `    ``return` `(``int``)(a[k - 1]);  ` `}  ` ` `  `// Driver code  ` `static` `void` `Main()  ` `{  ` `    ``int` `n = 10, k = 3;  ` `    ``Console.WriteLine(findK(n, k));  ` `}  ` `} ` `// This code is contributed by mits `

## PHP

 ` `

Output :

``` 5
```

The time complexity of the above approach is O(n), for an O(1) approach read the approach discussed here.

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :