📌  相关文章
📜  查找总和为N的K个不同的正奇整数

📅  最后修改于: 2021-05-07 05:36:36             🧑  作者: Mango

给定两个整数NK ,任务是找到K个不同的正奇数整数,以使它们的和等于给定数N。

例子:

方法:

仅当满足以下两个条件时,数字N才能表示为K个正奇整数的总和:

  1. 如果K的平方小于或等于N并且
  2. 如果N和K的和是偶数。

如果满足这些条件,则存在K个正整数,其和为N。

要生成K这样的奇数:

  • 打印从1开始的前K-1个奇数,即1、3、5、7、9……。
  • 最后一个奇数为:N –(第一个K-1个奇数正整数的总和)

下面是上述方法的实现:

C++
// C++ implementation to find K 
// odd positive integers such that
// their sum is equal to given number
  
#include 
using namespace std;
  
#define ll long long int
  
// Function to find K odd positive
// integers such that their sum is N
void findDistinctOddSum(ll n, ll k)
{
    // Condition to check if there
    // are enough values to check
    if ((k * k) <= n && 
        (n + k) % 2 == 0){
        int val = 1;
        int sum = 0;
        for(int i = 1; i < k; i++){
            cout << val << " ";
            sum += val;
            val += 2;
        }
        cout << n - sum << endl;
    }
    else
        cout << "NO \n";
}
  
// Driver Code
int main()
{
    ll n = 100;
    ll k = 4;
    findDistinctOddSum(n, k);
    return 0;
}


Java
// Java implementation to find K 
// odd positive integers such that
// their sum is equal to given number
import java.util.*;
  
class GFG{
   
// Function to find K odd positive
// integers such that their sum is N
static void findDistinctOddSum(int n, int k)
{
    // Condition to check if there
    // are enough values to check
    if ((k * k) <= n && 
        (n + k) % 2 == 0){
        int val = 1;
        int sum = 0;
        for(int i = 1; i < k; i++){
            System.out.print(val+ " ");
            sum += val;
            val += 2;
        }
        System.out.print(n - sum +"\n");
    }
    else
        System.out.print("NO \n");
}
   
// Driver Code
public static void main(String[] args)
{
    int n = 100;
    int k = 4;
    findDistinctOddSum(n, k);
}
}
  
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation to find K 
# odd positive integers such that
# their summ is equal to given number
  
# Function to find K odd positive
# integers such that their summ is N
def findDistinctOddsumm(n, k):
      
    # Condition to check if there
    # are enough values to check
    if ((k * k) <= n and (n + k) % 2 == 0):
        val = 1
        summ = 0
        for i in range(1, k):
            print(val, end =  " ")
            summ += val
            val += 2
        print(n - summ)
    else:
        print("NO")
  
# Driver Code
n = 100
k = 4
findDistinctOddsumm(n, k)
  
# This code is contributed by shubhamsingh10


C#
// C# implementation to find K 
// odd positive integers such that
// their sum is equal to given number
using System;
  
public class GFG{
    
// Function to find K odd positive
// integers such that their sum is N
static void findDistinctOddSum(int n, int k)
{
    // Condition to check if there
    // are enough values to check
    if ((k * k) <= n && 
        (n + k) % 2 == 0){
        int val = 1;
        int sum = 0;
        for(int i = 1; i < k; i++){
            Console.Write(val+ " ");
            sum += val;
            val += 2;
        }
        Console.Write(n - sum +"\n");
    }
    else
        Console.Write("NO \n");
}
    
// Driver Code
public static void Main(String[] args)
{
    int n = 100;
    int k = 4;
    findDistinctOddSum(n, k);
}
}
// This code is contributed by 29AjayKumar


输出:
1 3 5 91

性能分析:

  • 时间复杂度:在上述方法中,有一个循环会在最坏的情况下占用O(K)时间。因此,此方法的时间复杂度将为O(K)
  • 辅助空间: O(1)