Ninja has accidentally discovered a suitcase while digging a well in his lawn. But the suitcase is locked and there is a paper along with that suitcase on which instructions for opening the suitcase are given.
So instructions are defined as follows.1. Select any element from the array and increase it by β1β.
2. Double all the value of all the elements in the array
So your task is to find the minimum number of steps of making the array element starting from β0β you were be provided with the array βarrβ and if you double the all array element it would be counted as β1β and for each element increment by β1β considered as a separate count.
The first line contains an integer 'T' which denotes the number of test cases or queries to be run.
The first line of each test case contains a single integer βNβ denoting the size of the βarrβ array.
The next line contains βNβ space-separated integers denoting the values of elements of the βarrβ array.
Output Format :
For each test case, print a single line containing the minimum number of steps required to form the given array.
The output of each test case will be printed in a separate line.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N <= 5000
0 <= arr[i] < 10 ^ 6
Where βarr[i]β represents the elements of the array.
Time Limit: 1 sec
2
3
2 4 8
3
6 3 7
6
9
Test Case 1 :
For the first test case, the given array is { 2, 4, 8 } so we start from { 0, 0, 0 } and after the first β1β operation i.e adding β1β to β3rdβ index this will look like as { 0, 0, 1 } now in the 2nd operation we multiplied array βarrβ by β2β and now it will look like { 0, 0, 2 } now we increase element at index β1β by β1β now it will look like as { 0, 1, 2 } now we multiply the whole array with β2β and now it will look like as { 0, 2, 4 } now we increase the 0th index by β1β and now it will look like { 1, 2, 4 } now we multiply the array by β2β and we got { 2, 4, 8 } so we return β6β as the answer.
Test Case 2 :
For this test case, the given array is { 6, 3, 7 } so we start from { 0, 0, 0 } and after the first two operations this will look like as { 1, 0, 1 } now we multiplied it by β2β and it will look like { 2, 0, 2 } now we increase the element all element by β1β and now it will become { 3, 1, 3 } now we multiply it by β2β making it as { 6, 3, 6 } now we increase the element at last index by β1β and return β9β as the final answer.
2
2
5 5
4
6 1 3 5
6
9
Can you think of a recursive approach for finding the count?
The idea here is that we can say that number of increment operations required are equal to the number of set bits present in the elements of a given array because in one operation you can double all elements of the array. To get an even number you need to get even / 2 i.e. half of the number. So we need Increment operations only when our current element is odd
and in all other cases, we can divide the number by 2.
The function is created to count the number of minimum operations which can be performed of incrementation to make the desired array.
The function will take one parameter:
Int element for which we count the number of operations needed.
The function is created to count the number of minimum operations which can be performed of double the elements to make the desired function.
The function will take one parameter:
Int element for which we count the number of operations required.
O(N*log(MAX)), where βNβ represents the size of the given array and βMAXβ represents the maximum element present in the array.
As we are using a loop for iterating the array which takes O(N) time and in every iteration recursive function calls takes O(log(MAX) ) time as we are dividing the number by β2β in every recursive call.
O(N), Where n is the number of elements present in the array.
Recursive stack uses space of order of n.