📌  相关文章
📜  找出任意K个不同的奇数整数,使它们的总和等于N

📅  最后修改于: 2021-04-22 00:42:21             🧑  作者: Mango

给定两个整数NK ,任务是找到任意K个不同的奇数整数,以使它们的总和等于N。如果不存在这样的整数,则打印-1。
例子:

方法:

  • 这个问题的主要观察结果是,如果N和K具有不同的奇偶校验,则不可能找到K这样的不同整数,以使它们的总和等于N,
  • 否则,此类K – 1整数将由前K-1个奇数正整数组成
  • K奇数等于(N –第一个(K-1)个奇整数的总和)
Kth Odd number  = N - sum of first K-1 integer

下面是上述方法的实现:

C++
// C++ implementation to find k
// odd integers such that their sum is N
 
#include 
using namespace std;
 
// Function to find K odd integers
// such that their sum is N
void 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 code
int main()
{
    int n = 10;
    int k = 2;
 
    oddIntegers(n, k);
 
    return 0;
}


Java
// Java implementation to find k
// odd integers such that their sum is N
class GFG
{
 
// Function to find K odd integers
// such that their sum is N
static 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 code
public static void main (String[] args)
{
    int n = 10;
    int k = 2;
 
    oddIntegers(n, k);
}
}
 
// This code is contributed by shubhamsingh


Python3
# Python3 implementation to find k
# odd integers such that their sum is N
 
# Function to find K odd integers
# such that their sum is N
def 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 code
if __name__ == "__main__" :
 
    n = 10;
    k = 2;
 
    oddIntegers(n, k);
     
# This code is contributed by AnkitRai01


C#
// C# implementation to find k
// odd integers such that their sum is N
using System;
 
class GFG
{
  
// Function to find K odd integers
// such that their sum is N
static 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 code
public static void Main(String[] args)
{
    int n = 10;
    int k = 2;
  
    oddints(n, k);
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
1 9

性能分析:

  • 时间复杂度:与上述方法一样,在最坏的情况下,存在一个循环来查找这样的K个奇数整数,这需要O(K)时间。因此,时间复杂度将为O(K)
  • 辅助空间复杂度:与上述方法一样,没有使用额外的空间。因此,辅助空间复杂度将为O(1)