-
Notifications
You must be signed in to change notification settings - Fork 0
/
lastelement.py
63 lines (44 loc) · 1.13 KB
/
lastelement.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Python3 program for the above approach
# Function to find the last element
# remaining in the array after
# performing the given operations
def printLastElement(arr, N):
# Checks if traversal is from
# left to right or vice versa
leftTurn = True
# Store the elements currently
# present in the array
remainElements = N
# Store the distance between 2
# consecutive array elements
step = 1
# Store the first element
# of the remaining array
head = 1
# Iterate while elements
# are greater than 1
while (remainElements > 1):
# If left to right turn
if (leftTurn):
# Update head
head = head + step
# Otherwise, check if the
# remaining elements are odd
else:
# If true, update head
if (remainElements % 2 == 1):
head = head + step
# Eleminate half of the array elements
remainElements = remainElements // 2
# Double the steps after each turn
step = step * 2
# Alter the turn
leftTurn = not leftTurn
# Print the remaining element
print(arr[head - 1])
# Driver Code
if __name__ == "__main__":
arr = [ 2, 3, 5, 6 ]
N = len(arr)
printLastElement(arr, N)
# This code is made by 'naman_d0shi'