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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:38:28+00:00 2026-06-03T21:38:28+00:00

Giving an arraylist say [3,10,9,11,10,2,9], i need to write some code to pick out

  • 0

Giving an arraylist say [3,10,9,11,10,2,9], i need to write some code to pick out the sequence [10,9,11,10].

The way this have to be done is by creating a interval from an element and then check whether the next element(s) is in that interval.

fx.:

From the element 3 we get the interval, 3*0,80 and 3*1,20 = [2.4 – 3.6]. Now checking if the next element, 10, is in that interval – its not. Move to next element, 10.

From 10 we get the interval [8 – 12]. We see that 9, 11 and 10 is in that interval, 2 is not so stop!

What i have:

import java.util.ArrayList;
import java.util.List;


public class HelloWorld {

public static void main (String[] args){
    List<Integer> tops = new ArrayList<Integer>();
    tops.add(3);
    tops.add(10);
    tops.add(9);
    tops.add(11);
    tops.add(10);
    tops.add(2);
    tops.add(9);


    List<Integer> newtops = new ArrayList<Integer>();
    for (int i = 1; i < tops.size()-1; i++){
        double minValue = tops.get(i)*0.80;
        double maxValue = tops.get(i)*1.20;
        for (int k = i+1; k < tops.size()-1; k++){
            if (tops.get(i) > minValue && tops.get(i) < maxValue){
                newtops.add(tops.get(i));
            }
            else{
                break;
            }
        }
    }System.out.println(newtops);
}
}

Output:
[10, 10, 10, 10, 9, 9, 9, 11, 11, 10]

Expected output:
[10, 9, 11, 10]

  • 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-03T21:38:30+00:00Added an answer on June 3, 2026 at 9:38 pm

    I think this will work. Untested…

    List<Integer> list = new ArrayList<Integer>();
    list.add(3);
    list.add(10);
    list.add(9);
    list.add(11);
    list.add(10);
    list.add(2);
    list.add(9);
    
    List<Integer> result = new ArrayList<Integer>();
    double lowerFactor = 0.8d;
    double upperFactor = 1.2d;
    
    double lowerLimit = list.get(0) * lowerFactor;
    double upperLimit = list.get(0) * upperFactor;
    int index = 1;
    do
    {
         if (lowerLimit <= list.get(index) && list.get(index) <= upperLimit)
         {
             if (result.isEmpty())
             {
                 result.add(list.get(index - 1));
             }
             result.add(list.get(index));
         } else if (result.size() > 0)
         {
             break;
         } else
         {
             lowerLimit = lowerFactor * list.get(index);
             upperLimit = upperFactor * list.get(index);
         }
         index++;
    } while (index < list.size());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to clean up some legacy code and have some unchecked cast warnings
I'm fairly new to Java, and I need some help figuring out a good
JSLint is giving me this error: Problem at line 11 character 33: Use the
We are giving a demo in a couple days and I have to go
This line is giving error: Too few arguments. What is the problem? $InsertQuery =
Why is this thing giving the message in the second line (i.e. list convertion)?
I have an Arraylist of HashMap . Each HashMap element contains two columns: column
I have a JSP that takes an Arraylist from the session object and removes
I have traced an issue with an application I am developing, it is giving
Can Java nest generics? The following is giving me an error in Eclipse: ArrayList<ArrayList<Integer>>

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.