Kth odd number in an array
Given an array arr[] and an integer K, the task is to find the Kth odd element from the given array.
Examples:
Input: arr[] = {1, 2, 3, 4, 5}, K = 2
Output: 3
3 is the 2nd odd element from the given arrayInput: arr[] = {2, 4, 6, 18}, K = 5
Output: -1
There are no odd elements in the given array.
Approach: Traverse the array element by element and for every odd element encountered, decrement the value k by 1. If the value of k becomes equal to 0 then print the current element. Else after the traversal of the complete array, if the value of k is > 0 then print -1 as the total number of odd elements in the array are < k.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <iostream> using namespace std; // Function to return the kth odd element // from the array int kthOdd(int arr[], int n, int k) { // Traverse the array for (int i = 0; i <= n; i++) { // If current element is odd if ((arr[i] % 2) == 1) k--; // If kth odd element is found if (k == 0) return arr[i]; } // Total odd elements in the array are < k return -1; } // Driver code int main() { int arr[] = { 1, 2, 3, 4, 5 }; int n = sizeof(arr)/sizeof(arr[0]); int k = 2; cout << (kthOdd(arr, n, k)); return 0; } // This code is contributed by jit_t |
Java
// Java implementation of the approach public class GFG { // Function to return the kth odd element // from the array static int kthOdd(int arr[], int n, int k) { // Traverse the array for (int i = 0; i < n; i++) { // If current element is odd if (arr[i] % 2 == 1) k--; // If kth odd element is found if (k == 0) return arr[i]; } // Total odd elements in the array are < k return -1; } // Driver code public static void main(String args[]) { int arr[] = { 1, 2, 3, 4, 5 }; int n = arr.length; int k = 2; System.out.print(kthOdd(arr, n, k)); } } |
Python3
# Python3 implementation of the approach
# Function to return the kth odd
# element from the array
def kthOdd (arr, n, k):
# Traverse the array
for i in range(n):
# If current element is odd
if (arr[i] % 2 == 1):
k -= 1;
# If kth odd element is found
if (k == 0):
return arr[i];
# Total odd elements in the
# array are < k
return -1;
# Driver code
arr = [ 1, 2, 3, 4, 5 ];
n = len(arr);
k = 2;
print(kthOdd(arr, n, k));
# This code is contributed by mits
[tabby title ="C#"]
// C# implementation of the approach using System; class GFG { // Function to return the kth odd element // from the array static int kthOdd(int []arr, int n, int k) { // Traverse the array for (int i = 0; i < n; i++) { // If current element is odd if (arr[i] % 2 == 1) k--; // If kth odd element is found if (k == 0) return arr[i]; } // Total odd elements in the array are < k return -1; } // Driver code public static void Main() { int []arr = { 1, 2, 3, 4, 5 }; int n = arr.Length; int k = 2; Console.WriteLine(kthOdd(arr, n, k)); } } // This code is contributed by SoM15242 |
PHP
<?php // PHP implementation of the approach // Function to return the kth odd // element from the array function kthOdd ($arr, $n, $k) { // Traverse the array for ($i = 0; $i < $n; $i++) { // If current element is odd if ($arr[$i] % 2 == 1) $k--; // If kth odd element is found if ($k == 0) return $arr[$i]; } // Total odd elements in the // array are < k return -1; } // Driver code $arr = array( 1, 2, 3, 4, 5 ); $n = sizeof($arr); $k = 2; echo (kthOdd($arr, $n, $k)); // This code is contributed by ajit.. ?> |
3
Recommended Posts:
- Count number of permutation of an Array having no SubArray of size two or more from original Array
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- Rearrange the array to maximize the number of primes in prefix sum of the array
- Count the number of primes in the prefix sum array of the given array
- Insert minimum number in array so that sum of array becomes prime
- Maximum number formed from array with K number of adjacent swaps allowed
- Find a number which give minimum sum when XOR with every number of array of integers
- Maximum number of contiguous array elements with same number of set bits
- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Number of anomalies in an array
- Sum of every K’th prime number in an array
- Add minimum number to an array so that the sum becomes even
- Sum of every K'th prime number in an array
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.



