-2
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();
    long arr[]=new long[n];
    long m = in.nextLong();
    for(int a0 = 0; a0 < m; a0++){
        long a = in.nextLong();
        long b = in.nextLong();
        long k = in.nextLong();
        for(int i=(int)a-1;i<(int)b;i++)
            arr[i]+=k;
    }
    long large=-999;
    for(int i=0;i<n;i++)
    {  if(arr[i]>large)
         large=arr[i];
    }
    System.out.println(large);
    in.close();
}
}

this is my answer for " You are given a list(1-indexed) of size n, initialized with zeroes. You have to perform operations on the list and output the maximum of final values of all the n elements in the list. For every operation, you are given three integers a, b and k and you have to add value k to all the elements ranging from index a to b (both inclusive)." But I am getting "Timedout error" for half of it.Can anyone please pitch in some ideas here??

Passer By
  • 19,325
  • 6
  • 49
  • 96
  • Your program requires user input; do you provide any input at all? Also, there is no such thing as a `TimeoutError` defined in the JDK. – fge Sep 22 '17 at 13:47
  • I think the `TimeoutError` is coming from hackerrank when the solution takes too much time. – baudsp Sep 22 '17 at 14:07
  • input is provided by the platform during run time and the error code is not exactly as specified,sorry. – archana narayanan Sep 22 '17 at 17:18

1 Answers1

0

try using a long as counter in the loop, why are you casting to int? if you cast long to int you probably have overflow error and the end condition of the loop might never be fulfilled

for(long i=a-1;i<b;i++)
abinmorth
  • 527
  • 2
  • 8
  • 25
  • all arrays are **int** indexed. check out this link "https://stackoverflow.com/questions/14571557/create-an-array-of-long" . Correct me if I'm wrong. And Thankyou for your response. – archana narayanan Sep 22 '17 at 17:13