Closed MuhammadTausif closed 2 hours ago
class Solution:
def maxDistance(self, arr):
hist = {}
rev_hist = {}
n = len(arr)
for i in range(n):
if arr[i] not in hist:
hist[arr[i]] = i
for i in range(n):
rev_hist[arr[i]] = i
max_value = -1
for k, v in hist.items():
max_value = max(max_value, rev_hist[k] - hist[k])
return max_value
Max distance between same elements
Link
Difficulty: Easy
Given an array arr[] with repeated elements, the task is to find the maximum distance between two occurrences of an element.
Note: You may assume that every input array has repetitions.
Examples:
Expected Time Complexity : O(n) Expected Auxilliary Space : O(n)
Constraints: