| 6 | #check() return maximum price of the USB drive which is less than or equal to our budget |
| 7 | #This uses the binary search approach to find the required maximum value |
| 8 | def check(arr,start,end,n1,max): |
| 9 | if end==start+1 or end==start: |
| 10 | if arr[end]>max and arr[end]<=n1: |
| 11 | max=arr[end] |
| 12 | return max |
| 13 | if arr[start]>max and arr[start]<=n1: |
| 14 | max=arr[start] |
| 15 | return max |
| 16 | mid=(start+end)//2 |
| 17 | num=arr[mid] |
| 18 | if num<=n1: |
| 19 | if max<num: |
| 20 | max=num |
| 21 | return check(arr,mid,end,n1,max) |
| 22 | return check(arr,start,mid,n1,max) |
| 23 | |
| 24 | |
| 25 | #getMoneySpent() will return the amount required to buy the most expensive keyboard and USB drive. If not not possible returns -1. |