Guys i am trying to solve "Arrays Manipulation" in hacker rank
which is
You are given a list(1-indexed) of size , initialized with zeroes. You have to perform operations on the list and output the maximum of final values of all the elements in the list. For every operation, you are given three integers , and and you have to add value to all the elements ranging from index to (both inclusive).
For example, consider a list of size . The initial list would be = [, , ] and after performing the update = , the new list would be = [, , ]. Here, we've added value 30 to elements between indices 2 and 3. Note the index of the list starts from 1.
and my solution is
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
List arr = new ArrayList(Collections.nCopies(n, new Long(0)));
int m = in.nextInt();
for(int a0 = 0; a0 < m; a0++){
int a = in.nextInt();
int b = in.nextInt();
int k = in.nextInt();
for(int i=a-1;i<=b-1;i++){
arr.set(i, arr.get(i)+k);
}
}
in.close();
System.out.println(Collections.max(arr));
}
}
any help ... why i am getting "terminate due to timeout" ???