Return Subsets Sum to K Problem Statement

Given an integer array 'ARR' of size 'N' and an integer 'K', return all the subsets of 'ARR' which sum to 'K'.

Explanation:

A subset of an array 'ARR' is a tuple that can be obtained from 'ARR' by removing some (possibly all) elements of 'ARR'.

Note:

  • The order of subsets is not important.
  • The order of elements in a particular subset should be in increasing order of the index.

Input:

The first line of input contains an integer 'N', which denotes the size of the array. The second line contains 'N' single-space separated integers representing the elements of the array. The third line contains a single integer 'K', which denotes the integer to which the subsets should sum to.

Output:

For each test case, print single-space separated integers of a subset of 'ARR' having sum = 'K'. The output of each test case will be printed in a separate line.

Example:

Input:

3
1 2 3
3

Output:

1 2
3

Constraints:

  • 1 <= 'N' <= 16
  • - (10 ^ 6) <= ARR[i] <= (10 ^ 6)
  • - 16 * (10 ^ 6) <= 'K' <= 16 * (10 ^ 6)
  • Where 'ARR[i]' denotes the value for 'ith' element of the array 'ARR' and 'K' is the given sum.

Time Limit: 1 sec.

AnswerBot
4mo

Return all subsets of an array that sum to a given integer K.

  • Use recursion to generate all possible subsets of the array.

  • Keep track of the current subset and its sum while traversing the array.

  • If the ...read more

Help your peers!
Select
Add answer anonymously...

Avalara Technologies Software Developer interview questions & answers

A Software Developer was asked 11mo agoQ. Explain the various OOP concepts.
A Software Developer was asked 11mo agoQ. Explain various database concepts.
A Software Developer was asked Q. Palindrome Partitioning Problem Statement You are given a string S. Your task is...read more

Popular interview questions of Software Developer

A Software Developer was asked 11mo agoQ1. Explain the various OOP concepts.
A Software Developer was asked 11mo agoQ2. Explain various database concepts.
A Software Developer was asked Q3. Palindrome Partitioning Problem Statement You are given a string S. Your task is...read more
Avalara Technologies Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits