Skip to content

Commit

Permalink
partition problem
Browse files Browse the repository at this point in the history
  • Loading branch information
harrypotter0 committed Jan 21, 2018
1 parent fc41ada commit 49da372
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
40 changes: 40 additions & 0 deletions CP/January Challenge 2018/part.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
for kohli in range(int(raw_input())):
x,n=map(int,raw_input().split(' '))
su=(n*(n+1))/2-x
if n<=3:
print 'impossible'
elif su%2==1:
print 'impossible'
else:
su=su/2
a=['0']*n
a[x-1]='2'
z=n-1
while z>=0:
if a[z]!='2' and (z+1)<=su:
su=su-z-1
a[z]='1'
z=z-1

# Case 1 17
if su!=0 and x==1:
a[x-1]='2'
for i in range(len(a)):
if a[i]=='1':
a[i]='0'
a[i-1]='1'
a[x]='1'
a[x-1]='2'
break
# Case : 2 8
if su!=0 and x==2:
a[x-1]='2'
a[0]='0'
for i in range(len(a)):
if a[i]=='1':
a[i]='0'
a[i-1]='1'
a[x]='1'
a[x-1]='2'
break
print "".join(a)
4 changes: 3 additions & 1 deletion dp/kadane.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ def max_value_contigous_subsequence(arr):
for i in range(1, len(arr)):
A[i] = max(arr[i], arr[i] + A[i-1])
max_to_here = max(max_to_here, A[i])
#print(arr[i],max_to_here,arr[i] + A[i-1])
return max_to_here

if __name__ == "__main__":
x = [-2, -3, 4, -1, -2, 1, 5, -3]
y = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
z = [-1, 3, -5, 4, 6, -1, 2, -7, 13, -3]
print map(max_value_contigous_subsequence, [x, y, z])
print(max_value_contigous_subsequence(x))
#print map(max_value_contigous_subsequence, [x, y, z])

0 comments on commit 49da372

Please sign in to comment.