Skip to content
Related Articles

Related Articles

Check whether a number can be represented as sum of K distinct positive integers
  • Difficulty Level : Medium
  • Last Updated : 08 Apr, 2021

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++




// 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;
}

Java




// 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..

Python3




# 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

C#




// 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.

PHP




<?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
 
?>

Javascript




<script>
 
// Javascript 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
var n = 12, k = 4;
 
if (solve(n, k))
    document.write("Yes");
else
    document.write("No");
 
// This code is contributed by todaysgaurav
 
</script>
Output: 
Yes

 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :