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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:15:39+00:00 2026-06-09T05:15:39+00:00

This is an interview question: Find the largest possible difference in an array of

  • 0

This is an interview question:

Find the largest possible difference in an array of integers, such that the smaller integer occurs earlier in the array.

Constraint:
Numbers are not unique.
The range is Integer range of java. (or any other language)

Example:

input 1: {1, 100, 2, 105, -10, 30, 100}

The largest difference is between -10 and 100 -> 110 (here -10 is at the 5th index and 100 is at 7th index)

input 2: {1, 100, 2, 105, -10, 30, 80}

The largest difference is between 1 and 105 -> 104 (here 1 is at the 1st index and 105 is at 4th index)

Possible Solution:

One approach is check for all possible differences and keep a track of the biggest difference found till now O(n^2) complexity.

can this be done in better than O(n^2) 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-09T05:15:41+00:00Added an answer on June 9, 2026 at 5:15 am

    Start from the last element and move backwards. keep in memory the largest element occurred till now.
    for each element subtract from the max and store at the respective position.

    Also, you can keep an element to store the max difference and give the output straight away.
    O(n) time, O(1) space.

    int max = INT_MIN;
    int maxdiff = 0;
    
    for (i = sizeof(arr) / sizeof(int) - 1; i >= 0; i--) {
      if (max < arr[i]) {
        max = arr[i];
      }
      int diff = max - arr[i];
      if (maxdiff < diff) {
        maxdiff = diff;
      }
    }
    
    print maxdiff;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Find integer not occurring twice in an array Accenture interview question -
This is an interview question There is an array of integers. The elements in
Possible Duplicate: Counting inversions in an array This is an phone interview question :
I was asked this question in an interview: Assume an infinite array of integers
This is an interview question : How to process a integer matrix to find
This is an interview question: Given a string, find all its permutations that are
Question: Given a sorted array A find all possible difference of elements from A.
This was an interview question: Given an amount, say $167.37 find all the possible
This is an interview question. Given a number of strings find such strings, which
This is probably a Microsoft interview question. Find the kth smallest element (ignoring duplicates)

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.