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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:07:07+00:00 2026-06-14T23:07:07+00:00

I am trying to solve this exercise Two Intervals Intersection , I think that

  • 0

I am trying to solve this exercise Two Intervals Intersection, I think that I solved almost all the problem, but when I try the sample input I get 15 and 5, and I want to order that results, my idea is using an arraylist for the intervals, and then delete the duplicates, but I just want a better approach to solve this problem, if I enter as input 2 3 and 2 3 I get as output 2 3 and 2 3, that why I ask you a better approach to solve this exercise, any better ideas

thanks for help

this is my code so far

import java.util.*;
public class TwoIntervalIntersection {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n1 = sc.nextInt();
        int n2 = sc.nextInt();
        int n3 = sc.nextInt();
        int n4 = sc.nextInt();

        if(n1 >= n3 && n1 <= n4){
            System.out.print(n1);
            System.out.print(" ");
        }

        if(n2 >= n3 && n2 <= n4){
            System.out.print(n2);
            System.out.print(" ");
        }

        if(n3 >= n1 && n3 <= n2){
            System.out.print(n3);
            System.out.print(" ");
        }

       if(n4 >= n1 && n4 <= n2){
            System.out.print(n4);
        }
    }
}
  • 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-14T23:07:09+00:00Added an answer on June 14, 2026 at 11:07 pm

    Hey your approach isn’t bad though it’s better to store the things before print them in the end so you can change/swap/check their values before the final print in the console. To fix the part with the interval been posted on the wrong way you can do something like :

    public static void main(String args[]){
    
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter interval one: ");
            int left1 = sc.nextInt();
            int right1 = sc.nextInt();
            System.out.print("Enter interval two: ");
            int left2 = sc.nextInt();
            int right2 = sc.nextInt();
    
            int end1 = 0;
            int end2 = 0;
    
            if(left1 >= left2 && left1 <= right2)
                end1 = left1;
    
            if(right1 >= left2 && right1 <= right2)
                end2 = right1;
    
            if(left2 >= left1 && left2 <= right1)
               end1 = left2;
    
           if(right2 >= left1 && right2 <= right1)
                end2 = right2;
    
           if(end1 > end2){
                int h= end1;
                end1 = end2;
                end2 = h;
            }
    
           System.out.println(end1 + " " + end2);
    }
    

    If you want to improve the program more the same swap mechanism may be added for the entered intervals of the users so when he enter “15 1” instead of “1 15” the program still works correct.

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

Sidebar

Related Questions

I am trying to solve this problem http://www.spoj.pl/problems/PEBBMOV/ . I think I have the
I am trying to solve this exercise, It seems easy this , but I
I am trying to solve this problem: In an integer array all numbers occur
Trying to solve this problem: I have the following set of divs that when
Trying to solve this problem . I would like to learn how the bootstrapper
I am trying to solve this SPOJ problem . The question asks to find
I've been trying to solve this silly problem for a few days now and
I'm trying to solve this problem in a pure-functional way, without using set! .
I was trying to solve this problem - http://www.spoj.pl/problems/LISA/ I thought of Greedy initially,
I was trying to solve this problem on SPOJ (http://www.spoj.pl/problems/REC/) F(n) = a*F(n-1) +

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.