Given two arrays A[] and B[] of length N. Then your task is to choose a range [L, R] in such a way that both the conditions are met, Then the task is to output the Maximum possible sum of such B[L, R].
- A[L, R] contains unique elements.
- Sum of B[L, R] is the maximum possible.
Examples:
Input: N = 5, A[] = {0, 1, 2, 0, 2}, B[] = {5, 6, 7, 8, 2}
Output: 21
Explanation: If we took the range [L, R] as [2, 4]. It gives A[2, 4] = {1, 2, 0} and B[2, 4] = {6, 7, 8}. The sum of B[2, 4] is 6+7+8 = 21. All the elements of A[2, 4] are unique and B[2, 4] is maximum 21. Both the conditions are satisfied. Therefore, output is 21.Input: N = 3, A[] = {1, 1, 1}, B[] = {3, 4, 5}
Output: 5
Explanation: If we took range [L, R] as [3, 3]. It gives A[3, 3] = {1} and B[3, 3] = {5}. Then sum of B[3, 3] is 5. which is maximum possible according to the given constraints. Therefore, output is 5.
Approach: Implement the idea below to solve the problem
The problem is observation based and can be implemented easily. The problem requires using HashSet Data - Structure.
Steps were taken to solve the problem:
- Create a HashSet let say Set to store the unique elements.
- Declare three variables let say J, Sum and Max, initialize them all equal to 0.
- Run a loop for i = 0 to i < N and follow below mentioned steps under the scope of loop:
- Num = A[i]
- If (Set is Empty)
- Add Num into Set
- Add B[i] into Sum
- Else
- If (Set not contains Num)
- Add num into Set
- Add B[i] into Sum
- Else
- While (A[j] != Num)
- Remove A[j] from Set
- Sum -= B[j]
- Increment J
- Subtract B[j] from Sum
- Increment j
- Sum += B[i]
- While (A[j] != Num)
- Update Max with Sum if Sum is greater than Max.
- If (Set not contains Num)
- Output the value stored in Max.
Code to implement the approach:
//code by flutterfly
#include <iostream>
#include <unordered_set>
#include <climits>
#include <vector>
using namespace std;
// Method to output the max possible sum
void Max_sum(int N, vector<int>& A, vector<int>& B) {
// HashSet for counting unique sets
unordered_set<int> set;
// Variable to hold max sum
long long maxSum = LLONG_MIN;
int j = 0;
// variable to hold current sum
long long sum = 0;
// Loop for iterating over A
for (int i = 0; i < N; i++) {
// Current element
int num = A[i];
// If set is empty then add the current element
// into both sum and set
if (set.empty()) {
set.insert(num);
sum += B[i];
} else {
// If the current element is not repeated
// Then add it to both sum and set
if (set.find(num) == set.end()) {
set.insert(num);
sum += B[i];
} else {
// Remove the elements from set and sum
// until the set contains a unique element
while (A[j] != num) {
set.erase(A[j]);
sum -= B[j];
j++;
}
sum -= B[j];
j++;
sum += B[i];
}
}
// Updating max with the current sum
maxSum = max(maxSum, sum);
}
// Printing out the maximum possible sum
cout << maxSum << endl;
}
// Driver Function
int main() {
// Inputs
int N = 5;
vector<int> A = {0, 1, 2, 0, 2};
vector<int> B = {5, 6, 7, 8, 2};
// Function call
Max_sum(N, A, B);
return 0;
}
// Java code to implement the approach
import java.util.*;
// Driver Class
class Main {
// Driver Function
public static void main(String[] args)
{
// Inputs
int N = 5;
int[] A = { 0, 1, 2, 0, 2 };
int[] B = { 5, 6, 7, 8, 2 };
// Functionc call
Max_sum(N, A, B);
}
// Method to output the max possible sum
public static void Max_sum(int N, int[] A, int[] B)
{
// HashSet for counting unique sets
HashSet<Integer> set = new HashSet<>();
// Variable to hold max sum
long max = Integer.MIN_VALUE;
int j = 0;
// variable to hold current sum
long sum = 0;
// Loop for iterating over A
for (int i = 0; i < N; i++) {
// Current elemment
int num = A[i];
// If set is empty then add current element
// into both sum and set
if (set.isEmpty()) {
set.add(num);
sum += B[i];
}
// Else block will exexute when set will have at
// least one element Formally, not empty.
else {
// If current element is not repeated
// Then add in both sum and set
if (!set.contains(num)) {
set.add(num);
sum += B[i];
}
// Else remove the elements from set and
// sum until the set contains unique element
else {
while (A[j] != num) {
set.remove(A[j]);
sum -= B[j];
j++;
}
sum -= B[j];
j++;
sum += B[i];
}
}
// Updating max with current sum
max = Math.max(max, sum);
}
// Printing out the maximum possible sum
System.out.println(max);
}
}
# code by flutterfly
def max_sum(N, A, B):
# Set for counting unique elements
unique_set = set()
# Variables to hold max sum and current sum
max_sum = float('-inf')
j = 0
current_sum = 0
# Loop for iterating over A
for i in range(N):
# Current element
num = A[i]
# If set is empty, add the current element
# into both sum and set
if not unique_set:
unique_set.add(num)
current_sum += B[i]
else:
# If the current element is not repeated,
# add it to both sum and set
if num not in unique_set:
unique_set.add(num)
current_sum += B[i]
else:
# Remove elements from set and sum
# until the set contains a unique element
while A[j] != num:
unique_set.remove(A[j])
current_sum -= B[j]
j += 1
current_sum -= B[j]
j += 1
current_sum += B[i]
# Updating max with the current sum
max_sum = max(max_sum, current_sum)
# Printing out the maximum possible sum
print(max_sum)
# Driver Function
if __name__ == "__main__":
# Inputs
N = 5
A = [0, 1, 2, 0, 2]
B = [5, 6, 7, 8, 2]
# Function call
max_sum(N, A, B)
using System;
using System.Collections.Generic;
class Program
{
// Method to output the max possible sum
static void MaxSum(int N, int[] A, int[] B)
{
// HashSet for counting unique sets
HashSet<int> uniqueSet = new HashSet<int>();
// Variables to hold max sum and current sum
long maxSumValue = long.MinValue;
int j = 0;
long currentSum = 0;
// Loop for iterating over A
for (int i = 0; i < N; i++)
{
// Current element
int num = A[i];
// If set is empty, add the current element
// into both sum and set
if (uniqueSet.Count == 0)
{
uniqueSet.Add(num);
currentSum += B[i];
}
else
{
// If the current element is not repeated,
// add it to both sum and set
if (!uniqueSet.Contains(num))
{
uniqueSet.Add(num);
currentSum += B[i];
}
else
{
// Remove elements from set and sum
// until the set contains a unique element
while (A[j] != num)
{
uniqueSet.Remove(A[j]);
currentSum -= B[j];
j++;
}
currentSum -= B[j];
j++;
currentSum += B[i];
}
}
// Updating max with the current sum
maxSumValue = Math.Max(maxSumValue, currentSum);
}
// Printing out the maximum possible sum
Console.WriteLine(maxSumValue);
}
// Driver Function
static void Main()
{
// Inputs
int N = 5;
int[] A = { 0, 1, 2, 0, 2 };
int[] B = { 5, 6, 7, 8, 2 };
// Function call
MaxSum(N, A, B);
}
}
// Method to output the max possible sum
function maxSum(N, A, B) {
// Set for counting unique elements
const uniqueSet = new Set();
// Variables to hold max sum and current sum
let maxSumValue = Number.NEGATIVE_INFINITY;
let j = 0;
let currentSum = 0;
// Loop for iterating over A
for (let i = 0; i < N; i++) {
// Current element
const num = A[i];
// If set is empty, add the current element
// into both sum and set
if (uniqueSet.size === 0) {
uniqueSet.add(num);
currentSum += B[i];
} else {
// If the current element is not repeated,
// add it to both sum and set
if (!uniqueSet.has(num)) {
uniqueSet.add(num);
currentSum += B[i];
} else {
// Remove elements from set and sum
// until the set contains a unique element
while (A[j] !== num) {
uniqueSet.delete(A[j]);
currentSum -= B[j];
j++;
}
currentSum -= B[j];
j++;
currentSum += B[i];
}
}
// Updating max with the current sum
maxSumValue = Math.max(maxSumValue, currentSum);
}
// Printing out the maximum possible sum
console.log(maxSumValue);
}
// Driver Function
// Inputs
const N = 5;
const A = [0, 1, 2, 0, 2];
const B = [5, 6, 7, 8, 2];
// Function call
maxSum(N, A, B);
Output
21
Time Complexity: O(N)
Auxiliary space: O(N), As HashSet is used to store unique elements.