I am trying to solve the HackerRank: Array Manipulation problem using Java.
The problem that I am facing is that the program is running for half of the test cases, while for the other half it is showing "Execution couldn't be completed because of Timeout". It seems like there is a more efficient way to do the problem such that the program is executed within the given time frame. If anybody could guide me regarding the issue, it would be of great help.
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 your array.
For example, the length of your array of zeros n=10. Your list of queries is as follows:
a b k 1 5 3 4 8 7 6 9 1
Add the values of between the indices and inclusive:
index-> 1 2 3 4 5 6 7 8 9 10 [0,0,0, 0, 0,0,0,0,0, 0] [3,3,3, 3, 3,0,0,0,0, 0] [3,3,3,10,10,7,7,7,0, 0] [3,3,3,10,10,8,8,8,1, 0]
The largest value is 10 after all operations are performed.
Function Description
Complete the function arrayManipulation in the editor below. It must return an integer, the maximum value in the resulting array.
arrayManipulation has the following parameters:
- n - the number of elements in your array
- queries - a two dimensional array of queries where each queries[i] contains three integers, a, b, and k.
Input Format
The first line contains two space-separated integers \$n\$ and \$m\$, the size of the array and the number of operations.
Each of the next \$m\$ lines contains three space-separated integers \$a\$,\$b\$ and \$k\$, the left index, right index and summand.
Constraints
- \$ 3 \le n \le 10^7 \$
- \$ 1 \le m \le 2 \cdot 10^5 \$
- \$ 1 \le a \le b \le n \$
- \$ 0 \le k \le 10^9 \$
Output Format
Return the integer maximum value in the finished array.
My Solution:
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
// Complete the arrayManipulation function below.
static long arrayManipulation(int n, int[][] queries) {
int queryLength = queries.length;
System.out.println(queryLength);
long[] p = new long[n];
int a = 0 , b = 0 , k = 0;
long max=0;
// initialize p
for(int i = 0 ; i < n ; i++)
{
p[i] = 0 ;
}
for(int i = 0 ; i < queryLength ; i++ )
{
a = queries[i][0];
b = queries[i][1];
k = queries[i][2];
for(int j = (a-1) ; j <= (b-1) ; j++)
{
p[j] = p[j] + k ;
}
}
for(int i = 0 ; i < n ; i++)
{
if(p[i]>max)
{
max = p[i];
}
}
return max;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String[] nm = scanner.nextLine().split(" ");
int n = Integer.parseInt(nm[0]);
int m = Integer.parseInt(nm[1]);
int[][] queries = new int[m][3];
for (int i = 0; i < m; i++) {
String[] queriesRowItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int j = 0; j < 3; j++) {
int queriesItem = Integer.parseInt(queriesRowItems[j]);
queries[i][j] = queriesItem;
}
}
long result = arrayManipulation(n, queries);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedWriter.close();
scanner.close();
}
}