BEENUMS - Beehive Numbers Editorial SPOJ - UCS - Unleash-Coding-Skills

Saturday, 5 May 2018

BEENUMS - Beehive Numbers Editorial SPOJ - UCS


The link for the problem BEENUMS - Beehive Numbers is:

http://www.spoj.com/problems/BEENUMS/

The solution to this problem is very simple we just need to find the beehive sequence.The logic required for this sequence is found in the following link.

https://en.wikipedia.org/wiki/Centered_hexagonal_number

Based on the formula the beehive number is generated by  (3* n*(n-1)+1). The CPP solution for this problem which is O(n) is given below for the problem BEENUMS - Beehive Numbers.

#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<long long>v;
  long long   i=1;
  long long r = 3*i*(i-1)+1;
    while(r<=1000000000)
    {  
        i++;
        v.push_back(r);
        r = 3*i*(i-1)+1;
    }
    long long inp;
    while(1)
    {
        cin>>inp;
        if(inp==-1)
            break;
        else
        {
           int fl=0;
            for(i=0;i<v.size()&&v[i]<=inp;i++)
            {
                if(v[i]==inp)
                    fl=1;
            }
            if(fl==0)
                cout<<"N"<<endl;
            else
                cout<<"Y"<<endl;
        }
    }
    return 0;
}

Happy Coding............

No comments:

Post a comment