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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:55:41+00:00 2026-05-23T09:55:41+00:00

I have two Stacks s1 and s2. s1 containing negative integers and s2 containing

  • 0

I have two Stacks s1 and s2. s1 containing negative integers and s2 containing positive integers. Both Stacks have been sorted already from lowest value(at the bottom) to highest(at the top). x1 and x2 are integers in s1 and s2. I would like to check both Stacks to see whether [x1 + x2 = a given integer i]. What is the best way(or THE way) to do this in O(n)?

Update: x1 and x2 are integers..sorry

update 2: the method returns a boolean value and would have these parameters:

boolean method(Stack s1, Stack s2, int i)

method would return true if any integer x1 in s1 + any integer x2 in s2 = i

  • 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-23T09:55:42+00:00Added an answer on May 23, 2026 at 9:55 am

    I assume you mean any number in s1 + any number in s2 is a given integer i.

    If so,

    1. Pop off the top of both stacks
    2. Add them
    3. Do they equal i
      1. Yes –> you are done
    4. Is it greater than i?
      1. YES –> then, the negative number can have no counterpart, throw it away, pop off the next negative number from s1, goto 2
      2. NO –> then, the positive number can have no counterpart, throw it away, pop off the next positive number from s2, goto 2

    (at any time, if a stack is empty, you are done — there is no answer)

    EDIT: I think I am wrong on step 4 based on your sort order — but this basic idea should be close.

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

Sidebar

Related Questions

Hi everyone: Here i have created a queue from two stacks: You add to
I have two identical tables and need to copy rows from table to another.
I have two controls containing DataGridView s on different panes in a DockPanel .
I have two stacks(which follows LIFO). I would like to know if i can
I have two Objective-C classes and one is derived from the other as @interface
I have two questions, stemming from observed behavior of C# static methods (which I
I have two Haskell functions, both of which seem very similar to me. But
I have two multivariate time series x and y, both covering approximately the same
Let's say I have these two arrays: string[] arr1 = new string[2]{Hello, Stack} string[]
Have two folders with approx. 150 java property files. In a shell script, how

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.