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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:33:49+00:00 2026-05-27T07:33:49+00:00

Suppose you are given an array of unsorted integers as A = {3,4,5,1,4,2} Input

  • 0

Suppose you are given an array of unsorted integers as

A = {3,4,5,1,4,2}

Input : 6
Output : {5,1}, {4,2}

How can I do this in O(n) or O(log n). Any suggestions will be appreciated.

Update:
Can we write something more efficient than this?

for(int i=0;i<array.length-1;i++)  
{  
    if(array[i]+array[i+1]==6)  
        System.out.println("{"+array[i]+","+array[i+1]+"}");  
}  
  • 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-27T07:33:50+00:00Added an answer on May 27, 2026 at 7:33 am

    If the numbers stored in the input array are only positive then I’d create another array K of k+1 ArrayList elements. Where k is the number you need them to add up to.
    Only two numbers less than k can add up to k (assuming we deal with positive ints} or in special case {0,k}.
    Then I would iterate through all elements of input array and for each int m that is less or equal to k I’d take its index and add that index to the array of ArrayList K at index m.
    Then I would iterate through first half of the array K and for each index i that has some ints stored in it I would find complementary index [k-i] and see if there are any values in it. If there are then those are your pairs.
    And btw this is O(n).

    public static void findElemtsThatSumTo( int data[], int k){
        List arrayK[]= new List[k+1];
        for(int i=0; i<arrayK.length; i++)
            arrayK[i]= new ArrayList<Integer>();
    
        for(int i=0; i<data.length; i++){
            if(data[i]<=k)
                arrayK[data[i]].add(i);
        }
    
        for(int i=0; i<arrayK.length/2; i++){
            if(!arrayK[i].isEmpty() && !arrayK[k-i].isEmpty())
            {
                for(Object index: arrayK[i])
                    for(Object otherIndex: arrayK[k-i])
                        System.out.println("Numbers at indeces ["+index.toString()+", "+otherIndex.toString()+"] add up to "+k+".");
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

You are given an array of integers. You have to output the largest range
Suppose we are given an array of n integers representing stock prices on a
Suppose that my Haskell function is given an input, which is supposed to be
Suppose there is a System A which gives some output. This output is used
pt=[2] pt[0]=raw_input() when i do this , and give an input suppose 1011 ,
Suppose you pass an array as argument to a given method, say, public static
Suppose I make a 2d array like this: >>> A=np.arange(16).reshape((4,4)) >>> A array([[ 0,
Suppose that you're given an array A of n distinct elements drawn from some
You are given an Array of numbers and they are unsorted/random order. You are
How to parse a JSON Array,Suppose we have an array given below,how to parse

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.