Dados dos números N y K , la tarea es dividir este número en K enteros positivos de modo que su suma sea igual a N y ninguno de estos K enteros sea un múltiplo de K.
Nota: N>=2
Ejemplos:
Entrada: N = 10, K = 3
Salida: 1, 1, 8
Entrada: N = 18, K = 3
Salida: 1, 1, 16
Enfoque:
Para dividir N en K números, debemos abordar el problema mediante los siguientes pasos:
- Comprueba si N es divisible por K o no.
- Si N es divisible por K , entonces N – K + 1 no es divisible por K. Por lo tanto, podemos dividir N en N – K + 1 como una parte y todas las K – 1 partes restantes como 1 .
- Si N no es divisible por K :
- Si K es 2, entonces no es posible dividir.
- De lo contrario, divida N en K-2 partes como 1 y 2 y N – K como las dos partes restantes.
El siguiente código es la implementación del enfoque anterior:
C++
// C++ program to split a number // as sum of K numbers which are // not divisible by K #include <iostream> using namespace std; // Function to split into K parts // and print them void printKParts(int N, int K) { if (N % K == 0) { // Print 1 K - 1 times for (int i = 1; i < K; i++) cout << "1, "; // Print N - K + 1 cout << N - (K - 1) << endl; } else { if (K == 2) { cout << "Not Possible" << endl; return; } // Print 1 K-2 times for (int i = 1; i < K - 1; i++) cout << 1 << ", "; // Print 2 and N - K cout << 2 << ", " << N - K << endl; } } // Driver code int main() { int N = 18, K = 5; printKParts(N, K); return 0; }
Java
// Java program to split a number // as sum of K numbers which are // not divisible by K class GFG{ // Function to split into K parts // and print them static void printKParts(int N, int K) { if (N % K == 0) { // Print 1 K - 1 times for(int i = 1; i < K; i++) System.out.print("1, "); // Print N - K + 1 System.out.print(N - (K - 1) + "\n"); } else { if (K == 2) { System.out.print("Not Possible" + "\n"); return; } // Print 1 K-2 times for(int i = 1; i < K - 1; i++) System.out.print(1 + ", "); // Print 2 and N - K System.out.print(2 + ", " + (N - K) + "\n"); } } // Driver code public static void main(String[] args) { int N = 18, K = 5; printKParts(N, K); } } // This code is contributed by 29AjayKumar
Python3
# Python3 program to split a number # as sum of K numbers which are # not divisible by K # Function to split into K parts # and print them def printKParts(N, K): if (N % K == 0): # Print 1 K - 1 times for i in range(1, K): print("1, "); # Print N - K + 1 print(N - (K - 1), end = ""); else: if (K == 2): print("Not Possible", end = ""); return; # Print 1 K-2 times for i in range(1, K - 1): print(1, end = ", "); # Print 2 and N - K print(2, ", ", (N - K), end = ""); # Driver code if __name__ == '__main__': N = 18; K = 5; printKParts(N, K); # This code is contributed by Rohit_ranjan
C#
// C# program to split a number // as sum of K numbers which are // not divisible by K using System; class GFG{ // Function to split into K parts // and print them static void printKParts(int N, int K) { if (N % K == 0) { // Print 1 K - 1 times for(int i = 1; i < K; i++) Console.Write("1, "); // Print N - K + 1 Console.Write(N - (K - 1) + "\n"); } else { if (K == 2) { Console.Write("Not Possible" + "\n"); return; } // Print 1 K-2 times for(int i = 1; i < K - 1; i++) Console.Write(1 + ", "); // Print 2 and N - K Console.Write(2 + ", " + (N - K) + "\n"); } } // Driver code public static void Main(String[] args) { int N = 18, K = 5; printKParts(N, K); } } // This code is contributed by sapnasingh4991
Javascript
<script> // Javascript program to split a number // as sum of K numbers which are // not divisible by K // Function to split into K parts // and print them function printKParts(N, K) { if (N % K == 0) { // Print 1 K - 1 times for (var i = 1; i < K; i++) document.write( "1, "); // Print N - K + 1 document.write( (N - (K - 1)) + "<br>"); } else { if (K == 2) { document.write( "Not Possible" + "<br>"); return; } // Print 1 K-2 times for (var i = 1; i < K - 1; i++) document.write( 1 + ", "); // Print 2 and N - K document.write( 2 + ", " + (N - K) + "<br>"); } } // Driver code var N = 18, K = 5; printKParts(N, K); </script>
Producción:
1, 1, 1, 2, 13
Complejidad de tiempo: O(K)