Find the Minimum and Maximum Sum
Input Format
A single line of five space-separated integers.
Constraints
Output Format
Print two space-separated long integers denoting the respective minimum and maximum values.
Sample Input
1 2 3 4 5
Sample Output
10 14
- Find the Minimum and Maximum Element in array
- When you calculate the minSum and maxSum – discard the maxElement and minElement
- Trick is you may miss logic if the array contains 5 elements and all are of same value. In such case the maximum and minimum value elements are same
- For finding minimum number, start by assigning maximum number to minNum
. . public static void miniMaxSum(List<Integer> arr) { long minNum = 0; long maxNum = 0; long minSum = 0; long maxSum = 0; //To find maximum Number for(int num=0;num<arr.size();num++){ if(maxNum<=arr.get(num)){ maxNum = arr.get(num); } } //To find minimum Number minNum = maxNum; for(int num=0;num<arr.size();num++){ if(arr.get(num)<=minNum){ minNum = arr.get(num); } } for(int num=0;num<arr.size();num++){ //If the array elements are of same value if(num!=0 && arr.get(num) == arr.get(num-1)){ minSum +=arr.get(num); maxSum +=arr.get(num); } if(arr.get(num) != maxNum) minSum +=arr.get(num); if(arr.get(num) != minNum) maxSum +=arr.get(num); } System.out.println(minSum); System.out.println(maxSum); } . .
Get Count of biggest Element in array – Total Tallest Candles to be blown
Input Format
Array of Integers
Constraints
Output Format
Count of Biggest Element in array
Sample Input
3 2 1 3 5 6 5 4 5
Sample Output
1
- In the above example 6 is the biggest element and it occurs only once
- Find the biggest element in array
- Find the count of the biggest element
public static int birthdayCakeCandles(List<Integer> candles) { // Write your code here long tallestCandle = 0; int tallestCandleCnt = 0; for(int num=0;num<candles.size();num++){ if(tallestCandle<=candles.get(num)){ tallestCandle = candles.get(num); } } for(int num=0;num<candles.size();num++){ if(candles.get(num) == tallestCandle) tallestCandleCnt +=1; } return tallestCandleCnt; }