Skip to content

Instantly share code, notes, and snippets.

@uan4ik
Last active May 11, 2020 01:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uan4ik/5d4cd20526c24bed1dc4 to your computer and use it in GitHub Desktop.
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
'''
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)
@Neel-Bahurupi
Copy link

Your code logic in not proper as required

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment