Find any K distinct odd integers such that their sum is equal to N - GeeksforGeeks

08 Apr, 2021
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++
Java
Python3
C#
Javascript
// C++ implementation to find k// odd integers such that their sum is N #include <bits/stdc++.h>using namespace std; // Function to find K odd integers// such that their sum is Nvoid oddIntegers(int n, int k){    // Condition to check if there    // exist such K integers    if (n % 2 != k % 2) {        cout << "-1"             << "\n";        return;    }     int sum = 0;    int i = 1;    int j = 1;     // Loop to find first K-1    // distinct odd integers    while (j < k) {        sum = sum + i;        cout << i << " ";        i = i + 2;        j++;    }     // Final Kth odd number    int finalOdd = n - sum;     cout << finalOdd << "\n";} // Driver codeint main(){    int n = 10;    int k = 2;     oddIntegers(n, k);     return 0;}
// Java implementation to find k// odd integers such that their sum is Nclass GFG{ // Function to find K odd integers// such that their sum is Nstatic void oddIntegers(int n, int k){    // Condition to check if there    // exist such K integers    if (n % 2 != k % 2) {        System.out.println("-1");        return;    }     int sum = 0;    int i = 1;    int j = 1;     // Loop to find first K-1    // distinct odd integers    while (j < k) {        sum = sum + i;        System.out.print(i+" ");        i = i + 2;        j++;    }     // Final Kth odd number    int finalOdd = n - sum;         System.out.println(finalOdd);} // Driver codepublic static void main (String[] args){    int n = 10;    int k = 2;     oddIntegers(n, k);}} // This code is contributed by shubhamsingh
# Python3 implementation to find k# odd integers such that their sum is N # Function to find K odd integers# such that their sum is Ndef oddIntegers(n, k) :     # Condition to check if there    # exist such K integers    if (n % 2 != k % 2) :        print("-1");                 return;     sum = 0;    i = 1;    j = 1;     # Loop to find first K-1    # distinct odd integers    while (j < k) :        sum += i;        print(i,end= " ");        i += 2;        j += 1;     # Final Kth odd number    finalOdd = n - sum;     print(finalOdd); # Driver codeif __name__ == "__main__" :     n = 10;    k = 2;     oddIntegers(n, k);     # This code is contributed by AnkitRai01
// C# implementation to find k// odd integers such that their sum is Nusing System; class GFG{  // Function to find K odd integers// such that their sum is Nstatic void oddints(int n, int k){    // Condition to check if there    // exist such K integers    if (n % 2 != k % 2) {        Console.WriteLine("-1");        return;    }      int sum = 0;    int i = 1;    int j = 1;      // Loop to find first K-1    // distinct odd integers    while (j < k) {        sum = sum + i;        Console.Write(i+" ");        i = i + 2;        j++;    }      // Final Kth odd number    int finalOdd = n - sum;          Console.WriteLine(finalOdd);}  // Driver codepublic static void Main(String[] args){    int n = 10;    int k = 2;      oddints(n, k);}} // This code is contributed by PrinciRaj1992
<script> // JavaScript implementation to find k// odd integers such that their sum is N    // Function to find K odd integers// such that their sum is N    function oddIntegers(n , k)    {        // Condition to check if there        // exist such K integers        if (n % 2 != k % 2) {            document.write("-1");            return;        }         var sum = 0;        var i = 1;        var j = 1;         // Loop to find first K-1        // distinct odd integers        while (j < k) {            sum = sum + i;            document.write(i + " ");            i = i + 2;            j++;        }         // Final Kth odd number        var finalOdd = n - sum;         document.write(finalOdd);    }     // Driver code             var n = 10;        var k = 2;         oddIntegers(n, k); // This code contributed by Rajput-Ji </script>
1 9
 
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).
 
ankthon
SHUBHAMSINGH10
princiraj1992
Rajput-Ji
Numbers
Mathematical
Mathematical
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program for Decimal to Binary Conversion
Find all factors of a natural number | Set 1
Program to find sum of elements in a given array
The Knight's tour problem | Backtracking-1
Program for factorial of a number
Operators in C / C++
Minimum number of jumps to reach end