Even-Odd Operations Given an array of non-negative integers, perform a series of operations until the array becomes empty. Each of the operations gives a score, and the goal is to maximize the overall score, the sum of the scores from all operations. Determine the maximum possible score after performing the operations on the array. All operations are 1-indexed, and are defined as follows: 1. For every odd-indexed operation, the score is the sum of all integers present in the array. 2. For every even-indexed operation, the score is the negative of the sum of all integers present in the array. 3. After every operation (odd or even), remove either the leftmost or the rightmost integer from the array. For example: Let integerArray = [3, 6, 8] Initial score = 0 The operations are as follows: 1. Operation 1 is odd, so add the sum of the array to the score. score = 3 + 6 + 8 = 17 Choose to delete the rightmost integer (i.e. 8), and now integerArray = [3, 6] 2. Operation 2 is even, so subtract the sum of the array from the score. sum = 3 + 6 = 9 and score = 17 - sum = 17 - 9 = 8 Choose to delete the leftmost integer (i.e. 3), and now integerArray = [6] 3. Operation 3 is odd, so add the sum of the array to the score sum-hand score – 8I sum - 816 - 11 2. Uperation 2 is even, so subtract the sum of the array from the score. sum = 3 + 6 = 9 and score = 17 - sum = 17 - 9 = 8 Choose to delete the leftmost integer (i.e. 3), and now integerArray = [6] 3. Operation 3 is odd, so add the sum of the array to the score. sum = 6 and score = 8 + sum = 8 + 6 = 14 Only one element is left. It is both the leftmost and rightmost element, so delete it (i.e. 6), and now integerArray = [ ] The array is now empty, so no further operations are possible. The maximum possible score is 14. Function Description Complete the function getMaximumScore in the editor below. The function must return the maximum possible score after performing all the operations. getMaximumScore has the following parameter: integerArray: an array of integers Constraints • 1 s size of integerArray < 103 • 0 s integerArray[i] = 109

Respuesta :

Answer:

# include<iostream>

# include<stdio.h>

# include<stdlib.h>

using namespace::std;

int main()

{

   int *a=NULL;

   int n; int score=0;

   cout<<"Enter the value of N";

   cin>>n;

   a=new int[n];

   cout<<"Enter the elements of a";

   for(int i=0;i<=n;i++)

   {

       cin>>a[i];

   }

   int num=n;int k=n;int j=num;

   while(j>=num)

   {

   if(n%2==0)

   {

     for(int i=0;i<=k;i++)

     {

         score+=a[i];cout<<score;      }

     if(a[0]>a[n])

       {

             a[n]=0;

             n--;

         }

     else if(a[0]<a[n])

     {

           a[0]=0;

           n--;

     }

     else if(n==1)

     {

         exit(0);

     }

     

   }

   else

   {

       for(int i=0;i<=n;i++)

       {

           score-=a[i];

       }

       if(a[0]>a[n])

       {

             a[n]=0;

             n--;

       }

     else if(a[0]<a[n])

     {

           a[0]==0;

           n--;

     }

     else

     {

         exit(0);

     }

   }

   j--;

   }

   cout<<"score"<<score;

   return 0;

}

Explanation:

The array above is created dynamically, and rest is as mentioned in question.

Answer:

Explanation:

def getMaximumScore(array, n):

   """

       array: list of positive numbers/integers

       n : size of array

       return: score (maximum)

   """

   score=0  # initially zero

   operation = "Odd"

   while len(array)!=0:

       s = sum(array)

       if operation=="Odd":

           score+=s

           operation="Even"

           del(array[-1])

       elif operation=="Even":

           score-=s

           operation="Odd"

           del(array[0])

   return score

print("Maximum score: "+str(getMaximumScore([3,6,8],3)))

Ver imagen dayanandghelaro
ACCESS MORE