Maximum pair sum in the given index ranges of an Array

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

  1. Find the maximum and the second maximum in the given query range.
  2. Sum of maximum and second maximum will be the maximum pair sum of the given range.

Below is the implementation of the above approach:



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 query
struct node {
    int f;
    int s;
};
  
// Function to find the required sum
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
        cout << (f + s) << endl;
    }
}
  
// Driver code
int 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);
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find maximum
// pair sum in the given
// index range of an Array
class GFG{
   
// Node structure to store a query
static class node {
    int f;
    int s;
    public node(int f, int s) {
        this.f = f;
        this.s = s;
    }
      
};
   
// Function to find the required sum
static 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 code
public 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find maximum
// pair sum in the given
// index range of an Array
using System;
  
class GFG{
    
// Node structure to store a query
class node {
    public int f;
    public int s;
    public node(int f, int s) {
        this.f = f;
        this.s = s;
    }
       
};
    
// Function to find the required sum
static 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 code
public 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
chevron_right

Output:
11
15

Performance Analysis:

Efficient Approach: The idea is to use the segment tree where each node of the segment tree store two values:

Article Tags :
Practice Tags :