Data Structures & Algorithms Handson solutions cognizant (1)

Mini-Max Sum Data Structures and Algorithm Cognizant Handson Solution

In this post we are going to cover the Data Structures & Algorithms Mini-Max Sum handson solution asked in Cognizant.

Question of this handson is available here.

Steps to upload answer in GenC Platform:

  1. Click on this link.
  2. Insert the below code in Hacker Rank code editor.
  3. Compile & Execute the Code.
  4. Take screenshot of successful execution.
  5. 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 'miniMaxSum' function below.
     *
     * The function accepts INTEGER_ARRAY arr as parameter.
     */

    public static void miniMaxSum(List<Integer> arr) {
    // Write your code here
        Collections.sort(arr);
        Long minSum = 0L, maxSum = 0L;
        
        for(int i=0; i<4; i++)
            minSum += Long.valueOf(arr.get(i));

        for(int i=1; i<5; i++)
            maxSum += Long.valueOf(arr.get(i));
        
        System.out.print(minSum + " " + maxSum);
    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

        List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
            .map(Integer::parseInt)
            .collect(toList());

        Result.miniMaxSum(arr);

        bufferedReader.close();
    }
}

Similar Posts