Topic: Binary Search
Given an array of N integers and a value 'X', find the k closest integers to 'X', preferring smaller values when distances are the same. Output the k-closest integers in ascending order.
Topic: Graphs
You need to find the shortest path from a source city to a target city in a graph of N cities and M roads, while avoiding cities with landmines. If reaching the target city is impossible without crossing a landmine, return -1.
Topic: Two Pointers
Given an array of N integers and a target value K, check if there exist three distinct integers in the array whose sum equals K. Print "Yes" if such integers exist, otherwise print "No".
Topic: Stack
In this problem, for each element in the array, you are tasked to find the next greater element on its right side. If no such element exists, return -1.
Topic: Hashing
In this problem, you are asked to find the longest subarray of a given array that is divisible by a given integer K. The array may contain negative values as well.
Topic: Dynamic Programming
© 2024 AlgoUniversity Technology Fellowship
I hope I will select for the second stage. i am thrilled to be part of the AlgouniversityTechnologyFellowshipOver the past few months, I have significantly improved my skills in programming and data analysis. I look forward to applying what I've learned in upcoming projects and further developing my expertise in technology
Approach 1 (Easy) :
- Time Complexity: O(N Log N)
- Auxiliary Space Complexity: O(N)
- Steps:
- Python code:
n = int(input())
arr = list(map(int,input().split()))
k, x = map(int,input().split())
q = [(abs(i-x),i) for i in arr]
q.sort()
print(' '.join(map(str,sorted([val[1] for val in q[1:k+1]]))))
Approach 2 (Optimal) :
- Time Complexity: O(N Log N)
- Auxiliary Space Complexity: O(k)
- Steps:
- Python code:
n = int(input())
arr = list(map(int,input().split()))
k, x = map(int,input().split())
arr.sort()
# You may use binary search for X
idx = 0
for idx in range(len(arr)):
if arr[idx]==x:
break
l, r = idx-1, idx+1
val_l, val_r, selected = 0, 0, 0
impossible_val = -10**9-1
result = []
for i in range(k):
if l<0:
val_l = impossible_val
else:
val_l = arr[l]
if r>=len(arr):
val_r = impossible_val
else:
val_r = arr[r]
if abs(x-val_l)<=abs(x-val_r):
selected = val_l
l-=1
else:
selected = val_r
r+=1
result.append(selected)
result.sort()
print(" ".join(map(str, result)))
Approach :
- Time Complexity: O(N)
- Auxiliary Space Complexity: O(N)
- Steps:
- Python code:
def find_n_roads(mines, adj, s, t):
# queue with value denoting and number of roads travelled and current city
q = [(0,s)]
visited = set([s])
idx = 0
while idx<len(q):
n_roads, curr = q[idx]
idx+=1
if mines[curr]==1:
continue
if curr==t:
return n_roads
n_roads +=1
for city in adj[curr]:
if city not in visited:
q.append((n_roads, city))
visited.add(city)
return -1
n,m = map(int,input().split())
s,t = map(int,input().split())
mines = list(map(int,input().split()))
adj = [[] for _ in range(n)]
for _ in range(m):
u,v = map(int,input().split())
adj[u].append(v)
adj[v].append(u)
# ====
res = find_n_roads(mines, adj, s, t)
print(res)