View Single Post
Old 08-28-2005, 03:51 AM   #11 (permalink)
Locutus
Registered User
 
Join Date: Aug 2005
Posts: 20
Locutus is on a distinguished road
If I'm understanding the problem correctly we're looking for the number of pairs for which the product is even/odd. A number is even if it has 2 as one of it's factors and odd if it hasn't.

So:
even*even=even
even*odd=even
odd*odd=odd.

op then becomes ((num_o * num_o) - num_o).
The total number of pairs is ((size * size) - size), so ep becomes ((size * size) - size) - op.

Code:
void odd_even_pairs_fast(const int* vec, std::size_t size, unsigned& ep, unsigned& op)
{
	op = 0;		//Being able to call the function more than once, for whatever reason, is probably nice.
	std::size_t i;
	
	for (i = 0; i < size; ++i)
	{
		if (!even(vec[i])) ++op;
	}

	op = (op*op) - op;
	ep = ((size * size) - size) - op;
}
I should probably test the code before posting, but m3h
Locutus is offline   Reply With Quote