Hackerearth Min-Max

code:

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{

    int n,flag=0;
    cin>>n;
    int a[n];
    for(int i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
    }

    int    mini=a[0];
    int    maxi=a[0];

    for(int i=0; i<n; i++)
    {
        if(a[i]<mini)
        {
            mini=a[i];
        }
        if(a[i]>maxi)
        {
            maxi=a[i];
        }
    }
    //cout<<mini<<" "<<maxi;
    //  maxi-mini;

    for(int i=mini; i<maxi; i++)
    {
        for(int j=0; j<n; j++)
        {
            if(a[j]==i)
            {
                flag++;
                break;
            }
            else
            {
                continue;
            }
        }
    }
    int q=maxi-mini;
    if(flag==q)
    {
        printf("YES\n");
    }
    else
    {
        printf("NO\n");
    }
    //cout<<flag;

//    for(int i=1; i<=n; i++)
//    {
//        if(temp[i]==a[i])
//        {
//            printf("Yes\n");
//        }
//        else
//        {
//            printf("No\n");
//        }
//    }

    return 0;
}

partially accepted code with vector:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    cin>>n;
    vector<int> a(n);
    for(int i=0; i<n; i++)
    {
        cin>>a[i];
    }
    sort(a.begin(),a.end());
    bool yesnoo=true;
    for(int i=1; i<n; i++)
    {
        if((a[i]-a[i-1])!=1)
        {
            yesnoo=false;
        }
    }

    if(yesnoo)
        cout<<"YES"<<endl;
    else
        cout<<"NO"<<endl;


    return 0;
}

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published. Required fields are marked *