N number of painting kits
ith kit has strength Ai
Bob gets first chance to select kits
Bob can finish first if total strength of his kits is greater
Find minimum number of kits to be selected by Bob
[
{
input: [5, 3, 4, 1, 2],
output: 2,
},
]function kitSelection(kits) {
kits = mergeSort(kits)
let aliceSum = kits.reduce((acc, kit) => acc + kit)
let bobSum = 0
let count = 0
let length = kits.length
while (count < length) {
const kit = kits[length - 1 - count]
count++
bobSum += kit
aliceSum -= kit
if (bobSum > aliceSum) break
}
return count
}
function mergeSort(array) {
function mergeSortedArrays(array1, array2) {
const mergedArray = []
let index1 = 0
let index2 = 0
while (index1 < array1.length && index2 < array2.length) {
if (array1[index1] < array2[index2]) {
mergedArray.push(array1[index1])
index1++
} else {
mergedArray.push(array2[index2])
index2++
}
}
while (index1 < array1.length) {
mergedArray.push(array1[index1])
index1++
}
while (index2 < array2.length) {
mergedArray.push(array2[index2])
index2++
}
return mergedArray
}
if (array.length <= 1) return array
const middle = Math.floor(array.length / 2)
const left = mergeSort(array.slice(0, middle))
const right = mergeSort(array.slice(middle))
const sortedArray = mergeSortedArrays(left, right)
return sortedArray
}