Given two integers N and K, the task is to find any K distinct odd integers such that their sum is equal to N. If no such integers exists, print -1.
Examples:
Input: N = 10, K = 2
Output: 1, 9
Explanation:
There are two possible distinct odd integers, such that their sum is equal to N.
Possible K integers can be – {(1, 9), (3, 7)}
Input: N = 5, K = 4
Output: -1
Explanation:
There are no such 4 distinct odd integers such that their sum is 5.
Approach:
- The key observation in this problem is if N and K have different parity then it is not possible to find K such distinct integers such that their sum is equal to N,
- Otherwise such K – 1 integers will consist of first K-1 odd positive integers
- The Kth odd number will be equal to (N – the sum of first (K-1) odd integers)
Kth Odd number = N - sum of first K-1 integer
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void oddIntegers( int n, int k)
{
if (n % 2 != k % 2) {
cout << "-1"
<< "\n" ;
return ;
}
int sum = 0;
int i = 1;
int j = 1;
while (j < k) {
sum = sum + i;
cout << i << " " ;
i = i + 2;
j++;
}
int finalOdd = n - sum;
cout << finalOdd << "\n" ;
}
int main()
{
int n = 10;
int k = 2;
oddIntegers(n, k);
return 0;
}
|
Java
class GFG
{
static void oddIntegers( int n, int k)
{
if (n % 2 != k % 2 ) {
System.out.println( "-1" );
return ;
}
int sum = 0 ;
int i = 1 ;
int j = 1 ;
while (j < k) {
sum = sum + i;
System.out.print(i+ " " );
i = i + 2 ;
j++;
}
int finalOdd = n - sum;
System.out.println(finalOdd);
}
public static void main (String[] args)
{
int n = 10 ;
int k = 2 ;
oddIntegers(n, k);
}
}
|
Python3
def oddIntegers(n, k) :
if (n % 2 ! = k % 2 ) :
print ( "-1" );
return ;
sum = 0 ;
i = 1 ;
j = 1 ;
while (j < k) :
sum + = i;
print (i,end = " " );
i + = 2 ;
j + = 1 ;
finalOdd = n - sum ;
print (finalOdd);
if __name__ = = "__main__" :
n = 10 ;
k = 2 ;
oddIntegers(n, k);
|
C#
using System;
class GFG
{
static void oddints( int n, int k)
{
if (n % 2 != k % 2) {
Console.WriteLine( "-1" );
return ;
}
int sum = 0;
int i = 1;
int j = 1;
while (j < k) {
sum = sum + i;
Console.Write(i+ " " );
i = i + 2;
j++;
}
int finalOdd = n - sum;
Console.WriteLine(finalOdd);
}
public static void Main(String[] args)
{
int n = 10;
int k = 2;
oddints(n, k);
}
}
|
Javascript
<script>
function oddIntegers(n , k)
{
if (n % 2 != k % 2) {
document.write( "-1" );
return ;
}
var sum = 0;
var i = 1;
var j = 1;
while (j < k) {
sum = sum + i;
document.write(i + " " );
i = i + 2;
j++;
}
var finalOdd = n - sum;
document.write(finalOdd);
}
var n = 10;
var k = 2;
oddIntegers(n, k);
</script>
|
Performance Analysis:
- Time Complexity: As in the above approach, There is a loop to find such K odd integers which takes O(K) time in worst case. Hence the Time Complexity will be O(K).
- Auxiliary Space Complexity: As in the above approach, There is no extra space used. Hence the auxiliary space complexity will be O(1).
Using Dynamic Programming:
Approach:
- Define a function distinct_odd_integers_sum(N, K, memo={}) that takes three arguments – N, K, and memo. N is the sum we want to achieve and K is the number of distinct odd integers we want to use to achieve this sum. The memo parameter is used for memoization and is an empty dictionary by default.
- If the (N, K) tuple is already present in the memo dictionary, return its value.
- If K is equal to 1, then we can only use one integer to achieve the sum. Check if N is odd, if yes, return a list containing N, otherwise return None.
- If K is greater than 1, iterate over all odd integers from 1 to N (exclusive) with a step of 2. For each odd integer i, recursively call the distinct_odd_integers_sum function with the parameters N-i and K-1, and store the result in the remaining variable.
- If remaining is not None and i is not in remaining, then we can add i to the remaining list and return the list [i] + remaining.
- If we have exhausted all possible odd integers without finding a valid combination, then add the (N, K) tuple to the memo dictionary with a value of None and return None.
- Finally, outside the function, set N and K to the input values and call the distinct_odd_integers_sum function with these arguments. If the function returns a list, print the list, otherwise print -1.
Python3
def distinct_odd_integers_sum(N, K, memo = {}):
if (N, K) in memo:
return memo[(N, K)]
if K = = 1 :
return [N] if N % 2 ! = 0 else None
else :
for i in range ( 1 , N, 2 ):
remaining = distinct_odd_integers_sum(N - i, K - 1 , memo)
if remaining and i not in remaining:
memo[(N, K)] = [i] + remaining
return memo[(N, K)]
memo[(N, K)] = None
return None
N = 10
K = 2
result = distinct_odd_integers_sum(N, K)
if result:
print (result)
else :
print ( - 1 )
|
Time Complexity: O(NK)
Space Complexity: O(NK)