Split a number as sum of K numbers which are not divisible by K
Given two numbers N and K, the task is to split this number into K positive integers such that their sum is equal to N and none of these K integers is a multiple of K.
Note: N>=2
Examples:
Input: N = 10, K = 3
Output: 1, 1, 8
Input: N = 18, K = 3
Output:1, 1, 16
Approach:
To split N into K numbers, we need to approach the problem by the following steps:
- Check if N is divisible by K or not.
- If N is divisible by K, then N – K + 1 is not divisible by K. Hence, we can split N into N – K + 1 as one part and all the remaining K – 1 parts as 1.
- If N is not divisible by K:
- If K is 2, then it is not possible to split.
- Otherwise, split N into K-2 parts as all 1 and 2 and N – K as the remaining two parts.
Below code is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void printKParts( int N, int K)
{
if (N % K == 0) {
for ( int i = 1; i < K; i++)
cout << "1, " ;
cout << N - (K - 1) << endl;
}
else {
if (K == 2) {
cout << "Not Possible" << endl;
return ;
}
for ( int i = 1; i < K - 1; i++)
cout << 1 << ", " ;
cout << 2 << ", "
<< N - K << endl;
}
}
int main()
{
int N = 18, K = 5;
printKParts(N, K);
return 0;
}
|
Java
class GFG{
static void printKParts( int N, int K)
{
if (N % K == 0 )
{
for ( int i = 1 ; i < K; i++)
System.out.print( "1, " );
System.out.print(N - (K - 1 ) + "\n" );
}
else
{
if (K == 2 )
{
System.out.print( "Not Possible" + "\n" );
return ;
}
for ( int i = 1 ; i < K - 1 ; i++)
System.out.print( 1 + ", " );
System.out.print( 2 + ", " + (N - K) + "\n" );
}
}
public static void main(String[] args)
{
int N = 18 , K = 5 ;
printKParts(N, K);
}
}
|
Python3
def printKParts(N, K):
if (N % K = = 0 ):
for i in range ( 1 , K):
print ( "1, " );
print (N - (K - 1 ), end = "");
else :
if (K = = 2 ):
print ( "Not Possible" , end = "");
return ;
for i in range ( 1 , K - 1 ):
print ( 1 , end = ", " );
print ( 2 , ", " , (N - K), end = "");
if __name__ = = '__main__' :
N = 18 ;
K = 5 ;
printKParts(N, K);
|
C#
using System;
class GFG{
static void printKParts( int N, int K)
{
if (N % K == 0)
{
for ( int i = 1; i < K; i++)
Console.Write( "1, " );
Console.Write(N - (K - 1) + "\n" );
}
else
{
if (K == 2)
{
Console.Write( "Not Possible" + "\n" );
return ;
}
for ( int i = 1; i < K - 1; i++)
Console.Write(1 + ", " );
Console.Write(2 + ", " + (N - K) + "\n" );
}
}
public static void Main(String[] args)
{
int N = 18, K = 5;
printKParts(N, K);
}
}
|
Javascript
<script>
function printKParts(N, K)
{
if (N % K == 0) {
for ( var i = 1; i < K; i++)
document.write( "1, " );
document.write( (N - (K - 1)) + "<br>" );
}
else {
if (K == 2) {
document.write( "Not Possible" + "<br>" );
return ;
}
for ( var i = 1; i < K - 1; i++)
document.write( 1 + ", " );
document.write( 2 + ", "
+ (N - K) + "<br>" );
}
}
var N = 18, K = 5;
printKParts(N, K);
</script>
|
Time Complexity: O(K)
Auxiliary Space: O(1)
Last Updated :
16 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...