2723. Add Two Promises LeetCode Solution

In this guide, you will get 2723. Add Two Promises LeetCode Solution with the best time and space complexity. The solution to Add Two Promises problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Add Two Promises solution in C++
  4. Add Two Promises solution in Java
  5. Add Two Promises solution in Python
  6. Additional Resources
2723. Add Two Promises LeetCode Solution image

Problem Statement of Add Two Promises

Given two promises promise1 and promise2, return a new promise. promise1 and promise2 will both resolve with a number. The returned promise should resolve with the sum of the two numbers.

Example 1:

Input:
promise1 = new Promise(resolve => setTimeout(() => resolve(2), 20)),
promise2 = new Promise(resolve => setTimeout(() => resolve(5), 60))
Output: 7
Explanation: The two input promises resolve with the values of 2 and 5 respectively. The returned promise should resolve with a value of 2 + 5 = 7. The time the returned promise resolves is not judged for this problem.

Example 2:

Input:
promise1 = new Promise(resolve => setTimeout(() => resolve(10), 50)),
promise2 = new Promise(resolve => setTimeout(() => resolve(-12), 30))
Output: -2
Explanation: The two input promises resolve with the values of 10 and -12 respectively. The returned promise should resolve with a value of 10 + -12 = -2.

Constraints:

promise1 and promise2 are promises that resolve with a number

Complexity Analysis

  • Time Complexity: Google AdSense
  • Space Complexity: Google Analytics

2723. Add Two Promises LeetCode Solution in C++

type P = Promise<number>;

async function addTwoPromises(promise1: P, promise2: P): P {
  const results = await Promise.all([promise1, promise2]);
  return results[0] + results[1];
}
/* code provided by PROGIEZ */

2723. Add Two Promises LeetCode Solution in Java

N/A
// code provided by PROGIEZ

2723. Add Two Promises LeetCode Solution in Python

N/A
# code by PROGIEZ

Additional Resources

See also  3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.