Array Manipulation Data Structures and Algorithm Cognizant Handson Solution
In this post we are going to cover the Data Structures & Algorithms Array Manipulation handson solution asked in Cognizant.
Question of this handson is available here.
Steps to upload answer in GenC Platform:
- Click on this link.
- Insert the below code in Hacker Rank code editor.
- Compile & Execute the Code.
- Take screenshot of successful execution.
- And upload the screenshot in the platform.
import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; class Result { /* * Complete the 'arrayManipulation' function below. * * The function is expected to return a LONG_INTEGER. * The function accepts following parameters: * 1. INTEGER n * 2. 2D_INTEGER_ARRAY queries */ public static long arrayManipulation(int n, List<List<Integer>> queries) { // Write your code here long[] computation = new long[n]; for (int i = 0; i < queries.size(); i++) { int a = queries.get(i).get(0) - 1; int b = queries.get(i).get(1) - 1; int k = queries.get(i).get(2); computation[a] += k; if (b + 1 < n ) { computation[b + 1] -= k; } } long max = 0; long sum = 0; for (int i = 0; i < n; i++) { sum += computation[i]; max = Math.max(max, sum); } return max; } } public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" "); int n = Integer.parseInt(firstMultipleInput[0]); int m = Integer.parseInt(firstMultipleInput[1]); List<List<Integer>> queries = new ArrayList<>(); IntStream.range(0, m).forEach(i -> { try { queries.add( Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")) .map(Integer::parseInt) .collect(toList()) ); } catch (IOException ex) { throw new RuntimeException(ex); } }); long result = Result.arrayManipulation(n, queries); bufferedWriter.write(String.valueOf(result)); bufferedWriter.newLine(); bufferedReader.close(); bufferedWriter.close(); } }