Array Quadruplet #138
Unanswered
esthicodes
asked this question in
Q&A
Replies: 1 comment
-
esthi korea bscarlatarget = sdef find_array_quadruplet(arr, s): arr.sort() # O(nlog(n)) for i in range(n-3):
return [] print(find_array_quadruplet([2, 7, 4, 0, 9, 5, 1, 3], s = 20)) curr_sum + target_complement + curr_complement = 3 = constantO(constant) = O(1) -> space complexityO(n^3)n * n * n2 for loops in n-quadrat and one while loopeine while looparr = [2, 7, 4, 0, 9, 5, 1, 3], s = 20[0, 4, 7, 9]runtime: time: O(n)space complexity: O(1). zusätzlich speicherplatz verwendenspeicher eine platz. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Array Quadruplet
Given an unsorted array of integers arr and a number s, write a function findArrayQuadruplet that finds four numbers (quadruplet) in arr that sum up to s. Your function should return an array of these numbers in an ascending order. If such a quadruplet doesn’t exist, return an empty array.
Note that there may be more than one quadruplet in arr whose sum is s. You’re asked to return the first one you encounter (considering the results are sorted).
Explain and code the most efficient solution possible, and analyze its time and space complexities.
Example:
input: arr = [2, 7, 4, 0, 9, 5, 1, 3], s = 20
output: [0, 4, 7, 9] # The ordered quadruplet of (7, 4, 0, 9)
# whose sum is 20. Notice that there
# are two other quadruplets whose sum is 20:
# (7, 9, 1, 3) and (2, 4, 9, 5), but again you’re
# asked to return the just one quadruplet (in an
# ascending order)
Constraints:
[time limit] 5000ms
[input] array.integer arr
1 ≤ arr.length ≤ 100
[input] integer s
[output] array.integer
Beta Was this translation helpful? Give feedback.
All reactions