# 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 array

Input: arr[] = {2, 4, 6, 18}, K = 5
Output: -1
There are no odd elements in the given array.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 ` `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); ` `    ``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 `

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

 ` `

Output:

```3
```

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.