Skip to content

Instantly share code, notes, and snippets.

@Ram-Aditya
Last active June 12, 2018 23:18
Show Gist options
  • Save Ram-Aditya/389cfb128bb7ab21d9fa42b75a30c051 to your computer and use it in GitHub Desktop.
Save Ram-Aditya/389cfb128bb7ab21d9fa42b75a30c051 to your computer and use it in GitHub Desktop.
CodeBuddy Week5
a=input().split()
k=int(input())
for i in range(len(a)):
a[i]=int(a[i])
#a=[-5,-30,20,17,14,-7,-4,-9,11,20,23,42,-38,39,32,-8,-22,-50,-37]
ans=[]
a.sort()
tmin=[10000,-1,-1,-1]
for t in range(len(a)):
i=0
j=len(a)-1
while(i!=j):
if(i==t):
i+=1
continue
elif(j==t):
j-=1
continue
if(abs(a[t]+a[i]+a[j]-k)<tmin[0]):
tmin=[abs(a[t]+a[i]+a[j]-k),a[t],a[i],a[j]]
if((a[t]+a[i]+a[j]-k)<0):
i+=1
else:
j-=1
tmin[0]=tmin[1]+tmin[2]+tmin[3]
print(tmin[0])
#Greedy Approach - O(n), objF is the objective function value
#Keep choosing the jump value which gets you the furthest
def solution(arr):
n=len(arr)
objF=[arr[0],arr[0]]
count=1
for i in range(1,n):
objF[0]=max(objF[0],i+arr[i])
objF[1] -= 1
if (i==n-1):
return count
if (objF[1]==0):
objF[1]=objF[0]-i;
count+=1
if(i>=objF[0]):
return "Not Possible"
return "Not Possible"
a=input().split()
for i in range(len(a)):
a[i]=int(a[i])
print(solution(a))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment