1
// This Question Includes Both Binary Search And bit of Greedy Concept also.2
// See We Know Ans Always Lies Between 0 and maximum element according to given3
// question condition Because sum value at maximum element is same as any other4
// element greater than it. So we get our sum from getval function after that5
// you need to choose as to move forward(l = mid+1) or backward i.e.(h = mid-1)6
// so if sum value we obtain is less than target then l = mid+1 why so?? Because7
// 2 3 5 lets suppose you are having this array if we pick 2 as mid then sum8
// value will be 6 whereas if we pick 3 then sum value will be 8 and 10 when we9
// pick 5 so notice that sum will increase when we increase value and10
// correspondingly decrease when we decrease value...So yess This is all what we11
// did and got Accepted.14
int getval(int mid, vector<int> &arr) {16
for (int i = 0; i < arr.size(); i++) {24
int findBestValue(vector<int> &arr, int target) {26
int l = 0, h = *max_element(arr.begin(), arr.end());27
int ans = 0, min1 = INT_MAX;29
int mid = l + (h - l) / 2;30
int k = getval(mid, arr);33
} else if (k < target) {38
int j = abs(k - target);42
} else if (j == min1) {