2121. Intervals Between Identical Elements LeetCode Solution
In this guide, you will get 2121. Intervals Between Identical Elements LeetCode Solution with the best time and space complexity. The solution to Intervals Between Identical Elements problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Intervals Between Identical Elements solution in C++
- Intervals Between Identical Elements solution in Java
- Intervals Between Identical Elements solution in Python
- Additional Resources
Problem Statement of Intervals Between Identical Elements
You are given a 0-indexed array of n integers arr.
The interval between two elements in arr is defined as the absolute difference between their indices. More formally, the interval between arr[i] and arr[j] is |i – j|.
Return an array intervals of length n where intervals[i] is the sum of intervals between arr[i] and each element in arr with the same value as arr[i].
Note: |x| is the absolute value of x.
Example 1:
Input: arr = [2,1,3,1,2,3,3]
Output: [4,2,7,2,4,4,5]
Explanation:
– Index 0: Another 2 is found at index 4. |0 – 4| = 4
– Index 1: Another 1 is found at index 3. |1 – 3| = 2
– Index 2: Two more 3s are found at indices 5 and 6. |2 – 5| + |2 – 6| = 7
– Index 3: Another 1 is found at index 1. |3 – 1| = 2
– Index 4: Another 2 is found at index 0. |4 – 0| = 4
– Index 5: Two more 3s are found at indices 2 and 6. |5 – 2| + |5 – 6| = 4
– Index 6: Two more 3s are found at indices 2 and 5. |6 – 2| + |6 – 5| = 5
Example 2:
Input: arr = [10,5,10,10]
Output: [5,0,3,4]
Explanation:
– Index 0: Two more 10s are found at indices 2 and 3. |0 – 2| + |0 – 3| = 5
– Index 1: There is only one 5 in the array, so its sum of intervals to identical elements is 0.
– Index 2: Two more 10s are found at indices 0 and 3. |2 – 0| + |2 – 3| = 3
– Index 3: Two more 10s are found at indices 0 and 2. |3 – 0| + |3 – 2| = 4
Constraints:
n == arr.length
1 <= n <= 105
1 <= arr[i] <= 105
Note: This question is the same as 2615: Sum of Distances.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
2121. Intervals Between Identical Elements LeetCode Solution in C++
class Solution {
public:
vector<long long> getDistances(vector<int>& arr) {
const int n = arr.size();
vector<long long> ans(n);
vector<long> prefix(n);
vector<long> suffix(n);
unordered_map<int, vector<int>> numToIndices;
for (int i = 0; i < n; ++i)
numToIndices[arr[i]].push_back(i);
for (const auto& [_, indices] : numToIndices) {
for (int i = 1; i < indices.size(); ++i) {
const int currIndex = indices[i];
const int prevIndex = indices[i - 1];
prefix[currIndex] += prefix[prevIndex] + i * (currIndex - prevIndex);
}
for (int i = indices.size() - 2; i >= 0; --i) {
const int currIndex = indices[i];
const int prevIndex = indices[i + 1];
suffix[currIndex] += suffix[prevIndex] +
(indices.size() - i - 1) * (prevIndex - currIndex);
}
}
for (int i = 0; i < n; ++i)
ans[i] = prefix[i] + suffix[i];
return ans;
}
};
/* code provided by PROGIEZ */
2121. Intervals Between Identical Elements LeetCode Solution in Java
class Solution {
public long[] getDistances(int[] arr) {
final int n = arr.length;
long[] ans = new long[n];
long[] prefix = new long[n];
long[] suffix = new long[n];
Map<Integer, List<Integer>> numToIndices = new HashMap<>();
for (int i = 0; i < n; ++i) {
numToIndices.putIfAbsent(arr[i], new ArrayList<>());
numToIndices.get(arr[i]).add(i);
}
for (List<Integer> indices : numToIndices.values()) {
for (int i = 1; i < indices.size(); ++i) {
final int currIndex = indices.get(i);
final int prevIndex = indices.get(i - 1);
prefix[currIndex] += prefix[prevIndex] + i * (currIndex - prevIndex);
}
for (int i = indices.size() - 2; i >= 0; --i) {
final int currIndex = indices.get(i);
final int prevIndex = indices.get(i + 1);
suffix[currIndex] += suffix[prevIndex] + (indices.size() - i - 1) * (prevIndex - currIndex);
}
}
for (int i = 0; i < n; ++i)
ans[i] = prefix[i] + suffix[i];
return ans;
}
}
// code provided by PROGIEZ
2121. Intervals Between Identical Elements LeetCode Solution in Python
class Solution:
def getDistances(self, arr: list[int]) -> list[int]:
prefix = [0] * len(arr)
suffix = [0] * len(arr)
numToIndices = collections.defaultdict(list)
for i, a in enumerate(arr):
numToIndices[a].append(i)
for indices in numToIndices.values():
for i in range(1, len(indices)):
currIndex = indices[i]
prevIndex = indices[i - 1]
prefix[currIndex] += prefix[prevIndex] + i * (currIndex - prevIndex)
for i in range(len(indices) - 2, -1, -1):
currIndex = indices[i]
prevIndex = indices[i + 1]
suffix[currIndex] += (suffix[prevIndex] +
(len(indices) - i - 1) * (prevIndex - currIndex))
return [p + s for p, s in zip(prefix, suffix)]
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.