Skip to content

Instantly share code, notes, and snippets.

@Ram-Aditya
Last active May 31, 2018 06:54
Show Gist options
  • Save Ram-Aditya/aaed43880ec298afdae5148dd1183e74 to your computer and use it in GitHub Desktop.
Save Ram-Aditya/aaed43880ec298afdae5148dd1183e74 to your computer and use it in GitHub Desktop.
def solution(l,k):
t_sum=0
max_subArrInd=0
for i in range(k):
t_sum+=l[i]
max_sum=t_sum
for i in range(1,len(l)-k+1):
t_sum=t_sum-l[i-1]+l[i+k-1]
if(t_sum>max_sum):
max_sum=t_sum
max_subArrInd=i
print("Maximum k length sub-array: ",l[max_subArrInd:max_subArrInd+k],"Sum: ",max_sum )
inp=input("Enter array elements: ").split()
k=int(input("k = "))
for i in range(len(inp)):
inp[i]=int(inp[i])
solution(inp,k)
bool exists(TreeNode * root, int key)
{
if(root==NULL) return false;
if((*root).val==key) return true;
if(exists((*root).left,key))
return true;
else if(exists((*root).right,key))
return true;
return false;
}
int Solution::lca(TreeNode * A, int B, int C)
{
static int globFlag=1;
if(globFlag && !(exists(A,B) && exists(A,C)))
return -1;
if(globFlag)globFlag=0;
if(A==NULL) return -1;
if((*A).val==B || (*A).val==C)
return (*A).val;
int left_trav=Solution::lca((*A).left,B,C);
int right_trav=Solution::lca((*A).right,B,C);
if(left_trav!=-1 && right_trav!=-1)
return (*A).val;
else if(left_trav!=-1)
return left_trav;
return right_trav;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment