Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
[ad_1]
Given an integer array arr[] of dimension N and a constructive integer Okay, the duty is to reduce the utmost of the array by changing any component arr[i] into two constructive parts (X, Y) at most Okay instances such that arr[i] = X + Y.
Examples:
Enter: arr = {9}, Okay = 2
Output: 3
Clarification: Operation 1: Change component 9 into {6, 3} then array turns into {6, 3}.
Operation 2: Change component 6 into {3, 3} then array turns into {3, 3, 3}.
So, the utmost component in arr[] after acting at most Okay operations are 3.Enter: arr = {2, 4, 8, 2}, Okay = 4
Output: 2
The issue will be solved utilizing binary search primarily based on the next thought:
Initilze begin with minimal potential reply and finish with most potential reply, then calculate the edge worth mid = (begin + finish) /2 and verify whether it is potential to make each component lower than or equals to mid in at most Okay operations. Whether it is potential, replace the consequence and shift finish of vary to mid – 1. In any other case, shift begin of vary to mid + 1.
Comply with the steps beneath to implement the above thought:
Beneath is the implementation of the above strategy.
|
Time Complexity: O(log2(max(arr)) * N), the place max(arr) is the utmost component and N is the scale of the given array.
Auxiliary House: O(1)
[ad_2]