- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Pick maximum sum M elements such that contiguous repetitions do not exceed K in C++
In this problem, we are given array arr[] and two integers M and K. our task is to create an Array using elements of the given array. The size of the new array should M and any sub-array of size greater than K cannot have all elements the same. we have to print the maximum sum possible by the created array.
Let’s take an example to understand the problem
Input − arr[] = {1, 2, 4, 5, 7 }, M = 5, K = 2
Explanation − array created that satisfies the condition {7, 7, 5, 7, 7}. Here, no sub-array with size more than 2 can have all elements the same.
To solve this problem, we need to create an array using the element that has the maximum value. But we can’t use the max element more than k time, so after k times, we will have to use the second max element of the array. Insert one-second max value after every k max values in the array and create an array of M length. The final output will be the sum of all elements of this array.
Example
Program to show the implementation of our solution,
#include <iostream> using namespace std; long int arraySum(int arr[], int n, int m, int k){ int max1 = arr[0], max2 = arr[0]; for (int i = 1; i < n; i++) { if (arr[i] > max1) { max2 = max1; max1 = arr[i]; } else if (arr[i] > max2) max2 = arr[i]; } int max2count = m / (k + 1); long int sum = max2count * max2 + (m - max2count) * max1; return sum; } int main() { int arr[] = { 1, 3, 6, 7, 4, 5 }; int n = sizeof(arr) / sizeof(arr[0]); int m = 9, k = 2; cout<<"The maximum sum of array created from the given array such that no subarray of size greater than "<<k<<" will have same elements is "; cout<<arraySum(arr, n, m, k); return 0; }
Output
The maximum sum of array created from the given array such that no subarray of size greater than 2 will have same elements is 60
- Related Articles
- Count maximum elements of an array whose absolute difference does not exceed K in C++
- Maximum value K such that array has at-least K elements that are >= K in C++
- Maximum sum such that no two elements are adjacent in C++
- Maximum sum from three arrays such that picking elements consecutively from same is not allowed in C++
- Maximum sum such that no two elements are adjacent - Set 2 in C++
- Place K-knights such that they do not attack each other in C++
- Maximum sum in circular array such that no two elements are adjacent in C++
- Maximum subarray size, such that all subarrays of that size have sum less than k in C++
- Maximum sum such that no two elements are adjacent Alternate Method in C++ program
- Maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array in C++
- Maximum subarray size, such that all subarrays of that size have sum less than k in C++ program
- Maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array in C++ program
- Maximum contiguous sum of subarray in JavaScript
- Maximum sum subsequence with at-least k distant elements in C++
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k in C++
