Wednesday 8 August 2012

There is an integer array consisting positive and negative integers. Find maximum positive difference S defined as: S = a[i] - a[j] where i>j and S > 0


#include<iostream.h>
#include<conio.h>
void main()
{
      clrscr();
      int arr[] ={-3,5,2,-2,1,0,-6,7,-8,2};
      int n=10,min=arr[0],max=-32767      ;
      for(int i=1;i<n;i++)
      {
            if(arr[i]<min)
            {
                  min=arr[i];
                  continue;
            }
            if(arr[i]-min>max)
                  max=arr[i]-min;
      }
      cout<<"MAX DIFFERENCE IS : "<<max;
            getch();
}




No comments:

Post a Comment