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
// 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;
}
#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))