JavaScript Program to Construct an Array from its pair-sum Array
Last Updated :
18 Sep, 2023
The pair-sum array is a unique construction that holds the sum of all potential pairs of elements from the original array. At first glance, it might appear to be challenging, but in this article, we'll Construct an array from its pair-sum array and discover some of its most intriguing uses.
What is Pair - Sum Array?
A pair-sum array in JavaScript is an array that holds the total number of potential pairs of elements from the original array. The pair-sum array, designated as pairSumArr, will have a length of n*(n-1)/2 when an array arr of length n is given. This is because it contains the sum of all conceivable combinations of two entries from the original array.
Example 1:
Suppose we have the original array arr = [2, 5, 7]. The pair-sum array will be:
pairSumArr = [2+5, 2+7, 5+7] = [7, 9, 12]
Here, pairSumArr[0] contains the sum of the first two elements of arr, i.e., 2 + 5 = 7.
Similarly, pairSumArr[1] contains the sum of the first and last elements of arr, i.e., 2 + 7 = 9,
and pairSumArr[2] contains the sum of the last two elements of arr, i.e., 5 + 7 = 12
Example 2:
2. If we take another example Suppose we have the original array arr = [3, 6, 8, 2].
We will construct the pair-sum array for this array.
pairSumArr = [3+6, 3+8, 3+2, 6+8, 6+2, 8+2]
pairSumArr = [9, 11, 5, 14, 8, 10]
Use of Pair - Sum Array
- Array Reconstruction: If the pair-sum array is known, it can be used to rebuild the original array. In this procedure, the sum array is reverse-engineered to get the elements of the original array.
- Combinatorics: Applications of the pair-sum array include combinatorial issues, particularly when array combinations are involved.
- Optimization Problems: The pair-sum array can assist in identifying patterns and connections between items in some optimization problems, resulting in optimum solutions.
- Finding Missing Elements: When some items of an array are lost but their pairwise sums are still available, the pair-sum array can be useful. We may determine the elements that are missing by using the pair-sum array.
Constructing the Original Array from Pair-Sum Array
In JavaScript, we may apply a straightforward approach to create an array from its pair-sum array. The approach includes reconstructing the original array by going backward through the pair-sum array generation process given the pair-sum array pairSumArr. The fundamental concept is to use the provided pair-sum array to identify the original array's missing items. We must reverse engineer the pair-sum array's generation in order to create the original array from its pair-sum array. We may effectively rebuild the original array by recognizing the pattern and using the right algorithm.
Approach
The basic algorithm to construct an original array from a pair-sum array includes the following steps as follow.
- Create an array of size n to store the original array elements.
- Iterate n times and find the missing elements of the original array by performing reverse operations on the pair-sum array.
- Calculate the length of the pair-sum array.
- Calculate the length of the Original Array from the given Pair Sum Array.
- Return the reconstructed
Example:
JavaScript
// Function to construct original array
// from pair sum array
function constructArr(pair, n) {
let arr = new Array(n);
// Calculate the first element of
// the original array
// first element = (pair[0] + pair[1]
// - pair[n-1]) / 2
arr[0] = Math.floor(
(pair[0] + pair[1] - pair[n - 1]) / 2
);
for (let i = 1; i < n; i++)
arr[i] = pair[i - 1] - arr[0];
return arr;
}
// Function to calculate the length of the
// original array from the given pair sum array
function calculateOriginalArrayLength(pairSumArrayLength) {
return Math.ceil(
(Math.sqrt(8 * pairSumArrayLength + 1) + 1) / 2
);
}
// Given pair sum array
let pair = [13, 11, 10, 13, 10, 9, 12, 7, 10, 9];
// Calculate the length of the pair sum array
const arrayLength = pair.length;
// Calculate the length of the original array
const originalArrayLength =
calculateOriginalArrayLength(arrayLength);
// Number of elements in the original array
let n = originalArrayLength;
// Create an array which can store original array
let originalArr = constructArr(pair, n);
// Required Original array from the Pair sum Array
console.log("Original Array:", originalArr);
OutputOriginal Array: [ 7, 6, 4, 3, 6 ]
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read