
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum possible sum of a window in an array such that elements of same window in other array are unique in c++
In this tutorial, we will be discussing a program to find maximum possible sum of a window in an array such that elements of same window in other array are unique.
For this we will be provided with two arrays with equal number of elements. Our task is to find the window in one element with maximum sum such that the same window in other array is unique.
Example
#include <bits/stdc++.h> using namespace std; //returning maximum sum window int returnMaxSum(int A[], int B[], int n) { //storing elements with their count unordered_set<int> mp; int result = 0; int curr_sum = 0, curr_begin = 0; for (int i = 0; i < n; ++i) { while (mp.find(A[i]) != mp.end()) { mp.erase(A[curr_begin]); curr_sum -= B[curr_begin]; curr_begin++; } mp.insert(A[i]); curr_sum += B[i]; result = max(result, curr_sum); } return result; } int main() { int A[] = { 0, 1, 2, 3, 0, 1, 4 }; int B[] = { 9, 8, 1, 2, 3, 4, 5 }; int n = sizeof(A)/sizeof(A[0]); cout << returnMaxSum(A, B, n); return 0; }
Output
20
- Related Articles
- Maximum sum in circular array such that no two elements are adjacent in C++
- Maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array in C++
- Find maximum of minimum for every window size in a given array in C++
- Maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array in C++ program
- Count array elements that divide the sum of all other elements in C++
- Find an element in array such that sum of left array is equal to sum of right array using c++
- Maximum sum such that no two elements are adjacent in C++
- Maximum sum of n consecutive elements of array in JavaScript
- Maximum value K such that array has at-least K elements that are >= K in C++
- Unique number of occurrences of elements in an array in JavaScript
- Maximum possible XOR of every element in an array with another array in C++
- Maximum sum subarray such that start and end values are same in C++
- Maximum possible difference of two subsets of an array in C++
- Counting unique elements in an array in JavaScript
- Sliding Window Maximum in C++

Advertisements