This is a verified interview question from Infosys. Candidates reporting seeing this problem in recent Online Assessments (OAs) and onsite rounds. Mastering "Length of the Longest Subsequence That Sums to Target - Infosys Online Assessment" covers key patterns like Arrays.
"You are given an array `nums` of positive integers and a `target`. You need to return the length of the longest subsequence whose elements sum exactly to `target`. However, you must manage a cooldown mechanic: every time you include an element `nums[i]` in your subsequence, you are forbidden from including any of the next `k` elements from the original array in your subsequence, where `k = nums[i] % 4`. Find the maximum length of a valid subsequence. If no valid subsequence exists that sums exactly to `target`, return `-1`. **Input Format** * The first line contains an integer, `N`, denoting the size of the array. * The second line contains an integer, `target`, denoting the required sum. * The third line contains `N` space-separated integers denoting the `N` positive integers `nums`. **Constraints** * $1 \le N \le 1000$ * $1 \le target \le 1000$ * $1 \le nums[i] \le 1000$"
Join thousands of developers practicing for Infosys.