Skip to content

Commit a34f347

Browse files
Added soltion for max sum of numbers with equal sum digit.
1 parent 6e5b6d5 commit a34f347

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Happy to accept any contributions to this in any langugage.
5858
23. [Leet Code 59 - Spiral Matrix II](https://leetcode.com/problems/spiral-matrix-ii/) | [Solution](level_medium/SpiralMatrixII.ts)
5959
24. [Leet Code 151 - Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [Solution](level_medium/LeetCode_151_Reverse_Words_In_a_String.ts)
6060
25. [Leet Code 1647 - Minimum Deletions to Make Character Frequency Unique](https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/) | [Solution](level_medium/LeetCode_1647_Min_Deletions_For_Unique_Freq.ts)
61+
26. [Microsoft OA - Max Sum of Numbers with Equal Sum Digits](https://leetcode.com/discuss/interview-question/915683/) | [Solution](./level_medium/MaxSumOfNumbersWithEqualSumOfDigit.ts)
6162

6263

6364
## Hard
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { S_IFDIR } from "constants";
2+
import { diffieHellman } from "crypto";
3+
4+
/**
5+
* Problem: https://leetcode.com/discuss/interview-question/915683/
6+
*
7+
* Solution is based on the hashing technique. For any element which has the same digit sum, we can hash it by digit sum
8+
* and save it. Once we have any item saved, we can compamake all possible pairs of it and calculate the maximum sum.
9+
*
10+
* However, creating every pair is O(n*n), which can be optimized, by only keeping track of the max element so far.
11+
* If the new element, creates a sum which is max than the earlier, we know the new curr is maximum of all the previously
12+
* seen.
13+
*
14+
* Time: O(n), Space: O(n) for hashing.
15+
*/
16+
function findMaxSum(input: number[]): number {
17+
// Will keep track of the max element for each pair of the digit sum that we find.
18+
const map = new Map<number, number>();
19+
let max = -Infinity;
20+
input.forEach(curr => {
21+
const digitSum = getDigitSum(curr)
22+
if (map.has(digitSum)) {
23+
const prev = map.get(digitSum)
24+
max = Math.max(max, prev + curr)
25+
// Update the previous stored element for digit sum if this is greater.
26+
map.set(digitSum, Math.max(prev, curr));
27+
} else {
28+
map.set(digitSum, curr);
29+
}
30+
});
31+
32+
return max !== -Infinity ? max : -1;
33+
}
34+
35+
function getDigitSum(elem) {
36+
let sum = 0;
37+
while(elem !== 0) {
38+
sum += elem % 10;
39+
elem = Math.floor(elem / 10)
40+
}
41+
42+
return sum
43+
}
44+
45+
console.log(findMaxSum([51, 71, 17, 42]))
46+
console.log(findMaxSum([42, 33, 60]))
47+
console.log(findMaxSum([51, 32, 43]))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy