Cuente las rotaciones de N que son pares e impares

Dado un número n , la tarea es contar todas las rotaciones del número dado que son pares e impares. 
Ejemplos: 
 

Input: n = 1234
Output: Odd = 2, Even = 2
Total rotations: 1234, 2341, 3412, 4123
Odd rotations: 2341 and 4123
Even rotations: 1234 and 3412

Input: n = 246
Output: Odd = 0, Even = 3

Enfoque eficiente : para números grandes, es difícil rotar y verificar si es impar o no para cada rotación. Por lo tanto, en este enfoque, verifique el conteo de dígitos impares y dígitos pares presentes en el número. Estos serán la respuesta a este problema.
A continuación se muestra la implementación del enfoque anterior:
Implementación:
 

C++

// C++ implementation of the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to count of all rotations
// which are odd and even
void countOddRotations(int n)
{
    int odd_count = 0, even_count = 0;
    do {
        int digit = n % 10;
        if (digit % 2 == 1)
            odd_count++;
        else
            even_count++;
        n = n / 10;
    } while (n != 0);
 
    cout << "Odd = " << odd_count << endl;
    cout << "Even = " << even_count << endl;
}
 
// Driver Code
int main()
{
    int n = 1234;
    countOddRotations(n);
}

Java

// Java implementation of the above approach
 
class Solution {
 
    // Function to count of all rotations
    // which are odd and even
    static void countOddRotations(int n)
    {
        int odd_count = 0, even_count = 0;
        do {
            int digit = n % 10;
            if (digit % 2 == 1)
                odd_count++;
            else
                even_count++;
            n = n / 10;
        } while (n != 0);
 
        System.out.println("Odd = " + odd_count);
        System.out.println("Even = " + even_count);
    }
 
    public static void main(String[] args)
    {
        int n = 1234;
        countOddRotations(n);
    }
}

Python3

# Python implementation of the above approach
 
# Function to count of all rotations
# which are odd and even
def countOddRotations(n):
    odd_count = 0; even_count = 0
    while n != 0:
        digit = n % 10
        if digit % 2 == 0:
            odd_count += 1
        else:
            even_count += 1
        n = n//10
    print("Odd =", odd_count)
    print("Even =", even_count)
 
# Driver code
n = 1234
countOddRotations(n)
 
# This code is contributed by Shrikant13

C#

// CSharp implementation of the above approach
 
using System;
class Solution {
 
    // Function to count of all rotations
    // which are odd and even
    static void countOddRotations(int n)
    {
        int odd_count = 0, even_count = 0;
        do {
            int digit = n % 10;
            if (digit % 2 == 1)
                odd_count++;
            else
                even_count++;
            n = n / 10;
        } while (n != 0);
 
        Console.WriteLine("Odd = " + odd_count);
        Console.WriteLine("Even = " + even_count);
    }
 
    public static void Main()
    {
        int n = 1234;
        countOddRotations(n);
    }
}

PHP

<?php
// PHP implementation of the above approach
 
// Function to count of all rotations
// which are odd and even
function countOddRotations($n)
{
    $odd_count = 0;
    $even_count = 0;
    do {
        $digit = $n % 10;
        if ($digit % 2 == 1)
            $odd_count++;
        else
            $even_count++;
        $n = (int)($n / 10);
    } while ($n != 0);
 
    echo "Odd = ", $odd_count, "\n";
    echo "Even = ", $even_count, "\n";
}
 
// Driver Code
$n = 1234;
countOddRotations($n);
 
// This code is contributed by ajit..
?>

Javascript

<script>
 
// Javascript implementation of the above approach
 
// Function to count of all rotations
// which are odd and even
function countOddRotations(n)
{
    var odd_count = 0, even_count = 0;
    do {
        var digit = n % 10;
        if (digit % 2 == 1)
            odd_count++;
        else
            even_count++;
        n = parseInt(n / 10);
    } while (n != 0);
 
    document.write("Odd = " + odd_count + "<br>");
    document.write("Even = " + even_count + "<br>");
}
 
// Driver Code
var n = 1234;
countOddRotations(n);
 
// This code is contributed by rutvik_56.
 
</script>
Producción: 

Odd = 2
Even = 2

 

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

Publicación traducida automáticamente

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