Problem Statement
Find the contiguous subarray within a one-dimensional array of numbers which has the largest sum
What's a contiguous subarray? https://www.quora.com/What-is-difference-between-subarray-and-contiguous-subarray
arr=list(map(int, input().split()))
m=-1
x=arr[:]
for i in range(1,len(arr)):
arr[i]=arr[i]+arr[i-1]
for i in range(len(arr)-1,-1,-1):
max_val=arr[i]
if m<max_val:
m=max_val
left=0
right=i
print(m)
for j in range(0,i):
if m<arr[i]-arr[j]:
m=arr[i]-arr[j]
left=j
right=i
print(m)
print(x[left:right+1])
Output-
1 2 3 -2 5
9 [1, 2, 3, -2, 5]
Number Line Jumps
def kangaroo(x1, v1, x2, v2):while x1+v1<=x2+v2 and v1>v2:x1+=v1 x2+=v2 if x1==x2: return "YES" return "NO"Staircase
# # Complete the 'staircase' function below. # # The function accepts INTEGER n as parameter. # def staircase(n): # Write your code here for i in range(n): for j in range(n): if i+j>=n-1: print('#',end='') else: print(' ',end='') print() if __name__ == '__main__': n = int(input().strip()) staircase(n)Compare the Triplets
def compareTriplets(a, b): # Write your code here bscore=0 ascore=0 for i in range(3): if a[i]>b[i]: ascore+=1 if a[i]<b[i]: bscore+=1 return [ascore,bscore]Quicksort 1 - Partition
def quickSort(a): # Write your code here p=a[0] rindex=0 for i in range(1,len(a)): if a[i]<p: rindex+=1 a[i],a[rindex]=a[rindex],a[i] a[0],a[rindex]=a[rindex],a[0] return aBetween Two Sets
def getTotalX(a, b): # Write your code here minb=min(b) flist=[] n=len(a) m=len(b) for i in range(1,minb+1): for j in range(len(b)): if b[j]%i!=0: break if j==m-1 and b[j]%i==0: flist.append(i) res=0 for i in flist: for j in range(n): if i%a[j]!=0: break if j==n-1 and i%a[j]==0: res+=1 return resSuper Reduced String
def superReducedString(s): # Write your code here st=[] for i in s: if st!=[] and st[len(st)-1]==i: st.pop(len(st)-1) else: st.append(i) if len(st)==0: return 'Empty String' else: res=''.join(st) return resBill Division
def bonAppetit(bill, k, b): # Write your code here rb=(sum(bill)-bill[k])//2 if b==rb: print('Bon Appetit') else: print(b-rb)Subarray Division
def birthday(s, d, m): # Write your code here for i in range(1,len(s)): s[i]=s[i-1]+s[i] res=0 if a[m-1]==d: res=1 c=0 for j in range(m,len(s)): val=s[j]-s[c] if val==d: res+=1 c+=1 print(res)Migratory Birds
def migratoryBirds(arr): # Write your code here d={} for i in arr: if i in d: d[i]+=1 else: d[i]=1 res=6 m=0 for i in d: if m==d[i] and res>i: m=d[i] res=i elif m<d[i] : m=d[i] res=i return resDiagonal Difference
link-hackerrankdef diagonalDifference(arr): # Write your code here n=len(arr) r1,c1,r2,c2=0,0,0,n-1 sum1,sum2=0,0 while r1<n and r2<n and c1<n and c2>-1: sum1+=arr[r1][c1] sum2+=arr[r2][c2] r1+=1 r2+=1 c1+=1 c2-=1 res=int(math.fabs(sum1-sum2)) return resBirthday Cake Candles
link-hackerrankdef birthdayCakeCandles(candles): # Write your code here d={} for i in candles: if i in d: d[i]+=1 else: d[i]=1 ma=candles[0] res=d[ma] for i in d: if ma<i: ma=i res=d[i] return resGrading Students
def gradingStudents(grades): # Write your code here l=[] for i in grades: val=5*(i//5) if i%5!=0: val+=5 res=i diff=val-i if diff<3 and i>37: res=val l.append(res) return lDrawing Book
def pageCount(n, p): # Write your code here v1=(p-1)//2+(p-1)%2 if n%2==0: v2=(n-p)//2+(n-p)%2 else: v2=(n-p)//2 return min(v1,v2)Divisible Sum Pairs
def divisibleSumPairs(n, k, ar): # Write your code here res=0 for i in range(n-1): for j in range(i+1,n): if (ar[i]+ar[j])%k==0: res+=1 return resMini-Max Sum
def miniMaxSum(arr): # Write your code here s=sum(arr) ma=max(arr) mi=min(arr) print(s-ma,s-mi)Counting Valleys
def countingValleys(steps, path): # Write your code here c,res=0,0 for i in path: prev=c if i=='U': c+=1 else: c-=1 if prev<0 and c==0: res+=1 return resLibrary Fine
def libraryFine(d1, m1, y1, d2, m2, y2): # Write your code here if y1-y2>0: return 10000 elif m1-m2>0 and y1-y2>=0: return (m1-m2)*500 elif d1-d2>0 and m1-m2>=0 and y1-y2>=0: return (d1-d2)*15 else: return 0Time Conversion
def timeConversion(s): # Write your code here h=int(s[0:2]) m=int(s[3:5]) sec=int(s[6:8]) res='' if h==12 and s[8:]=='AM': h=str('00') res=h+s[2:8] elif h==12 and s[8:]=='PM': res=s[0:8] elif s[8:]=='PM': h=h+12 res=str(h)+s[2:8] else: # h=(h+12)%24 res=s[0:8] return res
Comments
Post a Comment