AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-array: java program to search a Number and its smaller and larger numbers too

Input specification:

Number of elements in the array and the N elements the number to be search.

Output specification:

Print "YES" or "NO" if the search number is present in the array,

the number of elements which are less than the search number and

the number of elements which are greater than the search number

Sample Input:

5

1

2

3

4

5

8

Sample Output:

NO

5

0

Explanation: 5 denotes the number of elements of the array, 1, 2, 3, 4, 5 are the elements of the array and 8 is the search number.

8 is not present in the array, so print "NO", and there are 5 numbers less than 8 and there is no number greater than 8, so the count is 0.

corejava x 353
array x 12
Posted On : 2017-07-20 19:22:10.0
profile Anonymous User - anyforum.in Anonymous User
1300
up-rate
5
down-rate

Answers


import java.util.Scanner;


public class NumberSearch {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int N=input.nextInt();
int[] numbers=new int[N];
for(int i=0;i<numbers.length;i++){
numbers[i]=input.nextInt();
}
int searchNumber=input.nextInt();
int smallerCount=0;
int greaterCount=0;
boolean isExist=false;
for(int i=0;i<numbers.length;i++){
if(numbers[i]==searchNumber){
isExist=true;
}else if(numbers[i]>searchNumber){
greaterCount++;
}else if(numbers[i]<searchNumber){
smallerCount++;
}
}
System.out.println(isExist?"YES":"NO");
System.out.println(smallerCount);
System.out.println(greaterCount);

}
}

Posted On : 2017-07-21 00:05:14
Satisfied : 1 Yes  0 No
profile Garima Gupta - anyforum.in Garima Gupta
596129558962
Reply This Thread
up-rate
1
down-rate

import java.util.ArrayList;
import java.util.Scanner;
class ElementSearch1
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<>();
int arraySize = scanner.nextInt();
int count = 0,grval=0,lesval=0;
while (count < arraySize)
{
list.add(scanner.nextInt());
count++;
}
int ele=scanner.nextInt();
for(int i = 0; i < list.size(); i++)
{
if(list.get(i) > ele)
{
grval++;
}
else
{
lesval++;
}
}
System.out.println(list.indexOf(ele)==-1?"NO":"YES");
System.out.println(lesval);
System.out.println(grval);
}
}


Posted On : 2017-07-21 01:56:42
Satisfied : 1 Yes  0 No
profile SudheerKumar Javvadi - anyforum.in SudheerKumar Javvadi
0960
Reply This Thread
up-rate
2
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in