Async Reduce Sum
JavaScript
Medium
4 views
Problem Description
Given n integers, sum them using async reduce pattern (await inside loop). Print sum.
Input Format
Line1: n. Line2: n integers.
Output Format
One integer sum.
Sample Test Case
Input:
3
1000000000000 2 3
Official Solution
const fs=require('fs');const raw=fs.readFileSync(0,'utf8').trim();if(!raw)process.exit(0);const a=raw.split(/\\s+/);let i=0;const n=Number(a[i++]);let sum=0n;const add=async (x,y)=>{await Promise.resolve();return x+y;};(async()=>{for(let j=0;j<n;j++){sum=await add(sum,BigInt(a[i++]));}process.stdout.write(sum.toString());})();
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!