You need to find the products which add up to a credit value from the list of values.
The problem might look very simple. But you need to be careful in deciding what solution to take
You need to subtract the credit value with the values in the list. If credit – value in the list is found in the main list of values, then the 2 index just found (one in the main list another in the credit-value list ) are the indexes which add up to the credit.
Better to avoid seeing the below code since it is very simple to make it to code. If you have not succeeded in that too, verify the below code logic.
- Avoid using maps or hash maps as it sorts the keys. If you use the normal index as the key and the second index of credit-value list as value, you will end up getting index in wrong order
- in C++, try to use getline version which doesn’t depend on the buffer size. If you define a buffer size then, our large data set will fail.
- There is no possibility of getting more than two values as we just take k and j on an iteration. The total performance at worst case will be around O(n2).
- Make it a habit to free memory