-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram21.java
More file actions
78 lines (52 loc) · 1.88 KB
/
program21.java
File metadata and controls
78 lines (52 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
2615. Sum of Distances
Medium
443
63
Companies
You are given a 0-indexed integer array nums. There exists an array arr of length nums.length, where arr[i] is the sum of |i - j| over all j such that nums[j] == nums[i] and j != i. If there is no such j, set arr[i] to be 0.
Return the array arr.
Example 1:
Input: nums = [1,3,1,1,2]
Output: [5,0,3,4,0]
Explanation:
When i = 0, nums[0] == nums[2] and nums[0] == nums[3]. Therefore, arr[0] = |0 - 2| + |0 - 3| = 5.
When i = 1, arr[1] = 0 because there is no other index with value 3.
When i = 2, nums[2] == nums[0] and nums[2] == nums[3]. Therefore, arr[2] = |2 - 0| + |2 - 3| = 3.
When i = 3, nums[3] == nums[0] and nums[3] == nums[2]. Therefore, arr[3] = |3 - 0| + |3 - 2| = 4.
When i = 4, arr[4] = 0 because there is no other index with value 2.
Example 2:
Input: nums = [0,5,3]
Output: [0,0,0]
Explanation: Since each element in nums is distinct, arr[i] = 0 for all i.
*/
package LeetCode;
public class program21 {
static long[] distance(int[] nums) {
long[] res = new long[nums.length];
for(int i=0;i<nums.length;i++){
long sum = 0;
boolean flag = false;
for(int j=0;j<nums.length;j++){
if(nums[i]==nums[j]){
flag =true;
sum = sum +Math.abs(i-j);
}
}
if(!flag){
res[i] = 0;
}
else{
res[i] = sum;
}
}
return res;
}
public static void main(String[] args) {
int[] nums = {0,5,3};
long[] a = distance(nums);
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
}
}