We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
The logic is simple, we sort both the arrays, then put pointer 1 on the end of 1 array and pointer 2 at the start of the other. Then just like doing 2 pointer problems, if the sum of value at pointer 1 + pointer 2 is less or equal than our target value, we store the result and increment pointer 2, if its greater then we decrement pointer 1, thereby only traversing both arrays only once.
`int getMoneySpent(vector keyboards, vector drives, int b) {
int n{static_cast(keyboards.size())};
int m{static_cast(drives.size())};
Electronics Shop
You are viewing a single comment's thread. Return to all comments →
O(nlogn)+O(mlogm)+O(n+k) solution
The logic is simple, we sort both the arrays, then put pointer 1 on the end of 1 array and pointer 2 at the start of the other. Then just like doing 2 pointer problems, if the sum of value at pointer 1 + pointer 2 is less or equal than our target value, we store the result and increment pointer 2, if its greater then we decrement pointer 1, thereby only traversing both arrays only once.
`int getMoneySpent(vector keyboards, vector drives, int b) { int n{static_cast(keyboards.size())}; int m{static_cast(drives.size())};
}