返回

LeetCode 2341. Maximum Number of Pairs in Array: A Comprehensive Walkthrough in Python

后端

Understanding the Problem

The problem statement for LeetCode 2341 is as follows:

Given an array of distinct integers nums and an integer k, return the maximum number of pairs (i, j) where i < j and nums[i] + nums[j] == k.

To solve this problem, we need to understand the following key points:

  1. The array nums contains distinct integers, meaning that each element appears only once in the array.
  2. We are interested in finding pairs (i, j) where i < j and nums[i] + nums[j] == k. In other words, we are looking for pairs of elements that sum up to k, and the indices i and j must be in ascending order.
  3. Our objective is to find the maximum number of such pairs that can be formed from the array.

Python Solution: A Step-by-Step Approach

Now, let's dive into the Python solution to this problem:

  1. Initialization : We start by initializing an empty hash map freq and a variable max_pairs to store the maximum number of pairs. freq will be used to keep track of the frequency of each element in the array.

  2. Iterating Through the Array : We iterate through the array nums using a for loop. For each element num, we perform the following steps:

    # Increment the frequency of the current element in the hash map
    freq[num] += 1
    
    # Check if there exists an element in the hash map whose frequency is greater than 0 and whose difference from the current element is equal to k
    if freq.get(num + k, 0) > 0:
        # Increment the maximum number of pairs
        max_pairs += freq.get(num + k, 0)
    
    # Check if there exists an element in the hash map whose frequency is greater than 0 and whose difference from the current element is equal to -k
    if freq.get(num - k, 0) > 0:
        # Increment the maximum number of pairs
        max_pairs += freq.get(num - k, 0)
    
  3. Returning the Maximum Number of Pairs : Finally, we return the value of max_pairs.

Here is the complete Python code for the solution:

def max_pairs(nums, k):
    """
    Returns the maximum number of pairs (i, j) where i < j and nums[i] + nums[j] == k.

    Args:
    nums: A list of distinct integers.
    k: An integer.

    Returns:
    The maximum number of pairs that can be formed from the array.
    """

    # Initialize a hash map to store the frequency of each element in the array
    freq = {}

    # Initialize a variable to store the maximum number of pairs
    max_pairs = 0

    # Iterate through the array
    for num in nums:
        # Increment the frequency of the current element in the hash map
        freq[num] += 1

        # Check if there exists an element in the hash map whose frequency is greater than 0 and whose difference from the current element is equal to k
        if freq.get(num + k, 0) > 0:
            # Increment the maximum number of pairs
            max_pairs += freq.get(num + k, 0)

        # Check if there exists an element in the hash map whose frequency is greater than 0 and whose difference from the current element is equal to -k
        if freq.get(num - k, 0) > 0:
            # Increment the maximum number of pairs
            max_pairs += freq.get(num - k, 0)

    # Return the maximum number of pairs
    return max_pairs

Conclusion

In this article, we thoroughly explored LeetCode problem 2341: "Maximum Number of Pairs in Array". We provided a detailed explanation of the problem statement, breaking down its key elements and requirements. Moreover, we presented a comprehensive Python solution that efficiently solves the problem. Our solution leverages a hash map to keep track of the frequency of each element in the array and employs a clever approach to identify and count the maximum number of pairs that satisfy the given conditions. Through this solution, we demonstrated the practical application of hash maps in solving algorithmic problems and highlighted the importance of carefully analyzing the problem statement to devise an optimal solution.