Can anyone tell me the difference between code(logic)?

here pro[] is same in both the logic case and have all positive number(and /or may have zero). I was trying to implement that if the modulus by 4 of array elements is not equal to 2 then the count will increase by 1.

logic 1:

for(p=0; p<k; p++)
{
if(pro[p]%2==0)
{
if(pro[p]%4==0)
{
count++;
}
}
else
{
count++;
}
}

logic 2:

for(p=0; p<k; p++)
{
if(pro[p]%4 !=2)
{
count++;
}
}

logic 1 got accepted but logic 2 shows wrong answer.

Which problem is this? Please share the link.

Also, if logic 1 got accepted, then basically you are just checking if the number is divisible by 4, you don’t need to check if it is by 2 or not.

In the second approach, the number pro[p] could be odd and then the count would be incremented, which is not what you want, and thus it is failing.

1 Like

Yes, please post the Problem link and the links to both solutions - as far as I can tell, if pro consists only of positive numbers, the two logics should be the same:

#include <iostream>
#include <vector>
#include <cassert>

using namespace std;

int main()
{
    const int k = 200;
    int p;
    vector<int> pro;
    for (int i = -10; i < k; i++)
    {
        pro.push_back(i);
    }

    vector<int> countedByLogic1;
    for(p=0; p<k; p++)
    {
        if(pro[p]%2==0)
        {
            if(pro[p]%4==0)
            {
                countedByLogic1.push_back(pro[p]);
            }
        }
        else
        {
            countedByLogic1.push_back(pro[p]);
        }
    }

    vector<int> countedByLogic2;
    for(p=0; p<k; p++)
    {
        if(pro[p]%4 !=2)
        {
            countedByLogic2.push_back(pro[p]);
        }
    }
    cout << "Counted by logic 1:" << endl;
    for (const auto x : countedByLogic1)
    {
        cout << x << " ";
    }
    cout << endl;
    cout << "Counted by logic 2:" << endl;
    for (const auto x : countedByLogic2)
    {
        cout << x << " ";
    }
    assert(countedByLogic1 == countedByLogic2);
}

They give different results if pro can contain negative numbers, though.

3 Likes

btw he has already written values are +ve/0

@tanmay_garg @ssjgz @sebastian thanks for your reply.

problem link- https://www.codechef.com/problems/SQRDSUB

solution 1(accepted)- https://www.codechef.com/viewsolution/33025541

solution 2(wrong ans)- https://www.codechef.com/viewsolution/33025571

The first one got partially accepted and the other is wrong, I just want to know why it is wrong in 2nd solution as elements of pro[] is same in both solutions, and the logic applied in both solution for the “count” is same according to me( but implemented differently in 2nd “for” loop in subarray function)

P.S. - Please check solutions only (i know it will not get fully correct/accepted by this approach).

As in logic 1 also i am incrementing count if the element of pro[] is odd.
Please check my reply having problem and solution link below.

Thanks for your reply. please check my reply having problem and solution links.

Thanks - consider the test input:

1        
3                          
9000001 9000002 9000003

Edit:

Wait - ignore that - both of your solutions give the same output for that (though it doesn’t match the setter’s output).

Edit2:

No, I was right the first time - the TLE solution agrees with the Setter’s solution; the WA answer does not :slight_smile:

2 Likes

How that is happening as output is same in both the solution but it doesn’t match with setters output.

I take a page from House, M.D.'s book and never, ever trust the patient’s assertions :slight_smile:

4 Likes

See my Edit2 :slight_smile:

2 Likes

@ssjgz
Yes, I checked. there is datatype issue as 9000001 * 9000002 * 9000003 is in 10^20 and the limit of long is in 10^18 only that’s why it is storing negative value. which datatype should I use to store 10^20 value?

theres no data type to handle that Large number(10 ^20) in cpp

1 Like

ok thanks for the info.