Computer Science, asked by uditjan5665, 1 year ago

You are given an array a of size n. You need to find the maximum-sum of the array provided that you cannot sum neighboring elements. So for each element of the array, you need to find maximum-sum from initial index till the element's index.

Answers

Answered by Ranauk456
4

// C++ program to find equilibrium  

// index of an array  

using namespace std;  

 

int equilibrium(int arr[], int n)  

{  

   int i, j;  

   int leftsum, rightsum;  

 

   /* Check for indexes one by one until  

   an equilibrium index is found */

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

   {      

 

       /* get left sum */

       leftsum = 0;  

       for (j = 0; j < i; j++)  

           leftsum += arr[j];  

 

       /* get right sum */

       rightsum = 0;  

       for (j = i + 1; j < n; j++)  

           rightsum += arr[j];  

 

       /* if leftsum and rightsum    

       are same, then we are done */

       if (leftsum == rightsum)  

           return i;  

   }  

 

   /* return -1 if no equilibrium  

   index is found */

   return -1;  

}  

 

// Driver code  

int main()  

{  

   int arr[] = { -7, 1, 5, 2, -4, 3, 0 };  

   int arr_size = sizeof(arr) / sizeof(arr[0]);  

   cout << equilibrium(arr, arr_size);  

   return 0;  

}  

 


Ranauk456: i am a student of computer science
rishabhsingh0156: Use this I recommend you
rishabhsingh0156: best place for Programmer
Ranauk456: okay thanks
rishabhsingh0156: and devs too
Ranauk456: hmm.
Ranauk456: thanks
Ranauk456: goodnight
rishabhsingh0156: good night
rishabhsingh0156: (~_^)
Answered by rishabhsingh0156
0

#python3

a = [1,2,3,4,5,6,8,5]

a.sort()

a.pop(-2)

new = []

for i in range(0,len(a)):

new.append(a[i])

print(sum(new))

Similar questions
Math, 6 months ago