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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:38:26+00:00 2026-05-22T16:38:26+00:00

I received a question during an Amazon interview and would like assistance with solving

  • 0

I received a question during an Amazon interview and would like assistance with solving it.

Given N arrays of size K each, each of these K elements in the N arrays are sorted, and each of these N*K elements are unique. Choose a single element from each of the N arrays, from the chosen subset of N elements. Subtract the minimum and maximum element. This difference should be the least possible minimum.

Sample:

N=3, K=3

N=1 : 6, 16, 67
N=2 : 11,17,68
N=3 : 10, 15, 100

here if 16, 17, 15 are chosen, we get the minimum difference as
17-15=2.

  • 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-22T16:38:27+00:00Added an answer on May 22, 2026 at 4:38 pm

    I can think of O(N*K*N)(edited after correctly pointed out by zivo, not a good solution now 🙁 ) solution.
    1. Take N pointer initially pointing to initial element each of N arrays.

    6, 16, 67
    ^ 
    11,17,68
    ^
    10, 15, 100
    ^ 
    

    2. Find out the highest and lowest element among the current pointer O(k) (6 and 11) and find the difference between them.(5)
    3. Increment the pointer which is pointing to lowest element by 1 in that array.

     6, 16, 67
        ^ 
     11,17,68
     ^
     10, 15, 100 (difference:5)
     ^ 
    

    4. Keep repeating step 2 and 3 and store the minimum difference.

     6, 16, 67
        ^ 
     11,17,68
     ^
     10,15,100 (difference:5)
        ^ 
    
    
     6, 16, 67
        ^ 
     11,17,68
        ^
     10,15,100 (difference:2)
        ^ 
    

    Above will be the required solution.

     6, 16, 67
        ^ 
     11,17,68
        ^
     10,15,100 (difference:84)
           ^ 
    
     6, 16, 67
            ^ 
     11,17,68
        ^
     10,15,100 (difference:83)
           ^ 
    

    And so on……

    EDIT:

    Its complexity can be reduced by using a heap (as suggested by Uri). I thought of it but faced a problem: Each time an element is extracted from heap, its array number has to be found out in order to increment the corresponding pointer for that array. An efficient way to find array number can definitely reduce the complexity to O(K*N log(K*N)). One naive way is to use a data structure like this

    Struct
    {
        int element;
        int arraynumer;
    }
    

    and reconstruct the initial data like

     6|0,16|0,67|0
    
     11|1,17|1,68|1
    
     10|2,15|2,100|2
    

    Initially keep the current max for first column and insert the pointed elements in heap. Now each time an element is extracted, its array number can be found out, pointer in that array is incremented , the newly pointed element can be compared to current max and max pointer can be adjusted accordingly.

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

Sidebar

Related Questions

This question I've received during interview. My answer is O(1), is it correct?
During the qualification round, the following question was asked: You've been given a list
I'm quoting part of an answer which I received for another question of mine
This is a question I asked on another forum which received some decent answers,
I recently asked a question about functional programming, and received (good!) answers that prompted
So this morning I posted a confused question about assembly and I received some
This is a follow-up to a previous question I had about interfaces. I received
I'm back with another Flex/Flash security question. I've already received some help from the
After reflecting upon all the great answers I received to my previous question about
I recently received an email from Authorize.net saying: During the week of March 16

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.