"Largest Product in a Series"

So, this problem was in the Project Euler Contest of HackerRank, it was an easy level problem, but I think that it would be a challenging one is we don't understand the basic concepts of string manipulation. It also required a concept of using ASCII values to convert a char to int, which is normally a big headache in cpp, well most of the things are in cpp...well, we had to find the greatest product of consecutive terms in a string, and output it at the end of every Test Case.

Basically we had to get a range every time to get the product and then every time check with the max element as always, it's a very common practice to get a max elem, most of the novices must also be well aware about finding the max of a list. The tricky part was just the setting of range for the inner loop and making it dynamic, well things would have been much easier in Python but... it's fine!

I couldn't do much today, started my lectures on networking, did duo, something like 4 problems from HackerRank, mostly solved using cpp, did some revision of algos, but I couldn't get my SQL practice, and missed the Sections too. Well tommorrow's plan would be to do those things on priotity and yeah, practice the priority queue too.


int max=0;
        for(int i=0;i<=n-k;i++){
            int prod=1;
            // if(i==n-k)cout<<"i is "<<i<<endl;
                for(int j=i;j<i+k;j++){
                    // cout<<"for i "<<i<<" j will go till "<<i+k-1<<endl;
                    
                    //The below line doesn't work on chars which is quite 
                    //disgusting but it is what it is in cpp
                    // int elem=stoi(st[i]);
                    int elem=num[j]-48;
                    prod=prod*elem;
                }
            // cout<<"prod is "<<prod<<endl;
                if(prod>max){
                    max=prod;
                }
        }
        cout<<max<<endl;

Comments

Popular Posts