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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:57:31+00:00 2026-06-06T11:57:31+00:00

Given is a array of numbers: 1, 2, 8, 6, 9, 0, 4 We

  • 0

Given is a array of numbers:

1, 2, 8, 6, 9, 0, 4

We need to find all the numbers in group of three which sums to a value N ( say 11 in this example). Here, the possible numbers in group of three are:

{1,2,8}, {1,4,6}, {0,2,9}

The first solution I could think was of O(n^3). Later I could improve a little(n^2 log n) with the approach:

1. Sort the array.
2. Select any two number and perform binary search for the third element.

Can it be improved further with some other approaches?

  • 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-06T11:57:33+00:00Added an answer on June 6, 2026 at 11:57 am

    You can certainly do it in O(n^2): for each i in the array, test whether two other values sum to N-i.

    You can test in O(n) whether two values in a sorted array sum to k by sweeping from both ends at once. If the sum of the two elements you’re on is too big, decrement the “right-to-left” index to make it smaller. If the sum is too small, increment the “left-to-right” index to make it bigger. If there’s a pair that works, you’ll find them, and you perform at most 2*n iterations before you run out of road at one end or the other. You might need code to ignore the value you’re using as i, depends what the rules are.

    You could instead use some kind of dynamic programming, working down from N, and you probably end up with time something like O(n*N) or so. Realistically I don’t think that’s any better: it looks like all your numbers are non-negative, so if n is much bigger than N then before you start you can quickly throw out any large values from the array, and also any duplicates beyond 3 copies of each value (or 2 copies, as long as you check whether 3*i == N before discarding the 3rd copy of i). After that step, n is O(N).

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

Sidebar

Related Questions

Given an integer array, i need to find which number occurred most number of
Given an array of real-valued numbers, A[1..n,1..n] , I wish to find the sub-array
Given array a= [1,4,5,9,2].I need to find/print combinations of two values where sum =
Given an array of size N I need to find the min number 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
This problem is taken from interviewstreet.com Given array of integers Y=y1,...,yn, we have n
Given an array with n values, for example: $arr[] = 'ABCDEFABC'; $arr[] = 'ABCDEFDEF';
Given an array like this: Array => ( [0] => 1, [1] => 2,
Given an array. How can we find sum of elements in index interval (i,

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.