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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:33:07+00:00 2026-05-24T13:33:07+00:00

zero-indexed array A consisting of N integers is given. A triplet (P, Q, R)

  • 0

zero-indexed array A consisting of N integers is given. A triplet (P, Q, R) is triangular if and

A[P] + A[Q] > A[R], 
A[Q] + A[R] > A[P], 
A[R] + A[P] > A[Q]. 

For example, consider array A such that

A[0] = 10    A[1] = 2    A[2] =  5
A[3] =  1    A[4] = 8    A[5] = 20

Triplet (0, 2, 4) is triangular.
Write a function

int triangle(const vector<int> &A);

that, given a zero-indexed array A consisting of N integers, returns 1 if there exists a triangular triplet for this array and returns 0 otherwise.

Assume that:

N is an integer within the range [0..100,000];
each element of array A is an integer within the range [-2,147,483,648..2,147,483,647].
For example, given array A such that

A[0] = 10 A[1] = 2 A[2] = 5
A[3] = 1 A[4] = 8 A[5] = 20
the function should return 1, as explained above. Given array A such that
A[0] = 10 A[1] = 50 A[2] = 5
A[3] = 1
the function should return 0.
Expected worst-case time complexity:
Expected worst-case space complexity: O(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-24T13:33:07+00:00Added an answer on May 24, 2026 at 1:33 pm

    If O(N³) is acceptable time complexity then the Pseudocode below should work. If you have stricter time complexity requirements then you’ll have to specify them.

    for (P in A){
        for (Q in A){
            for (R in A){
                if(A[P] > 0 && A[Q] > 0 && A[R] > 0){
                    if(A[P] > A[R] - A[Q] && A[Q] > A[P] - A[R] && A[R] > A[Q] - A[P]){
                        return 1;
                    }
                }
            }
        }
    }
    return 0;
    

    The reasoning behind the if statements is this:

    Since the ints can be anything up to max int you have to deal with overflow. Adding them together could cause a weird error if there are two very large ints in the array. So instead we test if they are positive and then rewrite the formulae to do the same checks, but with subtraction. We don’t need to do anything if any of the values are negative or 0, since:

    Assume x <= 0
    Assume x+y > z
    Assume x+z > y
    Then y > z and z > y which is a contradiction
    

    So no negative or zero valued ints will be a part of a triple

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

Sidebar

Related Questions

Find the first covering prefix of a given array. A non-empty zero-indexed array A
Given a simple zero based, numerically indexed array: var list = ['Foo', 'Bar', 'Baz'];
Arrays can be indexed using user-defined enumerated types. For example: type TIndexValue = (ZERO
Say that I have an integer-indexed array of length 400, and I want to
I'm trying to create and zero an array of int s based on a
I have a string indexed array that I would like to remove an item
image naturalWidth return zero... that's it, why ? var newimage = new Image(); newimage.src
I want to repeatedly zero a large 2d array in C. This is what
I have an object that contains two arrays, the first is a slope array:
I'm trying to write an application which will populate an array with random numbers.

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.