
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Performing power operations on an array of numbers in JavaScript
Problem
We are required to write a JavaScript function that takes in an array of integers, arr, of even length.
Suppose a number num where −
num = (arr[0] * arr[0] + arr[1] * arr[1]) * (arr[2] * arr[2] + arr[3] * arr[3]) * … * (arr[n-2] * arr[n-2] + arr[n-1] * arr[n-1])
Where n is the length of the array.
Our function should find and return an array of two numbers [A, B] such that −
A2 + B2 = num
For instance, if the array is −
[1, 2, 3, 4]
Then num = ( 1 + 4 ) * (9 + 16) = 125
Then output should be −
[2, 11]
Because 22 + 112 = 125
Example
Following is the code −
const arr = [1, 2, 3, 4]; const findMatchingSumArray = (arr = []) => { let squaredSum = 1; for(let i = 0; i < arr.length - 1; i += 2){ const curr = arr[i]; const next = arr[i + 1]; squaredSum *= (Math.pow(curr, 2) + Math.pow(next, 2)); }; for(let k = 0; k * k < squaredSum; k++){ for(let j = 0; (k * k) + (j * j) <= squaredSum; j++){ if((k * k) + (j * j) === squaredSum){ return [k, j]; }; }; }; return []; }; console.log(findMatchingSumArray(arr));
Output
Following is the console output −
[2, 11]
- Related Articles
- Maximum count of equal numbers in an array after performing given operations in C++
- Operations on an Array in Data Structures
- Maximum Possible Product in Array after performing given Operations in C++
- Checking power of 2 using bitwise operations in JavaScript
- Performing Bitwise Operations with BigInteger in Java
- Program to find expected sum of subarrays of a given array by performing some operations in Python
- Final string after performing given operations in C++
- Equal partition of an array of numbers - JavaScript
- Product of all other numbers an array in JavaScript
- Sum of all prime numbers in an array - JavaScript
- Calculating variance for an array of numbers in JavaScript
- Smallest Common Multiple of an array of numbers in JavaScript
- Realtime moving average of an array of numbers in JavaScript
- Finding missing element in an array of numbers in JavaScript
- Performing an opening operation on an image using OpenCV

Advertisements