Marc's Cakewalk Hackerrank Solution - Java
For Explanation watch video :
Code :
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 'marcsCakewalk' function below.
*
* The function is expected to return a LONG_INTEGER.
* The function accepts INTEGER_ARRAY calorie as parameter.
*/
public static long marcsCakewalk(List<Integer> calorie) {
Collections.sort(calorie,Collections.reverseOrder());
long min_miles = 0;
for(int i=0;i<calorie.size();i++){
min_miles = min_miles + ((long)Math.pow(2,i)*calorie.get(i));
}
return min_miles;
}
}
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")));
int n = Integer.parseInt(bufferedReader.readLine().trim());
List<Integer> calorie = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
long result = Result.marcsCakewalk(calorie);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
Comments
Post a Comment