Max Min Odd Even
Description
You are given an array A of size N, containing all positive elements. You have to find the difference between the largest odd number and the smallest even number present in the array.
Note: You may assume that an answer always exists.
Input
The first line of the input contains T, the number of test cases.
The first line of each test case contains N, the size of the array.
The next line of each test case contains N space separated integers denoting the values of the array.
Constraints
1 <= T <= 10
1 <= N <= 100
1 <= A[i] <= 100
Output
For each test case, print a single integer denoting the difference between the largest odd number and the smallest even number in the array, on a new line.
Hint
In the sample test case, the array has 5 elements. The largest odd number in the array is 5, while the smallest even number in the array is 2. Therefore, the difference between the two is 3, which is the required output.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int times = sc.nextInt();
for (int i =0;i<times;i++){
int lodd = 0;
int leven = 101;
int length = sc.nextInt();
int []array = new int[length];
for (int j =0;j<length;j++){
array[j]=sc.nextInt();
}
for (int k =0;k<length;k++){
if (array[k]%2==0){
if (array[k] < leven){
leven = array[k];
}
}else {
if (array[k]>lodd){
lodd=array[k];
}
}
}
System.out.println(lodd-leven);
}
}
}
No comments:
Post a Comment