-
Notifications
You must be signed in to change notification settings - Fork 0
/
SumOfSubArray.java
70 lines (62 loc) · 1.83 KB
/
SumOfSubArray.java
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
package org.example;
import java.util.Arrays;
public class package org.example;
import java.util.Arrays;
public class SumOfSubArray {
public static void main(String[] args) {
System.out.println(sumOfSubArr(new int[]{1,2,3,4}, 1, 5));
}
public static int sumOfSubArr(int[] arr, int li, int ri){
int newArray[] = new int[(arr.length*(arr.length + 1))/2];
int newArrIdx = 0;
int innerIdx = 0;
int sum = 0;
int ansSum = 0;
for (int i = 0;i < arr.length; i++) {
innerIdx = i;
sum = 0;
while (innerIdx<arr.length){
sum+=arr[innerIdx];
newArray[newArrIdx] = sum;
newArrIdx+=1;
innerIdx+=1;
}
}
Arrays.sort(newArray);
int ans = 0;
final int mod = (int) 1e9 + 7;
for (int i = li - 1; i < ri; i++) {
ans = (ans + newArray[i]) % mod;
}
return ans;
}
}
{
public static void main(String[] args) {
System.out.println(sumOfSubArr(new int[]{1,2,3,4}, 1, 5));
}
public static int sumOfSubArr(int[] arr, int li, int ri){
int newArray[] = new int[(arr.length*(arr.length + 1))/2];
int newArrIdx = 0;
int innerIdx = 0;
int sum = 0;
int ansSum = 0;
for (int i = 0;i < arr.length; i++) {
innerIdx = i;
sum = 0;
while (innerIdx<arr.length){
sum+=arr[innerIdx];
newArray[newArrIdx] = sum;
newArrIdx+=1;
innerIdx+=1;
}
}
Arrays.sort(newArray);
int ans = 0;
final int mod = (int) 1e9 + 7;
for (int i = li - 1; i < ri; i++) {
ans = (ans + newArray[i]) % mod;
}
return ans;
}
}