Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 010f349

Browse files
Adjacent Element Product
1 parent 663f7b4 commit 010f349

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

‎adjacentElementProduct.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#Given an array of integers,
2+
#find the pair of adjacent elements
3+
#that has the largest product and
4+
#return that product.
5+
6+
def adjacentElementsProduct(inputArray):
7+
8+
length = int(len(inputArray))
9+
10+
maxm = inputArray[0]*inputArray[1]
11+
product = 1
12+
for i in range(1, length-1):
13+
product = inputArray[i]*inputArray[i+1]
14+
15+
if product>maxm:
16+
maxm = product
17+
18+
return maxm
19+
20+
21+
# print(adjacentElementsProduct([3,6,7,5]))
22+
23+
print(adjacentElementsProduct([3, 6, -2, -5, 7, 3]))
24+
25+
#Alternate solution
26+
#return max([inputArray[i]*inputArray[i+1] for i in range(0, int(len(inputArray)-1))])
27+
28+
29+
30+
31+
32+
33+
34+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /