Kiran's Cool Collections
Kiran is an avid collector of unique trinkets. He has a large box filled with items, each with a specific integer 'coolness' value. He wants to find the largest possible number of pairs of items where the sum of their coolness values is exactly K. Each item can only be used in at most one pair. Help Kiran find this maximum number of pairs!
Input
The first line contains two integers N and K, the number of items and the target sum. The second line contains N integers, the coolness values of the items.
Output
Print a single integer, the maximum number of pairs that sum to K.
Constraints
1 <= N <= 200000, 1 <= K <= 200000, 0 <= coolness_value <= 200000
Example 1
5 10 1 9 5 5 5
2
Pairs are (1,9) and (5,5). Max 2 pairs.
Example 2
6 8 2 6 2 6 4 4
3
Pairs are (2,6), (2,6), (4,4). Max 3 pairs.
Hints
Similar problems
- Longest Subarray with At Most K Distinct ElementsMedium
- Count Unique Element PairsMedium
- Count Subarrays with Distinct ElementsMedium
- Longest Subarray with Distinct ElementsHard
- Distinct Subarrays with Bounded SumHard
- Longest Subarray with Equal Number of Zeroes and OnesHard
More topics: Hash Map and Hashing problems, Two Pointers problems
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
5 10 1 9 5 5 5
6 8 2 6 2 6 4 4