Last active
May 11, 2020 01:02
-
-
Save uan4ik/5d4cd20526c24bed1dc4 to your computer and use it in GitHub Desktop.
Greedy Algorithm find the maximum number of pairwise different pairs of integers that sum up to n
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Advanced code problem: pairwise different summands | |
Given an integer 1≤n≤109 find the maximal number k such that n can be represented as a sum of pairwise different positive integers. In the first line output k, in the next line output k summands. | |
Sample Input 1: | |
4 | |
Sample Output 1: | |
2 | |
1 3 | |
Sample Input 2: | |
6 | |
Sample Output 2: | |
3 | |
1 2 3 | |
Memory Limit: 256 MB | |
Time Limit: 5 seconds | |
''' | |
n = int(input()) | |
total = 0 | |
increment = 1 | |
count = 0 | |
listOfNum = [] | |
while total + increment <= n: | |
total += increment | |
listOfNum.append(increment) | |
increment += 1 | |
count += 1 | |
listOfNum[count-1] += n - total | |
print(count) | |
print(*listOfNum) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your code logic in not proper as required