Using Nested For Loop

Tomlai
2 min readApr 16, 2021
Photo by Tine Ivanič on Unsplash

Today we got a fun algorithm problem from HackerRank to look at. It is call Electronics Shop. This question has two array and they are looking for sum of elements between two arrays. It might sounds complicated at first but once you understand it, well it is a lot easier than it looks.

Question:

A person wants to determine the most expensive computer keyboard and USB drive that can be purchased with a give budget. Given price lists for keyboards and USB drives and a budget, find the cost to buy them. If it is not possible to buy both items, return -1.
Example
b = 60
keyboards = [40, 50, 60]
drives = [5, 8, 12]
The person can buy a 40 keyboard + 12 USB drive = 52, or a 50 keyboard+ 8 USB drive = 58 . Choose the latter as the more expensive option and return 58.

Returns

  • int: the maximum that can be spent, or -1 if it is not possible to buy both items

--

--