Algorithm for printing maximum no of pairs

Revision en1, by ss_96, 2017-09-23 17:27:37

The problem statement:

There is an array given of 1s and -1s and there is a distance K,the output required is the maximum no of pairs such that their sum is 0 and both the numbers are within K distance.

eg: for array:

1 -1 1 -1 and K = 1 answer:2

This can be done with brute force,but i think there would a more efficient algorithm than just brute force.Can anyone please suggest something better than this?

Thanks.

Tags c++, algorithm, array

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English ss_96 2017-09-23 18:01:48 45
en2 English ss_96 2017-09-23 17:58:58 41 Tiny change: ' this?\n\nThanks' -> ' this?\n\nEdit:A number can be taken only once.\n\nThanks'
en1 English ss_96 2017-09-23 17:27:37 473 Initial revision (published)