Recently, I got the below question in the Phone Screening round of Amazon Hyderabad. I could solve it partially at the time of interview. Any ideas how to solve it optimally.
Question:
Given an array of size n with unique positive integers and a positive integer K,
check if there exists a combination of elements in the array satisfying below constraints.
a. The sum of all such elements is K
b. None of those elements are adjacent in the original array
Ex:
Input:
arr = {1, 9, 8, 3, 6, 7, 5, 11, 12, 4}
K = 14
Output:
[3, 7, 4]