We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I also did it on Java. At first, it only passed up to test 3, due some precision loss, having int k instead of long k. This may be obvious to many here, but I still thought it was interesting to see how such a small detail changes it all. Here is the code (in comments, the code with int, which fails after test 3).
public static void main(String[] args) {
int N, M, a, b;
long k; // int k;
Scanner in = new Scanner(System.in);
N = in.nextInt();
M = in.nextInt();
long[] differenceArray = new long[N+1]; // int[] ...
for (int i=0; i<M; i++)
{
in.nextLine();
a = in.nextInt();
b = in.nextInt();
k = in.nextLong(); // in.nextInt();
differenceArray[a] += k;
if (b+1<=N)
differenceArray [b+1] -= k;
}
long max = 0, addedDifference = 0; // int
for (int i=1; i<=N; i++)
{
addedDifference = addedDifference + differenceArray[i];
if (max < addedDifference)
max = addedDifference;
}
System.out.println(max);
in.close();
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Array Manipulation
You are viewing a single comment's thread. Return to all comments →
I also did it on Java. At first, it only passed up to test 3, due some precision loss, having int k instead of long k. This may be obvious to many here, but I still thought it was interesting to see how such a small detail changes it all. Here is the code (in comments, the code with int, which fails after test 3).
import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;
public class Solution {
}