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.
Yes, please post the Problem link and the links to both solutions - as far as I can tell, ifpro 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.
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).
@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?