Answer to Question #311500 in C++ for xyz

Question #311500

You are given an array of N non-negative integers: A1A2, ..., AN. An alternating subsequence is a subsequence in which the indices of any two consecutive elements differ by exactly two in the original array. That is, if Ai1Ai2, ..., Aik is some subsequence, then for it to be an alternating subsequence, (i2 - i1 = 2), (i3 - i2 = 2), and so on should all hold true. Among all alternating subsequences, find the one which has maximum sum of elements, and output that sum.


1
Expert's answer
2022-03-14T13:04:28-0400

Here is my program:

int main()
{
	int const size = 5;
	int N[size]{3,5,3,1,20};
	int sum = 0;
	for (int i = 4; i >= 0 ; i--)
	{
		if (i == 0)
		{
			break;
		}
		sum += N[i] - N[i - 1];
		
	}
	cout << "Sum = "<< sum << endl;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment