Bei einem Paar-Produkt-Array pair[] besteht die Aufgabe darin, das ursprüngliche Array zu finden. Ein Paarprodukt-Array für ein Array arr[] ist das Array, das das Produkt aller Paare in geordneter Form enthält, dh {(arr[0] * arr[1]), (arr[0] * arr[2]), …, (arr[1] * arr[2]), (arr[1] * arr[3]), …, (arr[n – 2] * arr[n – 1])} .
Beispiele: 
 

Eingabe: Paar[] = {2, 3, 6} 
Ausgabe: 1 2 3
Eingabe: Paar[] = {48, 18, 24, 24, 32, 12} 
Ausgabe: 6 8 3 4 
 

Vorgehensweise: Finden Sie zuerst die Größe des benötigten Arrays aus dem gegebenen Paarprodukt-Array. Angenommen, die Größe des ursprünglichen Arrays sei N und die Größe des Paarprodukt-Arrays sei X . Daher kann durch Lösen von (N * (N – 1)) / 2 = X der Wert von N als N = (1 + (int)sqrt(1 + 8 * X)) / 2 berechnet werden . 
Sehen wir uns nun die Lösung anhand eines Beispiels an, sagen wir, das Paarprodukt-Array von [A, B, C, D] sei arr[AB, AC, AD, BC, BD, CD] und nehme dann sqrt((arr[0 ] * arr[1]) / arr[n – 1]) -> sqrt((AB ​​* AC) / BC) ergibt den Wert A
Sobald der Wert des ersten Elements wiederhergestellt wurde, können alle verbleibenden Elemente des Paarprodukt-Arrays dadurch dividiert werden, um die verbleibenden Elemente des ursprünglichen Arrays zu erhalten.
Unten ist die Implementierung des obigen Ansatzes: 
 

C++

// C++ implementation of the approach
#include <iostream>
#include <math.h>
using namespace std;
 
// Utility function to print the array
void printArr(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
 
// Function to generate the original
// array from the pair-product array
void constructArr(int pair[], int n)
{
    int size = (1 + (int)sqrt(1 + 8 * n)) / 2;
    int arr[size];
 
    // First element of the resulting array
    arr[0] = sqrt((pair[0] * pair[1]) / pair[size - 1]);
 
    // Find all the other elements
    for (int i = 1; i < size; i++)
        arr[i] = pair[i - 1] / arr[0];
 
    // Print the elements of the generated array
    printArr(arr, size);
}
 
// Driver code
int main()
{
    int pair[] = { 48, 18, 24, 24, 32, 12 };
    int n = sizeof(pair) / sizeof(int);
 
    constructArr(pair, n);
 
    return 0;
}

Java

// Java implementation of the approach
class GFG
{
 
// Utility function to print the array
static void printArr(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        System.out.print(arr[i] + " ");
}
 
// Function to generate the original
// array from the pair-product array
static void constructArr(int pair[], int n)
{
    int size = (1 + (int)Math.sqrt(1 + 8 * n)) / 2;
    int []arr = new int[size];
 
    // First element of the resulting array
    arr[0] = (int) Math.sqrt((pair[0] * pair[1]) /
                                        pair[size - 1]);
 
    // Find all the other elements
    for (int i = 1; i < size; i++)
        arr[i] = pair[i - 1] / arr[0];
 
    // Print the elements of the generated array
    printArr(arr, size);
}
 
// Driver code
public static void main(String[] args)
{
    int pair[] = { 48, 18, 24, 24, 32, 12 };
    int n = pair.length;
 
    constructArr(pair, n);
}
}
 
// This code is contributed by PrinciRaj1992

Python3

# Python3 implementation of the approach
from math import sqrt
 
# Utility function to print the array
def printArr(arr, n) :
 
    for i in range(n) :
        print(arr[i], end = " ");
 
# Function to generate the original
# array from the pair-product array
def constructArr(pair, n) :
 
    size = int((1 + sqrt(1 + 8 * n)) // 2);
    arr = [0] * (size);
 
    # First element of the resulting array
    arr[0] = int(sqrt((pair[0] * pair[1]) /
                       pair[size - 1]));
 
    # Find all the other elements
    for i in range(1, size) :
        arr[i] = pair[i - 1] // arr[0];
 
    # Print the elements of the generated array
    printArr(arr, size);
 
# Driver code
if __name__ == "__main__" :
 
    pair = [ 48, 18, 24, 24, 32, 12 ];
    n = len(pair);
 
    constructArr(pair, n);
 
# This code is contributed by AnkitRai01

C#

// C# implementation of the approach
using System;
     
class GFG
{
 
// Utility function to print the array
static void printArr(int []arr, int n)
{
    for (int i = 0; i < n; i++)
        Console.Write(arr[i] + " ");
}
 
// Function to generate the original
// array from the pair-product array
static void constructArr(int []pair, int n)
{
    int size = (1 + (int)Math.Sqrt(1 + 8 * n)) / 2;
    int []arr = new int[size];
 
    // First element of the resulting array
    arr[0] = (int) Math.Sqrt((pair[0] * pair[1]) /
                                        pair[size - 1]);
 
    // Find all the other elements
    for (int i = 1; i < size; i++)
        arr[i] = pair[i - 1] / arr[0];
 
    // Print the elements of the generated array
    printArr(arr, size);
}
 
// Driver code
public static void Main(String[] args)
{
    int []pair = { 48, 18, 24, 24, 32, 12 };
    int n = pair.Length;
 
    constructArr(pair, n);
}
}
 
// This code is contributed by Rajput-Ji

Javascript

<script>
 
// JavaScript implementation of the approach
 
 
// Utility function to print the array
function printArr(arr, n) {
    for (let i = 0; i < n; i++)
        document.write(arr[i] + " ");
}
 
// Function to generate the original
// array from the pair-product array
function constructArr(pair, n) {
    let size = Math.floor((1 + Math.sqrt(1 + 8 * n)) / 2);
    let arr = new Array(size);
 
    // First element of the resulting array
     
    arr[0] =
    Math.floor(Math.sqrt((pair[0] * pair[1]) / pair[size - 1]));
 
    // Find all the other elements
    for (let i = 1; i < size; i++)
        arr[i] = Math.floor(pair[i - 1] / arr[0]);
 
    // Print the elements of the generated array
    printArr(arr, size);
}
 
// Driver code
 
let pair = [48, 18, 24, 24, 32, 12];
let n = pair.length;
constructArr(pair, n);
 
</script>
Ausgabe: 

6 8 3 4

 

Zeitkomplexität : O(N).
Hilfsraum : O(N). 

Falls Sie an Live-Kursen mit Experten teilnehmen möchten , beziehen Sie sich bitte auf DSA Live-Kurse für Berufstätige und Competitive Programming Live for Students .