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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:44:58+00:00 2026-05-20T04:44:58+00:00

You are given an unsorted array A of n elements, now construct an array

  • 0

You are given an unsorted array A of n elements, now construct an array B for which
B[i] = A[j] where j is the least number such that A[j] > A[i] and j>i
if such a j does not exist B[i] = -1
Eg:

A={1,3,5,7,6,4,8}
B = {3 5 7 8 8 8 -1}

My solution

#include<iostream>
using namespace std;
int main()
{
    int a[7]={9,3,5,2,6,4,8},b[7];
    int i,j,largest = -1;
    for(i=0;i<7;i++)
    {
        j=i+1;
        while(j<7)
        {
         if(a[j]>a[i])
         {
             largest=a[j];
             break;
         }
         j++;
        }
        if(j == 7)
        largest = -1;
        b[i]= largest;
    }
    for(j=0;j<7;j++)
    cout<<b[j]<<endl;
    return 0;
}

can any one suggest o(nlogn) solution.

  • 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-20T04:44:59+00:00Added an answer on May 20, 2026 at 4:44 am

    Make an empty array called c[].

    Start at the end of a[] and work backwards.

    Do a binary search in c[] for the first value greater than a[i]. Put that into b[i], or a -1 if you can’t find one.

    Drop everything in c[] that is less than b[i].

    Append a[i] to the beginning of c[].

    c[] will always be sorted, allowing binary search.


    For example, with the sample A={1,3,5,7,6,4,8}

    Start at the end, A[i]=8, C={}
    First iteration is a bit weird.
    Binary search of C for the first value greater than 8 gives nothing, so B[i] = -1
    You don’t have to drop anything from C because it is empty, but you would have had to empty it anyway because of the -1.
    Append A[i]=8 to the beginning of C, so C={8}

    Now A[i]=4, C={8}
    Binary search of C for the first value greater than 4 gives 8, so B[i]=8
    Drop everything less than 8 from C, which still leaves C={8}
    Append A[i]=4 to the beginning of C, so C={4,8}

    Now A[i]=6, C={4,8}
    Binary search of C for the first value greater than 6 gives 8, so B[i]=8
    Drop everything less than 8 from C, which leaves C={8}
    Append A[i]=6 to the beginning of C, so C={6,8}

    Now A[i]=7, C={6,8}
    Binary search of C for the first value greater than 7 gives 8, so B[i]=8
    Drop everything less than 8 from C, which leaves C={8}
    Append A[i]=7 to the beginning of C, so C={7,8}

    Now A[i]=5, C={7,8}
    Binary search of C for the first value greater than 5 gives 7, so B[i]=7
    Drop everything less than 7 from C, which leaves C={7,8}
    Append A[i]=5 to the beginning of C, so C={5,7,8}

    Now A[i]=3, C={5,7,8}
    Binary search of C for the first value greater than 3 gives 5, so B[i]=5
    Drop everything less than 5 from C, which leaves C={5,7,8}
    Append A[i]=3 to the beginning of C, so C={3,5,7,8}

    Now A[i]=1, C={3,5,7,8}
    Binary search of C for the first value greater than 1 gives 3, so B[i]=3

    Done

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

Sidebar

Related Questions

Given an unsorted list in an array, will it necessarily take at least linear
Given an unsorted array of numbers, write a function that returns true if array
I'm currently trying to verify whether or not, given an unsorted array A of
Given an unsorted integer array, and without making any assumptions on the numbers in
You are given as input an unsorted array of n distinct numbers, where n
I've been thinking about this homework question for a bit now. Given an number
Given an unsorted sequence of integers that flows into your program as a stream.
Given a list of unsorted numbers, I want to find the smallest number larger
I had this question: Given an unsorted array of positive integers and an integer
Given that I have the array Let Sum be 16 dintptr = { 0

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.