Archive / 2021 / November 2021
The Bounteous Knight
Sir Galahad is known throughout the kingdom for his kindness and generosity. With the kingdom facing a deadly famine, he has decided to help as many villagers as he can by providing each villager with two sacks of rye produced in his own lands.
Sir Galahad has n number of sacks all weighing different amounts, as \(a\)\(1\), \(a\)\(2\), …,\(a\)\(n\). There are no two sacks of rye (\(i\), \(j\)) (\(1\) \(\leq\) \(i\), \(j\) \(\leq\) \(n\)) where \(i\) \(\neq\) \(j\) and \(a\)\(i\) = \(a\)\(j\).
If two different villagers are given sacks of rye weighing different amounts, Sir Galahad believes it may cause a conflict. Such that if one villager is given rye sacks \(a\)\(i\) and \(a\)\(j\), whilst another villager is given rye sacks \(a\)\(k\) and \(a\)\(p\) where, (\(a\)\(i\) + \(a\)\(j\)) \(\neq\) (\(a\)\(k\) + \(a\)\(p\)) it causes conflict. Sir Galahad wants to give two sacks of rye to each villager and a sack of rye cannot be shared between two or more villagers.
With the goal of helping as many villagers as he can, Sir Galahad instructs you to find the maximum number of villagers, he can provide two sacks of rye to, without causing any conflict.
Input Format
- The first line contains the number of rye sacks Sir Galahad has, integer \(n\).
- The second line contains \(n\) integers \(a\)\(1\), \(a\)\(2\),…,\(a\)\(n\), the weights of the rye sacks.
Output Format
- Single line containing the maximum number of villagers, Sir Galahad can provide two sacks of rye to, without causing any conflict.
Constraints
- The number of rye sacks, \(n\) is in the range \(2\) \(\leq\) \(n\) \(\leq\) \(1000\)
- The weights of the rye sacks, \(a\)\(i\) is in the range \(1\) \(\leq\) \(a\)\(i\) \(\leq\) \(10\)\(5\).
- All weights of the rye sacks are guaranteed to be distinct.
Sample Input 0
8
1 8 3 11 4 9 2 7
Sample Output 0
3
Sample Input 1
7
3 1 7 11 9 2 12
Sample Output 1
2
Open on HackerRank ↗ · Markdown source · Back to the archive