Thursday, September 13, 2018

CHOOSE THE SECOND SMALL

  • Problem Description

    Ravi and Rahul wants to play a pebble game.Ravi placed n heaps of pebbles.Each heap is of different count.Ravi tied the eyes of Rahul and asked him to find the second smallest heap from the n heaps .Help Rahul to find the second smallest heap.
  • CODING ARENA
  • #include <stdio.h>
    int main()
    {
      int s,ss;
      int a[100],size,i;
      scanf("%d",&size);
      for(i=0;i<size;i++)
        scanf("%d",&a[i]);
      if(a[0]<a[1])
      {
        s=a[0];
        ss=a[1];
      }
      else
      {
        s=a[1];
        ss=a[0];
      }
      for(i=2;i<size;i++)
      {
        if(a[1]<s)
        {
          ss=s;
          s=a[i];
        }
        else if(a[i]<ss)
        {
          ss=a[i];
        }
      }
      printf("%d",s);
    return 0;
    }
  • Test Case 1

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

    Input (stdin)
    3
    
    4 2 1
    
    
    Expected Output
    2

No comments:

Post a Comment