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.



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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.



Improved By : SoM15242, jit_t, Mithun Kumar