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 is < k.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int kthOdd( int arr[], int n, int k)
{
for ( int i = 0; i <= n; i++) {
if ((arr[i] % 2) == 1)
k--;
if (k == 0)
return arr[i];
}
return -1;
}
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;
}
|
Java
public class GFG {
static int kthOdd( int arr[], int n, int k)
{
for ( int i = 0 ; i < n; i++) {
if (arr[i] % 2 == 1 )
k--;
if (k == 0 )
return arr[i];
}
return - 1 ;
}
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
def kthOdd (arr, n, k):
for i in range (n):
if (arr[i] % 2 = = 1 ):
k - = 1 ;
if (k = = 0 ):
return arr[i];
return - 1 ;
arr = [ 1 , 2 , 3 , 4 , 5 ];
n = len (arr);
k = 2 ;
print (kthOdd(arr, n, k));
|
C#
using System;
class GFG
{
static int kthOdd( int []arr, int n, int k)
{
for ( int i = 0; i < n; i++)
{
if (arr[i] % 2 == 1)
k--;
if (k == 0)
return arr[i];
}
return -1;
}
public static void Main()
{
int []arr = { 1, 2, 3, 4, 5 };
int n = arr.Length;
int k = 2;
Console.WriteLine(kthOdd(arr, n, k));
}
}
|
Javascript
<script>
function kthOdd(arr , n , k) {
for (i = 0; i < n; i++) {
if (arr[i] % 2 == 1)
k--;
if (k == 0)
return arr[i];
}
return -1;
}
var arr = [ 1, 2, 3, 4, 5 ];
var n = arr.length;
var k = 2;
document.write(kthOdd(arr, n, k));
</script>
|
PHP
<?php
function kthOdd ( $arr , $n , $k )
{
for ( $i = 0; $i < $n ; $i ++)
{
if ( $arr [ $i ] % 2 == 1)
$k --;
if ( $k == 0)
return $arr [ $i ];
}
return -1;
}
$arr = array ( 1, 2, 3, 4, 5 );
$n = sizeof( $arr );
$k = 2;
echo (kthOdd( $arr , $n , $k ));
?>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Approach: Iterating the array and counting odd elements
Below is the implementation of the above approach:
C++
#include <iostream>
#include <vector>
int findKthOddElement(std::vector< int >& array, int K) {
int count = 0;
for ( int element : array) {
if (element % 2 != 0) {
count++;
if (count == K) {
return element;
}
}
}
return -1;
}
int main() {
std::vector< int > array = {1, 2, 3, 4, 5};
int K = 2;
int result = findKthOddElement(array, K);
std::cout << result << std::endl;
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
public class Main {
public static int findKthOddElement(List<Integer> list, int K) {
int count = 0 ;
for ( int element : list) {
if (element % 2 != 0 ) {
count++;
if (count == K) {
return element;
}
}
}
return - 1 ;
}
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add( 1 );
list.add( 2 );
list.add( 3 );
list.add( 4 );
list.add( 5 );
int K = 2 ;
int result = findKthOddElement(list, K);
System.out.println(result);
}
}
|
Python3
def find_kth_odd_element(array, K):
count = 0
for element in array:
if element % 2 ! = 0 :
count + = 1
if count = = K:
return element
return - 1
array = [ 1 , 2 , 3 , 4 , 5 ]
K = 2
result = find_kth_odd_element(array, K)
print (result)
|
C#
using System;
using System.Collections.Generic;
class Program
{
static int FindKthOddElement(List< int > list, int K)
{
int count = 0;
foreach ( int element in list)
{
if (element % 2 != 0)
{
count++;
if (count == K)
{
return element;
}
}
}
return -1;
}
static void Main()
{
List< int > list = new List< int > { 1, 2, 3, 4, 5 };
int K = 2;
int result = FindKthOddElement(list, K);
if (result != -1)
{
Console.WriteLine( "The " + K + "th odd element is: " + result);
}
else
{
Console.WriteLine( "There are fewer than " + K + " odd elements in the list." );
}
}
}
|
Javascript
function find_kth_odd_element(array, K) {
let count = 0;
for (let element of array) {
if (element % 2 != 0) {
count += 1;
if (count == K) {
return element;
}
}
}
return -1;
}
let array = [1, 2, 3, 4, 5];
let K = 2;
let result = find_kth_odd_element(array, K);
console.log(result);
|
Time Complexity: O(N), where N is the length of the input array.
Auxiliary Space: O(1)
Last Updated :
16 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...