Find the Minimum and Maximum Sum

Input Format
A single line of five space-separated integers.

Constraints

    \[1<arr[i]<10^9\]

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
  1. Find the Minimum and Maximum Element in array
  2. When you calculate the minSum and maxSum – discard the maxElement and minElement
  3. 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
  4. 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

    \[1<candles[i]<10^7\]

Output Format
Count of Biggest Element in array

Sample Input

3 2 1 3 5 6 5 4 5

Sample Output

1
  1. In the above example 6 is the biggest element and it occurs only once
  2. Find the biggest element in array
  3. 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;
    }

Comments are closed.