Interval Count - Hackerearth


C++ 14 Solution


#include<iostream>
using namespace std;
int main()
{
      int t ;
      cin>>t;
        while(t!=0)
        {
            int count = 0,i, n ;long long int l , r ,tmp = 0;
            cin>>n;
            cin>>l>>r;
            int p[n];
            long long int l1[r+1]={0};
            tmp = l;
            for(i=0;i<n;i++)
            {
                cin>>p[i];
                while(l<=r)
                {
                    if(l%p[i]==0&&l1[l]==0)
                    {
                        count++;
                        l1[l]=1;
                    }
                   
                    l++;
                }
               l = tmp;
            }
            cout<<count<<endl;
            t--;
        }
    return 0;
}

  
If anyone come up with better solution leave it in comment.
Keep Programming...!!!

Comments

Popular posts from this blog

First Unique Character in a String

Balanced Binary Tree

Majority Element

Sequential Digits