Related
You are given a list of integers nums of even length. Consider an operation where you pick any number in nums and update it with a value between [1, max(nums)]. Return the number of operations required such that for every i, nums[i] + nums[n - 1 - i] equals to the same number. The problem can be solved greedily.
Note: n is the size of the array and max(nums) is the maximum element in nums.
For example: nums = [1,5,4,5,9,3] the expected operations are 2.
Explanation: The maxnums is 9, so I can change any element of nums to any number between [1, 9] which costs one operation.
Choose 1 at index 0 and change it to 6
Choose 9 at index 4 and change it to 4.
Now this makes the nums[0] + nums[5] = nums[1] + nums[4] = nums[2] + nums[3] = 9. We had changed 2 numbers and it cost us 2 operations which is the minimum for this input.
The approach that I've used is to find the median of the sums and use that to find the number of operations greedily.
Let us find the all the sums of the array based on the given condition.
Sums can be calculated by nums[i] + nums[n-1-i].
Let i = 0, nums[0] + nums[6-1-0] = 4.
i = 1, nums[1] + nums[6-1-1] = 14.
i = 2, nums[2] + nums[6-1-2] = 9.
Store these sums in an array and sort it.
sums = [4,9,14] after sorting. Now find the median from sums which is 9 as it is the middle element.
Now I use this median to equalize the sums and we can find the number of operations. I've also added the code that I use to calculate the number of operations.
int operations = 0;
for(int i=0; i<nums.size()/2; i++) {
if(nums[i] + nums[nums.size()-1-i] == mid)
continue;
if(nums[i] + nums[nums.size()-1-i] > mid) {
if(nums[i] + 1 <= mid || 1 + nums[nums.size()-1-i] <= mid) {
operations++;
} else {
operations += 2;
}
} else if (maxnums + nums[nums.size()-1-i] >= mid || nums[i] + maxnums >= mid) {
operations++;
} else {
operations += 2;
}
}
The total operations for this example is 2 which is correct.
The problem here is that, for some cases choosing the median gives the wrong result. For example, the nums = [10, 7, 2, 9, 4, 1, 7, 3, 10, 8] expects 5 operations but my code gives 6 if the median (16) was chosen.
Is choosing the median not the most optimal approach? Can anyone help provide a better approach?
I think the following should work:
iterate pairs of numbers
for each pair, calculate the sum of that pair, as well as the min and max sum that can be achieved by changing just one of the values
update a dictionary/map with -1 when starting a new "region" requiring one fewer change, and +1 when that region is over
iterate the boundaries in that dictionary and update the total changes needed to find the sum that requires the fewest updates
Example code in Python, giving 9 as the best sum for your example, requiring 5 changes.
from collections import defaultdict
nums = [10, 7, 2, 9, 4, 1, 7, 3, 10, 8]
m = max(nums)
pairs = [(nums[i], nums[-1-i]) for i in range(len(nums)//2)]
print(pairs)
score = defaultdict(int)
for a, b in map(sorted, pairs):
low = a + 1
high = m + b
score[low] -= 1
score[a+b] -= 1
score[a+b+1] += 1
score[high+1] += 1
print(sorted(score.items()))
cur = best = len(nums)
num = None
for i in sorted(score):
cur += score[i]
print(i, cur)
if cur < best:
best, num = cur, i
print(best, num)
The total complexity of this should be O(nlogn), needing O(n) to create the dictionary, O(nlogn) for sorting, and O(n) for iterating the sorted values in that dictionary. (Do not use an array or the complexity could be much higher if max(nums) >> len(nums))
(UPDATED receiving additional information)
The optimal sum must be one of the following:
a sum of a pair -> because you can keep both numbers of that pair
the min value of a pair + 1 -> because it is the smallest possible sum you only need to change 1 of the numbers for that pair
the max value of a pair + the max overall value -> because it is the largest possible sum you only need to change 1 of the numbers for that pair
Hence, there are order N possible sums.
The total number of operations for this optimal sum can be calculated in various ways.
The O(N²) is quite trivial. And you can implement it quite easily if you want to confirm other solutions work.
Making it O(N log N)
getting all possible optimal sums O(N)
for each possible sum you can calculate occ the number of pairs having that exact sum and thus don't require any manipulation. O(N)
For all other pairs you just need to know if it requires 1 or 2 operations to get to that sum. Which is 2 when it is either impossible if the smallest of the pair is too big to reach sum with the smallest possible number or when the largest of the pair is too small to reach the sum with the largest possible number. Many data structures could be used for that (BIT, Tree, ..). I just used a sorted list and applied binary search (not exhaustively tested though). O(N log N)
Example solution in java:
int[] nums = new int[] {10, 7, 2, 9, 4, 1, 7, 3, 10, 8};
// preprocess pairs: O(N)
int min = 1
, max = nums[0];
List<Integer> minList = new ArrayList<>();
List<Integer> maxList = new ArrayList<>();
Map<Integer, Integer> occ = new HashMap<>();
for (int i=0;i<nums.length/2;i++) {
int curMin = Math.min(nums[i], nums[nums.length-1-i]);
int curMax = Math.max(nums[i], nums[nums.length-1-i]);
min = Math.min(min, curMin);
max = Math.max(max, curMax);
minList.add(curMin);
maxList.add(curMax);
// create all pair sums
int pairSum = nums[i] + nums[nums.length-1-i];
int currentOccurences = occ.getOrDefault(pairSum, 0);
occ.put(pairSum, currentOccurences + 1);
}
// sorting 0(N log N)
Collections.sort(minList);
Collections.sort(maxList);
// border cases
for (int a : minList) {
occ.putIfAbsent(a + max, 0);
}
for (int a : maxList) {
occ.putIfAbsent(a + min, 0);
}
// loop over all condidates O(N log N)
int best = (nums.length-2);
int med = max + min;
for (Map.Entry<Integer, Integer> entry : occ.entrySet()) {
int sum = entry.getKey();
int count = entry.getValue();
int requiredChanges = (nums.length / 2) - count;
if (sum > med) {
// border case where max of pair is too small to be changed to pair of sum
requiredChanges += countSmaller(maxList, sum - max);
} else if (sum < med) {
// border case where having a min of pair is too big to be changed to pair of sum
requiredChanges += countGreater(minList, sum - min);
}
System.out.println(sum + " -> " + requiredChanges);
best = Math.min(best, requiredChanges);
}
System.out.println("Result: " + best);
}
// O(log N)
private static int countGreater(List<Integer> list, int key) {
int low=0, high=list.size();
while(low < high) {
int mid = (low + high) / 2;
if (list.get(mid) <= key) {
low = mid + 1;
} else {
high = mid;
}
}
return list.size() - low;
}
// O(log N)
private static int countSmaller(List<Integer> list, int key) {
int low=0, high=list.size();
while(low < high) {
int mid = (low + high) / 2;
if (list.get(mid) < key) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
Just to offer some theory -- we can easily show that the upper bound for needed changes is n / 2, where n is the number of elements. This is because each pair can be made in one change to anything between 1 + C and max(nums) + C, where C is any of the two elements in a pair. For the smallest C, we can bind max(nums) + 1 at the highest; and for the largest C, we can bind 1 + max(nums) at the lowest.
Since those two bounds at the worst cases are equal, we are guaranteed there is some solution with at most N / 2 changes that leaves at least one C (array element) unchanged.
From that we conclude that an optimal solution either (1) has at least one pair where neither element is changed and the rest require only one change per pair, or (2) our optimal solution has n / 2 changes as discussed above.
We can therefore proceed to test each existing pair's single or zero change possibilities as candidates. We can iterate over a sorted list of two to three possibilities per pair, labeled with each cost and index. (Other authors on this page have offered similar ways and code.)
Given an array A of integers, of size N.
One can select any two adjacent indices, say i and i+1.
Operation: If A[i] == A[i+1], then you can remove A[i] and A[i+1], and put a single number in
their place with value A[i]+1.
Maximize the maximum possible value left in the array after performing the above operation 0 or more times.
Example:
A = 1, 2, 2, 3
After 1st operation -> 1 3 3
After 2nd operation -> 1 4
return 4
I couldn't come up with an optimized algorithm. I could think of the only Divide and Conquer approach.
Assuming Java. I came out with a solution but not performant like Collections.max, but I respected your algorithm:
List<Integer> a = new ArrayList<>(Arrays.asList(1, 2, 2, 3));
boolean repeat = true;
while (repeat) {
repeat = false;
for (int i = 0; i < a.size() - 1; i++) {
if (a.get(i).equals(a.get(i + 1))) {
a.set(i, a.get(i) + 1);
a.remove(i + 1);
repeat = true;
}
}
}
System.out.println(a.get(a.size() - 1));
I have an array that looks like this:
let arr = [1,2,3,4,5,6,7,8,9]
I know you can get min and max by:
let min = arr.min()
let max = arr.max()
But how do you get the median?
To get the median you can use the following:
let median = arr.sorted(by: <)[arr.count / 2]
In your case it will return 5.
As #Nirav pointed out [1,2,3,4,5,6,7,8] will return 5 but should return 4.5.
Use this instead:
func calculateMedian(array: [Int]) -> Float {
let sorted = array.sorted()
if sorted.count % 2 == 0 {
return Float((sorted[(sorted.count / 2)] + sorted[(sorted.count / 2) - 1])) / 2
} else {
return Float(sorted[(sorted.count - 1) / 2])
}
}
Usage:
let array = [1,2,3,4,5,6,7,8]
let m2 = calculateMedian(array: array) // 4.5
The median is defined as the number in the middle of the sequence. If there is not one middle number, then it's the average of the two middle numbers.
extension Array where Element == Int {
func median() -> Double {
let sortedArray = sorted()
if count % 2 != 0 {
return Double(sortedArray[count / 2])
} else {
return Double(sortedArray[count / 2] + sortedArray[count / 2 - 1]) / 2.0
}
}
}
Note that if the array is empty, the median is undefined. So a safe median function returns an optional, just like the min() and max() built-in methods do.
extension Array where Element == Int {
func median() -> Double? {
guard count > 0 else { return nil }
let sortedArray = self.sorted()
if count % 2 != 0 {
return Double(sortedArray[count/2])
} else {
return Double(sortedArray[count/2] + sortedArray[count/2 - 1]) / 2.0
}
}
}
With that defined, you can write:
if let median = arr.median() {
// do something
}
If someone (like me) likes two*-liners:
let sorted = arr.sorted(by: <)
let median = Double(sorted[arr.count/2] + sorted.reversed()[arr.count/2])/2.0
Algorithms that use sorted take O(n log n) time. That's typically not a problem for 9 numbers, but if your array is large, use an algorithm that completes in O(n) time. An example is this k-th largest element algorithm. It recursively partitions the array, but doesn’t have to go through all the work to sort it, so it’s much faster.
I need help with making an algorithm.
I am currently designing something for a class I am taking.
Given 4 numbers, I need to find all (or at least the first) combination of the 4 numbers using basic operations (+-*/) to make a certain answer.
For example, if given numbers [1,2,3,4].
and I have to make the answer 12.
I can see (without a program) that (2-1)*3*4 = 12
But for more complex numbers, it may be harder to solve just by thinking about it. So I require a program to help me find at least one possible combination to solve the problem.
Note that, in the given 4 numbers, the numbers may repeat, but each number can only used once.
For example, the set of 4 can be [2,3,3,4]. But in that set, 2 and 4 cannot be used more than once.
I originally had a plan to brute force find all the possible combinations/orders of each 4 numbers, then iterating through all the operations. I later realized that this won't work as it doesn't take into account an operations like (1-2)*(3+4).
So I was wondering if anyone had an idea of how I can approach solving this problem?
Please keep in mind, that I am still fairly new to programming, so I may not understand some of the more advanced terms and functions. But I can keep up pretty well with things like loops and arrays.
There aren't actually that many combinations to check, because the number of precedence cases is limited to 5:
((a:b):c):d
(a:b):(c:d)
(a:(b:c)):d
a:((b:c):d)
a:(b:(c:d))
so with 24 permutations and 3 choices from 4 possible operators, that gives 7680 combinations. And many of these combinations are really identical, because the precedence is unimportant in cases like:
a+b+c+d
a+b+c-d
a*b*c*d
a*b*c/d
Run the code snippet to see a simple loop-based algorithm which checks these 7680 combinations in action. There are a surprising number of solutions for the case 1:2:3:4=12.
function findArithmetic(target, numbers) {
// PUT THE ARITHMETIC FUNCTIONS IN AN ARRAY, SO WE CAN ITERATE OVER THEM
function sum(a, b) {return a + b}
function dif(a, b) {return a - b}
function prd(a, b) {return a * b}
function div(a, b) {return a / b}
var func = [sum, dif, prd, div];
// DEFINE THE ORDER OF THE CALCULATIONS FOR THE 5 PRECEDENCE CASES
var prec = [[0, 1, 4, 2, 5, 3], // 0,1,2,3 are the four numbers
[0, 1, 2, 3, 4, 5], // 4 is the result of the 1st calculation
[1, 2, 0, 4, 5, 3], // 5 is the result of the 2nd calculation
[1, 2, 4, 3, 0, 5], // so here, do 1:2, then result1:3, then 0:result2
[2, 3, 1, 4, 0, 5]]; // and here, do 2:3, then 1:result1, then 0:result2
// FIND ALL PERMUTATIONS OF THE NUMBERS AND STORE THEM IN ARRAY "NUMS"
var nums = [];
for (var a = 0; a < 4; a++) {
for (var b = 0; b < 4; b++) {
if (a == b) continue;
for (var c = 0; c < 4; c++) {
if (a == c || b == c) continue;
for (var d = 0; d < 4; d++) {
if (a == d || b == d || c == d) continue;
nums.push([numbers[a], numbers[b], numbers[c], numbers[d]]);
}
}
}
}
// NOW GET DOWN TO BUSINESS
var solutions = [];
// ITERATE OVER ALL 24 PERMUTATIONS
for (var n = 0; n < nums.length; n++) {
// ITERATE OVER ALL 5 PRECEDENCE CASES
for (var p = 0; p < 5; p++) {
// ITERATE OVER THE 4 OPERATORS FOR THE FIRST CALCULATION
for (var i = 0; i < 4; i++) {
// ITERATE OVER THE 4 OPERATORS FOR THE SECOND CALCULATION
for (var j = 0; j < 4; j++) {
// ITERATE OVER THE 4 OPERATORS FOR THE THIRD CALCULATION
for (var k = 0; k < 4; k++) {
// DO THE CALCULATIONS
nums[n][4] = func[i](nums[n][prec[p][0]], nums[n][prec[p][1]]);
nums[n][5] = func[j](nums[n][prec[p][2]], nums[n][prec[p][3]]);
var result = func[k](nums[n][prec[p][4]], nums[n][prec[p][5]]);
// IF THE RESULT IS CORRECT, MAKE A STRING AND ADD TO SOLUTIONS
if (result == target) {
solutions.push(makeString(n, p, i, j, k));
}
}
}
}
}
}
return solutions;
// TURN THE RESULT INTO A PRESENTABLE STRING
// this is a bit fiddly, because in each precedence case, the calculations are done in a different order
function makeString(n, p, i, j, k) {
// CHOOSE THE RIGHT STRING TEMPLATE, BASED ON THE PREFERENCE CASE
var str = ["((aAb)Bc)Cd", "(aAb)B(cCd)", "(aA(bBc))Cd", "aA((bBc)Cd)", "aA(bB(cCd))"][p];
// REPLACE "a", "b", "c", AND "d" WITH THE NUMBERS
for (var c = 0; c < 4; c++) str = str.replace(["a","b","c","d"][c], nums[n][c]);
// REPLACE "A", "B" AND "C" WITH THE OPERATORS, BASED ON EXECUTION ORDER IN PREFERENCE CASE
var order = [["A","B","C"], ["A","C","B"], ["B","A","C"], ["B","C","A"], ["C","B","A"]];
for (var c = 0; c < 3; c++) str = str.replace(order[p][c], ["+","-","*","/"][[i,j,k][c]]);
return str + "=" + target;
}
}
// RUN THE FUNCTION AND DISPLAY THE RESULTS IN THE CONSOLE
var sol = findArithmetic(12, [1,2,3,4]);
document.write(sol.length + " solutions found:<BR><PRE>");
for (var s in sol) document.write(sol[s] + "<BR>");
This is a simpler solution, without the precedence array. It has the calculations for the five precedence cases written out seperately. Usually programmers would consider this an unelegant solution, because it breaks the "don't repeat yourself" rule; however, in this case it makes the code much easier to understand, and it greatly simplifies the displaying of the results, so for once I think it makes sense to do it this way.
This version only returns one solution per permutation of the numbers and combination of operators, because solutions with different bracket placement, like (a*b)+(c-d) and ((a*b)+c)-d, are really just duplicates. (That's what the continue statement after each calculation is for.)
function findArithmetic(target, numbers) {
// PUT THE ARITHMETIC FUNCTIONS IN AN ARRAY, SO WE CAN ITERATE OVER THEM
function sum(a, b) {return a + b}
function dif(a, b) {return a - b}
function prd(a, b) {return a * b}
function div(a, b) {return a / b}
var func = [sum, dif, prd, div];
// FIND ALL PERMUTATIONS OF THE NUMBERS AND STORE THEM IN ARRAY "NUMS"
var nums = [];
for (var a = 0; a < 4; a++) {
for (var b = 0; b < 4; b++) {
if (a == b) continue;
for (var c = 0; c < 4; c++) {
if (a == c || b == c) continue;
for (var d = 0; d < 4; d++) {
if (a == d || b == d || c == d) continue;
nums.push([numbers[a], numbers[b], numbers[c], numbers[d]]);
}
}
}
}
// NOW GET DOWN TO BUSINESS
var solutions = [];
var op = ["+","-","*","/"];
// ITERATE OVER ALL 24 PERMUTATIONS
for (var n = 0; n < nums.length; n++) {
var a = nums[n][0], b = nums[n][1], c = nums[n][2], d = nums[n][3];
// ITERATE OVER THE 4 OPERATORS FOR THE FIRST CALCULATION
for (var i = 0; i < 4; i++) {
// ITERATE OVER THE 4 OPERATORS FOR THE SECOND CALCULATION
for (var j = 0; j < 4; j++) {
// ITERATE OVER THE 4 OPERATORS FOR THE THIRD CALCULATION
for (var k = 0; k < 4; k++) {
// CHECK PRECEDENCE CASE 1: ((a:b):c):d
if (target == func[k](func[j](func[i](a, b), c), d)) {
solutions.push("((" + a + op[i] + b + ")" + op[j] + c + ")" + op[k] + d + "=" + target);
continue;
}
// CHECK PRECEDENCE CASE 2: (a:b):(c:d)
if (target == func[j](func[i](a, b), func[k](c, d))) {
solutions.push("(" + a + op[i] + b + ")" + op[j] + "(" + c + op[k] + d + ")=" + target);
continue;
}
// CHECK PRECEDENCE CASE 3: (a:(b:c)):d
if (target == func[k](func[i](a, func[j](b, c)), d)) {
solutions.push("(" + a + op[i] + "(" + b + op[j] + c + "))" + op[k] + d + "=" + target);
continue;
}
// CHECK PRECEDENCE CASE 4: a:((b:c):d)
if (target == func[i](a, func[k](func[j](b, c), d))) {
solutions.push(a + op[i] + "((" + b + op[j] + c + ")" + op[k] + d + ")=" + target);
continue;
}
// CHECK PRECEDENCE CASE 5: a:(b:(c:d))
if (target == func[i](a, func[j](b, func[k](c, d)))) {
solutions.push(a + op[i] + "(" + b + op[j] + "(" + c + op[k] + d + "))=" + target);
}
}
}
}
}
return solutions;
}
// RUN THE FUNCTION AND DISPLAY THE RESULTS IN THE CONSOLE
var sol = findArithmetic(2, [4,5,6,12]);
document.write(sol.length + " solutions found:<BR><PRE>");
for (var s in sol) document.write(sol[s] + "<BR>");
You are looking for binary expression trees with 4 leafs. There is always a top-level node (+,*,-,/ in your case). For a given top level node, organize your search by the number of leafs to the left of the top-level node, which must be a number in the range 1 to 3. There is a natural recursive solution since, for example, if the left side has three leafs then it itself is a binary expression tree with three leafs. You really don't need to use tree data structures -- you can use fully parenthesized strings (e.g. like "(((1 + 2) * 3) / 4)" for the tree whose top node is "/" and whose left side is the tree "((1 + 2)*3)" and whose right side is the leaf 4).
I'm trying to find the second smallest element in an array of n elements using only n + ceil(lg n) - 2 comparisons. The hint in CLRS says to find the smallest element.
This takes n - 1 comparisons so I'm left with ceil(lg n) - 1 comparisons to find the second smallest, once I know the largest.
Any ideas?
Thanks,
bclayman
Let's say we've got a list a1...an with n being a power of 2.
First pair the elements up, let's say a1 with a2, a3 with a4 and so on, and compare them with each other. This gives you n/2 comparisons.
Advance all the winners to the next round, which only has n/2 elements now, and repeat the same process. This requires n/4 more comparisons.
Repeat the above until you've only got 1 element left, the ultimate winner. To get there you had to do n/2 + n/4 + ... + 1 = n-1 comparisons.
That's great but which one could be the second smallest? Well, it has to be one of the elements your winner had beaten along the way to the top. There are lg n such losers, so you need to compare them amongst each other to find the smallest (requiring a further lg n - 1 comparisons).
And the smallest of the losers is the second smallest overall.
It's easy to prove why the above method always finds the second smallest: since it's smaller than every element but the ultimate winner, it would win every round apart from the one against the champion.
If n isn't a power of 2, the process is almost exactly the same, except the list of losers will be as long as it would be for the next exact power of 2 which is why you end up with ceil(lg n).
Here is a basic implementation in JavaScript, not sure it fully respects your O() requirements in all cases though. The initial array will also affect the comparison count.
var elements = [ 12, 1 , 3, 4, 65, 7, -43, 8, 3, 8, 45, 2 ];
var nCompare = 0;
var smallest = elements[0], smaller = elements[0];
for(var i = 1; i < elements.length; ++i) {
++nCompare;
if(elements[i] < smaller) {
++nCompare;
if(elements[i] < smallest) {
smaller = smallest;
smallest = elements[i];
}
else
smaller = elements[i];
}
}
document.body.innerHTML = '<pre>\n' +
'Elements: [ ' + elements.join(', ') + ' ]\n' +
'# element: ' + elements.length + '\n' +
'\n' +
'Smallest: ' + smallest + '\n' +
'2nd smallest: ' + smaller + '\n' +
'# compare: ' + nCompare +
'</pre>';
Below is a solution with O(n) complexity in Java:
public class MainClass {
public static void main(String[] args) {
int[] a = { 4, 2, 8, -2, 56, 0 };
c(a);
}
private static void c(int[] a) {
int s = Integer.MAX_VALUE;
int ss = Integer.MAX_VALUE;
for (int i : a) {
if (i < s) {
ss = s;
s = i;
} else if (i < ss) {
ss = i;
}
}
System.out.println("smallest : " + s + " second smallest : " + ss);
}
}
Output : smallest : -2 second smallest : 0
I think their is no need to for cel(log n) -1 additional comparison as it can be done in O(n) only i.e in one scan with the help of an extra variable as given below:-
for(i,0,no_of_elem-1)
{
if(min>elem[i])
{
second_smallest=min;
min=elem[i];
}
}
You just store previous minimum in a variable as that will be your answer after scanning all elements.