Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 39796

Yet another number game question-getting "time limit exceeded"

$
0
0

I submitted the following solution for the problem-Yet another number game.

#include <iostream>

int main()
{
    using namespace std;

    int test;
    int num;
    int k;
    cin >> test;
    int *p = new int[test];

    int b;
    for(int i=0;i<test;i++)
    {
        cin >> num;

        if(num==1){
            p[i] = 1;
        }
        b=0;
        while(num > 1){

            b = b+1;
            if(num%2==0){

                num = num-(num/2);

            }else{

            for(k=3;k<=(num/2);k=k+2)
            {
                if(num%k==0){

                    num = num - (num/k);


                    break;
                }
            }
            if((num%k) != 0){

                num = num-1;
            }

         }

        }
            p[i] = b;


    }
        for(int j=0;j<test;j++)
        {
            if(p[j]%2==0){

                cout << "BOB" << endl;
            }else{

                cout << "ALICE" << endl;
            }

        }

        return 0;
}

But I am getting time limit exceeded .I have tried optimizing my approach but still getting the error. Please help me fix it.


Viewing all articles
Browse latest Browse all 39796

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>