Ques:- If we display var1 then what will b displayed in below condition.77 var1 pic s9(2) value -10.77 var1 pic s9(2) value -11.” ” ” -12.” ” ” -13. -14 … … -19.
Asked In :-
Damco Solutions, Polestar Solutions & Services India LLP,
Please login to post an answer.
Input: arr = [3, 5, 2, 3, 5, 3, 2], num1 = 3, num2 = 2
Output: 1
(Closest 3 and 2 are at indices 3 and 4 or 5 and 6)
✅ Efficient Solution (O(n) time, O(1) space):
You only need a single pass through the array.
🧠 Idea:
Track last seen positions of num1 and num2
Update minimum distance when both have been seen
✅ Python Code:
def min_distance(arr, num1, num2):
min_dist = float(‘inf’)
last_pos1 = last_pos2 = -1