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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:59:23+00:00 2026-06-15T15:59:23+00:00

This is an interview question I saw somewhere and came up with this: import

  • 0

This is an interview question I saw somewhere and came up with this:

import java.util.Random;
import java.util.Arrays;
class SumTwo
{
    public static void main(String arg[])
    {
        Random r=new Random();

        int arr[]=new int[5];
        for(int i=0;i<arr.length;i++)
        {
            arr[i]=r.nextInt(20);
        }
        Arrays.sort(arr);
        printArr(arr);
        System.out.println(checkSum(arr));

    }

    public static boolean checkSum(int[] arr)
    {
        for(int i=2;i<arr.length;i++)
        {
            if(check(arr,0,i-1,arr[i]))
                return true;
        }
        return false;
    }


    public static boolean check(int[] arr, int st, int en, int sum)
    {

        int add=0;
        while(st<en)
        {
            add=arr[st]+arr[en];
            if(add==sum)
                return true;
            else if(add>sum)
                en--;
            else
                st++;
        }
        return false;
    }

    public static void printArr(int[] arr)
    {
        System.out.println("\n");
        for(int i=0;i<arr.length;i++)
            System.out.print(" "+arr[i]);
        System.out.println("\n");
    }
}
  • 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-15T15:59:24+00:00Added an answer on June 15, 2026 at 3:59 pm

    Well, yours is O(n^3). (EDIT: I misread it, you are correct, yours is O(n^2)) Like Louis Wasserman said, there’s a simple algorithm for O(n^2 log n), and here’s one for O(n^2) using a bit of extra memory:

    public static boolean checkSum(int[] arr) {
        HashSet<Integer> set = new HashSet<Integer>();
        for (int n : arr) {
            set.add(n);
        }
    
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (set.contains(arr[i] + arr[j])) return true;
            }
        }
        return false;
    }
    

    Might need a special case for arrays containing zeros, depending on the problem definition, but the idea is there.

    Intuitively, it seems there might be a clever way of doing this that’s O(n log n) or so, but I can’t immediately find any. I’ll keep thinking on it a bit more, this is a good question.

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

Sidebar

Related Questions

I came across this interview question Many irregularly shaped objects are moving in random
I saw this interview question recently for a firm that said: Group of people,
This is an interview question I think of a solution. It uses queue. public
This is an interview question I saw online and I am not sure I
This is an interview question (phone screen): write a function (in Java) to find
This is an interview question I came across: find K first digits of the
I had a recursion interview question problem in Java,Need your help on this. Write
The Title is self explanatory. This was an interview question. In java, List is
This is an interview question I saw on some site. It was mentioned that
I saw this in an interview question , Given a sorting order string, you

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.