Dsa
Backtracking
Medium
Digit-Restricted Subset Sum
Given a target sum T and a list of N integers, find the number of distinct subsets that sum up to exactly T. There is an additional constraint: in any chosen subset, you cannot have more than TWO numbers that end in the same digit (e.g., you can include 12, 22, but not 32).
Input:
First line: T (target) and N (number of integers).
Second line: N space-separated integers.
Output: Total number of valid subsets.
Example:
Input:
10 3
5 5 2
Output: 1
Explanation: The subset {5, 5} sums to 10 and has two numbers ending in 5 (allowed). {2} is too small.
Key concepts
backtrackingsubset sumfrequency constraint
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now