k-th number in the Odd-Even sequence

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.

Naive Approach :

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

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find k-th 
// element in the Odd-Even 
// sequence.
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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<Integer> a = new ArrayList<Integer>(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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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".

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find k-th 
// element in the Odd-Even 
// sequence.
  
function findK($n, $k)
{
    $a;
    $index = 0;
      
    // insert all the odd 
    // numbers from 1 to n. 
    for ($i = 1; $i < $n; $i++) 
        if ($i % 2 == 1)
            $a[$index++] = $i;
      
    // insert all the even 
    // numbers from 1 to n.
    for ($i = 1; $i < $n; $i++) 
        if ($i % 2 == 0)
            $a[$index++] = $i;
      
    return ($a[$k - 1]);
}
  
// Driver code
$n = 10;
$k = 3;
echo findK($n, $k);
  
// This code is contributed by mits.
?>

chevron_right



Output :

 5

Time Complexity : O(n).

Efficient Approach :
step 1:- calculate half = (n+1)/2
step 2:- check if k is greater than half, the output will be 2 * (k – half) otherwise the output will be 2 * (k – 1)

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find k-th element in the
// Odd-Even sequence.
#include <bits/stdc++.h>
using namespace std;
  
int findK(int n, int k)
{
    int half = (n + 1) / 2;
    if (k > n)
        return (2 * (k - half));
    else
        return (2 * k - 1);
}
  
// Driver code
int main()
{
    long n = 10, k = 3;
    cout << findK(n, k) << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find k-th element in the
// Odd-Even sequence.
  
import java.io.*;
  
class GFG {
  
    static int findK(int n, int k)
    {
          
        int half = (n + 1) / 2;
        if (k > n)
            return (2 * (k - half));
        else
            return (2 * 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 Ajit.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to find k-th element 
# in the Odd-Even sequence.
  
def findK(n, k):
    half = int((n + 1) / 2)
    if k > n:
        return (2 * (k - half))
    else:
        return (2 * k - 1)
          
# Driver code
n = 10
k = 3
print(findK(n, k))
  
# This code is contributed
# by "Sharad_Bhardwaj".

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find k-th element in the
// Odd-Even sequence.
using System;
  
class GFG {
  
    static int findK(int n, int k)
    {
          
        int half = (n + 1) / 2;
        if (k > n)
            return (2 * (k - half));
        else
            return (2 * k - 1);
    }
  
    // Driver code
    public static void Main()
    {
          
        int n = 10, k = 3;
          
        Console.WriteLine(findK(n, k));
          
  
    }
}
  
// This code is contributed by anuj_67.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find 
// k-th element in the
// Odd-Even sequence.
  
// function returns the
// k-th element in the
// Odd-Even sequence.
function findK($n, $k)
{
    $half = ($n + 1) / 2;
    if ($k > $n)
        return (2 * ($k - $half));
    else
        return (2 * $k - 1);
}
  
    // Driver code
    $n = 10;
    $k = 3;
    echo findK($n, $k);
      
// This code is contributed by anuj_67.
?>

chevron_right



Output :

5

Time Complexity : O(1).



My Personal Notes arrow_drop_up

Intern at GeeksforGeeks

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.



Improved By : vt_m, Mithun Kumar



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.