
- 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 sum in circular array such that no two elements are adjacent in C++
In this problem, we are given a circular array cirArr[]. Our task is to create a program to find the Maximum sum in circular array such that no two elements are adjacent in C++.
Problem Description
For the circular array, we need to find the maximum sum sum of elements of the array such that adjacent elements cannot be taken i.e. we need to take alternate elements.
Circular Array is a special type of array in which the last element of the array is connected to the first element.
Let’s take an example to understand the problem,
Input
cirArr[] = {4, 1, 5, 3, 2}
Output
9
Explanation
The maximum sum circular subsequence is [4, 5, 2]. Sum = 9
Solution Approach
The solution to the problem is using a dynamic programming approach to find the maximum sum. The sum can be extracted by treating the circular array as two arrays, one from index 0 to N-2 and other from index 1 to n-1. This will create two arrays and the maximum sum out the sums from these array’s will be the result.
Example
Program to illustrate the working of our solution,
#include <iostream> using namespace std; int calcMaxVal(int a, int b){ if(a > b) return a; return b; } int calcMaxSumSubSeq(int cirArr[], int start, int end, int n) { int DP[n]; int maxSum = 0; for (int i = start; i < (end + 1); i++) { DP[i] = cirArr[i]; if (maxSum < cirArr[i]) maxSum = cirArr[i]; } for (int i = (start + 2); i < (end + 1); i++) { for (int j = 0; j < i - 1; j++) { if (DP[i] < DP[j] + cirArr[i]) { DP[i] = DP[j] + cirArr[i]; if (maxSum < DP[i]) maxSum = DP[i]; } } } return maxSum; } int findMaxSum(int cirArr[], int n){ int maxSumArray1 = calcMaxSumSubSeq(cirArr, 0, (n-2), n); int maxSumArray2 = calcMaxSumSubSeq(cirArr, 1, (n-1), n); int maxSum = calcMaxVal(maxSumArray1, maxSumArray2); return maxSum; } int main(){ int cirArr[] = {4, 1, 5, 3, 2}; int n = sizeof(cirArr)/sizeof(cirArr[0]); cout<<"The maximum sum in circular array such that no two elements are adjacent is "<<findMaxSum(cirArr, n); return 0; }
Output
The maximum sum in circular array such that no two elements are adjacent is 9
- Related Articles
- Maximum sum such that no two elements are adjacent in C++
- Maximum sum such that no two elements are adjacent - Set 2 in C++
- Maximum sum such that no two elements are adjacent Alternate Method in C++ program
- Maximum sum in a 2 x n grid such that no two elements are adjacent in C++
- Maximum sum of nodes in Binary tree such that no two are adjacent in C++
- Maximum sum of nodes in Binary tree such that no two are adjacent | Dynamic Programming In C++
- Maximum sum of nodes in Binary tree such that no two are adjacent using Dynamic Programming in C++ program
- Maximum subsequence sum such that no three are consecutive
- Rearrange characters in a string such that no two adjacent are same in C++
- Maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array in C++
- Maximum subsequence sum such that no three are consecutive 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 set bit sum in array without considering adjacent elements in C++
- Maximum possible sum of a window in an array such that elements of same window in other array are unique in c++
- Maximum sum of difference of adjacent elements in C++
