Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

50%
+0 −0
Challenges Find all unique quintuplets in an array that sum to a given target

Inspiration: Leetcode's [3Sum] linkLink on CG&CC Problem Given an array nums of n (not necessarily distinct) integers, and given a target number target, return an array of all of the unique...

1 answer  ·  posted 11d ago by CrSb0001‭  ·  last activity 8d ago by Shaggy‭

#2: Post edited by user avatar CrSb0001‭ · 2024-12-11T12:17:21Z (11 days ago)
  • <sup>Inspiration: Leetcode's [3Sum] [link](https://leetcode.com/problems/3sum/)<br>[Link on CG&CC](https://codegolf.stackexchange.com/questions/277165/find-all-unique-quintuplets-in-an-array-that-sum-to-a-given-target)</sup>
  • >### Problem
  • >
  • >Given an array `nums` of `n` (not necessarily distinct) integers, and given a target number `target`, return an array of all of the ***unique*** quintuplets `[nums[a],nums[b],nums[c],nums[d],nums[e]]` such that the following conditions are held:
  • >1. `0 <= a,b,c,d,e < n` (or `1 <= a,b,c,d,e <= n` if using 1-indexing)
  • >2. All of `a,b,c,d,e` are distinct.
  • >3. `nums[a] + nums[b] + nums[c] + nums[d] + nums[e] = target`
  • > * In the case of multiple arrays, we also add the requirements that at least two values in each of the arrays are distinct.
  • >
  • >If all 3 conditions cannot be satisfied, you can return a junk value of your liking, or just an empty array `[[]]` or `[]`. The testcases down below will use `-1` as the specified junk value.
  • >
  • >You may return the answer in any order. For example, given the array `[-5,-2,-2,1,3,4,6]` and target `0`, you could return any permutation of `[[-5,-2,-2,3,6]]`. **You do not need to return all possible permutations of one single array.**
  • Testcases:
  • ```
  • Array: [-5,-2,-2,1,3,4,6]
  • Target: 0
  • Output: [[-5,-2,-2,3,6]]
  • Array: [-5,-4,-2,0,1,2,6]
  • Target: 1
  • Output: [[-5,-2,0,2,6],[-4,-2,0,1,6]]
  • # Note that outputting `[[-4,-2,0,1,6],[-5,-2,0,2,6]]` is also valid,
  • # although returning just `[[-4,-2,0,1,6]]` or `[[-5,-2,0,2,6]]` is not.
  • Array: [0,-1,2,3]
  • Target: 4
  • Output: -1
  • Array: [0,1,-9,6,7]
  • Target: 6
  • Output: -1
  • Array: [0,1,9,9,5]
  • Target: 45
  • Output: -1
  • Array: [1,4,6,9,-4]
  • Target: 16
  • Output: [[1,4,6,9,-4]]
  • Array: [1,0,9,6,5,0]
  • Target: 21
  • Output: [[1,0,9,6,5]]
  • Array: [1,0,9,6,5,4,7]
  • Target: 21
  • Output: [[1,0,9,6,5],[1,0,9,4,7]]
  • Array: [1,0,9,6,5,4,4,7]
  • Target: 21
  • Output: [[1,0,9,6,5],[1,0,9,4,7],[0,6,4,4,7],[1,5,4,4,7]]
  • Array: [1,1,2,2,3,3,4,4]
  • Target: 11
  • Output: [[1,2,2,3,3],[1,1,2,3,4]]
  • ```
  • This is [code-golf], so the shortest solution wins!
  • <sup>Inspiration: Leetcode's [3Sum] [link](https://leetcode.com/problems/3sum/)<br>[Link on CG&CC](https://codegolf.stackexchange.com/questions/277165/find-all-unique-quintuplets-in-an-array-that-sum-to-a-given-target)</sup>
  • >### Problem
  • >
  • >Given an array `nums` of `n` (not necessarily distinct) integers, and given a target number `target`, return an array of all of the ***unique*** quintuplets `[nums[a],nums[b],nums[c],nums[d],nums[e]]` such that the following conditions are held:
  • >1. `0 <= a,b,c,d,e < n` (or `1 <= a,b,c,d,e <= n` if using 1-indexing)
  • >2. All of `a,b,c,d,e` are distinct.
  • >3. `nums[a] + nums[b] + nums[c] + nums[d] + nums[e] = target`
  • > * In the case of multiple arrays, we also add the requirements that at least two values in each of the arrays are distinct.
  • >
  • >If all 3 conditions cannot be satisfied, you can return a junk value of your liking, or just an empty array `[[]]` or `[]`. The testcases down below will use `-1` as the specified junk value.
  • >
  • >You may return the answer in any order. For example, given the array `[-5,-2,-2,1,3,4,6]` and target `0`, you could return any permutation of `[[-5,-2,-2,3,6]]`. **You do not need to return all possible permutations of one single array.**
  • Testcases:
  • ```
  • Array: [-5,-2,-2,1,3,4,6]
  • Target: 0
  • Output: [[-5,-2,-2,3,6]]
  • Array: [-5,-4,-2,0,1,2,6]
  • Target: 1
  • Output: [[-5,-2,0,2,6],[-4,-2,0,1,6]]
  • # Note that outputting `[[-4,-2,0,1,6],[-5,-2,0,2,6]]` is also valid,
  • # although returning just `[[-4,-2,0,1,6]]` or `[[-5,-2,0,2,6]]` is not.
  • Array: [0,-1,2,3]
  • Target: 4
  • Output: -1
  • Array: [0,1,-9,6,7]
  • Target: 6
  • Output: -1
  • Array: [0,1,9,9,5]
  • Target: 45
  • Output: -1
  • Array: [1,4,6,9,-4]
  • Target: 16
  • Output: [[1,4,6,9,-4]]
  • Array: [1,0,9,6,5,0]
  • Target: 21
  • Output: [[1,0,9,6,5]]
  • Array: [1,0,9,6,5,4,7]
  • Target: 21
  • Output: [[1,0,9,6,5],[1,0,9,4,7]]
  • Array: [1,0,9,6,5,4,4,7]
  • Target: 21
  • Output: [[1,0,9,6,5],[1,0,9,4,7],[0,6,4,4,7],[1,5,4,4,7]]
  • Array: [1,1,2,2,3,3,4,4]
  • Target: 11
  • Output: [[1,2,2,3,3],[1,1,2,3,4]]
  • Array: [-1,1,1,1,1,1,3]
  • Target: 5
  • Output: [[1,1,1,1,1],[-1,1,1,1,3]]
  • # Above test case suggested by @trichoplax
  • ```
  • This is [code-golf], so the shortest solution wins!
#1: Initial revision by user avatar CrSb0001‭ · 2024-12-11T01:33:26Z (11 days ago)
Find all unique quintuplets in an array that sum to a given target
<sup>Inspiration: Leetcode's [3Sum] [link](https://leetcode.com/problems/3sum/)<br>[Link on CG&CC](https://codegolf.stackexchange.com/questions/277165/find-all-unique-quintuplets-in-an-array-that-sum-to-a-given-target)</sup>

>### Problem
>
>Given an array `nums` of `n` (not necessarily distinct) integers, and given a target number `target`, return an array of all of the ***unique*** quintuplets `[nums[a],nums[b],nums[c],nums[d],nums[e]]` such that the following conditions are held:
>1. `0 <= a,b,c,d,e < n` (or `1 <= a,b,c,d,e <= n` if using 1-indexing)
>2. All of `a,b,c,d,e` are distinct.
>3. `nums[a] + nums[b] + nums[c] + nums[d] + nums[e] = target`
>    * In the case of multiple arrays, we also add the requirements that at least two values in each of the arrays are distinct.
>
>If all 3 conditions cannot be satisfied, you can return a junk value of your liking, or just an empty array `[[]]` or `[]`. The testcases down below will use `-1` as the specified junk value.
>
>You may return the answer in any order. For example, given the array `[-5,-2,-2,1,3,4,6]` and target `0`, you could return any permutation of `[[-5,-2,-2,3,6]]`. **You do not need to return all possible permutations of one single array.**

Testcases:

```
 Array: [-5,-2,-2,1,3,4,6]
Target: 0
Output: [[-5,-2,-2,3,6]]

 Array: [-5,-4,-2,0,1,2,6]
Target: 1
Output: [[-5,-2,0,2,6],[-4,-2,0,1,6]]

# Note that outputting `[[-4,-2,0,1,6],[-5,-2,0,2,6]]` is also valid,
# although returning just `[[-4,-2,0,1,6]]` or `[[-5,-2,0,2,6]]` is not.

 Array: [0,-1,2,3]
Target: 4
Output: -1

 Array: [0,1,-9,6,7]
Target: 6
Output: -1

 Array: [0,1,9,9,5]
Target: 45
Output: -1

 Array: [1,4,6,9,-4]
Target: 16
Output: [[1,4,6,9,-4]]

 Array: [1,0,9,6,5,0]
Target: 21
Output: [[1,0,9,6,5]]

 Array: [1,0,9,6,5,4,7]
Target: 21
Output: [[1,0,9,6,5],[1,0,9,4,7]]

 Array: [1,0,9,6,5,4,4,7]
Target: 21
Output: [[1,0,9,6,5],[1,0,9,4,7],[0,6,4,4,7],[1,5,4,4,7]]

 Array: [1,1,2,2,3,3,4,4]
Target: 11
Output: [[1,2,2,3,3],[1,1,2,3,4]]
```
This is [code-golf], so the shortest solution wins!