Given an array arr containing N positive integers and the number of queries Q, for each query task is to find the maximum pair sum in the given index range [L, R] where L and R are the respective low and high indices.
Examples:
Input: arr = {3, 4, 5, 6, 7, 8}, Q[][2] = [[0, 3], [3, 5]]
Output:
11
15
Explanation:
For the first query, subarray is [3, 4, 5, 6]
All the pairs are (3, 4), (3, 5), (3, 6), (4, 5), (4, 6), (5, 6)
Hence the maximum pair sum = (5 + 6) = 11
For the second query, subarray is [6, 7, 8]
All the pairs of the subarray are (6, 7), (6, 8), (7, 8)
Hence the maximum pair sum = (7 + 8) = 15
Naive Approach: For each query range, do the following
- Find the maximum and the second maximum in the given query range.
- Sum of maximum and second maximum will be the maximum pair sum of the given range.
Below is the implementation of the above approach:
C++
// C++ program to find maximum// pair sum in the given// index range of an Array#include <bits/stdc++.h>using namespace std; // Node structure to store a querystruct node { int f; int s;}; // Function to find the required sumvoid findSum(int* arr, int n, int Q, node* query){ // Run a loop to iterate // over query array for (int i = 0; i < Q; i++) { // declare first 'f' // and second 's' variables int f, s; // Initialise them with 0 f = s = 0; // Iterate over the // given array from // range query[i].f to query[i].s for (int j = query[i].f; j <= query[i].s; j++) { // If the array element // value is greater than // current f, store // current f in s and // array element value in f if (arr[j] >= f) { s = f; f = arr[j]; } // else if element // is greater than s, // update s with // array element value else if (arr[j] > s) s = arr[j]; } // print the sum of f and s cout << (f + s) << endl; }} // Driver codeint main(){ // Given array and number of queries int arr[] = { 3, 4, 5, 6, 7, 8 }, Q = 2; int n = sizeof(arr) / sizeof(arr[0]); // Declare and define queries node query[Q]; query[0] = { 0, 3 }; query[1] = { 3, 5 }; findSum(arr, n, Q, query);} |
Java
// Java program to find maximum// pair sum in the given// index range of an Arrayclass GFG{ // Node structure to store a querystatic class node { int f; int s; public node(int f, int s) { this.f = f; this.s = s; } }; // Function to find the required sumstatic void findSum(int []arr, int n, int Q, node []query){ // Run a loop to iterate // over query array for (int i = 0; i < Q; i++) { // declare first 'f' // and second 's' variables int f, s; // Initialise them with 0 f = s = 0; // Iterate over the // given array from // range query[i].f to query[i].s for (int j = query[i].f; j <= query[i].s; j++) { // If the array element // value is greater than // current f, store // current f in s and // array element value in f if (arr[j] >= f) { s = f; f = arr[j]; } // else if element // is greater than s, // update s with // array element value else if (arr[j] > s) s = arr[j]; } // print the sum of f and s System.out.print((f + s) +"\n"); }} // Driver codepublic static void main(String[] args){ // Given array and number of queries int arr[] = { 3, 4, 5, 6, 7, 8 }, Q = 2; int n = arr.length; // Declare and define queries node []query = new node[2]; query[0] = new node( 0, 3 ); query[1] = new node(3, 5 ); findSum(arr, n, Q, query);}} // This code is contributed by PrinciRaj1992 |
C#
// C# program to find maximum// pair sum in the given// index range of an Arrayusing System; class GFG{ // Node structure to store a queryclass node { public int f; public int s; public node(int f, int s) { this.f = f; this.s = s; } }; // Function to find the required sumstatic void findSum(int []arr, int n, int Q, node []query){ // Run a loop to iterate // over query array for (int i = 0; i < Q; i++) { // declare first 'f' // and second 's' variables int f, s; // Initialise them with 0 f = s = 0; // Iterate over the // given array from // range query[i].f to query[i].s for (int j = query[i].f; j <= query[i].s; j++) { // If the array element // value is greater than // current f, store // current f in s and // array element value in f if (arr[j] >= f) { s = f; f = arr[j]; } // else if element // is greater than s, // update s with // array element value else if (arr[j] > s) s = arr[j]; } // print the sum of f and s Console.Write((f + s) +"\n"); }} // Driver codepublic static void Main(String[] args){ // Given array and number of queries int []arr = { 3, 4, 5, 6, 7, 8 }; int Q = 2; int n = arr.Length; // Declare and define queries node []query = new node[2]; query[0] = new node( 0, 3 ); query[1] = new node(3, 5 ); findSum(arr, n, Q, query);}} // This code is contributed by PrinciRaj1992 |
11 15
Performance Analysis:
- Time Complexity: In the above approach, we are looper over the array of length N for each of Q query. Thus the time complexity would be O(N*Q).
- Auxiliary Space: In the above approach there is no extra space used, therefore the Auxiliary Space complexity will be O(1).
Efficient Approach: The idea is to use the segment tree where each node of the segment tree store two values:
- Maximum of the given below subtree
- Second maximum of the given below subtree
- If size of the array is 1, then we simply store first max=arr[start] and second max=0 where start is the index of the array.
- Else
- Recursively determine the firstMax
(l1)and the second max(l2)for the left half of the array. - Recursively determine the firstMax
(r1)and the second max(r2)for the right half of the array.
- Recursively determine the firstMax
- Now we will determine the first max and the second max of the present node as
firstMax=max(l1, r1)andsecondMax=max(min(l1, r1), max(l2, r2)) - If the range of the node is within l and r then return the value of the present
node - else if the range of the node is completely outside l and r then return 0
- else recursively find
first max(l1)andsecond max(l2)from
left half and thefirstMax(r1)and thesecondMax(r2)from the right
half. - return the
max(l1, r1) and max(min(l1, r1), max(l2, r2)). - Time Complexity: In the above approach, we are building a segment tree which is of time complexity O(N) Then for each of Q queries, we find the solution in O(logN) time. So overall time complexity is O(N+Q*logN)
- Auxiliary Space Complexity: In the above approach, we are using extra space for storing the segment tree which is taking (4 * N + 1) space. So Auxiliary space complexity is O(N)
Algorithm to build the required segment tree
Algorithm for the Query to find maximum pair sum of given range
Below is the implementation of the above approach:
CPP
// C++ program to find maximum// pair sum in the given// index range of an Array#include <bits/stdc++.h>usingnamespacestd;// Node structure to store a querystructnode {intf;ints;};// Function to build the treevoidbuild(intarr[], node tree[],intstart,intend,intindex){// If there is just one element// in the array range,// set maximum as that element,// and second maximum as zeroif(start == end) {tree[index].f = arr[start];tree[index].s = 0;return;}// Calculate the mid valueintmid = start + (end - start) / 2;// Recursively build the tree// for the range [start, mid]// and store the value// at index (2 * index + 1)build(arr, tree, start,mid, 2 * index + 1);// Recursively build the tree// for the range [mid + 1, end]// and store the value// at index (2 * index + 2)build(arr, tree, mid + 1,end, 2 * index + 2);// Get the maximum and// second maximum from// both left and right subtreesintl1 = tree[2 * index + 1].f;intl2 = tree[2 * index + 1].s;intr1 = tree[2 * index + 2].f;intr2 = tree[2 * index + 2].s;// Set the maximum for this node as// maximum of l1 and r1tree[index].f = max(l1, r1);// Set the second maximum for this// node as maximum of// min(l1, r1) & max(l2, r2)tree[index].s= max(min(l1, r1),max(l2, r2));}// Function to execute a// query on the segment treenode runQuery(node tree[],intstart,intend,intindex,intL,intR){// If the range of// the node is completely// outside l and r then return 0if(R < start or L > end) {return{ 0, 0 };}// If the range of the// node is within l and r// then return the value// of the present nodeif(L <= start and R >= end) {returntree[index];}// calculate mid valueintmid = start + (end - start) / 2;// Recursively find first// max and second max from// the left halfnode Left= runQuery(tree, start,mid, 2 * index + 1,L, R);// Recursively find first// max and second max from// the right halfnode Right= runQuery(tree, mid + 1,end, 2 * index + 2,L, R);// Get the values// of l1, l2, r1, r2intl1 = Left.f;intl2 = Left.s;intr1 = Right.f;intr2 = Right.s;// return the maximum// as max(l1, r1), and// second maximum as// max(min(l1, r1), max(l2, r2))return{ max(l1, r1),max(min(l1, r1),max(l2, r2)) };}// Function to find the required sumvoidfindSum(int* arr,intn,intQ, node* query){// Declare an array of// length '4 * n + 1' and// build the treenode tree[4 * n + 1];build(arr, tree, 0, n - 1, 0);// Run a loop to iterate// over each queryfor(inti = 0; i < Q; i++) {// Call the query function// with given rangenode temp= runQuery(tree, 0,n - 1, 0,query[i].f,query[i].s);// Store maximum and second// maximum in variablesintf = temp.f;ints = temp.s;// Return sum of the maximum// and second maximumcout << (f + s) << endl;}}// Driver codeintmain(){// Given array and number of queriesintarr[] = { 3, 4, 5, 6, 7, 8 }, Q = 2;intn =sizeof(arr) /sizeof(arr[0]);// Declare and define queriesnode query[Q];query[0] = { 0, 3 };query[1] = { 3, 5 };findSum(arr, n, Q, query);}Java
// Java program to find maximum// pair sum in the given// index range of an ArrayclassGFG{// Node structure to store a querystaticclassnode {intf;ints;publicnode(intf,ints) {this.f = f;this.s = s;}publicnode() {// TODO Auto-generated constructor stub}};// Function to build the treestaticvoidbuild(intarr[], node tree[],intstart,intend,intindex){// If there is just one element// in the array range,// set maximum as that element,// and second maximum as zeroif(start == end ) {tree[index].f = arr[start];tree[index].s =0;return;}// Calculate the mid valueintmid = start + (end - start) /2;// Recursively build the tree// for the range [start, mid]// and store the value// at index (2 * index + 1)build(arr, tree, start,mid,2* index +1);// Recursively build the tree// for the range [mid + 1, end]// and store the value// at index (2 * index + 2)build(arr, tree, mid +1,end,2* index +2);// Get the maximum and// second maximum from// both left and right subtreesintl1 = tree[2* index +1].f;intl2 = tree[2* index +1].s;intr1 = tree[2* index +2].f;intr2 = tree[2* index +2].s;// Set the maximum for this node as// maximum of l1 and r1tree[index].f = Math.max(l1, r1);// Set the second maximum for this// node as maximum of// Math.min(l1, r1) & Math.max(l2, r2)tree[index].s= Math.max(Math.min(l1, r1),Math.max(l2, r2));}// Function to execute a// query on the segment treestaticnode runQuery(node tree[],intstart,intend,intindex,intL,intR){// If the range of// the node is completely// outside l and r then return 0if(R < start || L > end) {returnnewnode(0,0);}// If the range of the// node is within l and r// then return the value// of the present nodeif(L <= start && R >= end) {returntree[index];}// calculate mid valueintmid = start + (end - start) /2;// Recursively find first// max and second max from// the left halfnode Left= runQuery(tree, start,mid,2* index +1,L, R);// Recursively find first// max and second max from// the right halfnode Right= runQuery(tree, mid +1,end,2* index +2,L, R);// Get the values// of l1, l2, r1, r2intl1 = Left.f;intl2 = Left.s;intr1 = Right.f;intr2 = Right.s;// return the maximum// as Math.max(l1, r1), and// second maximum as// Math.max(Math.min(l1, r1), Math.max(l2, r2))returnnewnode( Math.max(l1, r1),Math.max(Math.min(l1, r1),Math.max(l2, r2)) );}// Function to find the required sumstaticvoidfindSum(int[]arr,intn,intQ, node []query){// Declare an array of// length '4 * n + 1' and// build the treenode []tree =newnode[4* n +1];for(inti=0;i<4* n +1;i++) {tree[i] =newnode();}build(arr, tree,0, n -1,0);// Run a loop to iterate// over each queryfor(inti =0; i < Q; i++) {// Call the query function// with given rangenode temp= runQuery(tree,0,n -1,0,query[i].f,query[i].s);// Store maximum and second// maximum in variablesintf = temp.f;ints = temp.s;// Return sum of the maximum// and second maximumSystem.out.print((f + s) +"\n");}}// Driver codepublicstaticvoidmain(String[] args){// Given array and number of queriesintarr[] = {3,4,5,6,7,8}, Q =2;intn = arr.length;// Declare and define queriesnode []query =newnode[Q];query[0] =newnode(0,3);query[1] =newnode(3,5);findSum(arr, n, Q, query);}}// This code is contributed by Princi SinghC#
// C# program to find maximum// pair sum in the given// index range of an ArrayusingSystem;classGFG{// Node structure to store a queryclassnode {publicintf;publicints;publicnode(intf,ints) {this.f = f;this.s = s;}publicnode() {// TODO Auto-generated constructor stub}};// Function to build the treestaticvoidbuild(int[]arr, node []tree,intstart,intend,intindex){// If there is just one element// in the array range,// set maximum as that element,// and second maximum as zeroif(start == end ) {tree[index].f = arr[start];tree[index].s = 0;return;}// Calculate the mid valueintmid = start + (end - start) / 2;// Recursively build the tree// for the range [start, mid]// and store the value// at index (2 * index + 1)build(arr, tree, start,mid, 2 * index + 1);// Recursively build the tree// for the range [mid + 1, end]// and store the value// at index (2 * index + 2)build(arr, tree, mid + 1,end, 2 * index + 2);// Get the maximum and// second maximum from// both left and right subtreesintl1 = tree[2 * index + 1].f;intl2 = tree[2 * index + 1].s;intr1 = tree[2 * index + 2].f;intr2 = tree[2 * index + 2].s;// Set the maximum for this node as// maximum of l1 and r1tree[index].f = Math.Max(l1, r1);// Set the second maximum for this// node as maximum of// Math.Min(l1, r1) & Math.Max(l2, r2)tree[index].s= Math.Max(Math.Min(l1, r1),Math.Max(l2, r2));}// Function to execute a// query on the segment treestaticnode runQuery(node []tree,intstart,intend,intindex,intL,intR){// If the range of// the node is completely// outside l and r then return 0if(R < start || L > end) {returnnewnode( 0, 0 );}// If the range of the// node is within l and r// then return the value// of the present nodeif(L <= start && R >= end) {returntree[index];}// calculate mid valueintmid = start + (end - start) / 2;// Recursively find first// max and second max from// the left halfnode Left= runQuery(tree, start,mid, 2 * index + 1,L, R);// Recursively find first// max and second max from// the right halfnode Right= runQuery(tree, mid + 1,end, 2 * index + 2,L, R);// Get the values// of l1, l2, r1, r2intl1 = Left.f;intl2 = Left.s;intr1 = Right.f;intr2 = Right.s;// return the maximum// as Math.Max(l1, r1), and// second maximum as// Math.Max(Math.Min(l1, r1), Math.Max(l2, r2))returnnewnode( Math.Max(l1, r1),Math.Max(Math.Min(l1, r1),Math.Max(l2, r2)) );}// Function to find the required sumstaticvoidfindSum(int[]arr,intn,intQ, node []query){// Declare an array of// length '4 * n + 1' and// build the treenode []tree =newnode[4 * n + 1];for(inti=0;i<4 * n + 1;i++) {tree[i] =newnode();}build(arr, tree, 0, n - 1, 0);// Run a loop to iterate// over each queryfor(inti = 0; i < Q; i++) {// Call the query function// with given rangenode temp= runQuery(tree, 0,n - 1, 0,query[i].f,query[i].s);// Store maximum and second// maximum in variablesintf = temp.f;ints = temp.s;// Return sum of the maximum// and second maximumConsole.Write((f + s) +"\n");}}// Driver codepublicstaticvoidMain(String[] args){// Given array and number of queriesint[]arr = { 3, 4, 5, 6, 7, 8 };intQ = 2;intn = arr.Length;// Declare and define queriesnode []query =newnode[Q];query[0] =newnode( 0, 3 );query[1] =newnode( 3, 5 );findSum(arr, n, Q, query);}}// This code is contributed by Rajput-JiOutput:11 15
Performance Analysis:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.


