Find Kth element in an array containing odd elements first and then even elements
Last Updated :
14 Jun, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int getNumber( int n, int k)
{
int arr[n];
int i = 0;
int odd = 1;
while (odd <= n) {
arr[i++] = odd;
odd += 2;
}
int even = 2;
while (even <= n) {
arr[i++] = even;
even += 2;
}
return arr[k - 1];
}
int main()
{
int n = 8, k = 5;
cout << getNumber(n, k);
return 0;
}
|
Java
class GFG
{
static int getNumber( int n, int k)
{
int []arr = new int [n];
int i = 0 ;
int odd = 1 ;
while (odd <= n)
{
arr[i++] = odd;
odd += 2 ;
}
int even = 2 ;
while (even <= n)
{
arr[i++] = even;
even += 2 ;
}
return arr[k - 1 ];
}
public static void main(String[] args)
{
int n = 8 , k = 5 ;
System.out.println(getNumber(n, k));
}
}
|
Python3
def getNumber(n, k):
arr = [ 0 ] * n;
i = 0 ;
odd = 1 ;
while (odd < = n):
arr[i] = odd;
i + = 1 ;
odd + = 2 ;
even = 2 ;
while (even < = n):
arr[i] = even;
i + = 1 ;
even + = 2 ;
return arr[k - 1 ];
if __name__ = = '__main__' :
n = 8 ;
k = 5 ;
print (getNumber(n, k));
|
C#
using System;
class GFG
{
static int getNumber( int n, int k)
{
int []arr = new int [n];
int i = 0;
int odd = 1;
while (odd <= n)
{
arr[i++] = odd;
odd += 2;
}
int even = 2;
while (even <= n)
{
arr[i++] = even;
even += 2;
}
return arr[k - 1];
}
public static void Main(String[] args)
{
int n = 8, k = 5;
Console.WriteLine(getNumber(n, k));
}
}
|
Javascript
<script>
function getNumber(n, k)
{
var arr = Array(n).fill(n);
var i = 0;
var odd = 1;
while (odd <= n) {
arr[i++] = odd;
odd += 2;
}
var even = 2;
while (even <= n) {
arr[i++] = even;
even += 2;
}
return arr[k - 1];
}
var n = 8, k = 5;
document.write(getNumber(n, k));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n), as extra space of size n is used
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++
#include <bits/stdc++.h>
using namespace std;
int getNumber( int n, int k)
{
int pos;
if (n % 2 == 0) {
pos = n / 2;
}
else {
pos = (n / 2) + 1;
}
if (k <= pos) {
return (k * 2 - 1);
}
else
return ((k - pos) * 2);
}
int main()
{
int n = 8, k = 5;
cout << getNumber(n, k);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int getNumber( int n, int k)
{
int pos;
if ((n % 2 ) == 0 )
{
pos = n / 2 ;
}
else
{
pos = (n / 2 ) + 1 ;
}
if (k <= pos)
{
return (k * 2 - 1 );
}
else
return ((k - pos) * 2 );
}
public static void main (String[] args)
{
int n = 8 , k = 5 ;
System.out.println (getNumber(n, k));
}
}
|
Python3
def getNumber(n, k) :
if (n % 2 = = 0 ) :
pos = n / / 2 ;
else :
pos = (n / / 2 ) + 1 ;
if (k < = pos) :
return (k * 2 - 1 );
else :
return ((k - pos) * 2 );
if __name__ = = "__main__" :
n = 8 ; k = 5 ;
print (getNumber(n, k));
|
C#
using System;
class GFG
{
static int getNumber( int n, int k)
{
int pos;
if ((n % 2) == 0)
{
pos = n / 2;
}
else
{
pos = (n / 2) + 1;
}
if (k <= pos)
{
return (k * 2 - 1);
}
else
return ((k - pos) * 2);
}
static public void Main ()
{
int n = 8, k = 5;
Console.Write(getNumber(n, k));
}
}
|
Javascript
<script>
function getNumber(n, k)
{
let pos;
if ((n % 2) == 0)
{
pos = parseInt(n / 2, 10);
}
else
{
pos = parseInt(n / 2, 10) + 1;
}
if (k <= pos)
{
return (k * 2 - 1);
}
else
return ((k - pos) * 2);
}
let n = 8, k = 5;
document.write(getNumber(n, k));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...