Parallel Squares Sum
JavaScript
Medium
3 views
Problem Description
Given n numbers, square each number using async function in parallel (Promise.all). Print sum of squares.
Input Format
Line1: n. Line2: n integers.
Output Format
One integer sum.
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 arr=[];for(let j=0;j<n;j++)arr.push(BigInt(a[i++]));const sq=async x=>{await Promise.resolve();return x*x;};(async()=>{const res=await Promise.all(arr.map(sq));let sum=0n;for(const v of res)sum+=v;process.stdout.write(sum.toString());})();
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!