Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
1. Given the following list of sorted integers:
0 1 5 8 14 18 44 81 89 99 102

2. And 2 numbers
10 20

3. Find how many numbers are in between those 2 numbers in the original list,
including those 2 numbers if present
eg. in above case the output should be:
2


4. Try to optimize on time complexity

What I have tried:

I tried in Java. But am not able to find answers.
Posted
Updated 23-Nov-22 6:18am
Comments
Richard Deeming 23-Nov-22 11:03am    
By writing code. You don't have long, so you better stop wasting your time looking for ready-made answers, or begging for someone else to do your homework for you, and get cracking!

If you really don't know where to start, then talk to your teacher. Nobody here is going to do the work for you.
Richard MacCutchan 23-Nov-22 11:11am    
If it is not homework then why do you need to submit it before tomorrow?
Sonam sinha 23-Nov-22 12:19pm    
I have solved this question , can anyone help me with the time complexity
CHill60 23-Nov-22 12:46pm    
You should put the code into your question and delete the solution to ensure people actually look at your now-solved post - it is no longer in the list of Unanswered Questions. Be sure to update your question to reflect the new question is only about point 4

1 solution

Java
import java.util.Scanner;
import java.util.ArrayList;

public class ListQues {
	
	public static int find(ArrayList<Integer> list , int min , int max)
	{
		int count = 0;
		for(int i =0 ; i<list.size();i++)
		{
			if(list.get(i)>= min && list.get(i) <= max)
			{
				count++;
			}
		}
		return count;
		
	}
	public static void main(String[] args) {
	 Scanner sc = new Scanner(System.in);
	 System.out.println("Enter the size of list");
	 int size = sc.nextInt();
	 ArrayList<Integer> list = new ArrayList<>();
	 System.out.println("Enter the numbers");
	 for(int i =0; i<size;i++)
	 {
		 list.add(sc.nextInt());
	 }
	 System.out.println("Enter  minimum and maximum value");
	 int min = sc.nextInt();
	 int max = sc.nextInt();
     int ans = find(list , min , max);
     System.out.println("Number of elements in between min and max are " + ans);
      
     
	}<pre><pre lang="Java"><pre lang="Java"><pre><pre lang="Java"><pre lang="Java">

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900