Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8906625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:42:17+00:00 2026-06-15T02:42:17+00:00

I’m writing a program: For example input is 5 ( it can be not

  • 0

I’m writing a program:

For example input is 5 ( it can be not only 5 ) numbers and I read data in array: 1, 2, 3, 4, 5. I can choose some element from this array ( not first or last ), e.g. 3, then I delete this number in array, and to sum (which initially is 0 ) adding multiplied first-to-the-left with first-to-the-right elements ( means 2*4 in this case ). Resulted array is 1, 2, 4, 5, then I do it again and again until number of elements equals 2 ( exactly 1 and 5 as we can’t delete those numbers ).

For example: (where A, B, C, D are pairs of numbers 1 and 2, 2 and 3 and etc.. )

 A B C D
1 2 3 4 5

There are 6 possible combinations of the order deleting elements ( and adding left-right multiplication to sum ):

A (B (C D))
A ((B C) D)
(A B) (C D)
(A (B C)) D
((A B) C) D
A (B (C D))

The goal is to find smallest sum! There are 2 ways of solving, some clever algorithm or using recursion for every combination and then choosing smallest one. Can anyone give me a tip how to write such recursion, where to start writing ( or perhaps some clever algorithm ). Tnx

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:42:19+00:00Added an answer on June 15, 2026 at 2:42 am

    The recursive backtracking solution is fairly straightforward (pseudocode):

    def solve (list): 
        if list.length == 2:
            return 0
        ans = INFINITY
        for each interior element:
            remove element from list
            ans = min(ans, leftelement * rightelement + solve(list))
            place element back in original position in list
        return ans
    

    However, this algorithm is not fast enough to work on non-trivial datasets, as its runtime is factorial (O(n!)). The usual method to optimize recursive solutions is dynamic programming. Let’s come up with the substates:

    dp[a][b]: minimum cost to reduce array[a .. b] to two elements on the edge
              (array[a] and array[b])
    

    The base cases are dp[i][i + 1], i = {0 .. size - 1) (two adjacent elements). Since there are no interior elements to delete, this substate is set to 0.

    For all other cases dp[a][b] where b - a >= 2, we can divide array[a .. b] by removing any interior element indexed between [a + 1, b - 1]. If we divide the subarray on element i, the cost is dp[a][i] + dp[i][b] + array[a] * array[b]. We want to minimize the cost for each substate, so we will take the minimum of these values for all possible dividing elements. The final answer is simply dp[0][size - 1].

    Since there are O(n^2) substates, each with an average of O(n) dividing elements to consider, the total runtime is cubic (O(n ^ 3)), which should run for small to medium datasets in a reasonable time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array which has BIG numbers and small numbers in it. I
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.