Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmRepresent N as sum of K odd numbers with repetitions allowed

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++




// 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;
}


Java




// 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


Python3




# 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   


C#




// 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


JavaScript




<script>
 
// Javascript implementation to represent
// N as sum of K even numbers
 
// Function to print the representation
function sumOddNumbers(N, K)
{
    var check = N - (K - 1);
    var i;
 
    // N must be greater than equal to 2*K
    // and must be odd
    if (check > 0 && check % 2 == 1) {
        for (i = 0; i < K - 1; i++) {
             document.write("1,"+" ");
        }
        document.write(check + " ");
    }
    else
        document.write("-1");
}
 
// Driver Code
    var N = 5;
    var K = 3;
 
    sumOddNumbers(N, K);
     
</script>


Output: 

1 1 3

 

Time Complexity: O(k)

Auxiliary Space: O(1)
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Nango Kalahttps://www.kala.co.za
Experienced Support Engineer with a demonstrated history of working in the information technology and services industry. Skilled in Microsoft Excel, Customer Service, Microsoft Word, Technical Support, and Microsoft Office. Strong information technology professional with a Microsoft Certificate Solutions Expert (Privet Cloud) focused in Information Technology from Broadband Collage Of Technology.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments