Skip to content

Instantly share code, notes, and snippets.

@vporoshok
Created March 1, 2015 16:29
Show Gist options
  • Save vporoshok/0e44a1515080c0cc33ae to your computer and use it in GitHub Desktop.
Save vporoshok/0e44a1515080c0cc33ae to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import print_function
def has_sum(x, S):
i = 0
j = len(S) - 1
while i < j:
if S[i] + S[j] < x:
i += 1
elif S[i] + S[j] > x:
j -= 1
else:
return True
return False
if __name__ == '__main__':
x = 56
S = [1, 3, 4, 12, 22, 34, 98] # На сортировку как раз потратили O(n*lg(n))
print('Yes' if has_sum(x, S) else 'No')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment