Comprueba si el número es par o impar cuyos dígitos y base (base) se dan

Dada una array arr[] de tamaño N que representa los dígitos de un número y un entero r que es la base (base) del número dado, es decir , n = arr[n – 1] * r 0 + arr[n – 2] * r 1 + … + a[0] * r norte – 1 . La tarea es encontrar si el número dado es par o impar.
Ejemplos: 
 

Entrada: arr[] = {1, 0}, r = 2 
Salida: Par 
(10) 2 = (2) 10
Entrada: arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}, r = 10 
Salida: Impar 
 

Enfoque ingenuo: el enfoque más simple es calcular el número n multiplicando los dígitos con la potencia de base correspondiente. Pero como el número de dígitos puede ser del orden de 10 5 , este enfoque no funcionará para un n grande.
Enfoque eficiente: Hay dos casos posibles. 
 

  1. Si r es par , la respuesta final depende del último dígito, es decir , arr[n – 1] .
  2. Si r es impar , entonces tenemos que contar el número de dígitos impares. Si el número de dígitos impares es par, entonces la suma es par. De lo contrario, la suma es impar.

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

C++

// C++ implementation of the approach
#include <iostream>
using namespace std;
 
// Function that returns true if the number
// represented by arr[] is even in base r
bool isEven(int arr[], int n, int r)
{
 
    // If the base is even, then
    // the last digit is checked
    if (r % 2 == 0) {
        if (arr[n - 1] % 2 == 0)
            return true;
    }
 
    // If base is odd, then the
    // number of odd digits are checked
    else {
 
        // To store the count of odd digits
        int oddCount = 0;
        for (int i = 0; i < n; ++i) {
            if (arr[i] % 2 != 0)
                oddCount++;
        }
        if (oddCount % 2 == 0)
            return true;
    }
 
    // Number is odd
    return false;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 0 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int r = 2;
 
    if (isEven(arr, n, r))
        cout << "Even";
    else
        cout << "Odd";
 
    return 0;
}

Java

// Java implementation of the approach
import java.io.*;
 
class GFG
{
     
// Function that returns true if the number
// represented by arr[] is even in base r
static boolean isEven(int arr[], int n, int r)
{
 
    // If the base is even, then
    // the last digit is checked
    if (r % 2 == 0)
    {
        if (arr[n - 1] % 2 == 0)
            return true;
    }
 
    // If base is odd, then the
    // number of odd digits are checked
    else {
 
        // To store the count of odd digits
        int oddCount = 0;
        for (int i = 0; i < n; ++i) {
            if (arr[i] % 2 != 0)
                oddCount++;
        }
        if (oddCount % 2 == 0)
            return true;
    }
 
    // Number is odd
    return false;
}
 
// Driver code
public static void main (String[] args)
{
     
    int arr[] = { 1, 0 };
    int n = arr.length;
    int r = 2;
 
    if (isEven(arr, n, r))
 
        System.out.println ("Even");
    else
     
        System.out.println("Odd");
}
}
 
// This code is contributed by jit_t.

Python3

# Python 3 implementation of the approach
 
# Function that returns true if the number
# represented by arr[] is even in base r
def isEven(arr, n, r):
     
    # If the base is even, then
    # the last digit is checked
    if (r % 2 == 0):
        if (arr[n - 1] % 2 == 0):
            return True
 
    # If base is odd, then the
    # number of odd digits are checked
    else:
        # To store the count of odd digits
        oddCount = 0
        for i in range(n):
            if (arr[i] % 2 != 0):
                oddCount += 1
        if (oddCount % 2 == 0):
            return True
 
    # Number is odd
    return False
 
# Driver code
if __name__ == '__main__':
    arr = [1, 0]
    n = len(arr)
    r = 2
 
    if (isEven(arr, n, r)):
        print("Even")
    else:
        print("Odd")
 
# This code is contributed by
# Surendra_Gangwar

C#

// C# implementation of the approach
using System;
 
class GFG
{
     
// Function that returns true if the number
// represented by arr[] is even in base r
static bool isEven(int []arr, int n, int r)
{
 
    // If the base is even, then
    // the last digit is checked
    if (r % 2 == 0)
    {
        if (arr[n - 1] % 2 == 0)
            return true;
    }
 
    // If base is odd, then the
    // number of odd digits are checked
    else
    {
 
        // To store the count of odd digits
        int oddCount = 0;
        for (int i = 0; i < n; ++i)
        {
            if (arr[i] % 2 != 0)
                oddCount++;
        }
        if (oddCount % 2 == 0)
            return true;
    }
 
    // Number is odd
    return false;
}
 
// Driver code
public static void Main ()
{
     
    int []arr = { 1, 0 };
    int n = arr.Length;
    int r = 2;
 
    if (isEven(arr, n, r))
 
        Console.WriteLine ("Even");
    else
     
        Console.WriteLine("Odd");
}
}
 
// This code is contributed by anuj_67...

PHP

<?php
 
// PHP implementation of the approach
 
 
// Function that returns true if the number
// represented by arr[] is even in base r
function isEven($arr, $n, $r)
{
 
    // If the base is even, then
    // the last digit is checked
    if ($r % 2 == 0)
    {
        if ($arr[$n - 1] % 2 == 0)
            return true;
    }
 
    // If base is odd, then the
    // number of odd digits are checked
    else
    {
 
        // To store the count of odd digits
        $oddCount = 0;
        for ($i = 0; $i < $n; ++$i)
        {
            if ($arr[$i] % 2 != 0)
                $oddCount++;
        }
        if ($oddCount % 2 == 0)
            return true;
    }
 
    // Number is odd
    return false;
}
 
    // Driver code
    $arr = array( 1, 0 );
    $n = Count($arr);
    $r = 2;
 
    if (isEven($arr, $n, $r))
        echo "Even";
    else
        echo "Odd";
 
// This code is contributed by andrew1234
?>

Javascript

<script>   
    // Javascript implementation of the approach
     
    // Function that returns true if the number
    // represented by arr[] is even in base r
    function isEven(arr, n, r)
    {
 
        // If the base is even, then
        // the last digit is checked
        if (r % 2 == 0)
        {
            if (arr[n - 1] % 2 == 0)
                return true;
        }
 
        // If base is odd, then the
        // number of odd digits are checked
        else
        {
 
            // To store the count of odd digits
            let oddCount = 0;
            for (let i = 0; i < n; ++i)
            {
                if (arr[i] % 2 != 0)
                    oddCount++;
            }
            if (oddCount % 2 == 0)
                return true;
        }
 
        // Number is odd
        return false;
    }
     
    let arr = [ 1, 0 ];
    let n = arr.length;
    let r = 2;
   
    if (isEven(arr, n, r))
   
        document.write("Even");
    else
        document.write("Odd");
 
</script>
Producción: 

Even

 

Complejidad de tiempo: O(n)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por AbhishekSharma32 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 *