Se le asigna un número ‘N’. Tu tarea es dividir este número en 3 enteros positivos x, y y z, de modo que su suma sea igual a ‘N’ y ninguno de los 3 enteros sea múltiplo de 3. Dado que N>=2.
Ejemplos:
Entrada: N = 10
Salida: x = 1, y = 2, z = 7
Tenga en cuenta que x + y + z = N y x, y & z no son divisibles por N.
Entrada: 18
Salida: x = 1, y = 1, z = 16
Enfoque:
para dividir N en 3 números, dividimos N como
- Si N es divisible por 3, entonces los números x, y, z pueden ser 1, 1 y N-2, respectivamente. Todo x, y y z no son divisibles por 3. Y (1)+(1)+(N-2)=N .
- Si N no es divisible por 3, entonces N-3 tampoco será divisible por 3. Por lo tanto, podemos tener x=1, y=2 y z=N-3. Además, (1)+(2)+( N-3)=N.
C++
// CPP program to split a number into three parts such // than none of them is divisible by 3. #include <iostream> using namespace std; void printThreeParts(int N) { // Print x = 1, y = 1 and z = N - 2 if (N % 3 == 0) cout << " x = 1, y = 1, z = " << N - 2 << endl; // Otherwise, print x = 1, y = 2 and z = N - 3 else cout << " x = 1, y = 2, z = " << N - 3 << endl; } // Driver code int main() { int N = 10; printThreeParts(N); return 0; }
Java
// Java program to split a number into three parts such // than none of them is divisible by 3. import java.util.*; class solution { static void printThreeParts(int N) { // Print x = 1, y = 1 and z = N - 2 if (N % 3 == 0) System.out.println("x = 1, y = 1, z = "+ (N-2)); // Otherwise, print x = 1, y = 2 and z = N - 3 else System.out.println(" x = 1, y = 2, z = "+ (N-3)); } // Driver code public static void main(String args[]) { int N = 10; printThreeParts(N); } }
Python3
# Python3 program to split a number into three parts such # than none of them is divisible by 3. def printThreeParts(N) : # Print x = 1, y = 1 and z = N - 2 if (N % 3 == 0) : print(" x = 1, y = 1, z = ",N - 2) # Otherwise, print x = 1, y = 2 and z = N - 3 else : print(" x = 1, y = 2, z = ",N - 3) # Driver code if __name__ == "__main__" : N = 10 printThreeParts(N) # This code is contributed by Ryuga
C#
// C# program to split a number into three parts such // than none of them is divisible by 3. using System; public class GFG{ static void printThreeParts(int N) { // Print x = 1, y = 1 and z = N - 2 if (N % 3 == 0) Console.WriteLine(" x = 1, y = 1, z = "+(N - 2)); // Otherwise, print x = 1, y = 2 and z = N - 3 else Console.WriteLine(" x = 1, y = 2, z = "+(N - 3)); } // Driver code static public void Main (){ int N = 10; printThreeParts(N); } // This code is contributed by ajit. }
PHP
<?php // PHP program to split a number into // three parts such than none of them // is divisible by 3. function printThreeParts($N) { // Print x = 1, y = 1 and z = N - 2 if ($N % 3 == 0) echo " x = 1, y = 1, z = " . ($N - 2) . "\n"; // Otherwise, print x = 1, // y = 2 and z = N - 3 else echo " x = 1, y = 2, z = " . ($N - 3) . "\n"; } // Driver code $N = 10; printThreeParts($N); // This code is contributed by ita_c ?>
Javascript
<script> // javascript program to split a number into three parts such // than none of them is divisible by 3. function printThreeParts(N) { // Print x = 1, y = 1 and z = N - 2 if (N % 3 == 0) document.write("x = 1, y = 1, z = "+ (N-2)); // Otherwise, print x = 1, y = 2 and z = N - 3 else document.write(" x = 1, y = 2, z = "+ (N-3)); } // Driver code var N = 10; printThreeParts(N); // This code contributed by Princi Singh </script>
Producción:
x = 1, y = 2, z = 7
Complejidad temporal: O(1) , ya que no hay bucle ni recursión.
Espacio Auxiliar: O(1) , ya que no se ha ocupado ningún espacio extra.