Maximum Sum of Array With No Repeating Digits

Computer Science Level pending

Find the maximum sum such that the subset of given integer array should not have any digit in common.

Let me give you an example, Suppose there is an array of 5 integers with positive integers as 14, 12, 23, 45, 39 .

14 and 12 cannot be taken in the subset as 1 is common in both. Similarly {12, 23}, {23, 39}, {14, 45} cannot be included in the same subset.

So the subset which forms the maximum sum is {12, 45, 39}. The maximum sum such formed is 96.

Solve for the following array with 100 elements:

[2608 3309 1051 6743 7560 2146 9625 3431 7103 695 6632 7911 5392 8303 2939 6380 2034 8788 5729 2365 3420 6945 7570 1709 3760 9273 6602 3182 8410 1063 5284 2140 2069 9415 315 4699 189 8212 8325 2557 8527 9435 8681 4548 5027 5654 4520 7469 2144 9380 7517 3692 984 4312 1161 8259 7636 8395 1398 2810 6810 9258 9253 8522 1988 6799 1443 416 3364 4540 7283 5109 1629 5125 9132 4561 6833 8136 7709 4709 3329 3882 7675 6351 3124 7211 8776 3268 1177 1729 4633 7023 3988 4715 8632 2193 2953 3814 7130 6392]


The answer is 24835.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Vishal Mittal
Nov 14, 2019

There are 2^N subsets possible for a set of N elements. Of course, in this problem, most of those 2^N sets cannot be considered. This is because of the constraint that the numbers in the subset must have mutually exclusive set of digits. In fact, a set will never have more than 10 numbers. Although, considering all sets of cardinality less than or equal to 10 is also impossible because there may simply be too many.

The key insight in this problem is that if we have two numbers made off the same set of digits, then we are always interested in the larger one. Hence, we never really have to consider more than 2^10 different numbers - since there are only 10 digits. Of course, a set of digits can be represented by a bit set of 10 bits. Thus, we store the largest given number for each bit set.

We know that two bitsets can be combined if they don’t have a set bit for the same digit. This can be tested by a bitwise-and operation. If two bitsets can be combined, we will update the combined bit-set with the larger sum. Just like the dynamic programming formulation for 0-1 knapsack, we can maintain the largest sum for each bit set as follows:

SUM[1 .. 1024] = {0}

for i = 1 to N
    b = bitset for Ai
    for m = 0 to 1024
        if (m & b) = 0
            SUM[m+b] = max(SUM[m+b], SUM[m] + SUM[b])

The above accomplishes finding the maximum sums for each bit set (with combined numbers) in O(N * 2^10) time.

C++ Solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
int maxSum(vector<int> &A) 
{
    int sum[1024] = {0};
    int num;

    int n = A.size();

    for(int i = 0; i < n; i++)
    {
        num = A[i];
        int nm = 0;
        while(num)
        {
            nm |= (1 << (num % 10));
            num /= 10;
        }
        sum[nm] = max(sum[nm], A[i]);
    }

    for(int i = 0; i < n; i++)
    {
        num = A[i];
        int nm = 0;
        while(num)
        {
            nm |= (1 << (num % 10));
            num /= 10;
        }

        for(int j = 0; j < 1024; j++)
        {
            if((nm & j) == 0)
                sum[nm + j] = max(sum[nm + j], sum[nm] + sum[j]);
        }
    }

    int mx = 0;

    for(int i = 0; i < 1024; i++)
        mx = max(mx, sum[i]);
    return mx;
}

The interesting part of this problem is there are ways to concatenate 4 of these elements together without overlap
{4540, 1161, 8788, 2939} = 17428
{3329, 4540, 1161, 8788} = 17818
But neither of those sums are larger than the sum created by combining only 3 elements
{6632, 8788, 9415} = 24835


Kyle T - 1 year, 6 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...