Saturday, August 18, 2018

SMALLEST VALUES


  • Problem Description

    You are given a sequence a1, a2, ..., aN. Find the smallest possible value of ai + aj, where 1 ? i < j ? N.The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each description consists of a single integer N. The second line of each description contains N space separated integers - a1, a2, ..., aN respectively. For each test case, output a single line containing a single integer - the smallest possible sum for the corresponding test case.
  • CODING ARENA
  • #include <stdio.h>
    #include<stdlib.h>
    int compare(const void *a,const void *b);
    int main()
    {
      int n,j,t,ans;
      scanf("%d",&t);
      while(t!=0)
      {
        scanf("%d",&n);
        int a[n];
        for(j=0;j<n;j++)
        {
          scanf("%d",&a[j]);
        }
        qsort(a,n,sizeof(int),compare);
        ans=a[0]+a[1];
        printf("%d",ans);
        t--;
      }
      return 0;
    }
    int compare(const void *a,const void *b)
    {
      return(*(int*)a-*(int*)b);
    }


      

  • Test Case 1

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

    Input (stdin)
    1
    
    3
    
    2 3 4
    
    
    Expected Output
    5

No comments:

Post a Comment