Google Interview Onsite (University Grad – 2020)
Question :Given an array having 2n elements you can choose n elements from either end of the array such that the values obtained result in maximum sum.
Input : 1 3 100 25 20 4 Output : 103
Approach: Initially, I tried recursive approach by showing both possibilities of an element that it can either be included or excluded, but he told to optimize it and I came up with prefix sum approach.
Idea: The main idea behind the prefix sum approach was if we select ‘x’ elements from left we can select ‘n-x’ elements from the right.