Números en un rango con raíz digital dada

Dado un entero K y un rango de números consecutivos [L, R] . La tarea es contar los números del rango dado que tienen raíz digital como K (1 ≤ K ≤ 9). La raíz digital es la suma de los dígitos de un número hasta que se convierte en un número de un solo dígito. Por ejemplo, 256 -> 2 + 5 + 6 = 13 -> 1 + 3 = 4.

Ejemplos:  

Entrada: L = 10, R = 22, K = 3 
Salida:
12 y 21 son los únicos números del rango cuya suma de dígitos es 3.

Entrada: L = 100, R = 200, K = 5 
Salida: 11 
 

Acercarse:  

  • Lo primero es tener en cuenta que para cualquier número, la suma de los dígitos es igual al número % 9. Si el resto es 0, entonces la suma de los dígitos es 9.
  • Entonces, si K = 9, entonces reemplace K con 0.
  • Tarea, ahora es encontrar el conteo de números en el rango L a R con módulo 9 igual a K.
  • Divida todo el rango en los máximos grupos posibles de 9 comenzando por L (TotalRange / 9), ya que en cada rango habrá exactamente un número con módulo 9 igual a K.
  • Realice un bucle sobre el número restante de elementos de R a R: recuento de elementos restantes y verifique si algún número satisface la condición.

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

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
 
// Function to return the count
// of required numbers
int countNumbers(int L, int R, int K)
{
    if (K == 9)
        K = 0;
 
    // Count of numbers present
    // in given range
    int totalnumbers = R - L + 1;
 
    // Number of groups of 9 elements
    // starting from L
    int factor9 = totalnumbers / 9;
 
    // Left over elements not covered
    // in factor 9
    int rem = totalnumbers % 9;
 
    // One Number in each group of 9
    int ans = factor9;
 
    // To check if any number in rem
    // satisfy the property
    for (int i = R; i > R - rem; i--) {
        int rem1 = i % 9;
        if (rem1 == K)
            ans++;
    }
 
    return ans;
}
 
// Driver code
int main()
{
    int L = 10;
    int R = 22;
    int K = 3;
    cout << countNumbers(L, R, K);
 
    return 0;
}

Java

// Java implementation of the approach
 
class GFG {
 
// Function to return the count
// of required numbers
    static int countNumbers(int L, int R, int K) {
        if (K == 9) {
            K = 0;
        }
 
        // Count of numbers present
        // in given range
        int totalnumbers = R - L + 1;
 
        // Number of groups of 9 elements
        // starting from L
        int factor9 = totalnumbers / 9;
 
        // Left over elements not covered
        // in factor 9
        int rem = totalnumbers % 9;
 
        // One Number in each group of 9
        int ans = factor9;
 
        // To check if any number in rem
        // satisfy the property
        for (int i = R; i > R - rem; i--) {
            int rem1 = i % 9;
            if (rem1 == K) {
                ans++;
            }
        }
 
        return ans;
    }
 
// Driver code
    public static void main(String[] args) {
        int L = 10;
        int R = 22;
        int K = 3;
        System.out.println(countNumbers(L, R, K));
    }
}
/* This code contributed by PrinciRaj1992 */

Python3

# Python3 implementation of the approach
 
# Function to return the count
# of required numbers
def countNumbers(L, R, K):
 
    if (K == 9):
        K = 0
 
    # Count of numbers present
    # in given range
    totalnumbers = R - L + 1
 
    # Number of groups of 9 elements
    # starting from L
    factor9 = totalnumbers // 9
 
    # Left over elements not covered
    # in factor 9
    rem = totalnumbers % 9
 
    # One Number in each group of 9
    ans = factor9
 
    # To check if any number in rem
    # satisfy the property
    for i in range(R, R - rem, -1):
        rem1 = i % 9
        if (rem1 == K):
            ans += 1
     
    return ans
 
# Driver code
L = 10
R = 22
K = 3
print(countNumbers(L, R, K))
 
# This code is contributed
# by mohit kumar

C#

// C# implementation of the approach
using System ;
 
class GFG
{
 
    // Function to return the count
    // of required numbers
    static int countNumbers(int L, int R, int K)
    {
        if (K == 9)
        {
            K = 0;
        }
 
        // Count of numbers present
        // in given range
        int totalnumbers = R - L + 1;
 
        // Number of groups of 9 elements
        // starting from L
        int factor9 = totalnumbers / 9;
 
        // Left over elements not covered
        // in factor 9
        int rem = totalnumbers % 9;
 
        // One Number in each group of 9
        int ans = factor9;
 
        // To check if any number in rem
        // satisfy the property
        for (int i = R; i > R - rem; i--)
        {
            int rem1 = i % 9;
            if (rem1 == K)
            {
                ans++;
            }
        }
 
        return ans;
    }
 
    // Driver code
    public static void Main()
    {
        int L = 10;
        int R = 22;
        int K = 3;
         
        Console.WriteLine(countNumbers(L, R, K));
    }
}
 
/* This code is contributed by Ryuga */

PHP

<?php
// PHP implementation of the approach
 
// Function to return the count
// of required numbers
function countNumbers($L, $R, $K)
{
    if ($K == 9)
        $K = 0;
 
    // Count of numbers present
    // in given range
    $totalnumbers = $R - $L + 1;
 
    // Number of groups of 9 elements
    // starting from L
    $factor9 = intval($totalnumbers / 9);
 
    // Left over elements not covered
    // in factor 9
    $rem = $totalnumbers % 9;
 
    // One Number in each group of 9
    $ans = $factor9;
 
    // To check if any number in rem
    // satisfy the property
    for ($i = $R; $i > $R - $rem; $i--)
    {
        $rem1 = $i % 9;
        if ($rem1 == $K)
            $ans++;
    }
 
    return $ans;
}
 
// Driver code
$L = 10;
$R = 22;
$K = 3;
echo countNumbers($L, $R, $K);
 
// This code is contributed by Ita_c
?>

Javascript

<script>
 
// Javascript implementation of the approach
 
// Function to return the count
// of required numbers
function countNumbers(L, R, K)
{
    if (K == 9)
    {
        K = 0;
    }
 
    // Count of numbers present
    // in given range
    var totalnumbers = R - L + 1;
 
    // Number of groups of 9 elements
    // starting from L
    var factor9 = totalnumbers / 9;
 
    // Left over elements not covered
    // in factor 9
    var rem = totalnumbers % 9;
 
    // One Number in each group of 9
    var ans = factor9;
 
    // To check if any number in rem
    // satisfy the property
    for(var i = R; i > R - rem; i--)
    {
        var rem1 = i % 9;
        if (rem1 == K)
        {
            ans++;
        }
    }
 
    return ans;
}
  
// Driver Code
var L = 10;
var R = 22;
var K = 3;
 
document.write(Math.round(countNumbers(L, R, K)));
 
// This code is contributed by Ankita saini
                     
</script>
Producción: 

2

 

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

Publicación traducida automáticamente

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