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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:16:11+00:00 2026-06-01T07:16:11+00:00

I came across this question on a website. As mentioned there, it was asked

  • 0

I came across this question on a website. As mentioned there, it was asked in amazon interview. I couldn’t figure out a proper solution in given constraint.

Given an array of n integers, find 3 elements such that a[i] < a[j] < a[k] and i < j < k in O(n) time.

  • 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-01T07:16:12+00:00Added an answer on June 1, 2026 at 7:16 am

    So here is how you can solve the problem. You need to iterate over the array three times. On the first iteration mark all the values that have an element greater than them on the right and on the second iteration mark all the elements smaller than them on their left. Now your answer would be with an element that has both:

    int greater_on_right[SIZE];
    int smaller_on_left[SIZE];
    memset(greater_on_rigth, -1, sizeof(greater_on_right));
    memset(smaller_on_left, -1, sizeof(greater_on_right));
    
    int n; // number of elements;
    int a[n]; // actual elements;
    int greatest_value_so_far = a[n- 1];
    int greatest_index = n- 1;
    for (int i = n -2; i >= 0; --i) {
       if (greatest_value_so_far > a[i]) {
         greater_on_right[i] = greatest_index;
       } else {
         greatest_value_so_far = a[i];
         greatest_index = i;
       }
    }
    
    // Do the same on the left with smaller values
    
    
    for (int i =0;i<n;++i) {
      if (greater_on_right[i] != -1 && smaller_on_left[i] != -1) {
        cout << "Indices:" << smaller_on_left[i] << ", " << i << ", " << greater_on_right[i] << endl;
      }
    }
    

    This solution iterates 3 times over the whole array and is therefore linear. I have not provided the whole solution so that you can train yourself on the left to see if you get my idea. I am sorry not to give just some tips but I couldn’t figure out how to give a tip without showing the actual solution.

    Hope this solves your problem.

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

Sidebar

Related Questions

I came across this question on an interview website - We are given 4
I came across this question on an interview questions thread. Here is the question:
I am preparing for my interview and came across this question: Consider that i
i came across this question on this website called codility, but i cant really
I came across this question in one Interview. Please help me in getting the
I came across this question in an interview. Any number with 3 in its
I came across this question after searching for a ODBC or JDBC. To my
I came across this question: say given two weights 1 and 3, u can
I came across this question on transcender: What should you apply to a field
Today I came across this question: you have a code static int counter =

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.