-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayManipulation.java
More file actions
62 lines (45 loc) · 1.25 KB
/
ArrayManipulation.java
File metadata and controls
62 lines (45 loc) · 1.25 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
/*
Name: Array Manipulation
Source: HackerRank
Link: https://www.hackerrank.com/challenges/crush/problem
Statement: Starting with a 1-indexed array of zeros and a list of operations, for each operation add
a value to each of the array element between two given indices, inclusive. Once all operations have
been performed, return the maximum value in the array.
---------------------------- Incomplete. Need to try again-----------------------
*/
public class ArrayManipulation {
static long arrayManipulation(int n, int[][] queries) {
int start, end, value,largest;
int row= queries.length;
int [] arr = new int [n];
for(int i=0; i<row; i++)
{
start = queries[i][0]-1;
end= queries[i][1];
value = queries[i][2];
arr[start]+= value;
if(end!=n){
arr[end]+= -(value);
}
}
for(int i=1; i<n ; i++)
{
arr[i]= arr[i]+arr[i-1];
}
largest = arr[0];
for(int i=1; i<n ; i++)
{
if(largest<arr[i])
{
largest= arr[i];
}
}
return largest;
}
// private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int [][] queries= {{1,2,100},{2,5,100},{3,4,100}};
long result = arrayManipulation(5, queries);
System.out.println(result);
}
}