Check whether a number can be represented as sum of K distinct positive integers

Given two integers N and K, the task is to check whether N can be represented as sum of K distinct positive integers.

Examples:

Input: N = 12, K = 4
Output: Yes
N = 1 + 2 + 4 + 5 = 12 (12 as sum of 4 distinct integers)



Input: N = 8, K = 4
Output: No

Approach: Consider the series 1 + 2 + 3 + … + K which has exactly K distinct integers with minimum possible sum i.e. Sum = (K * (K – 1)) / 2. Now, if N < Sum then it is not possible to represent N as the sum of K distinct positive integers but if N ≥ Sum then any integer say X ≥ 0 can be added to Sum to generate the sum equal to N i.e. 1 + 2 + 3 + … + (K – 1) + (K + X) ensuring that there are exactly K distinct positive integers.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <iostream>
using namespace std;
  
// Function that returns true if n
// can be represented as the sum of
// exactly k distinct positive integers
bool solve(int n, int k)
{
    // If n can be represented as
    // 1 + 2 + 3 + ... + (k - 1) + (k + x)
    if (n >= (k * (k + 1)) / 2) {
        return true;
    }
  
    return false;
}
  
// Driver code
int main()
{
    int n = 12, k = 4;
  
    if (solve(n, k))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.io.*;
  
class GFG {
  
    // Function that returns true if n
    // can be represented as the sum of
    // exactly k distinct positive integers
    static boolean solve(int n, int k)
    {
        // If n can be represented as
        // 1 + 2 + 3 + ... + (k - 1) + (k + x)
        if (n >= (k * (k + 1)) / 2) {
            return true;
        }
  
        return false;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int n = 12, k = 4;
  
        if (solve(n, k))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
  
// This code is contributed by anuj_67..

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation of the approach
  
# Function that returns true if n
# can be represented as the sum of
# exactly k distinct positive integers
def solve(n,k):
    # If n can be represented as
    # 1 + 2 + 3 + ... + (k - 1) + (k + x)
    if (n >= (k * (k + 1)) // 2):
        return True
  
    return False
  
# Driver code
if __name__ == '__main__':
    n = 12
    k = 4
  
    if (solve(n, k)):
        print("Yes")
    else:
        print("No")
  
# This code is contributed by
# Surendra_Gangwar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
    // Function that returns true if n
    // can be represented as the sum of
    // exactly k distinct positive integers
    static bool solve(int n, int k)
    {
        // If n can be represented as
        // 1 + 2 + 3 + ... + (k - 1) + (k + x)
        if (n >= (k * (k + 1)) / 2) {
            return true;
        }
  
        return false;
    }
  
    // Driver code
    static public void Main ()
    {
        int n = 12, k = 4;
  
        if (solve(n, k))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
  
// This code is contributed by ajit.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP implementation of the approach
  
// Function that returns true if n
// can be represented as the sum of
// exactly k distinct positive integers
function solve($n, $k)
{
    // If n can be represented as
    // 1 + 2 + 3 + ... + (k - 1) + (k + x)
    if ($n >= ($k * ($k + 1)) / 2) {
        return true;
    }
  
    return false;
}
  
// Driver code
  
$n = 12;
$k = 4;
  
if (solve($n, $k))
    echo  "Yes";
else
    echo  "No";
  
// This code is contributed by ihritik
  
?>

chevron_right


Output:

Yes

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.