Check whether or not a quantity might be represented as sum of Okay distinct optimistic integers

Given two integers N and Okay, the duty is to examine whether or not N might be represented as sum of Okay distinct optimistic integers.

Examples:

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

Input: N = eight, Okay = four
Output: No

Approach: Consider the collection 1 + 2 + three + … + Okay which has precisely Okay distinct integers with minimal potential sum i.e. Sum = (Okay * (Okay – 1)) / 2. Now, if N < Sum then it isn’t potential to signify N because the sum of Okay distinct optimistic integers but when N ≥ Sum then any integer say X ≥ zero might be added to Sum to generate the sum equal to N i.e. 1 + 2 + three + … + (Okay – 1) + (Okay + X) guaranteeing that there are precisely Okay distinct optimistic integers.

Below is the implementation of the above method:

#embrace

utilizing namespace std;

  

bool resolve(int n, int ok)

  

int major()

Second yr Department of Information Technology Jadavpur University

If you want GeeksforGeeks and want to contribute, you can too write an article utilizing contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article showing on the GeeksforGeeks major web page and assist different Geeks.

Please Improve this text when you discover something incorrect by clicking on the “Improve Article” button beneath.

Article Tags :

thumb_up
Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any challenge with the above content material.

Post navigation

Previous

first_page Check whether or not product of integers from a to b is optimistic , unfavorable or zero

Next

last_page Find the node whose sum with X has most set bits

Share this publish!