Dados dos números enteros ‘N’ y ‘K’, la tarea es encontrar la suma de los dígitos de ‘N’ en sus lugares impares (de derecha a izquierda) y verificar si la suma es divisible por ‘K’. Si es divisible, emite SÍ , de lo contrario, emite NO .
Ejemplos:
Entrada: N = 4325, K = 4
Salida: SI
Ya que, 3 + 5 = 8, que es divisible por 4.Entrada: N = 1209, K = 3
Salida: NO
Acercarse:
- Encuentra la suma de los dígitos de ‘N’ en lugares impares (de derecha a izquierda).
- Luego verifica la divisibilidad de la suma tomando su módulo con ‘K’.
- Si es divisible, emita ‘SÍ’; de lo contrario, emita ‘NO’.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // function that checks the // divisibility of the sum // of the digits at odd places // of the given number bool SumDivisible(int n, int k) { int sum = 0, position = 1; while (n > 0) { // if position is odd if (position % 2 == 1) sum += n % 10; n = n / 10; position++; } if (sum % k == 0) return true; return false; } // Driver code int main() { int n = 592452; int k = 3; if (SumDivisible(n, k)) cout << "YES"; else cout << "NO"; return 0; }
Java
// Java implementation of the approach import java.util.*; class solution { // function that checks the // divisibility of the sum // of the digits at odd places // of the given number static boolean SumDivisible(int n, int k) { int sum = 0, position = 1; while (n > 0) { // if position is odd if (position % 2 == 1) sum += n % 10; n = n / 10; position++; } if (sum % k == 0) return true; return false; } // Driver code public static void main(String arr[]) { int n = 592452; int k = 3; if (SumDivisible(n, k)) System.out.println("YES"); else System.out.println("NO"); } } //This code is contributed by Surendra_Gangwar
Python 3
# Python 3 implementation of the approach # function that checks the divisibility # of the sum of the digits at odd places # of the given number def SumDivisible(n, k): sum = 0 position = 1 while (n > 0) : # if position is odd if (position % 2 == 1): sum += n % 10 n = n // 10 position += 1 if (sum % k == 0): return True return False # Driver code if __name__ =="__main__": n = 592452 k = 3 if (SumDivisible(n, k)): print("YES") else: print("NO") # This code is contributed # by ChitraNayal
C#
// C# implementation of the approach using System; class GFG { // function that checks the // divisibility of the sum // of the digits at odd places // of the given number static bool SumDivisible(int n, int k) { int sum = 0, position = 1; while (n > 0) { // if position is odd if (position % 2 == 1) sum += n % 10; n = n / 10; position++; } if (sum % k == 0) return true; return false; } // Driver code static public void Main () { int n = 592452; int k = 3; if (SumDivisible(n, k)) Console.WriteLine("YES"); else Console.WriteLine("NO"); } } // This code is contributed by Sachin
PHP
<?php // PHP implementation of the approach // function that checks the divisibility // of the sum of the digits at odd places // of the given number function SumDivisible($n, $k) { $sum = 0; $position = 1; while ($n > 0) { // if position is odd if ($position % 2 == 1) $sum += $n % 10; $n = (int)$n / 10; $position++; } if ($sum % $k == 0) return true; return false; } // Driver code $n = 592452; $k = 3; if (SumDivisible($n, $k)) echo "YES"; else echo "NO"; // This code is contributed // by Sach_Code ?>
Javascript
<script> // JavaScript implementation of the approach // function that checks the // divisibility of the sum // of the digits at odd places // of the given number function SumDivisible(n, k) { let sum = 0, position = 1; while (n > 0) { // if position is odd if (position % 2 == 1) sum += n % 10; n = Math.floor(n / 10); position++; } if (sum % k == 0) return true; return false; } // Driver code let n = 592452; let k = 3; if (SumDivisible(n, k)) document.write("YES"); else document.write("NO"); // This code is contributed by Surbhi Tyagi. </script>
YES
Complejidad Temporal: O(log 10 n), ya que cada vez el valor de n se reduce a n/10.
Espacio Auxiliar: (1), ya que no se ha ocupado ningún espacio extra.
Método #2: Usando el método string():
- Convierta el número entero en una string, luego recorra la string y encuentre la suma de todos los índices impares almacenándolos en sum.
- Si la suma es divisible por k, devuelve Verdadero o Falso.
A continuación se muestra la implementación:
C++
// C++ implementation of the // above approach #include <bits/stdc++.h> using namespace std; bool sumDivisible(int n, int k) { int sum = 0; // Converting integer to string string num = to_string(n); int i; // Traversing the string for (i = 0; i < num.size(); i++) { if (i % 2 != 0) { sum = sum + (num[i] - '0'); } } if (sum % k == 0) { return true; } else { return false; } } // Driver code int main() { int n = 592452; int k = 3; if (sumDivisible(n, k)) { cout << ("YES"); } else { cout << ("NO"); } return 0; } // This code is contributed by gauravrajput1
Java
// Java implementation of the // above approach import java.io.*; class GFG { static boolean sumDivisible(int n, int k) { int sum = 0; // Converting integer to string String num = Integer.toString(n); int i; // Traversing the string for (i = 0; i < num.length(); i++) { if (i % 2 != 0) { sum = sum + (num.charAt(i) - '0'); } } if (sum % k == 0) { return true; } else { return false; } } // Driver code public static void main(String[] args) { int n = 592452; int k = 3; if (sumDivisible(n, k)) { System.out.println("YES"); } else { System.out.println("NO"); } } } // This code is contributed by shivanisinghss2110
Python3
# Python3 implementation of the # above approach def sumDivisible(n, k): sum = 0 # Converting integer to string num = str(n) # Traversing the string for i in range(len(num)): if(i % 2 != 0): sum = sum+int(num[i]) if sum % k == 0: return True return False # Driver code n = 592452 k = 3 if sumDivisible(n, k) == True: print("YES") else: print("NO") # This code is contributed by vikkycirus
C#
// C# implementation of the // above approach using System; class GFG { static bool sumDivisible(int n, int k) { int sum = 0; // Converting integer to string string num = n.ToString(); int i; // Traversing the string for (i = 0; i < num.Length; i++) { if (i % 2 != 0) { sum = sum + (num[i] - '0'); } } if (sum % k == 0) { return true; } else { return false; } } // Driver code static public void Main () { int n = 592452; int k = 3; if (sumDivisible(n, k)) { Console.Write("YES"); } else { Console.Write("NO"); } } } // This code is contributed by shivanisinghss2110
Javascript
<script> // javascript implementation of the // above approach function sumDivisible(n, k){ var sum = 0 // Converting integer to string var num = n.toString() // Traversing the string for(var i=0 ; i < num.length ; i++) { if(i % 2 != 0) { sum = sum + Number(num[i]) } } if (sum % k == 0){ return true; } else{ return false; } } // Driver code var n = 592452 var k = 3 if(sumDivisible(n, k)){ document.write("YES"); } else{ document.write("NO"); } </script>
Producción:
Yes
Complejidad temporal: O(d), donde d es el número de dígitos en n.
Espacio Auxiliar: O(d), donde d es el número de dígitos en n.
Publicación traducida automáticamente
Artículo escrito por Shashank_Sharma y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA