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

  • Home
  • SEARCH
  • 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 8830105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:59:28+00:00 2026-06-14T07:59:28+00:00

I have a system that generates values in a text file which contains values

  • 0

I have a system that generates values in a text file which contains values as below

Line 1 : Total value possible

Line 2 : No of elements in the array

Line 3(extra lines if required) : The numbers themselves

I am now thinking of an approach where I can subtract the total value from the first integer in the array and then searching the array for the remainder and then doing the same until the pair is found.

The other approach is to add the two integers in the array on a permutation and combination basis and finding the pair.

As per my analysis the first solution is better since it cuts down on the number of iterations.Is my analysis correct here and is there any other better approach?

Edit :
I’ll give a sample here to make it more clear
Line 1 : 200
Line 2=10
Line 3 : 10 20 80 78 19 25 198 120 12 65

Now the valid pair here is 80,120 since it sums up to 200 (represented in line one as Total Value possible in the input file) and their positions in the array would be 3,8.So find to this pair I listed out my approach where I take the first element and I subtract it with the Total value possible and searching the other element through basic search algorithms.

Using the example here I first take 10 and subtract it with 200 which gives 190,then I search for 190,if it is found then the pair is found otherwise continue the same process.

  • 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-14T07:59:29+00:00Added an answer on June 14, 2026 at 7:59 am

    If you’re trying to find a pair (2) numbers which sum to a third number, in general you’ll have something like:

    for(i=0;i<N;i++)
       for(j=i+1;j<N;j++)
          if(numbers[i]+numbers[j]==result)
             The answer is <i,j>
             end
    

    which is O(n^2). However, it is possible to do better.

    If the list of numbers is sorted (which takes O(n log n) time) then you can try:

    for(i=0;i<N;i++)
       binary_search 'numbers[i+1:N]' for result-numbers[i]
       if search succeeds:
          The answer is <i, search_result_index>
          end
    

    That is you can step through each number and then do a binary search on the remaining list for its companion number. This takes O(n log n) time. You may need to implement the search function above yourself as built-in functions may just walk down the list in O(n) time leading to an O(n^2) result.

    For both methods, you’ll want to check to for the special case that the current number is equal to your result.

    Both algorithms use no more space than is taken by the array itself.

    Apologies for the coding style, I’m not terribly familiar with Java and it’s the ideas here which are important.

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

Sidebar

Related Questions

Greetings! Situation: We have a CRM system that generates unique customer IDs. Now we
So I have a log in system that generates a random token for each
I have a C# method, see code below, that generates an Excel spreadsheet using
I have a system that will generate messages sporadically, and I would like to
I'm developing a system, and I have build a code generator that emits a
We have system here that uses Java JNI to call a function in a
I have a system that takes dynamic data, puts it in HTML for layout
I have big system that make my system crash hard. When I boot up,
I have a system that runs like this: main.exe runs sub.exe runs sub2.exe and
We have a system that has some Bash scripts running besides Java code. Since

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.