Latest Updates ☛
  • Graphs: Introduction of graphs and types of graphs
  • SPOJ : Geeky solves geeky solution
  • Solution Code : Pass the time problem on SPOJ.
  • Find the minimum shortest path solution in c language ! !

Friday, September 6, 2013

SPOJ:Solution to the Fear of Three for Ross problem (simply lovable)



Question :
You know what? Ross had three marriages. All unsuccessful :D Because of this, he is angry with everyone. Whenever he gets a list of numbers during his research work, he wants to remove the maximum 3 numbers out of it to show his anger. He asked you to write a program for him, that takes a list of numbers and prints the 3 maximum numbers in it in non increasing order.

Inputs
First Line will contain "N" the no. of elements in the list. Next Line will contain "N" integers.

Outputs :
The only line of output should contain 3 integers, that are maximum in the list in non increasing order.

Constraints
3 <= N <= 100, Numbers in the list can be between 1 and 1000 inclusive.




Level :Starter,Cakewalk

Solution to the Problem :


#include<stdio.h>
void perform(int *arr,int n)
{
     int max1,max2,max3;
     max1=arr[0];
     int i,j,k;
     int l;
     i=0;
     for(l=1;l<n;l++)
     {
         if(arr[l]>max1)
         {
           i=l;
           max1=arr[i];
         }
     }
     
     if(i==0)
     {
        j=1;
        max2=arr[1];
     }
     else
     {
         j=0;
         max2=arr[0];
     }
     
     for(l=0;l<n;l++)
     {
       if(l==i)
         continue;
       if(arr[l]>max2)
         {
            j=l;
            max2=arr[j];
         }
     }
     
     max3=-1;
     for(l=0;l<n;l++)
     {
       if(l==i||l==j)
        continue;
          if(arr[l]>max3)
            {
              k=l;
              max3=arr[k];
            }
     }
     printf("%d %d %d\n",max1,max2,max3);
}


main()
{
  int n,i;
  scanf("%d",&n);
  int arr[n];
  for(i=0;i<n;i++)
  scanf("%d",&arr[i]);
  perform(arr,n);
      
}




No comments :

Post a Comment

Privacy Policy | Disclaimer | Terms of Service

Copyright © 2013, Code Author. Powered by Blogger.