Posted on by Kalkicode
Code Hash

Check whether one array is constructed by pair of another array

Here given code implementation process.

``````/*
Java program
Check whether one array is constructed by pair of another array
*/
import java.util.Set;
import java.util.HashSet;

public class PairArray
{
//This function are displaying given array elements
public void display(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
System.out.print(" " + arr[i]);
}
System.out.print("\n");
}

// This function is determine that result array is constructed by x pair element or not
// x is provide pair element
public void pairElement(int []result, int []x)
{
// Get the length
int n = x.length;
int m = result.length;
int value = 0;
// Loop controlling variables i and j
int i = 0;
int j = 0;
// Use to collect unique element
Set<Integer> element = new HashSet< Integer > ();

// Get unique elements of resultant array
for (i = 0; i < m ; i++)
{
element.add(result[i]);
}
// Outer loop execute through by pair element
for (i = 0; i < n-1 && element.size() > 0 ; i++)
{
// Inner loop construct pair of next upcoming element
for (j = i+1; j < n  && element.size() > 0; j++)
{
// Get pair sum
value = x[i] + x[j];

if(element.contains(value))
{
// When pair is exist then remove of this pair element in set
element.remove(value);
}
}
}
System.out.print(" X : \n");
display(x,n);
System.out.print(" Result : \n");
display(result,m);
if(element.size() > 0)
{
System.out.print(" No, result array is not constructed by given x pair element\n");
}
else
{
System.out.print(" Yes, result array is constructed by given x pair element\n");
}

}
public static void main(String[] args)
{
PairArray task = new PairArray();

// Pair array
int[] x = {1,6,2,9,5,-1,3};
// Given of resultant array
int []result1  = {3, 13, 11, 5};
int []result2  = {0,12,8,7,9, 4, 8, 7};
// Test Cases
task.pairElement(result1,x);
task.pairElement(result2,x);
}
}``````

Output

`````` X :
1 6 2 9 5 -1 3
Result :
3 13 11 5
No, result array is not constructed by given x pair element
X :
1 6 2 9 5 -1 3
Result :
0 12 8 7 9 4 8 7
Yes, result array is constructed by given x pair element``````
``````// Include header file
#include <iostream>

#include <set>

using namespace std;
class PairArray
{
public:
//This function are displaying given array elements
void display(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << " " << arr[i];
}
cout << "\n";
}
// This function is determine that result array is constructed by x pair element or not
// x is provide pair element
void pairElement(int result[], int x[], int n, int m)
{

int value = 0;
// Loop controlling variables i and j
int i = 0;
int j = 0;
// Use to collect unique element
set < int > element ;
// Get unique elements of resultant array
for (i = 0; i < m; i++)
{
element.insert(result[i]);
}
// Outer loop execute through by pair element
for (i = 0; i < n - 1 && element.size() > 0; i++)
{
// Inner loop construct pair of next upcoming element
for (j = i + 1; j < n && element.size() > 0; j++)
{
// Get pair sum
value = x[i] + x[j];
if (element.find(value) != element.end())
{
element.erase(value);
}
}
}
cout << " X : \n";
this->display(x, n);
cout << " Result : \n";
this->display(result, m);
if (element.size() > 0)
{
cout << " No, result array is not constructed by given x pair element\n";
}
else
{
cout << " Yes, result array is constructed by given x pair element\n";
}
}
};
int main()
{
PairArray task = PairArray();
// Pair array
int x[] = {
1 , 6 , 2 , 9 , 5 , -1 , 3
};
// Given of resultant array
int result1[] = {
3 , 13 , 11 , 5
};
int result2[] = {
0 , 12 , 8 , 7 , 9 , 4 , 8 , 7
};
// Get the length
int n = sizeof(x) / sizeof(x[0]);
int m = sizeof(result1) / sizeof(result1[0]);
// Test Cases
task.pairElement(result1, x,n,m);
m = sizeof(result2) / sizeof(result2[0]);
task.pairElement(result2, x,n,m);
return 0;
}``````

Output

`````` X :
1 6 2 9 5 -1 3
Result :
3 13 11 5
No, result array is not constructed by given x pair element
X :
1 6 2 9 5 -1 3
Result :
0 12 8 7 9 4 8 7
Yes, result array is constructed by given x pair element``````
``````// Include namespace system
using System;
using System.Collections.Generic;
/*
C# program
Check whether one array is constructed by pair of another array
*/
public class PairArray
{
//This function are displaying given array elements
public void display(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
Console.Write(" " + arr[i]);
}
Console.Write("\n");
}
// This function is determine that result array is constructed by x pair element or not
// x is provide pair element
public void pairElement(int[] result, int[] x)
{
// Get the length
int n = x.Length;
int m = result.Length;
int value = 0;
// Loop controlling variables i and j
int i = 0;
int j = 0;
// Use to collect unique element
HashSet < int > element = new HashSet < int > ();
// Get unique elements of resultant array
for (i = 0; i < m; i++)
{
element.Add(result[i]);
}
// Outer loop execute through by pair element
for (i = 0; i < n - 1 && element.Count > 0; i++)
{
// Inner loop construct pair of next upcoming element
for (j = i + 1; j < n && element.Count > 0; j++)
{
// Get pair sum
value = x[i] + x[j];
if (element.Contains(value))
{
element.Remove(value);
}
}
}
Console.Write(" X : \n");
display(x, n);
Console.Write(" Result : \n");
display(result, m);
if (element.Count > 0)
{
Console.Write(" No, result array is not constructed by given x pair element\n");
}
else
{
Console.Write(" Yes, result array is constructed by given x pair element\n");
}
}
public static void Main(String[] args)
{
PairArray task = new PairArray();
// Pair array
int[] x = {
1 , 6 , 2 , 9 , 5 , -1 , 3
};
// Given of resultant array
int[] result1 = {
3 , 13 , 11 , 5
};
int[] result2 = {
0 , 12 , 8 , 7 , 9 , 4 , 8 , 7
};
// Test Cases
task.pairElement(result1, x);
task.pairElement(result2, x);
}
}``````

Output

`````` X :
1 6 2 9 5 -1 3
Result :
3 13 11 5
No, result array is not constructed by given x pair element
X :
1 6 2 9 5 -1 3
Result :
0 12 8 7 9 4 8 7
Yes, result array is constructed by given x pair element``````
``````<?php
/*
Php program
Check whether one array is constructed by pair of another array
*/
class PairArray
{
//This function are displaying given array elements
public	function display( & \$arr, \$size)
{
for (\$i = 0; \$i < \$size; ++\$i)
{
echo " ". \$arr[\$i];
}
echo "\n";
}
// This function is determine that result array is constructed by x pair element or not
// x is provide pair element
public	function pairElement( & \$result, & \$x)
{
// Get the length
\$n = count(\$x);
\$m = count(\$result);
\$value = 0;
// Loop controlling variables i and j
\$i = 0;
\$j = 0;
// Use to collect unique element
\$element = array();
// Get unique elements of resultant array
for (\$i = 0; \$i < \$m; \$i++)
{
if (in_array(\$result[\$i], \$element,True) == false)
{
\$element[] = \$result[\$i];
}
}
// Outer loop execute through by pair element
for (\$i = 0; \$i < \$n - 1 && count(\$element) > 0; \$i++)
{
// Inner loop construct pair of next upcoming element
for (\$j = \$i + 1; \$j < \$n && count(\$element) > 0; \$j++)
{
// Get pair sum
\$value = \$x[\$i] + \$x[\$j];
if (in_array(\$value, \$element, TRUE))
{	\$key = array_search(\$value, \$element);
unset(\$element[\$key]);
}
}
}
echo " X : \n";
\$this->display(\$x, \$n);
echo " Result : \n";
\$this->display(\$result, \$m);
if (count(\$element) > 0)
{
echo " No, result array is not constructed by given x pair element\n";
}
else
{
echo " Yes, result array is constructed by given x pair element\n";
}
}
}

function main()
{
\$task = new PairArray();
// Pair array
\$x = array(1, 6, 2, 9, 5, -1, 3);
// Given of resultant array
\$result1 = array(3, 13, 11, 5);
\$result2 = array(0, 12, 8, 7, 9, 4, 8, 7);
// Test Cases
\$task->pairElement(\$result1, \$x);
\$task->pairElement(\$result2, \$x);
}
main();``````

Output

`````` X :
1 6 2 9 5 -1 3
Result :
3 13 11 5
No, result array is not constructed by given x pair element
X :
1 6 2 9 5 -1 3
Result :
0 12 8 7 9 4 8 7
Yes, result array is constructed by given x pair element``````
``````/*
Node Js program
Check whether one array is constructed by pair of another array
*/
class PairArray
{
//This function are displaying given array elements
display(arr, size)
{
for (var i = 0; i < size; ++i)
{
process.stdout.write(" " + arr[i]);
}
process.stdout.write("\n");
}
// This function is determine that result array is constructed by x pair element or not
// x is provide pair element
pairElement(result, x)
{
// Get the length
var n = x.length;
var m = result.length;
var value = 0;
// Loop controlling variables i and j
var i = 0;
var j = 0;
// Use to collect unique element
var element = new Set();
// Get unique elements of resultant array
for (i = 0; i < m; i++)
{
element.add(result[i]);
}
// Outer loop execute through by pair element
for (i = 0; i < n - 1 && element.size > 0; i++)
{
// Inner loop construct pair of next upcoming element
for (j = i + 1; j < n && element.size > 0; j++)
{
// Get pair sum
value = x[i] + x[j];
if (element.has(value))
{
element.delete(value);
}
}
}
process.stdout.write(" X : \n");
this.display(x, n);
process.stdout.write(" Result : \n");
this.display(result, m);
if (element.size > 0)
{
process.stdout.write(" No, result array is not constructed by given x pair element\n");
}
else
{
process.stdout.write(" Yes, result array is constructed by given x pair element\n");
}
}
}

function main()
{
var task = new PairArray();
// Pair array
var x = [1, 6, 2, 9, 5, -1, 3];
// Given of resultant array
var result1 = [3, 13, 11, 5];
var result2 = [0, 12, 8, 7, 9, 4, 8, 7];
// Test Cases
task.pairElement(result1, x);
task.pairElement(result2, x);
}
main();``````

Output

`````` X :
1 6 2 9 5 -1 3
Result :
3 13 11 5
No, result array is not constructed by given x pair element
X :
1 6 2 9 5 -1 3
Result :
0 12 8 7 9 4 8 7
Yes, result array is constructed by given x pair element``````
``````#   Python 3 program
#   Check whether one array is constructed by pair of another array

class PairArray :
# This function are displaying given list elements
def display(self, arr, size) :
i = 0
while (i < size) :
print(" ", arr[i], end = "")
i += 1

print(end = "\n")

#  This function is determine that result list is constructed by x pair element or not
#  x is provide pair element
def pairElement(self, result, x) :
#  Get the length
n = len(x)
m = len(result)
value = 0
#  Loop controlling variables i and j
i = 0
j = 0
#  Use to collect unique element
element = set()
#  Get unique elements of resultant list
while (i < m) :
element.add(result[i])
i += 1

#  Outer loop execute through by pair element
i = 0
while (i < n - 1 and len(element) > 0) :
#  Inner loop construct pair of next upcoming element
j = i + 1
while (j < n and len(element) > 0) :
#  Get pair sum
value = x[i] + x[j]
if (value in element) :
element.remove(value)

j += 1

i += 1

print(" X : ")
self.display(x, n)
print(" Result : ")
self.display(result, m)
if (len(element) > 0) :
print(" No, result array is not constructed by given x pair element")
else :
print(" Yes, result array is constructed by given x pair element")

def main() :
task = PairArray()
#  Pair list
x = [1, 6, 2, 9, 5, -1, 3]
#  Given of resultant list
result1 = [3, 13, 11, 5]
result2 = [0, 12, 8, 7, 9, 4, 8, 7]
#  Test Cases
task.pairElement(result1, x)
task.pairElement(result2, x)

if __name__ == "__main__": main()``````

Output

`````` X :
1  6  2  9  5  -1  3
Result :
3  13  11  5
No, result array is not constructed by given x pair element
X :
1  6  2  9  5  -1  3
Result :
0  12  8  7  9  4  8  7
Yes, result array is constructed by given x pair element``````
``````#   Ruby program
#   Check whether one array is constructed by pair of another array

class PairArray
# This function are displaying given array elements
def display(arr, size)
i = 0
while (i < size)
print(" ", arr[i])
i += 1
end

print("\n")
end

#  This function is determine that result array is constructed by x pair element or not
#  x is provide pair element
def pairElement(result, x)
#  Get the length
n = x.length
m = result.length
value = 0
#  Loop controlling variables i and j
i = 0
j = 0
#  Use to collect unique element
element = []
#  Get unique elements of resultant array
while (i < m)
if (element.include?(result[i])==false)
element.push(result[i])
end
i += 1
end

#  Outer loop execute through by pair element
i = 0
while (i < n - 1 && element.size > 0)
#  Inner loop construct pair of next upcoming element
j = i + 1
while (j < n && element.size > 0)
#  Get pair sum
value = x[i] + x[j]
if (element.include?(value))
element.delete(value)
end

j += 1
end

i += 1
end

print(" X : \n")
self.display(x, n)
print(" Result : \n")
self.display(result, m)
if (element.size > 0)
print(" No, result array is not constructed by given x pair element\n")
else
print(" Yes, result array is constructed by given x pair element\n")
end

end

end

def main()
task = PairArray.new()
#  Pair array
x = [1, 6, 2, 9, 5, -1, 3]
#  Given of resultant array
result1 = [3, 13, 11, 5]
result2 = [0, 12, 8, 7, 9, 4, 8, 7]
#  Test Cases
task.pairElement(result1, x)
task.pairElement(result2, x)
end

main()``````

Output

`````` X :
1 6 2 9 5 -1 3
Result :
3 13 11 5
No, result array is not constructed by given x pair element
X :
1 6 2 9 5 -1 3
Result :
0 12 8 7 9 4 8 7
Yes, result array is constructed by given x pair element
``````
``````import scala.collection.mutable._;
/*
Scala program
Check whether one array is constructed by pair of another array
*/
class PairArray
{
//This function are displaying given array elements
def display(arr: Array[Int], size: Int): Unit = {
var i: Int = 0;
while (i < size)
{
print(" " + arr(i));
i += 1;
}
print("\n");
}
// This function is determine that result array is constructed by x pair element or not
// x is provide pair element
def pairElement(result: Array[Int], x: Array[Int]): Unit = {
// Get the length
var n: Int = x.length;
var m: Int = result.length;
var value: Int = 0;
// Loop controlling variables i and j
var i: Int = 0;
var j: Int = 0;
// Use to collect unique element
var element: Set[Int] = Set();
// Get unique elements of resultant array
while (i < m)
{
element.add(result(i));
i += 1;
}
// Outer loop execute through by pair element
i = 0;
while (i < n - 1 && element.size > 0)
{
// Inner loop construct pair of next upcoming element
j = i + 1;
while (j < n && element.size > 0)
{
// Get pair sum
value = x(i) + x(j);
if (element.contains(value))
{
element.remove(value);
}
j += 1;
}
i += 1;
}
print(" X : \n");
this.display(x, n);
print(" Result : \n");
this.display(result, m);
if (element.size > 0)
{
print(" No, result array is not constructed by given x pair element\n");
}
else
{
print(" Yes, result array is constructed by given x pair element\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: PairArray = new PairArray();
// Pair array
var x: Array[Int] = Array(1, 6, 2, 9, 5, -1, 3);
// Given of resultant array
var result1: Array[Int] = Array(3, 13, 11, 5);
var result2: Array[Int] = Array(0, 12, 8, 7, 9, 4, 8, 7);
// Test Cases
task.pairElement(result1, x);
task.pairElement(result2, x);
}
}``````

Output

`````` X :
1 6 2 9 5 -1 3
Result :
3 13 11 5
No, result array is not constructed by given x pair element
X :
1 6 2 9 5 -1 3
Result :
0 12 8 7 9 4 8 7
Yes, result array is constructed by given x pair element``````
``````/*
Swift 4 program
Check whether one array is constructed by pair of another array
*/
class PairArray
{
//This function are displaying given array elements
func display(_ arr: [Int], _ size: Int)
{
var i: Int = 0;
while (i < size)
{
print(" ", arr[i], terminator: "");
i += 1;
}
print(terminator: "\n");
}
// This function is determine that result array is constructed by x pair element or not
// x is provide pair element
func pairElement(_ result: [Int], _ x: [Int])
{
// Get the length
let n: Int = x.count;
let m: Int = result.count;
var value: Int = 0;
// Loop controlling variables i and j
var i: Int = 0;
var j: Int = 0;
// Use to collect unique element
var element = Set<Int>()
// Get unique elements of resultant array
while (i < m)
{
element.insert(result[i]);
i += 1;
}
// Outer loop execute through by pair element
i = 0;
while (i < n - 1 && element.count > 0)
{
// Inner loop construct pair of next upcoming element
j = i + 1;
while (j < n && element.count > 0)
{
// Get pair sum
value = x[i] + x[j];
if (element.contains(value))
{
element.remove(value);
}
j += 1;
}
i += 1;
}
print(" X : ");
self.display(x, n);
print(" Result : ");
self.display(result, m);
if (element.count > 0)
{
print(" No, result array is not constructed by given x pair element");
}
else
{
print(" Yes, result array is constructed by given x pair element");
}
}
}
func main()
{
let task: PairArray = PairArray();
// Pair array
let x: [Int] = [1, 6, 2, 9, 5, -1, 3];
// Given of resultant array
let result1: [Int] = [3, 13, 11, 5];
let result2: [Int] = [0, 12, 8, 7, 9, 4, 8, 7];
// Test Cases
task.pairElement(result1, x);
task.pairElement(result2, x);
}
main();``````

Output

`````` X :
1  6  2  9  5  -1  3
Result :
3  13  11  5
No, result array is not constructed by given x pair element
X :
1  6  2  9  5  -1  3
Result :
0  12  8  7  9  4  8  7
Yes, result array is constructed by given x pair element``````
``````/*
Kotlin program
Check whether one array is constructed by pair of another array
*/
class PairArray
{
//This function are displaying given array elements
fun display(arr: Array < Int > , size: Int): Unit
{
var i: Int = 0;
while (i < size)
{
print(" " + arr[i]);
i += 1;
}
print("\n");
}
// This function is determine that result array is constructed by x pair element or not
// x is provide pair element
fun pairElement(result: Array < Int > , x: Array < Int > ): Unit
{
// Get the length
var n: Int = x.count();
var m: Int = result.count();
var value: Int ;
// Loop controlling variables i and j
var i: Int = 0;
var j: Int ;
// Use to collect unique element
var element: MutableSet <Int> = mutableSetOf <Int> ();
// Get unique elements of resultant array
while (i < m)
{
element.add(result[i]);
i += 1;
}
// Outer loop execute through by pair element
i = 0;
while (i < n - 1 && element.count() > 0)
{
// Inner loop construct pair of next upcoming element
j = i + 1;
while (j < n && element.count() > 0)
{
// Get pair sum
value = x[i] + x[j];
if (element.contains(value))
{
element.remove(value);
}
j += 1;
}
i += 1;
}
print(" X : \n");
this.display(x, n);
print(" Result : \n");
this.display(result, m);
if (element.count() > 0)
{
print(" No, result array is not constructed by given x pair element\n");
}
else
{
print(" Yes, result array is constructed by given x pair element\n");
}
}
}
fun main(args: Array < String > ): Unit
{
var task: PairArray = PairArray();
// Pair array
var x: Array < Int > = arrayOf(1, 6, 2, 9, 5, -1, 3);
// Given of resultant array
var result1: Array < Int > = arrayOf(3, 13, 11, 5);
var result2: Array < Int > = arrayOf(0, 12, 8, 7, 9, 4, 8, 7);
// Test Cases
task.pairElement(result1, x);
task.pairElement(result2, x);
}``````

Output

`````` X :
1 6 2 9 5 -1 3
Result :
3 13 11 5
No, result array is not constructed by given x pair element
X :
1 6 2 9 5 -1 3
Result :
0 12 8 7 9 4 8 7
Yes, result array is constructed by given x pair element``````

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post