Find Kth element in an array containing odd elements first and then even elements

Given the length of an array of integers N and an integer K. The task is to modify the array in such a way that the array contains first all odd integers from 1 to N in ascending order, then all even integers from 1 to N in ascending order and then print the Kth element in the modified array.

Examples:

Input: N = 8, K = 5
Output: 2
The array will be {1, 3, 5, 7, 2, 4, 6, 8}
and the fifth element is 2.



Input: N = 7, K = 2
Output: 3

Naive approach: A simple approach is to store the odd numbers first, one by one till N and then storing the even numbers one by one till N, and then printing the kth element.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the kth element
// in the modified array
int getNumber(int n, int k)
{
    int arr[n];
  
    int i = 0;
  
    // First odd number
    int odd = 1;
    while (odd <= n) {
  
        // Insert the odd number
        arr[i++] = odd;
  
        // Next odd number
        odd += 2;
    }
  
    // First even number
    int even = 2;
    while (even <= n) {
  
        // Insert the even number
        arr[i++] = even;
  
        // Next even number
        even += 2;
    }
  
    // Return the kth element
    return arr[k - 1];
}
  
// Driver code
int main()
{
    int n = 8, k = 5;
    cout << getNumber(n, k);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG 
{
  
// Function to return the kth element
// in the modified array
static int getNumber(int n, int k)
{
    int []arr = new int[n];
  
    int i = 0;
  
    // First odd number
    int odd = 1;
    while (odd <= n)
    {
  
        // Insert the odd number
        arr[i++] = odd;
  
        // Next odd number
        odd += 2;
    }
  
    // First even number
    int even = 2;
    while (even <= n)
    {
  
        // Insert the even number
        arr[i++] = even;
  
        // Next even number
        even += 2;
    }
  
    // Return the kth element
    return arr[k - 1];
}
  
// Driver code
public static void main(String[] args)
{
    int n = 8, k = 5;
    System.out.println(getNumber(n, k));
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to return the kth element
# in the modified array
def getNumber(n, k):
    arr = [0] * n;
  
    i = 0;
  
    # First odd number
    odd = 1;
    while (odd <= n):
          
        # Insert the odd number
        arr[i] = odd;
        i += 1;
  
        # Next odd number
        odd += 2;
  
    # First even number
    even = 2;
    while (even <= n):
        # Insert the even number
        arr[i] = even;
        i += 1;
  
        # Next even number
        even += 2;
  
    # Return the kth element
    return arr[k - 1];
  
# Driver code
if __name__ == '__main__':
    n = 8;
    k = 5;
    print(getNumber(n, k));
  
# This code is contributed by Rajput-Ji

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
      
class GFG 
{
  
// Function to return the kth element
// in the modified array
static int getNumber(int n, int k)
{
    int []arr = new int[n];
  
    int i = 0;
  
    // First odd number
    int odd = 1;
    while (odd <= n)
    {
  
        // Insert the odd number
        arr[i++] = odd;
  
        // Next odd number
        odd += 2;
    }
  
    // First even number
    int even = 2;
    while (even <= n)
    {
  
        // Insert the even number
        arr[i++] = even;
  
        // Next even number
        even += 2;
    }
  
    // Return the kth element
    return arr[k - 1];
}
  
// Driver code
public static void Main(String[] args)
{
    int n = 8, k = 5;
    Console.WriteLine(getNumber(n, k));
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Output:

2

Efficient approach: Find the index where the first even element will be stored in the generated array. Now if the value of k is less then or equal to index then the desired number will be k * 2 – 1 else the desired number will be (k – index) * 2

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the kth element
// in the modified array
int getNumber(int n, int k)
{
    int pos;
  
    // Finding the index from where the
    // even numbers will be stored
    if (n % 2 == 0) {
        pos = n / 2;
    }
    else {
        pos = (n / 2) + 1;
    }
  
    // Return the kth element
    if (k <= pos) {
        return (k * 2 - 1);
    }
    else
  
        return ((k - pos) * 2);
}
  
// Driver code
int main()
{
    int n = 8, k = 5;
  
    cout << getNumber(n, k);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.io.*;
  
class GFG
{
      
// Function to return the kth element
// in the modified array
static int getNumber(int n, int k)
{
    int pos;
  
    // Finding the index from where the
    // even numbers will be stored
    if ((n % 2) == 0
    {
        pos = n / 2;
    }
    else 
    {
        pos = (n / 2) + 1;
    }
  
    // Return the kth element
    if (k <= pos)
    {
        return (k * 2 - 1);
    }
    else
        return ((k - pos) * 2);
}
  
// Driver code
public static void main (String[] args)
{
    int n = 8, k = 5;
    System.out.println (getNumber(n, k));
}
}
  
// This code is contributed by @tushil. 

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function to return the kth element 
# in the modified array 
def getNumber(n, k) : 
  
    # Finding the index from where the 
    # even numbers will be stored 
    if (n % 2 == 0) : 
        pos = n // 2
      
    else :
        pos = (n // 2) + 1
  
    # Return the kth element 
    if (k <= pos) :
        return (k * 2 - 1); 
          
    else :
        return ((k - pos) * 2); 
  
# Driver code 
if __name__ == "__main__" :
    n = 8; k = 5;
      
    print(getNumber(n, k)); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
          
// Function to return the kth element
// in the modified array
static int getNumber(int n, int k)
{
    int pos;
  
    // Finding the index from where the
    // even numbers will be stored
    if ((n % 2) == 0) 
    {
        pos = n / 2;
    }
    else
    {
        pos = (n / 2) + 1;
    }
  
    // Return the kth element
    if (k <= pos)
    {
        return (k * 2 - 1);
    }
    else
        return ((k - pos) * 2);
}
  
// Driver code
static public void Main ()
{
    int n = 8, k = 5;
    Console.Write(getNumber(n, k));
}
}
  
// This code is contributed by @ajit.

chevron_right


Output:

2


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.





Article Tags :
Practice Tags :


1


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