
- 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
Find maximum sum taking every Kth element in the array in C++
In this problem, we are given an array arr[] and an integer k. Our task is to Find the maximum sum taking every Kth element in the array.
Problem description: We need to find the maximum sum of the elements of the array such that they are k indexes apart. i.e. we need to maximize the sum,
sum = arr[i] + arr[i+k] + arr[i + 2*k] + …. arr[i + p*k], such that (i + p*k) < n
Let’s take an example to understand the problem,
Input
arr[] = {5, 3, −1, 2, 4, −5, 6}, k = 4
Output
9
Explanation
All sums of every kth element is 5 + 4 = 9 3 − 5 = −2 −1 + 6 = 5 2 4 −5 6 The maximum is 9
Solution Approach
A Simple solution to the problem is by using two nested loops, the outer one for each elements of the array and the inner one is used to find the sum of taking every kth element from i. i.e. sum = arr[i] + arr[i + k] + arr[i + 2*k] + … + arr[i + p*k] such that (i + p*k) < n.
And return the maximum sum.
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; int findMaxSumK(int arr[], int n, int K){ int maxSum = -1000; for (int i = 0; i < n; i++) { int current_Sum = 0; for (int j = i; j < n; j += K) current_Sum = current_Sum + arr[j]; maxSum = max(maxSum, current_Sum); } return maxSum; } int main(){ int arr[] = {5, 3, -1, 2, 4, -5, 6}; int n = sizeof(arr) / sizeof(arr[0]); int K = 3; cout<<"The maximum sum taking every Kth element in the array is "<<findMaxSumK(arr, n, K); return (0); }
Output
The maximum sum taking every Kth element in the array is 13
Program to illustrate the working of a solution,
Example
#include <iostream> using namespace std; int findMaxSumK(int arr[], int n, int K) { int maxSum = -1000; int suffSum[n] = {0}; for (int i = n - 1; i >= 0; i--) { if (i + K < n) suffSum[i] = suffSum[i + K] + arr[i]; else suffSum[i] = arr[i]; maxSum = max(maxSum, suffSum[i]); } return maxSum; } int main(){ int arr[] = {5, 3, -1, 2, 4, -5, 6}; int n = sizeof(arr) / sizeof(arr[0]); int K = 3; cout<<"The maximum sum taking every Kth element in the array is "<<findMaxSumK(arr, n, K); return (0); }
Output
The maximum sum taking every Kth element in the array is 13
- Related Articles
- Kth smallest element after every insertion in C++
- Maximum possible XOR of every element in an array with another array in C++
- Find closest value for every element in array in C++
- Kth Largest Element in an Array
- Maximum number by concatenating every element in a rotation of an array in C++
- Find closest greater value for every element in array in C++
- Find closest smaller value for every element in array in C++
- Python – Extract Kth element of every Nth tuple in List
- Finding sum of every nth element of array in JavaScript
- Kth Largest Element in an Array in Python
- Find last element after deleting every second element in array of n integers in C++
- Maximum triplet sum in array in C++
- Find the element that appears once in an array where every other element appears twice in C++
- Maximum Unique Element in every subarray of size K in c++
- Floor of every element in same array in C++
