Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Difficulty Level : Basic
  • Last Updated : 13 May, 2021

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:
The array will be {1, 3, 5, 7, 2, 4, 6, 8} 
and the fifth element is 2.
Input: N = 7, K = 2 
Output:
 

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

 



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




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

Java




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

Python3




# 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

C#




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

Javascript




<script>
 
// C++ implementation of the approach
 
// Function to return the kth element
// in the modified array
function getNumber(n, k)
{
    var arr = Array(n).fill(n);
 
    var i = 0;
 
    // First odd number
    var odd = 1;
    while (odd <= n) {
 
        // Insert the odd number
        arr[i++] = odd;
 
        // Next odd number
        odd += 2;
    }
 
    // First even number
    var 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
    var n = 8, k = 5;
    document.write(getNumber(n, k));
 
</script>
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 than or equal to the 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++




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

Java




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

Python3




# 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

C#




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

Javascript




<script>
 
    // Javascript implementation of the approach
     
    // Function to return the kth element
    // in the modified array
    function getNumber(n, k)
    {
        let pos;
 
        // Finding the index from where the
        // even numbers will be stored
        if ((n % 2) == 0)
        {
            pos = parseInt(n / 2, 10);
        }
        else
        {
            pos = parseInt(n / 2, 10) + 1;
        }
 
        // Return the kth element
        if (k <= pos)
        {
            return (k * 2 - 1);
        }
        else
            return ((k - pos) * 2);
    }
     
    let n = 8, k = 5;
    document.write(getNumber(n, k));
 
</script>
Output: 
2

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!