Skip to content

Commit 7ce41ab

Browse files
committed
1775. Equal Sum Arrays With Minimum Number of Operations (Counting)
1 parent b88a113 commit 7ce41ab

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.cheehwatang.leetcode;
2+
3+
// Time Complexity : O(n + m),
4+
// where 'n' is the length of 'nums1', and 'm' is the length of 'nums2'.
5+
// We traverse 'nums1' once, and 'nums2' once.
6+
//
7+
// Space Complexity : O(1),
8+
// as the auxiliary space used is independent of the input.
9+
10+
public class EqualSumArraysWithMinimumNumberOfOperations_Counting {
11+
12+
// Approach:
13+
// Using counting array to keep track of the frequency of both arrays.
14+
// Since both array's elements can be manipulated, the counting array records the inversion of the second array.
15+
// Example: nums1 = [1,1], nums2 = [6,5,4], counting array = [3,1,1,0,0,0].
16+
// This is because, to make the sum equal, in this example,
17+
// we can either increase the 1 in 'nums1' to 6 or decrease the 6 in 'nums2' to 1.
18+
// In both cases, the 'difference' is decreased by 5.
19+
// Once we have the counting array, and the sum of both arrays,
20+
// we get change the smallest number in the counting array first as it has the largest impact on the 'difference'
21+
// (Greedy Approach).
22+
23+
public int minOperations(int[] nums1, int[] nums2) {
24+
// Case where it is impossible to get equal sum,
25+
// when one array is longer than 6 multiples of the second array length.
26+
if ((nums1.length * 6 < nums2.length) || (nums1.length > nums2.length * 6)) return -1;
27+
28+
// Use the countArray to record the frequency of each number in 'nums1',
29+
// as well as the inversion of the numbers in 'nums2'.
30+
// Additionally, record the sum of each array.
31+
int[] countArray = new int[7];
32+
int sumNum1 = 0;
33+
for (int number : nums1) {
34+
countArray[number]++;
35+
sumNum1 += number;
36+
}
37+
int sumNum2 = 0;
38+
for (int number : nums2) {
39+
countArray[7 - number]++;
40+
sumNum2 += number;
41+
}
42+
43+
int count = 0;
44+
int difference = sumNum1 - sumNum2;
45+
// In both cases, from the number with the largest impact (1 if difference is negative, 6 if difference is positive),
46+
// the operations to make is either:
47+
// - all the frequency of the number (with remainders of the difference), or
48+
// - the remainders of the difference divided by the number.
49+
if (difference < 0) {
50+
for (int i = 1; i < 6; i++) {
51+
int operations = Math.min(countArray[i], -difference / (6 - i) + (-difference % (6 - i) == 0 ? 0 : 1));
52+
difference += operations * (6 - i);
53+
count += operations;
54+
if (difference >= 0) break;
55+
}
56+
} else if (difference > 0) {
57+
for (int i = 6; i > 1; i--) {
58+
int operations = Math.min(countArray[i], difference / (i - 1) + (difference % (i - 1) == 0 ? 0 : 1));
59+
difference -= operations * (i - 1);
60+
count += operations;
61+
if (difference <= 0) break;
62+
}
63+
}
64+
return count;
65+
}
66+
}

0 commit comments

Comments
 (0)