Skip to content

Instantly share code, notes, and snippets.

@rishi93
Last active April 5, 2016 01:17
Show Gist options
  • Save rishi93/26ac6c9ff699134ed712 to your computer and use it in GitHub Desktop.
Save rishi93/26ac6c9ff699134ed712 to your computer and use it in GitHub Desktop.
Supernumbers in a permutation - SUPPER
def lis(arr,n):
dp = [1 for x in range(0,n)]
for i in range(1,n):
for j in range(0,i):
if dp[j] >= dp[i] and arr[j] < arr[i]:
dp[i] += 1
result = []
max_value = max(dp)
i = n-1
while i >= 0:
if dp[i] == max_value:
while dp[i] == max_value and i >= 0:
result.append(arr[i])
i -= 1
max_value -= 1
else:
i -= 1
result.sort()
return result
for testcase in range(0,10):
n = int(input())
arr = [int(x) for x in input().split()]
answer = lis(arr,n)
print(len(answer))
print(' '.join([str(x) for x in answer]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment