DATA STRUCTURE - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# DATA STRUCTURE

Posts: 2Member
PLS HELP ME SOLVE THIS QUESTION.................ITS VERY URGENT FOR ME..

1. An array contains 25 positive integers. Write a module which
(a) Finds all pairs of elements whose sum is 25.
(b) Find the number EVNUM of elements of A which are even, and the number ODNUM of elements of A which are odd.

• Posts: 6Member
> Finds all pairs of elements whose sum is 25
You can try to sort them first:
1. Sort the list - you can do it in O(n log n)
2.
[code]
j = n - 1;
for (i = 0; i < n - 1; i++)
{
for (; j > i; j--)
if (data[i] + data[j] == 25)
report(i, j);
else if (data[i] + data[j] < 25)
break;
if (data[i] + data[i+1] > 25)
break;
}
[/code]
> Find the number EVNUM of elements of A which are even, and the number ODNUM of elements of A which are odd
It's a linear task. Can't think of optimization unless some low level involved.