
- 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 subarray sum in array formed by repeating the given array k times in C++
In this problem, we are given an array and a number k. Our task is to create a program that will find the maximum subarray sum in an array formed by repeating the given array k time in c++.
Problem description − Here, we will find the maximum sum of the subarray formed from array formed by repeating the given array k times.
Let’s take an example to understand the problem,
Input − array = {3, 5, 1} k = 2
Output − 18
Explanation −
array formed by repeating k times, array = {3, 5, 1, 3, 5, 1} Maximum subarray sum = 3+5+1+3+5+1 = 18
To solve this problem, we will calculate the sum of all elements of the array.
Then based on the sum, we will calculate the subarray sum calculation.
If sum > 0, we will multiply the sum of array k times which given the actual sum.
If sum < 0, we will find the subarray with maxSum taking two array repetitions.
Example
Program to show the implementation of our solution,
#include<iostream> using namespace std; void repeatArray(int *arr, int *b, int k,int len) { int j = 0; while (k > 0){ for (int i = 0; i < len; i++) b[j++] = arr[i]; k--; } } long subArraySum(int *a,int len) { int max = 0; long newmax = 0; for (int i = 0; i < len; i++) { newmax = newmax + a[i]; if (max < newmax) max = newmax; if (newmax < 0) newmax = 0; } return max; } long findMaxSubArraySum(int *arr, int k,int len) { int arraySum = 0; long maxSum = 0; int b[(2 * len)]= {0}; repeatArray(arr, b, 2,len); for (int i = 0; i < len; i++) arraySum += arr[i]; maxSum = subArraySum(b,2*len); if (arraySum > 0) maxSum = subArraySum(b,2*len) + (k - 2) * arraySum; return maxSum; } int main() { int arr[] = { 3, 5, 1}; int length=sizeof(arr)/sizeof(arr[0]); int k = 3; cout<<"The maximum subarray sum in array formed by repeating the given array "<<k<<" times is "<<findMaxSubArraySum(arr, k,length); return 0; }
Output
The maximum subarray sum in array formed by repeating the given array 3 times is 27
- Related Articles
- Maximum subarray sum by flipping signs of at most K array elements in C++
- Maximum subarray sum in circular array using JavaScript
- Python – Sort Matrix by K Sized Subarray Maximum Sum
- Maximum subarray sum after dividing array into subarrays based on the given queries in Java
- Check a Subarray is Formed by Consecutive Integers from a Given Array in Java
- Maximum Size Subarray Sum Equals k in C++
- Maximum subarray sum in an array created after repeated concatenation in C++
- Maximum subarray sum in an array created after repeated concatenation in C++ Program
- Maximum Subarray Sum in a given Range in C++
- Find Sum of all unique subarray sum for a given array in C++
- Subarray Sum Equals K 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++
- Find maximum (or minimum) sum of a subarray of size k in C++
- Maximum Sum Circular Subarray in C++
- Maximum sum bitonic subarray in C++
