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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:02:45+00:00 2026-05-27T07:02:45+00:00

Inputs: n ( int ) and n values ( float ) that represent exchange

  • 0

Inputs: n (int) and n values (float) that represent exchange rates
(different between them) with a random value between 4 and 5.

Output: compute the maximum number of values that can be used (in the
same order) to represent an ascending then descending curve?


e.x. The eight values

4.5 4.6 4.3 4.0 4.8 4.4 4.7 4.1

should output

5 (4.5 4.6 4.8 4.4 4.1)


My approach

  • If I try successive ifs, I get a random array that respects the curve condition, but not the longest.
  • I have not tried backtracking because I am not that familiar with it, but something tells me I have to compute all the solutions with it then pick the longest.
  • And lastly: brute force, but since it is an assignment for algorithm design; I may as well not hand it in. 🙂

Is there a simpler/more efficient/faster method?

Here’s my try based on Daniel Lemire’s algorithm. It seems it doesn’t take into account the positions 0, i and n. I’m sure the ifs are the problem, how can I fix them?

for(int i = 0; i<n-1; i++){
            int countp=0;   // count ascending
            int countn=0;   // count descending
            for(int j=0;j<=i;j++){
                    if(currency[j]<currency[j+1]){
                        countp++;
                        System.out.print(j+" ");
                    }
                }
            System.out.print("|| ");
            for(int j=i;j<n-1;j++){
                if(currency[j]>currency[j+1]){
                    countn++;
                    System.out.print(j+" ");
                }
            }
        System.out.println();
        if(countn+countp>maxcount) maxcount=countn+countp;                   
        }
  • 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-27T07:02:46+00:00Added an answer on May 27, 2026 at 7:02 am

    Firstly, you want to be able to compute the longest monotonic subsequence from one point to another. (Whether it is increasing or decreasing does not affect the problem much.) To do this, you may use dynamic programming. For example, to solve the problem given indexes 0 to i, you start by solving the problem from 0 to 0 (trivial!), then from 0 to 1, then from 0 to 2, and so on, each time recording (in an array) your best solution.

    For example, here is some code in python to compute the longest non-decreasing sequence going from index 0 to index i. We use an array (bbest) to store the solution from 0 to j for all j’s from 0 to i: that is, the length of the longest non-decreasing subsequence from 0 to j. (The strategy used is dynamic programming.)

    def countasc(array,i):
      mmin = array[0] # must start with mmin
      mmax= array[i] # must end with mmax
      bbest=[1] # going from 0 to 0 the best we can do is length 1
      for j in range(1,i+1): # j goes from 1 to i
        if(array[j]>mmax):
          bbest.append(0) # can't be used
          continue
        best = 0 # store best result
        for k in range(j-1,-1,-1): # count backward from j-1 to 0
          if(array[k]>array[j]) :
            continue # can't be used
          if(bbest[k]+1>best):
              best = bbest[k]+1
        bbest.append(best)
      return bbest[-1] # return last value of array bbest
    

    or equivalently in Java (provided by request):

    int countasc(float[] array,int i) {
        float mmin = array[0];
        float mmax = array[i];
        ArrayList<Integer> bbest= new ArrayList<Integer>();
        bbest.add(1);
        for (int j = 1; j<=i;++j) {
            if(array[j]>mmax){
                bbest.add(0);
                continue;
            }
            int best = 0;
            for(int k = j-1; k>=0;--k) {
                if(array[k]>array[j]) 
                    continue;
                if(bbest.get(k).intValue()+1>best)
                    best = bbest.get(k).intValue()+1;
            }
            bbest.add(best);
        }
        return bbest.get(bbest.size()-1);
    }
    

    You can write the same type of function to find the longest non-increasing sequence from i to n-1 (left as an exercise).

    Note that countasc runs in linear time.

    Now, we can solve the actual problem:

    Start with S, an empty array
    For i an index that goes from 0 to n-1 :
      compute the length of the longest increasing subsequence from 0 to i (see function countasc above)
      compute the length of the longest decreasing subsequence from n-1 to i
      add these two numbers, add the sum to S
    return the max of S
    

    It has quadratic complexity. I am sure you can improve this solution. There is a lot of redundancy in this approach. For example, for speed, you should probably not repeatedly call countasc with an uninitialized array bbest: it can be computed once. Possibly you can bring down the complexity to O(n log n) with some more work.

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

Sidebar

Related Questions

Are there any known hash algorithms which input a vector of int's and output
Im trying to create a method that take 2 int array as the input
I'd like to call methods of a class dynamically with parameter values that are
I have set of value in float (always less than 0). Which I want
Background Users type information into web form inputs such as: <input type=text name=report_name value=First
I'm working on a few report output scripts that need to do some rudimentary
This should tokenize space delimited fields in the string str: float values[2*linesnum(str, length)]; char
I have a simple homework... to make a C program that takes 3 values
Here is two variants. First: int n = 42; int* some_function(int* input) { int*
public static string RatingCalculator(int input) { if (input < 10) { return string.Empty; }

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.