Ball Arrangement for Maximum Engagement

Ball Arrangement for Maximum Engagement

FULLTIME

In a content creator challenge, a user with a bag of balls, each labeled with different numerical values, aims to arrange the balls in a circle to create a video with the highest level of engagement.

Formally, you are provided an array values representing the attributes of each ball. The objective is to arrange these elements in a circle. You have the creative freedom to determine the order in which the elements are positioned in the circle, and you can insert each element at any location within the circle.

The level of engagement for a particular arrangement is calculated as follows:

  • The first element placed in the circle contributes 0 to the engagement.
  • For each subsequent element placed, its contribution is equal to the smaller of the numerical values of the elements to its left and right.
  • The challenge involves finding the arrangement that results in the highest total engagement and producing a TikTok video that captivates the audience.


    Function Description

    Complete the function findMaximumEngagement in the editor below.

    findMaximumEngagement has the following parameters:

  • int values[n] : an array of integers

  • Returns

    long : maximum total engagement that can be achieved overall arrangements


    Example 1 :

    Input: values = [1, 3, 5, 3, 2]
    Output: 14
    Explanation: A possible arrangement which gives maximum total engagement = 14 is shown as:

    - The latest element arranged is highlighted in red.
    - The smaller values on the left and right of the highlighted element are added to the total engagement in each step.


    Constraints:

  • 2 ≤ n ≤ 2*105



  • Get one-to-one training from Google Facebook engineers

    Top-notch Professionals

    Learn from Facebook and Google senior engineers interviewed 100+ candidates.
    Most recent interview questions and system design topics gathered from aonecode alumnus.
    One-to-one online classes. Get feedbacks from real interviewers.

    Customized Private Class

    Already a coding expert? - Advance straight to hard interview topics of your interest.
    New to the ground? - Develop basic coding skills with your own designated mentor.
    Days before interview? - Focus on most important problems in target company question bank.