Canna

Thursday, 27 September 2018

Minimum and Maximum

  • Problem Description

    " Chef loves to play with arrays by himself. Today, he has an array A consisting of N distinct integers. He wants to perform the following operation on his array A.
    Select a pair of adjacent integers and remove the larger one of these two. This decreases the array size by 1. Cost of this operation will be equal to the smaller of them. 
    Find out minimum sum of costs of operations needed to convert the array into a single element.
    Input
    First line of input contains a single integer T denoting the number of test cases. First line of each test case starts with an integer N denoting the size of the array A. Next line of input contains N space separated integers, where the ith integer denotes the value Ai.
    "
  • Test Case 1

    Input (stdin)
    1
    
    2
    
    3 4
    
    
    Expected Output
    3
  • Test Case 2

    Input (stdin)
    1
    
    5
    
    1 2 3 4 5
    
    
    Expected Output
    4
  • Program
  • #include <stdio.h>
    
    int main()
    {
     int i, t, n;
    
     scanf("%d", &t);
    
     for (i = 0; i < t; ++i) {
      scanf("%d", &n);
    
      int v[n];
    
      scanf("%d", & v[0]);
      
      int min = v[0], j;
      //int count = 0;
    
      for(j = 1; j < n; ++j) {
       scanf("%d", &v[j]);
       
       if (min > v[j]) {
        min = v[j];
       }
    
       //count += min;
      
      }
    
      
      printf("%ld\n", (long)min * (n - 1));
     }
    
     return 0;
    }

No comments:

Post a Comment

Note: only a member of this blog may post a comment.