K-ésimo número impar en una array

Dada una array arr[] y un entero K , la tarea es encontrar el K -ésimo elemento impar de la array dada.

Ejemplos: 

Entrada: arr[] = {1, 2, 3, 4, 5}, K = 2 
Salida:
3 es el segundo elemento impar de la array dada

Entrada: arr[] = {2, 4, 6, 18}, K = 5 
Salida: -1 
No hay elementos extraños en la array dada. 

Enfoque: recorra la array elemento por elemento y, por cada elemento impar que encuentre, disminuya el valor k en 1 . Si el valor de k se vuelve igual a 0 , imprima el elemento actual. De lo contrario, después del recorrido de la array completa, si el valor de k es > 0 , imprima -1 como el número total de elementos impares en la array es < k .

A continuación se muestra la implementación del enfoque anterior: 

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

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

Javascript

<script>
 
// JavaScript 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
     
        var arr = [ 1, 2, 3, 4, 5 ];
        var n = arr.length;
        var k = 2;
        document.write(kthOdd(arr, n, k));
 
// This code contributed by Rajput-Ji
 
</script>
Producción

3

Complejidad temporal: O(n)
Espacio auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por arora_yash y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *