Skip to content

Instantly share code, notes, and snippets.

@jatinsharrma
Created May 9, 2020 20:22
Show Gist options
  • Save jatinsharrma/1e3588ec6c30db95283398f8b4ecface to your computer and use it in GitHub Desktop.
Save jatinsharrma/1e3588ec6c30db95283398f8b4ecface to your computer and use it in GitHub Desktop.
Special triplets
# ///////////////////////////////////////
# ------------Find the Triplets---------
# //////////////////////////////////////
'''
Question : You are given an array of integer values and a interger value (suppose x).
You have to return number of triplets found in the given array such that
2nd element = 1st element + x
3rd element = 2nd element + x
Example = we are given ( [1,2,3,6,7,8] , 1 )
Our answer (2)
Because ([1,2,3] , [6,7,8])
'''
def helper(array, x,d):
for i in range(2):
temp = x + d
if temp not in array:
return False
x = temp
return True
def triple(array,d):
answer = 0
for i in array:
if helper(array,i,d):
answer += 1
return answer
print(triple([1,2,3,5,6,7,9,10,11],1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment