Check whether a number can be represented as sum of K distinct positive integers
Last Updated :
20 Sep, 2023
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++
#include <iostream>
using namespace std;
bool solve(int n, int k)
{
if (n >= (k * (k + 1)) / 2) {
return true;
}
return false;
}
int main()
{
int n = 12, k = 4;
if (solve(n, k))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
import java.io.*;
class GFG {
static boolean solve(int n, int k)
{
if (n >= (k * (k + 1)) / 2) {
return true;
}
return false;
}
public static void main(String[] args)
{
int n = 12, k = 4;
if (solve(n, k))
System.out.println("Yes");
else
System.out.println("No");
}
}
Python3
def solve(n,k):
if (n >= (k * (k + 1)) // 2):
return True
return False
if __name__ == '__main__':
n = 12
k = 4
if (solve(n, k)):
print("Yes")
else:
print("No")
C#
using System;
class GFG
{
static bool solve(int n, int k)
{
if (n >= (k * (k + 1)) / 2) {
return true;
}
return false;
}
static public void Main ()
{
int n = 12, k = 4;
if (solve(n, k))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
Javascript
<script>
function solve(n, k)
{
if (n >= (k * (k + 1)) / 2)
{
return true;
}
return false;
}
var n = 12, k = 4;
if (solve(n, k))
document.write("Yes");
else
document.write("No");
</script>
PHP
<?php
function solve($n, $k)
{
if ($n >= ($k * ($k + 1)) / 2) {
return true;
}
return false;
}
$n = 12;
$k = 4;
if (solve($n, $k))
echo "Yes";
else
echo "No";
?>
Time Complexity: O(1)
Auxiliary Space: O(1)
Approach 2: Dynamic Programming:
Here’s the dynamic programming (DP) approach to solve the same problem:
Define a 2D array dp of size (n+1) x (k+1).
Initialize dp[i][j] to false if either i is 0 or j is 0, and to true if j is 1.
For i from 1 to n and j from 2 to k, do the following steps:
a. If i >= j, then set dp[i][j] to dp[i-1][j] || dp[i-j][j-1].
b. If i < j, then set dp[i][j] to dp[i-1][j].
If dp[n][k] is true, return true, else return false.
Here’s the C++ code for the above DP approach:
C++
#include <iostream>
#include <vector>
using namespace std;
bool canSumToDistinctIntegers(int n, int k) {
vector<vector<bool>> dp(n+1, vector<bool>(k+1, false));
for (int i = 0; i <= n; i++) {
dp[i][0] = false;
}
for (int j = 0; j <= k; j++) {
dp[0][j] = false;
}
for (int j = 1; j <= k; j++) {
dp[1][j] = true;
}
for (int i = 1; i <= n; i++) {
for (int j = 2; j <= k; j++) {
if (i >= j) {
dp[i][j] = dp[i-1][j] || dp[i-j][j-1];
}
else {
dp[i][j] = dp[i-1][j];
}
}
}
return dp[n][k];
}
int main() {
int n = 12, k = 4;
if (canSumToDistinctIntegers(n, k)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
}
Java
public class GFG {
public static boolean canSumToDistinctIntegers(int n, int k) {
boolean[][] dp = new boolean[n + 1][k + 1];
for (int i = 0; i <= n; i++) {
dp[i][0] = false;
}
for (int j = 0; j <= k; j++) {
dp[0][j] = false;
}
for (int j = 1; j <= k; j++) {
dp[1][j] = true;
}
for (int i = 1; i <= n; i++) {
for (int j = 2; j <= k; j++) {
if (i >= j) {
dp[i][j] = dp[i - 1][j] || dp[i - j][j - 1];
} else {
dp[i][j] = dp[i - 1][j];
}
}
}
return dp[n][k];
}
public static void main(String[] args) {
int n = 12, k = 4;
if (canSumToDistinctIntegers(n, k)) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}
Python3
def can_sum_to_distinct_integers(n, k):
dp = [[False] * (k+1) for _ in range(n+1)]
for i in range(n+1):
dp[i][0] = False
for j in range(k+1):
dp[0][j] = False
for j in range(1, k+1):
dp[1][j] = True
for i in range(1, n+1):
for j in range(2, k+1):
if i >= j:
dp[i][j] = dp[i-1][j] or dp[i-j][j-1]
else:
dp[i][j] = dp[i-1][j]
return dp[n][k]
n = 12
k = 4
if can_sum_to_distinct_integers(n, k):
print("YES")
else:
print("No")
C#
using System;
public class GFG
{
public static bool CanSumToDistinctIntegers(int n, int k)
{
bool[,] dp = new bool[n + 1, k + 1];
for (int i = 0; i <= n; i++)
{
dp[i, 0] = false;
}
for (int j = 0; j <= k; j++)
{
dp[0, j] = false;
}
for (int j = 1; j <= k; j++)
{
dp[1, j] = true;
}
for (int i = 1; i <= n; i++)
{
for (int j = 2; j <= k; j++)
{
if (i >= j)
{
dp[i, j] = dp[i - 1, j] || dp[i - j, j - 1];
}
else
{
dp[i, j] = dp[i - 1, j];
}
}
}
return dp[n, k];
}
public static void Main()
{
int n = 12, k = 4;
if (CanSumToDistinctIntegers(n, k))
{
Console.WriteLine("Yes");
}
else
{
Console.WriteLine("No");
}
Console.ReadLine();
}
}
Javascript
function canSumToDistinctIntegers(n, k) {
let dp = new Array(n+1);
for (let i = 0; i <= n; i++) {
dp[i] = new Array(k+1).fill(false);
}
for (let i = 0; i <= n; i++) {
dp[i][0] = false;
}
for (let j = 0; j <= k; j++) {
dp[0][j] = false;
}
for (let j = 1; j <= k; j++) {
dp[1][j] = true;
}
for (let i = 1; i <= n; i++) {
for (let j = 2; j <= k; j++) {
if (i >= j) {
dp[i][j] = dp[i-1][j] || dp[i-j][j-1];
}
else {
dp[i][j] = dp[i-1][j];
}
}
}
return dp[n][k];
}
let n = 12, k = 4;
if (canSumToDistinctIntegers(n, k)) {
console.log("Yes");
}
else {
console.log("No");
}
Output:
Yes
Time Complexity: O(nk) , where n is the maximum possible value of n (the input number), and k is the maximum possible value of k
Auxiliary Space: O(nk) because we need to store the intermediate results in a 2D array.
Share your thoughts in the comments
Add Your Comment
Please Login to comment...