Maximize sum of atmost K elements in array by taking only corner elements | Set 2
Last Updated :
09 Jun, 2021
Given an array arr[] and an integer K, the task is to find and maximize the sum of at most K elements in the Array by taking only corner elements.
A corner element is an element from the start of the array or from the end of the array.
Examples:
Input: N = 8, arr[] = {6, -1, 14, -15, 2, 1, 2, -5}, K = 4
Output: 19
Explanation:
Here the optimal choice is to pick three cards from the beginning. After that if we want to pick the next card, our points will decrease. So maximum points is arr[0] + arr[1] + arr[2] = 19.
Input : N = 5, arr[] = {-2, -1, -6, -3, 1}, K = 2
Output : 1
Here optimal choice is to pick last card. So maximum possible points is arr[4] = 1. Any further selection will reduce the value.
Naive Approach:
To solve the problem mentioned above we will use Recursion. As we can only take a start or end index value hence initialize two variables and take at most K steps and return the maximum sum among all the possible combinations. Update the maximum sum only if it is greater than the previous sum otherwise skip to the next possible combination. The recursive approach has exponential complexity due to its overlapping subproblem and optimal substructure property.
Below is the implementation of the above approach:
C++
// C++ implementation to Maximize sum of atmost
// K elements in Array by taking only corner elements
#include <bits/stdc++.h>
using namespace std;
// Function to return maximum points
int maxPointCount(int arr[], int K, int start, int end,
int points, int max_points)
{
if (K == 0) {
return max_points;
}
// Pick the start index
int points_start = points + arr[start];
// Update maximum points if necessary
max_points = max(max_points, points_start);
// Pick the end index
int points_end = points + arr[end];
// Update maximum points if necessary
max_points = max(max_points, points_end);
// Recursive call to get max value
return max(maxPointCount(arr, K - 1, start + 1, end,
points_start, max_points),
maxPointCount(arr, K - 1, start, end - 1,
points_end, max_points));
}
// Driver code
int main()
{
int arr[] = { -2, -1, -6, -3, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 2;
int points = 0;
int max_points = 0;
// beginning index
int start = 0;
// end index
int end = N - 1;
cout << maxPointCount(arr, K, start,
end, points, max_points);
return 0;
}
Java
// Java implementation to Maximize
// sum of atmost K elements in Array
// by taking only corner elements
import java.util.*;
class GFG{
// Function to return maximum points
static int maxPointCount(int arr[], int K,
int start, int end,
int points, int max_points)
{
if (K == 0)
{
return max_points;
}
// Pick the start index
int points_start = points + arr[start];
// Update maximum points if necessary
max_points = Math.max(max_points, points_start);
// Pick the end index
int points_end = points + arr[end];
// Update maximum points if necessary
max_points = Math.max(max_points, points_end);
// Recursive call to get max value
return Math.max(maxPointCount(arr, K - 1,
start + 1, end,
points_start, max_points),
maxPointCount(arr, K - 1,
start, end - 1,
points_end, max_points));
}
// Driver code
public static void main(String[] args)
{
int arr[] = { -2, -1, -6, -3, 1 };
int N = arr.length;
int K = 2;
int points = 0;
int max_points = 0;
// Beginning index
int start = 0;
// End index
int end = N - 1;
System.out.print(maxPointCount(arr, K, start,
end, points,
max_points));
}
}
// This code is contributed by Princi Singh
Python3
# Python3 implementation to maximize sum
# of atmost K elements in array by taking
# only corner elements
# Function to return maximum points
def maxPointCount(arr, K, start, end,
points, max_points):
if (K == 0):
return max_points
# Pick the start index
points_start = points + arr[start]
# Update maximum points if necessary
max_points = max(max_points, points_start)
# Pick the end index
points_end = points + arr[end]
# Update maximum points if necessary
max_points = max(max_points, points_end)
# Recursive call to get max value
return max(maxPointCount(arr, K - 1, start + 1, end,
points_start, max_points),
maxPointCount(arr, K - 1, start, end - 1,
points_end, max_points))
# Driver code
if __name__ == "__main__":
arr = [ -2, -1, -6, -3, 1 ]
N = len(arr)
K = 2
points = 0
max_points = 0
# Beginning index
start = 0
# end index
end = N - 1
print(maxPointCount(arr, K, start,
end, points,
max_points))
# This code is contributed by chitranayal
C#
// C# implementation to Maximize
// sum of atmost K elements in Array
// by taking only corner elements
using System;
class GFG{
// Function to return maximum points
static int maxPointCount(int []arr, int K,
int start, int end,
int points, int max_points)
{
if (K == 0)
{
return max_points;
}
// Pick the start index
int points_start = points + arr[start];
// Update maximum points if necessary
max_points = Math.Max(max_points, points_start);
// Pick the end index
int points_end = points + arr[end];
// Update maximum points if necessary
max_points = Math.Max(max_points, points_end);
// Recursive call to get max value
return Math.Max(maxPointCount(arr, K - 1,
start + 1, end,
points_start, max_points),
maxPointCount(arr, K - 1,
start, end - 1,
points_end, max_points));
}
// Driver code
public static void Main(String[] args)
{
int []arr = { -2, -1, -6, -3, 1 };
int N = arr.Length;
int K = 2;
int points = 0;
int max_points = 0;
// Beginning index
int start = 0;
// End index
int end = N - 1;
Console.Write(maxPointCount(arr, K, start,
end, points,
max_points));
}
}
// This code is contributed by sapnasingh4991
JavaScript
<script>
// Javascript implementation to Maximize
// sum of atmost K elements in Array
// by taking only corner elements
// Function to return maximum points
function maxPointCount(arr,K,start,end,points,max_points)
{
if (K == 0)
{
return max_points;
}
// Pick the start index
let points_start = points + arr[start];
// Update maximum points if necessary
max_points = Math.max(max_points, points_start);
// Pick the end index
let points_end = points + arr[end];
// Update maximum points if necessary
max_points = Math.max(max_points, points_end);
// Recursive call to get max value
return Math.max(maxPointCount(arr, K - 1,
start + 1, end,
points_start, max_points),
maxPointCount(arr, K - 1,
start, end - 1,
points_end, max_points));
}
// Driver code
let arr=[-2, -1, -6, -3, 1];
let N = arr.length;
let K = 2;
let points = 0;
let max_points = 0;
// Beginning index
let start = 0;
// End index
let end = N - 1;
document.write(maxPointCount(arr, K, start,
end, points,
max_points));
// This code is contributed by avanitrachhadiya2155
</script>
Efficient Approach:
To optimize the above solution we will implement the sliding window concept.
- Initially, the window size is 0 as we don't pick any element from the array. We take two-variable curr_points and max_points to represents current points and maximum points.
- Consider K elements one by one from the beginning. So in each step we calculate current points and update maximum points if necessary and after including K elements from the array our sliding window size becomes K, which is the maximum possible.
- After that in each step, we pick elements from the end and remove the rightmost element from the previously selected window with first K elements. Update curr_points and max_points. In the end, the window contains K cards from the end of the array.
- Finally, in each step remove the leftmost card from the previously selected window with K elements from the end. Update the values for curr_points and max_points. In the end, the window size will be 0 again.
Let us look at this example to understand it better, arr[] = {-2, -1, -6, -3, 1}, K = 2
Below is the implementation of the above approach:
C++
// C++ implementation to Maximize sum
// of atmost K elements in Array by taking
// only corner elements
#include <bits/stdc++.h>
using namespace std;
// Function to return maximum points
int maxPointCount(int arr[], int K, int size)
{
// Initialization of current points
// and max points so far
int curr_points = 0;
int max_points = 0;
// Add elements from the beginning
for (int i = 0; i < K; i++) {
curr_points += arr[i];
max_points = max(curr_points, max_points);
}
// Points to the end of array element
int j = size - 1;
// Add K elements from end of array
for (int i = K - 1; i >= 0; i--) {
curr_points = curr_points + arr[j] - arr[i];
max_points = max(curr_points, max_points);
// Decrement the value for j
j--;
}
j = size - K;
for (; j < size; j++) {
curr_points = curr_points - arr[j];
max_points = max(curr_points, max_points);
}
// Return the final result
return max_points;
}
// Driver code
int main()
{
int arr[] = { -2, -1, -6, -3, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 2;
cout << maxPointCount(arr, K, N);
return 0;
}
Java
// Java implementation to maximize
// sum of atmost K elements in Array
// by taking only corner elements
import java.util.Scanner;
import java.util.Arrays;
class GFG{
// Function to return maximum points
public static int maxPointCount(int arr[],
int K,
int size)
{
// Initialization of current points
// and max points so far
int curr_points = 0;
int max_points = 0;
// Add elements from the beginning
for(int i = 0; i < K; i++)
{
curr_points += arr[i];
max_points = Math.max(curr_points,
max_points);
}
// Points to the end of array element
int j = size - 1;
// Add K elements from end of array
for(int i = K - 1; i >= 0; i--)
{
curr_points = curr_points +
arr[j] - arr[i];
max_points = Math.max(curr_points,
max_points);
// Decrement the value for j
j--;
}
j = size - K;
for(; j < size; j++)
{
curr_points = curr_points - arr[j];
max_points = Math.max(curr_points,
max_points);
}
// Return the final result
return max_points;
}
// Driver code
public static void main(String args[])
{
int []arr = { -2, -1, -6, -3, 1 };
int N = arr.length;
int K = 2;
System.out.print(maxPointCount(arr, K, N));
}
}
// This code is contributed by SoumikMondal
Python3
# Python3 implementation to
# Maximize sum of atmost K
# elements in Array by taking
# only corner elements
# Function to return maximum
# points
def maxPointCount(arr, K, size):
# Initialization of current
# points and max points so far
curr_points = 0;
max_points = 0;
# Add elements from
# the beginning
for i in range(K):
curr_points += arr[i];
max_points = max(curr_points,
max_points)
# Points to the end
# of array element
j = size - 1;
# Add K elements from
# end of array
for i in range(K - 1, -1, -1):
curr_points = (curr_points +
arr[j] - arr[i]);
max_points = max(curr_points,
max_points);
# Decrement the
# value for j
j -= 1;
for j in range(size - K, size):
curr_points = (curr_points -
arr[j]);
max_points = max(curr_points,
max_points);
# Return the final result
return max_points;
# Driver code
if __name__ == "__main__":
arr = [-2, -1, -6, -3, 1]
N = len(arr)
K = 2;
print(maxPointCount(arr,K,N))
# This code is contributed by rutvik_56
C#
// C# implementation to maximize
// sum of atmost K elements in Array
// by taking only corner elements
using System;
class GFG{
// Function to return maximum points
static int maxPointCount(int[] arr, int K,
int size)
{
// Initialization of current points
// and max points so far
int curr_points = 0;
int max_points = 0;
// Add elements from the beginning
for(int i = 0; i < K; i++)
{
curr_points += arr[i];
max_points = Math.Max(curr_points,
max_points);
}
// Points to the end of array element
int j = size - 1;
// Add K elements from end of array
for(int i = K - 1; i >= 0; i--)
{
curr_points = curr_points + arr[j] -
arr[i];
max_points = Math.Max(curr_points,
max_points);
// Decrement the value for j
j--;
}
j = size - K;
for(; j < size; j++)
{
curr_points = curr_points - arr[j];
max_points = Math.Max(curr_points,
max_points);
}
// Return the final result
return max_points;
}
// Driver code
static void Main()
{
int[] arr = { -2, -1, -6, -3, 1 };
int N = arr.Length;
int K = 2;
Console.WriteLine(maxPointCount(arr, K, N));
}
}
// This code is contributed by divyeshrabadiya07
JavaScript
<script>
// JavaScript implementation to maximize
// sum of atmost K elements in Array
// by taking only corner elements
// Function to return maximum points
function maxPointCount(arr,K,size)
{
// Initialization of current points
// and max points so far
let curr_points = 0;
let max_points = 0;
// Add elements from the beginning
for(let i = 0; i < K; i++)
{
curr_points += arr[i];
max_points = Math.max(curr_points,
max_points);
}
// Points to the end of array element
let j = size - 1;
// Add K elements from end of array
for(let i = K - 1; i >= 0; i--)
{
curr_points = curr_points +
arr[j] - arr[i];
max_points = Math.max(curr_points,
max_points);
// Decrement the value for j
j--;
}
j = size - K;
for(; j < size; j++)
{
curr_points = curr_points - arr[j];
max_points = Math.max(curr_points,
max_points);
}
// Return the final result
return max_points;
}
// Driver code
let arr=[ -2, -1, -6, -3, 1 ];
let N = arr.length;
let K = 2;
document.write(maxPointCount(arr, K, N));
// This code is contributed by rag2127
</script>
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar article: Maximize sum of K elements in Array by taking only corner elements
Similar Reads
Maximize sum of K corner elements in Array
Given an array arr[] and an integer K, the task is to find the maximize the sum of K elements in the Array by taking only corner elements. A corner element is an element from the start of the array or from the end of the array. Examples: Input: arr[] = {8, 4, 4, 8, 12, 3, 2, 9}, K = 3 Output: 21 Exp
13 min read
Maximize total set bits of elements in N sized Array with sum M
Given two integers N and M denoting the size of an array and the sum of the elements of the array, the task is to find the maximum possible count of total set bits of all the elements of the array such that the sum of the elements is M. Examples: Input: N = 1, M = 15Output: 4Explanation: Since N =1,
8 min read
Maximize Bitwise OR of Array by incrementing elements by at most K
Given an array arr[], and an integer K, the task is to maximize the bitwise OR of the array arr[], where each element of arr[] can be incremented by almost K. Examples: Input: arr[]= {1, 3, 7, 0, 6, 1}, K = 2Output: [1 3 8 0 6 1]Explanation: Increase the number 7 by 1 ie 8 after that or of the array
7 min read
Maximum sum by picking elements from two arrays in order | Set 2
Given two arrays A[] and B[], each of size N, and two integers X and Y denoting the maximum number of elements that can be picked from A[] and B[] respectively, the task is to find the maximum possible sum by selecting N elements in such a way that for any index i, either A[i] or B[i] can be chosen.
10 min read
Maximize element at index K in an array with at most sum M and difference between adjacent elements at most 1
Given a positive integer N, the task is to construct an array of length N and find the maximum value at index K such that the sum of all the array elements is at most M and the absolute difference between any two consecutive array elements is at most 1.Examples:Input: N = 3, M = 7, K = 1Output: 3Exp
6 min read
Maximize count of distinct elements in a subsequence of size K in given array
Given an array arr[] of N integers and an integer K, the task is to find the maximum count of distinct elements over all the subsequences of K integers. Example: Input: arr[]={1, 1, 2, 2}, K=3Output: 2Explanation: The subsequence {1, 1, 2} has 3 integers and the number of distinct integers in it are
4 min read
Maximize frequency of an element by at most one increment or decrement of all array elements | Set 2
Given an array arr[] of size N, the task is to find the maximum frequency of any array element by incrementing or decrementing each array element by 1 at most once. Examples: Input: arr[] = { 3, 1, 4, 1, 5, 9, 2 } Output: 4 Explanation: Decrementing the value of arr[0] by 1 modifies arr[] to { 2, 1,
6 min read
Maximize the sum of differences of consecutive elements after removing exactly K elements
Given a sorted array arr[] of length N and an integer K such that K < N, the task is to remove exactly K elements from the array such that the sum of the differences of the consecutive elements of the array is maximized.Examples: Input: arr[] = {1, 2, 3, 4}, K = 1 Output: 3 Let's consider all the
4 min read
Smallest element with K set bits such that sum of Bitwise AND of each array element with K is maximum
Given an array arr[] consisting of N integers and integer K, the task is to find the smallest integer X with exactly K set bits such that the sum of Bitwise AND of X with every array element arr[i] is maximum. Examples: Input: arr[] = {3, 4, 5, 1}, K = 1Output: 4Explanation: Consider the value of X
8 min read
Maximize the minimum difference between any element pair by selecting K elements from given Array
Given an array of N integers the task is to select K elements out of these N elements in such a way that the minimum difference between each of the K numbers is the Largest. Return the largest minimum difference after choosing any K elements. Examples: Input: N = 4, K = 3, arr = [2, 6, 2, 5]Output:
11 min read