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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:41:41+00:00 2026-06-08T08:41:41+00:00

Consider there are three array list each of equal length and having positive, negative

  • 0

Consider there are three array list each of equal length and having positive, negative and zero in them. I had to write a program to find combinations of which sum comes to zero. So basically, if the arrays were:-

A = {0, -1, 2}
B = {0, -1, -2}
C = {0, -2, 0}

O/P: A[0] + B[0] + C[0], A[2] + B[2] + C[2] etc.

I could think of two ways,
1. Have 3 for loops and calculate the sum using a[i] + b[j] + c[k], if zero print the index.
Big O will be O(N^3)
2. Have two for loop but use Binary Search to find the third element which would give the sum as zero.
Big O will be O(N^2LogN)

Any other ways?

Thanks.

EDIT:
Based on the answers given below, my first soln is the fastest possible. But if the question is about “finding” the number of combinations and NOT printing them, then please see Grigor Gevorgyan answer below.

  • 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-08T08:41:42+00:00Added an answer on June 8, 2026 at 8:41 am

    Can be done in O(n^2) with 2 pointers method.

    Sort the arrays. Now do following:
    Set ans = 0.
    Have an external loop running through array a with index i. Now set j = 0, k = n – 1.
    Look at sum = a[ i ] + b[ j ] + c[ k ].
    If sum < 0, increase j.
    If sum > 0 decrease k.
    If sum == 0, find the range of elements equal to b[ j ] and c[ k ] and add ranges lengths product to the answer. Then set j and k to first elements out of that ranges.
    This works because arrays are sorted and addition is a linear function.
    Internal part runs in O(n), with overall O(n^2) complexity.

    Example code in C++:

    sort( a, a + n );
    sort( b, b + n );
    sort( c, c + n );
    ans = 0;
    for( i = 0; i < n; ++i )
    {
       j = 0, k = n - 1;
       while( j < n && k > 0 )
       {
          sum = a[ i ] + b[ j ] + c[ k ];
          if( sum < 0 ) ++j;
              else if( sum > 0 ) --k;
              else 
              { 
                 // find the equal range
                 for( jj = j; jj < n && b[ jj ] == b[ j ]; ++jj );
                 for( kk = k; kk >= 0 && c[ kk ] == c[ k ]; --kk );
                 // add pairs quantity from these ranges
                 ans += ( jj - j ) * ( k - kk );
                 j = jj, k = kk;
              }
    }
    

    Note: Sorting of array a is not necessary, just did it to look good 🙂

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

Sidebar

Related Questions

Consider there is a QTablWidget and a QTextEdit. Both of them are in a
Consider this problem: There's a square grid defined, each tile being either passable (1)
Consider these three mysql statements: select * from Users; select id, title, value from
Consider three tables: one of items, one of tags on those items, and the
Consider there are two wi-fk networks A & B using the same channel and
const (and in ) Consider there is C function: unsigned int foo(const unsigned int
Consider that there is a bunch of tables which link to countries or currencies
Consider the three following classes: EntityTransformer contains a map associating an Entity with a
Please consider the following three .NET types: I have an interface, an abstract class,
Are there some standards that you consider to be so obvious that they would

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.