0% found this document useful (0 votes)
55 views6 pages

AP Worksheet 2 - Alasso

Experiment 1.2 details an aim to implement stacks and queues in Hackerrank. It summarizes two questions: Q1 asks to find the starting index of a truck tour around a circle of petrol pumps. A brute force approach checks all indices to find one that can complete the loop. Q2 describes a game where a player removes integers from two stacks to maximize their total sum. The problem is solved with a brute force approach in O(n^2) time and an optimized approach in O(n) time. The document concludes with the time complexities of common queue and stack operations, noting queues have O(N^2) time for the brute force truck tour solution while stacks have

Uploaded by

Avnish Ranjan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views6 pages

AP Worksheet 2 - Alasso

Experiment 1.2 details an aim to implement stacks and queues in Hackerrank. It summarizes two questions: Q1 asks to find the starting index of a truck tour around a circle of petrol pumps. A brute force approach checks all indices to find one that can complete the loop. Q2 describes a game where a player removes integers from two stacks to maximize their total sum. The problem is solved with a brute force approach in O(n^2) time and an optimized approach in O(n) time. The document concludes with the time complexities of common queue and stack operations, noting queues have O(N^2) time for the brute force truck tour solution while stacks have

Uploaded by

Avnish Ranjan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment 1.

NAME: Alasso UID: 21BCS


Branch: Computer Science and Eng. Section/Group:
Semester: 5th Semester DOP: 8/10/2023
Subject Name: Advance Programming Subject Code: 21CSP-314

AIM: Implement the Stacks and Queues in Hackerrank.

Q1) TRUCK TOUR


Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0
to (N-1). You have two pieces of information corresponding to each of the petrol pump: (1) the
amout of petrol that particular petrol pump will give…

Brute Force Approach:We can start from each index and check whether we are able to
complete the loop. If not we start again from the next index. We keep on doing this till we get the
first index from which we start and are able to complete the whoole loop.
CODE:
# Harshit Singh Bisht

# 21 BCS 5431
def solve(petrol,distance,n):
start,tank=0,0
for i in range(n): #Start from each index
start=i
tank=0 #Reset the tank for each iteration
for j in range(n):
current=(start+j)%n #To make the index in range fo
r circular array
tank+=petrol[current]-
distance[current] #Add the petrol and subtract the petrol required
to reach next station
if tank<0: #If petrol in tank becomes negative, we ca
nnot complete the loop
break
if j==n-
1: #If we visit all n pumps, task is completed
return start

n=int(input())
petrol,distance=[],[]
for i in range(n):
p,d=[int(x) for x in input().split()]
petrol.append(p),distance.append(d)
print(solve(petrol,distance,n))
OUTPUT:

QUES 2) GAME OF STACKS:


Alexa has two stacks of non-negative intergers, stack a[n] and stack b[m] where index 0 denotes
the top of the stack. Alexa challenfes Nick to play the following game:
• In each moves, Nick can remove one integer from the top of either stack a or stack b.
• Nick keeps a running sum of the integers he removes from the two stacks.
• Nick’s final score is the total numver of integers he has removed from the two stacks.
CODE:
def brute(a,b):
# Harshit Singh Bisht
# 21 BCS 5431

ans = 0
a = [0]+a
b = [0]+b
for i in range(len(a)):
for j in range(len(b)):
sa = sum(a[:i+1])
sb = sum(b[:j+1])
if sa+sb > x:
break
ans = max(ans, i+j)

return ans

def solve(a,b):
total = 0
i = len(a)
ans = i
j = 1
for i in range(len(a)):
total += a[i]
if total > x:
ans = i
break

ans_total = x + 1
total = sum(a[:i])
while (total <= x and i > 0) or j < len(b):
if total < x:
total += b[j - 1]
j += 1
elif total > x:
total -= a[i - 1]
i -= 1
else:
total -= a[i - 1]
i -= 1
total += b[j - 1]
j += 1

if i < 1 and total > x:


break
ans = max(ans, i + j - 2)
ans_total = min(total, ans_total)

return ans if ans_total <= x else ans - 1

for _ in range(int(input())):
n, m, x = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
_sum_a, _sum_b = sum(a), sum(b)
if _sum_a + _sum_b <= x:
print(n+m)
elif n <= 200 and m <= 200:
print(brute(a,b))
else:
ans = solve(a,b) if _sum_a < _sum_b else solve(b,a)
print(ans)
OUTPUT:

COMPLEXITY:
The time complexity for the QUEUE is O(N2).
The time complexity for the STACK is O(1), except for the search operation which has a time
complexity of O(n).

You might also like