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++
// 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 |
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++
// 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. |
2
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.
Recommended Posts:
- Sort all even numbers in ascending order and then sort all odd numbers in descending order
- Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K
- Sort even-placed elements in increasing and odd-placed in decreasing order
- Insertion sort to sort even and odd positioned elements in different orders
- Sort even and odd placed elements in increasing order
- Delete odd and even numbers at alternate step such that sum of remaining elements is minimized
- Kth smallest or largest element in unsorted Array | Set 4
- Sort an array containing two types of elements
- Minimize difference after changing all odd elements to even
- Rearrange array such that even positioned are greater than odd
- Segregate even and odd numbers | Set 3
- Rearrange Odd and Even values in Alternate Fashion in Ascending Order
- Find the Kth pair in ordered list of all possible sorted pairs of the Array
- Kth Smallest Element of a Matrix of given dimensions filled with product of indices
- Number of subsequences of maximum length K containing no repeated elements
- Longest subsequence with first and last element greater than all other elements
- Odd-Even Sort / Brick Sort
- Check whether a given number is even or odd
- C/C++ Program for Odd-Even Sort / Brick Sort
- Java Program for Odd-Even Sort / Brick Sort
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.

