easyHashingPattern: Hashing

Distinct Hash Sum Lane Assignment Solution

Problem Statement

You are given an array of integers and the number of lanes. Assign each integer to a lane such that no two integers with the same hash value are in the same lane. The hash value of an integer is the sum of its digits.

Examples

Example 1:
Input:[123, 456, 789]
Output:[0, 1, 2]
Explanation: Here, the hash values are 6, 15, and 24 respectively.
Example 2:
Input:[111, 222, 333]
Output:[0, 1, 2]
Explanation: Here, the hash values are 3, 6, and 9 respectively.
Example 3:
Input:[1234, 2345, 3456]
Output:[0, 1, 2]
Explanation: Here, the hash values are 10, 15, and 20 respectively.

Constraints

  • The length of the input array can be greater than the number of lanes.
  • The input array can contain negative integers.
  • The number of lanes can be 1.
Time: O(n log n) Space: O(n)
The optimized approach is to calculate the hash values of all the integers in the array and then sort them in ascending order. Finally, assign each integer to a lane based on its hash value.

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.