Represent N as sum of K odd numbers with repetitions allowed

Given two integers N and K, the task is to represent N as sum of K odd numbers. If it is not possible to create the sum then output -1.

Note: The representation may contain duplicate odd numbers.

Examples:

Input: N = 5, K = 3
Output: 1, 1, 3
Explanation:
The given number N can be represented as 1 + 1 + 3 = 5

Input: N = 7, K = 5
Output: 1, 1, 1, 1, 3
Explanation:
The given number N can be represented as 1 + 1 + 1 + 1 + 3 = 7



Approach:

To solve the problem mentioned above a simple solution is to maximise the occurrence of 1 which is the possible smallest odd number. Necessary conditions for representing the number N as K odd numbers are:

  • (K – 1) must be less than N.
  • N – (K – 1) must be a Odd number.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to represent
// N as sum of K even numbers
  
#include <bits/stdc++.h>
  
using namespace std;
  
// Function to print the representation
void sumOddNumbers(int N, int K)
{
    int check = N - (K - 1);
  
    // N must be greater than equal to 2*K
    // and must be odd
    if (check > 0 && check % 2 == 1) {
        for (int i = 0; i < K - 1; i++) {
            cout << "1 ";
        }
        cout << check;
    }
    else
        cout << "-1";
}
  
// Driver Code
int main()
{
  
    int N = 5;
    int K = 3;
  
    sumOddNumbers(N, K);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to represent
// N as sum of K even numbers
import java.util.*;
  
class GFG{
  
// Function to print the representation
static void sumOddNumbers(int N, int K)
{
    int check = N - (K - 1);
  
    // N must be greater than equal 
    // to 2*K and must be odd
    if (check > 0 && check % 2 == 1)
    {
        for(int i = 0; i < K - 1; i++)
        {
           System.out.print("1 ");
        }
        System.out.print(+check);
    }
    else
        System.out.println("-1 ");
}
  
// Driver Code
public static void main(String args[])
{
    int N = 5;
    int K = 3;
  
    sumOddNumbers(N, K);
}
}
  
// This code is contributed by AbhiThakur

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to represent 
# N as sum of K even numbers 
  
# Function to print the representation 
def sumOddNumbers(N, K):
  
    check = N - (K - 1
  
    # N must be greater than equal  
    # to 2*K and must be odd 
    if (check > 0 and check % 2 == 1): 
        for i in range(0, K - 1): 
            print("1", end = " "
  
        print(check, end = " "
  
    else:
        print("-1"
  
# Driver Code 
N = 5
K = 3
  
sumOddNumbers(N, K) 
  
# This code is contributed by PratikBasu    

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to represent
// N as sum of K even numbers
using System;
  
class GFG{
  
// Function to print the representation
static void sumOddNumbers(int N, int K)
{
    int check = N - (K - 1);
  
    // N must be greater than equal 
    // to 2*K and must be odd
    if (check > 0 && check % 2 == 1)
    {
        for(int i = 0; i < K - 1; i++)
        {
           Console.Write("1 ");
        }
        Console.Write(+check);
    }
    else
        Console.WriteLine("-1 ");
}
  
// Driver Code
public static void Main()
{
    int N = 5;
    int K = 3;
  
    sumOddNumbers(N, K);
}
}
  
// This code is contributed by Code_Mech

chevron_right


Output:

1 1 3

Time Complexity: O(K)

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.