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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:54:25+00:00 2026-06-12T18:54:25+00:00

Let [a_1 a_2 … a_n] be a list of distinct integers in the range

  • 0

Let [a_1 a_2 ... a_n] be a list of distinct integers in the range [1,10n]. Give an algorithm that returns true if there are three distinct elements x,y,z such that -1 <= x+y-z <= 1, and false otherwise.

A brute force algorithm (checking all possible combinations of x+y-z, runs in time O(n^3). Are there more efficient algorithms?

  • 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-12T18:54:26+00:00Added an answer on June 12, 2026 at 6:54 pm

    Yes, there is. Here is an O(n^2) worst case algorithm that uses O(n) additional space.

    The idea is to check for all possible pairs (instead of triples), and iteratively marks which elements you have already seen, and compare against them the sum of each pair.

    For each pair, check its sum has matching element which is exactly the sum (x+y-z == 0) or an element you can get to if you add 1 (x+y+1-z == 0 -> x+y-z = -1) or you can get to if you reduce 1 (x+y-1-z == 0 -> x + y - z == 1)

    Pseudo code:

    mark = new boolean[10n]; //all initialized to false
    sort arr //O(nlogn)
    for each i in n,1: (reverse order)
       for each j in 1,i-1:
          //neglected range check, make sure it is done
          if (mark[arr[i]+arr[j]] || mark[arr[i]+arr[j]+1] || mark[arr[i]+arr[j]-1]):
              return true
          mark[arr[i]] = true
    return false
    

    Note that we iterate i from n to 1, because z > x and z > y – and we want to make sure we are checking all pairs with element that is already in the list if it is there

    Correctness Proof:

    If there is a solution x+y-z = 0 – then z > x and z > y (all elements are positive distinct integers).

    Without loss of generality, let’s assume x > y. So, when iterating arr[i]=x in outer loop, there is some j<i such that arr[j]=y. Also, since z>x – mark[z] == true – since we marked it when we previously iterated it.

    Thus: The algorithm will find mark[arr[x] + arr[y]] == true, and yield true.

    similar proof for the +-1 cases.

    If the algorithm yielded true, then it found one of the conditions true. Let’s assume it is mark[arr[i] + arr[j]] (The proof for the other cases will be similar).

    So, we found out mark[arr[i] + arr[j]] == true – so we inserted it since there is some element z such that z = arr[i] + arr[j], and the algorithm is correct for this case.

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

Sidebar

Related Questions

I have a 2-dimensional list of named tuples (let's say that each tuple has
Given a list of N non-negative integers, propose an algorithm to check if the
Let A[1]<=A[2]<=....<=A[n]. Let X be an arbitrary number. Give an algorithm find all pairs
Let v be a list of numbers v = [3,5,2,4,8,6,1] Why the following code
Let's say that I create a vector/array in Scheme: (let* ((x #(1 2 3)))
I have a function that takes a list and replaces some elements. I have
Let's say i have some items, that have a defined length and horizontal position
Let's say I have a list of tuples: a = [(1, 2), (3, 4),
The following code generates prime from 1 to n: (defun prime-list(n) (let ((a)(b)(x (floor
Let's say I have a Huuge database onto a server that works with a

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.