# Pick from both sides?

Stack Overflow Asked by helloworld on December 13, 2020

The problem statement is :
Given an integer array A of size N.

You can pick B elements from either left or right end of the array A to get maximum sum.
Find and return this maximum possible sum.

NOTE: Suppose B = 4 and array A contains 10 elements then:

You can pick first four elements or can pick last four elements or can pick 1 from front and 3 from back etc . you need to return the maximum possible sum of elements you can pick.

public class Solution {
ArrayList<Integer> c = new ArrayList<>();
ArrayList<Integer> A= new ArrayList<>();
public int solve(ArrayList<Integer> A, int B) {

if (B>A.size()){
int sum=0;
for(int i=0;i<A.size();i++)
sum= sum+A.get(i);
return sum;
}

int max_sum=0;
for(int i=0;i<A.size();i++){
if((max_sum<suffix(A.size()-(B-i))+prefix(i-1)) ){
max_sum=suffix(A.size()-(B-i))+prefix(i-1);
}
}
return max_sum;
}

int prefix_sum=0;
int prefix(int a)   {

for(int p=0;p<a+1;p++){
c=A;
prefix_sum=prefix_sum + c.get(p);
}
return prefix_sum;
}

int suffix_sum=0;
int suffix(int b){
c=A;
for(int q=b;q<c.size();q++){
suffix_sum=suffix_sum+c.get(q);
}
return suffix_sum;
}


}

I am getting runtime error, I have tried to implement the suffix and prefix methods which return the sum from the index[ 0, i] and sum from [i, N-i] respectively, then in the solve function I am trying to find the sum of prefix [a-1] +suffix[N-(b-a)] and find out the maximum sum, the syntax is completely correct, there is something wrong with the logic I assume, please help me find the correct solution by correcting this code instead of providing an alternative method

    package com.array;

import java.util.Arrays;
import java.util.List;

public class PickFromBothSides {

public static void main(String[] args) {
Integer[] arr = { 5, -2, 3, 1, 2 };
System.out.println(solve(Arrays.asList(arr), 3));

}

public static int solve(List<Integer> A, int B) {

int n = A.size();

int result = 0;

for (int i = 0; i < B; i++) {
result += A.get(i);
}

int sum = result;

for (int i = 0; i < B; i++) {
sum -= A.get(B - 1 - i);
sum += A.get(n - 1 - i);

result = Math.max(result, sum);
}

return result;

}
}


Runtime O(n) Space complexity O(1)

Answered by vaquar khan on December 13, 2020

You are declaring int prefix_sum=0; and int suffix_sum=0; as fields, not as local variables of the respective methods.

You are calling suffix(A.size()-(B-i)) so with your example that is 10 - (4 -i) which is 6 + i. You iterate through i being in the range {0, ..., 10} so the value 6 + i will be all the numbers 6 through 16. You cannot index in the array above 9, so you get an exception.

You need to change

for(int i=0;i<A.size();i++){


to

for(int i=0; i <= B; i++){


because you are trying to ask each iteration "how many numbers are taken from the beginning"? 0, 1, 2, 3 or 4 if B is 4

1. You are calling suffix(A.size()-(B-i))+prefix(i-1)) twice in a row. Call it only once, store it in a variable and reuse.

2. You are calling prefix(i-1) but inside prefix() you are using the parameter a as a + 1. You don't need to subtract one and add one to the same thing

Answered by Hawk on December 13, 2020

## Related Questions

### Bash: insert a line after each line

6  Asked on December 23, 2021 by mr-krisey

### How the caller thread wait till task under ScheduledExecutorService finish the job periodicaly

3  Asked on December 23, 2021 by ajay-kumar-jaiswal

### Karatsuba Algorithm without BigInteger in Java, unexpected behaviour while recursion

1  Asked on December 23, 2021 by brownboi

### Why doesn’t UI Automation condition find element by UIA_IsScrollPatternAvailablePropertyId?

1  Asked on December 23, 2021 by user3161924

### I am trying to code a discord bot that has a number game within it. For some reason it doesn’t register author and contents as a command

0  Asked on December 23, 2021 by bingbong123

### Python Programming involving Side Effects

3  Asked on December 23, 2021

### Recoding groups to create a summary accordingly

1  Asked on December 23, 2021 by rjunkie2

### Is it a good idea to nest custom serializer classes in django?

1  Asked on December 23, 2021 by john-cymmer

### create view by using two different array

3  Asked on December 23, 2021 by akshay-namdeo

### Koltin: Pass Context from inner class

3  Asked on December 23, 2021 by sarah-smith

### how to write mocha test that have timeout and async both

1  Asked on December 23, 2021 by jogiter

### SQL CONCAT drops zeros from expression

2  Asked on December 23, 2021

### Sharing array of objects with Python multiprocessing

1  Asked on December 23, 2021 by 119631

### Python – arguments to dictionary

0  Asked on December 23, 2021 by user13986859

### Updating the index in an SQL database (python)

1  Asked on December 23, 2021 by luke-teo

### Using mask to filter dataframe by multiple day of week

1  Asked on December 23, 2021 by meronpan

### JQuery Menu Editor is not Compatible with AngularJS 1.5

1  Asked on December 23, 2021 by atique-ahmed

### How do I set firebase database rules to not allow delete or update of children?

2  Asked on December 22, 2021 by hotpopper80

### Need Help Implementing An If/Else Or Case Statement Into Python Script

1  Asked on December 22, 2021 by sam-lee