Find the largest pair sum in an unsorted array
The largest pair sum in an unsorted array is the sum of the two largest numbers in the array. An array is a collection of elements, and when it is unsorted, the elements are not arranged in any particular order. To find the largest pair sum, we need to identify the two largest numbers in the array, add them together, and the result is the largest pair sum. The process involves searching through the array to find the maximum and second maximum elements, and then adding them together to get the answer. The largest pair sum can be useful in various applications, such as finding the most profitable trades in financial markets or determining the optimal resource allocation in resource management systems.
Program
//C Program
//Find the largest pair sum in an unsorted array
#include <stdio.h>
//Find the sum of largest pair in an unsorted array
void largest_sum(int arr[],int size)
{
if(size<=1)
{
return;
}
//Set initial array index
int first = 0;
int second =size-1;
for (int i = 0,j=size-1; i < size; ++i,j--)
{
if(i!=second && arr[i] > arr[first])
{
//When get a new max element
first = i;
}
if(j!=first && arr[j]>arr[second])
{
second=j;
}
}
//Display the sum of two largest element
printf("(%d ,%d) : %d\n",arr[first],arr[second] ,arr[first]+arr[second]);
}
int main()
{
//Define the value of array elements
int arr[] = {6,2,9,5,-2,4,8,2,7,1,3};
//Get the size of array
int size=sizeof(arr)/sizeof(arr[0]);
largest_sum(arr,size);
return 0;
}
Output
(9 ,8) : 17
#include<iostream>
using namespace std;
/*
C++ Program
Find the largest pair sum in an unsorted array
*/
class MyArray {
public:
//Find the sum of largest pair in an unsorted array
void largest_sum(int arr[], int size) {
if (size <= 1) {
return;
}
//Set initial array index
int first = 0;
int second = size - 1;
for (int i = 0, j = size - 1; i < size; ++i, j--) {
if (i != second && arr[i] > arr[first]) {
//When get a new max element
first = i;
}
if (j != first && arr[j] > arr[second]) {
second = j;
}
}
//Display the sum of two largest element
cout << "(" << arr[first] << " ," << arr[second] << ") : " << (arr[first] + arr[second]) << "\n";
}
};
int main() {
MyArray obj = MyArray();
int arr[] = {
6,
2,
9,
5,
-2,
4,
8,
2,
7,
1,
3
};
//Get the size of array
int size = sizeof(arr) / sizeof(arr[0]);
obj.largest_sum(arr, size);
return 0;
}
Output
(9 ,8) : 17
/*
Java Program
Find the largest pair sum in an unsorted array
*/
public class MyArray
{
//Find the sum of largest pair in an unsorted array
public void largest_sum(int []arr,int size)
{
if(size<=1)
{
return;
}
//Set initial array index
int first = 0;
int second =size-1;
for (int i = 0,j=size-1; i < size; ++i,j--)
{
if(i!=second && arr[i] > arr[first])
{
//When get a new max element
first = i;
}
if(j!=first && arr[j]>arr[second])
{
second=j;
}
}
//Display the sum of two largest element
System.out.print("("+arr[first]+" ,"+arr[second]+") : "+(arr[first]+arr[second])+"\n");
}
public static void main(String[] args) {
MyArray obj = new MyArray();
//Define the value of array elements
int []arr = {6,2,9,5,-2,4,8,2,7,1,3};
//Get the size of array
int size = arr.length;
obj.largest_sum(arr,size);
}
}
Output
(9 ,8) : 17
/*
C# Program
Find the largest pair sum in an unsorted array
*/
using System;
public class MyArray {
//Find the sum of largest pair in an unsorted array
public void largest_sum(int[] arr, int size) {
if (size <= 1) {
return;
}
//Set initial array index
int first = 0;
int second = size - 1;
for (int i = 0, j = size - 1; i < size; ++i, j--) {
if (i != second && arr[i] > arr[first]) {
//When get a new max element
first = i;
}
if (j != first && arr[j] > arr[second]) {
second = j;
}
}
Console.Write("(" + arr[first] + " ," + arr[second] + ") : " + (arr[first] + arr[second]) + "\n");
}
public static void Main(String[] args) {
MyArray obj = new MyArray();
int[]
//Define the value of array elements
arr = {
6,
2,
9,
5,
-2,
4,
8,
2,
7,
1,
3
};
//Get the size of array
int size = arr.Length;
obj.largest_sum(arr, size);
}
}
Output
(9 ,8) : 17
<?php
/*
Php Program
Find the largest pair sum in an unsorted array
*/
class MyArray {
//Find the sum of largest pair in an unsorted array
public function largest_sum($arr, $size) {
if ($size <= 1) {
return;
}
//Set initial array index
$first = 0;
$second = $size - 1;
for ($i = 0, $j = $size - 1; $i < $size; ++$i, $j--) {
if ($i != $second && $arr[$i] > $arr[$first]) {
//When get a new max element
$first = $i;
}
if ($j != $first && $arr[$j] > $arr[$second]) {
$second = $j;
}
}
//Display the sum of two largest element
echo("(". $arr[$first] ." ,". $arr[$second] .") : ". ($arr[$first] + $arr[$second]) ."\n");
}
}
function main() {
$obj = new MyArray();
//Define the value of array elements
$arr = array(6, 2, 9, 5, -2, 4, 8, 2, 7, 1, 3);
//Get the size of array
$size = count($arr);
$obj->largest_sum($arr, $size);
}
main();
Output
(9 ,8) : 17
/*
Node Js Program
Find the largest pair sum in an unsorted array
*/
class MyArray {
//Find the sum of largest pair in an unsorted array
largest_sum(arr, size) {
if (size <= 1) {
return;
}
//Set initial array index
var first = 0;
var second = size - 1;
for (var i = 0,j = size - 1; i < size; ++i, j--) {
if (i != second && arr[i] > arr[first]) {
//When get a new max element
first = i;
}
if (j != first && arr[j] > arr[second]) {
second = j;
}
}
//Display the sum of two largest element
process.stdout.write("(" + arr[first] + " ," + arr[second] + ") : " + (arr[first] + arr[second]) + "\n");
}
}
function main(args) {
var obj = new MyArray();
//Define the value of array elements
var arr = [6, 2, 9, 5, -2, 4, 8, 2, 7, 1, 3];
//Get the size of array
var size = arr.length;
obj.largest_sum(arr, size);
}
main();
Output
(9 ,8) : 17
# Python 3 Program
# Find the largest pair sum in an unsorted array
class MyArray :
# Find the sum of largest pair in an unsorted array
def largest_sum(self, arr, size) :
if (size <= 1) :
return
first = 0
second = size - 1
i = 0
j = size - 1
while (i < size) :
if (i != second and arr[i] > arr[first]) :
# When get a new max element
first = i
if (j != first and arr[j] > arr[second]) :
second = j
i += 1
j -= 1
print("(", arr[first] ," ,", arr[second] ,") : ", (arr[first] + arr[second]) ,"\n", end = "")
def main() :
obj = MyArray()
arr = [6, 2, 9, 5, -2, 4, 8, 2, 7, 1, 3]
size = len(arr)
obj.largest_sum(arr, size)
if __name__ == "__main__":
main()
Output
( 9 , 8 ) : 17
# Ruby Program
# Find the largest pair sum in an unsorted array
class MyArray
# Find the sum of largest pair in an unsorted array
def largest_sum(arr, size)
if (size <= 1)
return
end
first = 0
second = size - 1
i = 0
j = size - 1
while (i < size)
if (i != second && arr[i] > arr[first])
# When get a new max element
first = i
end
if (j != first && arr[j] > arr[second])
second = j
end
i += 1
j -= 1
end
print("(", arr[first] ," ,", arr[second] ,") :", (arr[first] + arr[second]) ,"\n")
end
end
def main()
obj = MyArray.new()
arr = [6, 2, 9, 5, -2, 4, 8, 2, 7, 1, 3]
size = arr.length
obj.largest_sum(arr, size)
end
main()
Output
(9 ,8) :17
/*
Scala Program
Find the largest pair sum in an unsorted array
*/
class MyArray {
//Find the sum of largest pair in an unsorted array
def largest_sum(arr: Array[Int], size: Int): Unit = {
if (size <= 1) {
return;
}
var first: Int = 0;
var second: Int = size - 1;
var i: Int = 0;
var j: Int = size - 1;
while (i < size) {
if (i != second && arr(i) > arr(first)) {
//When get a new max element
first = i;
}
if (j != first && arr(j) > arr(second)) {
second = j;
}
i += 1;
j -= 1;
}
print("(" + arr(first) + " ," + arr(second) + ") : " + (arr(first) + arr(second)) + "\n");
}
}
object Main {
def main(args: Array[String]): Unit = {
val obj: MyArray = new MyArray();
val arr: Array[Int] = Array(6, 2, 9, 5, -2, 4, 8, 2, 7, 1, 3);
val size: Int = arr.length;
obj.largest_sum(arr, size);
}
}
Output
(9 ,8) : 17
/*
Swift Program
Find the largest pair sum in an unsorted array
*/
class MyArray {
//Find the sum of largest pair in an unsorted array
func largest_sum(_ arr: [Int], _ size: Int) {
if (size <= 1) {
return;
}
var first: Int = 0;
var second: Int = size - 1;
var i: Int = 0;
var j: Int = size - 1;
while (i < size) {
if (i != second && arr[i] > arr[first]) {
//When get a new max element
first = i;
}
if (j != first && arr[j] > arr[second]) {
second = j;
}
i += 1;
j -= 1;
}
print("(", arr[first] ,",", arr[second] ,") :", (arr[first] + arr[second]) ,"\n", terminator: "");
}
}
func main() {
let obj: MyArray = MyArray();
let arr: [Int] = [6, 2, 9, 5, -2, 4, 8, 2, 7, 1, 3];
let size: Int = arr.count;
obj.largest_sum(arr, size);
}
main();
Output
( 9 , 8 ) : 17
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.
New Comment