Skip to content

Commit 5236527

Browse files
committed
leetcode
1 parent c2e8c16 commit 5236527

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
3+
4+
-* 1685. Sum of Absolute Differences in a Sorted Array *-
5+
6+
7+
8+
9+
You are given an integer array nums sorted in non-decreasing order.
10+
11+
Build and return an integer array result with the same length as nums such that result[i] is equal to the summation of absolute differences between nums[i] and all the other elements in the array.
12+
13+
In other words, result[i] is equal to sum(|nums[i]-nums[j]|) where 0 <= j < nums.length and j != i (0-indexed).
14+
15+
16+
17+
Example 1:
18+
19+
Input: nums = [2,3,5]
20+
Output: [4,3,5]
21+
Explanation: Assuming the arrays are 0-indexed, then
22+
result[0] = |2-2| + |2-3| + |2-5| = 0 + 1 + 3 = 4,
23+
result[1] = |3-2| + |3-3| + |3-5| = 1 + 0 + 2 = 3,
24+
result[2] = |5-2| + |5-3| + |5-5| = 3 + 2 + 0 = 5.
25+
Example 2:
26+
27+
Input: nums = [1,4,6,8,10]
28+
Output: [24,15,13,15,21]
29+
30+
31+
Constraints:
32+
33+
2 <= nums.length <= 105
34+
1 <= nums[i] <= nums[i + 1] <= 104
35+
36+
*/
37+
38+
List<int> getSumAbsoluteDifferences(List<int> nums) {
39+
int x = 0;
40+
int y = 0;
41+
final int len = nums.length;
42+
43+
for (int i = 0; i < len; i++) {
44+
y += nums[i];
45+
}
46+
47+
for (int i = 0; i < len; i++) {
48+
x += nums[i];
49+
int tmp = (nums[i] * (i + 1) - x) + (y - nums[i] * (len - i));
50+
51+
y -= nums[i];
52+
nums[i] = tmp;
53+
}
54+
55+
return nums;
56+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
func getSumAbsoluteDifferences(nums []int) []int {
4+
var x, y int = 0, 0
5+
var len int = len(nums)
6+
7+
for i := 0; i < len; i++ {
8+
y += nums[i]
9+
}
10+
11+
for i := 0; i < len; i++ {
12+
x += nums[i]
13+
tmp := (nums[i]*(i+1) - x) + (y - nums[i]*(len-i))
14+
15+
y -= nums[i]
16+
nums[i] = tmp
17+
}
18+
19+
return nums
20+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# 🔥 Sum of Absolute Differences in a Sorted Array 🔥 || Simple Fast and Easy || with Explanation 😈
2+
3+
## Intuition
4+
5+
The goal is to find the sum of absolute differences between each element and all other elements in the array. The code uses a prefix and suffix sum approach to efficiently calculate these absolute differences.
6+
7+
### Approach
8+
9+
1. **Suffix Sum Calculation (Initialization):**
10+
- Calculate the total sum of the array and store it in the variable `y`. This is done using a loop that iterates through each element of the array.
11+
12+
2. **Absolute Difference Calculation:**
13+
- Iterate through the array again.
14+
- For each element, calculate the sum of absolute differences using the prefix sum (`x`) and the suffix sum (`y`). Update the element in the array with this calculated value.
15+
- Update the prefix sum (`x`) and suffix sum (`y`) accordingly for the next iteration.
16+
17+
### Time Complexity
18+
19+
- The time complexity of this algorithm is O(n), where n is the length of the input array. This is because the code iterates through the array twice, and each iteration takes O(n) time.
20+
21+
### Space Complexity
22+
23+
- The space complexity is O(1) since the algorithm uses a constant amount of additional space, regardless of the size of the input array. The input array is modified in place.
24+
25+
This approach efficiently calculates the sum of absolute differences without the need for nested loops, resulting in a linear time complexity solution.
26+
27+
## Code
28+
29+
```go
30+
func getSumAbsoluteDifferences(nums []int) []int {
31+
// Initialize variables
32+
var x, y int = 0, 0
33+
var len int = len(nums)
34+
35+
// Calculate the total sum of the array (suffix sum)
36+
for i := 0; i < len; i++ {
37+
y += nums[i]
38+
}
39+
40+
// Iterate through the array to calculate the absolute differences
41+
for i := 0; i < len; i++ {
42+
// Calculate the sum of absolute differences using prefix and suffix sums
43+
x += nums[i]
44+
tmp := (nums[i]*(i+1) - x) + (y - nums[i]*(len-i))
45+
46+
// Update the suffix sum for the next iteration
47+
y -= nums[i]
48+
49+
// Update the array with the calculated absolute difference
50+
nums[i] = tmp
51+
}
52+
53+
// Return the array with updated absolute differences
54+
return nums
55+
}
56+
```
57+
58+
## [GitHub](https://github.com/ayoubzulfiqar/leetcode)

0 commit comments

Comments
 (0)