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

  • SEARCH
  • Home
  • 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 7097953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T10:59:51+00:00 2026-05-28T10:59:51+00:00

I found this question on an online forum: Really interested on how it can

  • 0

I found this question on an online forum: Really interested on how it can be solved:

Given an array A of positive integers. Convert it to a sorted array with minimum cost. The only valid operation are:
1) Decrement with cost = 1
2) Delete an element completely from the array with cost = value of element

This is an interview question asked for a tech company

  • 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-05-28T10:59:53+00:00Added an answer on May 28, 2026 at 10:59 am

    NOTE : The original answer has been replaced with one in which I have a lot more confidence (and I can explain it, too). Both answers produced the same results on my set of test cases.

    You can solve this problem using a dynamic programming approach. The key observation is that it never makes sense to decrement a number to a value not found in the original array. (Informal proof: suppose that you decremented a number O1 to a value X that is not in the original sequence in order to avoid removing a number O2 > X from the result sequence. Then you can decrement O1 to O2 instead, and reduce the cost by O2-X).

    Now the solution becomes easy to understand: it is a DP in two dimensions. If we sort the elements of the distinct elements of the original sequence d into a sorted array s, the length of d becomes the first dimension of the DP; the length of s becomes the second dimension.

    We declare dp[d.Length,s.Length]. The value of dp[i,j] is the cost of solving subproblem d[0 to i] while keeping the last element of the solution under s[j]. Note: this cost includes the cost of eliminating d[i] if it is less than s[j].

    The first row dp[0,j] is computed as the cost of trimming d[0] to s[j], or zero if d[0] < s[j]. The value of dp[i,j] next row is calculated as the minimum of dp[i-1, 0 to j] + trim, where trim is the cost of trimming d[i] to s[j], or d[i] if it needs to be eliminated because s[j] is bigger than d[i].

    The answer is calculated as the minimum of the last row dp[d.Length-1, 0 to s.Length].

    Here is an implementation in C#:

    static int Cost(int[] d) {
        var s = d.Distinct().OrderBy(v => v).ToArray();
        var dp = new int[d.Length,s.Length];
        for (var j = 0 ; j != s.Length ; j++) {
            dp[0, j] = Math.Max(d[0] - s[j], 0);
        }
        for (var i = 1; i != d.Length; i++) {
            for (var j = 0 ; j != s.Length ; j++) {
                dp[i, j] = int.MaxValue;
                var trim = d[i] - s[j];
                if (trim < 0) {
                    trim = d[i];
                }
                dp[i, j] = int.MaxValue;
                for (var k = j ; k >= 0 ; k--) {
                    dp[i, j] = Math.Min(dp[i, j], dp[i - 1, k] + trim);
                }
            }
        }
        var best = int.MaxValue;
        for (var j = 0 ; j != s.Length ; j++) {
            best = Math.Min(best, dp[d.Length - 1, j]);
        }
        return best;
    }
    

    This direct implementation has space complexity of O(N^2). You can reduce it to O(N) by observing that only two last rows are used at the same time.

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

Sidebar

Related Questions

I found this open question online. How do you process a large data file
I tried to search online to solve this question but I didn't found anything.
I can't find a specific answer to this question online. If I take a
Reading this question I found this as (note the quotation marks) code to solve
I found this question in an old question in your website so i thought
I found this question that is discussing what I would like to do, but
I found this question that was very useful in learning the basics of the
I found this question which is a great starting point towards creating embedded widgets
I found this question but it was only similar and, more importantly, dated by
I searched and found this question but did not like the answer. Is there

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.