# Kth odd number in an array

• Last Updated : 01 Apr, 2021

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

Attention reader! All those who say programming isn't for kids, just haven't met the right mentors yet. Join the  Demo Class for First Step to Coding Coursespecifically designed for students of class 8 to 12.

The students will get to learn more about the world of programming in these free classes which will definitely help them in making a wise career choice in the future.

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

 ``

## Javascript

 ``
Output:
`3`

My Personal Notes arrow_drop_up