Тёмный

IBM Coding Assessment | Find Total Cost Array Reduction | All testcases passed 

Coding Cha Kida
Подписаться 408
Просмотров 1,1 тыс.
50% 1

🔔 Subscribe to our channel for more solution updates! 🔔
Don't forget to like 👍, share 📲 with your friends
📌Instagram :
/ codingchakida
📌Telegram :
t.me/+wWGfGTOG...
Coding Question:
Given an array arr of n integers, a sequence of n-1 operations must be performed on the array.
In each operation,
• Remove the minimum and maximum elements from the current array and add their sum back to the array.
• The cost of an operation, cost = ceil((minimum_element + maximum_element) / (maximum_element-minimum_element + 1)).
Find the total cost to reduce the array to a single element.
Example
Given arr = [2, 3, 4, 5, 7].
The possible sequence of operations are:
1. Choose 2 and 7, the cost = ceil((2+7)/(7-2+1)] = ceil[9/6] = 2. Remove 2 and 7, append 9, arr = [3, 4, 5, 9], total_cost = 2.
2. Choose 3 and 9, the cost = ceil((3 + 9)/(9-3+1)] = ceil[12/7]=2, arr = [4, 5, 12], total_cost = 2+2=4.
3. Choose 4 and 12, the cost = ceil[(4+12)/(12-4+1)] = ceil[16/9]=2, arr = [5,16], total_cost = 4+2=6.
4. Choose 5 and 15, the cost = ceil[(5+15)/(15-5+ 1)] = ceil[20/11] = 2, arr = [6, 20], total_cost = 6 + 2=8.
5. Choose 6 and 20, the cost = ceil[(6 +20) / (20-6+ 1)] = ceil[26/15] = 2, arr = [26], total_cost = 8+2 = 10.

Опубликовано:

 

30 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 1   
@nausheenshaik5025
@nausheenshaik5025 5 дней назад
. Choose 5 and 15, the cost = ceil[(5+15)/(15-5+ 1)] = ceil[20/11] = 2, arr = [6, 20], total_cost = 6 + 2=8. it should be 5 and 16 then answer will be 8
Далее
Is Computer Science still worth it?
20:08
Просмотров 346 тыс.
Se las dejo ahí.
00:10
Просмотров 5 млн
titan tvman's plan (skibidi toilet 77)
01:00
Просмотров 6 млн
8 Must Know JavaScript Array Methods
10:05
Просмотров 1 млн
Mindset of Successful Programmers
4:56
Просмотров 1 млн
Top 7 Algorithms for Coding Interviews Explained SIMPLY
21:22
Rotate Array - Leetcode 189 - Python
8:59
Просмотров 149 тыс.