UVA 11057 Exact Sum Solution

Solution in C++:

///**********ALLAH IS ALMIGHTY************///

///AH Tonmoy

///Department of CSE,23rd batch

///Islamic University,Bangladesh 

#include <bits/stdc++.h>

using namespace std;

int main()

{

    int n,i,m,mn,j,x,y,b1,b2,d;

    while(cin>>n)

    {

        int a[n+1]= {0};

        for(i=0; i<n; i++)

        {

            cin>>a[i];

        }

        sort(a,a+n);

        cin>>m;

        mn=1000000;

       for(i=0;i<n;i++)

       {

           for(j=i;j<n;j++)

           {

               x=a[i];

               y=a[j];

               d=abs(x-y);

               if(d<mn&&x+y==m)

               {

                   b1=x;

                   b2=y;

                   mn=d;

               }

           }

       }

        printf("Peter should buy books whose prices are %d and %d.\n\n",b1,b2);

    }

}


No comments

Most View Post

Recent post

Codeforces Round 925 (Div. 3) 1931D. Divisible Pairs Solution

    Problem Link  :   https://codeforces.com/contest/1931/problem/D S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. ...

Powered by Blogger.