Subarray Sum Positive
Programming Interview
Medium
5 views
Problem Description
Consider {x}. Output YES if there exists a subarray with sum exactly S else NO.
Input Format
Line1 n S. Line2 n integers (positive).
Official Solution
import sys
p=sys.stdin.read().strip().split()
if len(p)<2: sys.exit(0)
n=int(p[0]); S=int(p[1])
a=list(map(int,p[2:2+n]))
left=0
sm=0
ok=False
for right in range(n):
sm+=a[right]
while sm>S and left<=right:
sm-=a[left]
left+=1
if sm==S:
ok=True
break
sys.stdout.write('YES' if ok else 'NO')
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!