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 6026007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:22:57+00:00 2026-05-23T04:22:57+00:00

I’m doing an exercise where given an array of N values, I need to

  • 0

I’m doing an exercise where given an array of N values, I need to get the two numbers that their subtraction (the highest – the lowest) is the most positive number. I want v to be larger than c… the case is that… let’s say I want to buy auctions at price C so I can sell them at price V and get the maximum profit, and each cell of the array is the price of that auction at day t, so I want to buy at the lowest price possible so I can sell at the highest price possible so C must appear before V in the array. For example:

n = 8
arr = {6,7,3,8,9,2,1,4,20}

I want c = 1 and v = 20, because 20 - 1 = 19 (it means the subtraction from this 2 numbers is the highest)

Another example:

n = 6
arr = {8,12,45,40,18,29}

I want c = 8 and v = 45 because their subtraction is the highest number of all the other subtractions. (I want to clarify that c is not always the smallest number in the array). BOTH NUMBERS DO NOT NEED TO BE NEXT TO EACH OTHER. If I have n = 1, {1} then c = 1 and v = 1.

This example demonstrates c and v are not always the lowest/highest values.

n = 6
arr = {19,27,5,6,7,8}

In this case c = 19 and v = 27

Also, I need to solve this using merge sort’s code many (examples divide it by two methods: mergesort which handles the recursions, and merge that does the change of positions using an aux array).

I’m using mergesort code(merge is unnecessary in my opinion because I don’t care about sorting), so far I have the following code, but it is obviously wrong, could someone tell me what I’m not doing right?

public static void mergeSort(int start, int end) {
    if(start < end) {
        int half = (start + end) / 2;
        mergeSort(start, half);
        for(int i = start; start < half; start++, i++){
            if((arr[i+1] - arr[i]) > temp){
                temp = arr[i+1] - arr[i];
                c = i;
                v = i+1;
            }
        }
        mergeSort(half+1, end);
        for(int i = half+1; i < end; half++, i++){
            if((arr[i+1] - arr[i]) > temp){
                temp = arr[i+1] - arr[i];
                c = i;
                v = i+1;
            }
        }
    }
}

Thanks in advance for any help you can offer!

  • 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-23T04:22:58+00:00Added an answer on May 23, 2026 at 4:22 am

    I guess the name mergeSort in your code is inherited….

    As you have already do the recursion, there is no need to iterate through all the elements, because after recursion, the result is already presented. For example, one possible way is to swap the minimum to the first place and the maximum to the last place, and later, on the “upper” level of recursion you can just retrieve them directly.


    Here is another solution which takes advantage of the philosophy of merge-sort, but only returns max.

    public class test {
        static int arr [] = {6,7,3,8,9,2,1,4,20};
    
        public static void main (String args[]) {
            System.out.println(merge_select_max(0, arr.length - 1));
        }
    
        public static int merge_select_max (int start, int end) { // both inclusive
            if (start == end) {
                return arr[start];          
            }
            else {
                int half = (start + end) / 2;
                int first = merge_select_max (start, half);
                int second = merge_select_max (half + 1, end);
                return (first > second ? first : second);           
            }       
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I am trying to understand how to use SyndicationItem to display feed which is
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and

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.