In: Computer Science
this should be written in Java- Problem: Min/Max Search by Index Develop a program that, given a sequence S of integers as input, produces as output two sequences of positive integers, the first of which indicates all those positions in S at which S's minimum value occurs and the second of which indicates all those positions at which S's maximum value occurs. Positions are numbered starting at zero (0).
Facts ● Scanner has a method that returns a boolean indicating whether a next integer exists in its input stream ( hasNextInt() ) ● Scanner objects can be initialized to to scan String data as input.
Sample input
3
3 6 -1 4 6 5 3
0 0 0 0
-4 45 2 0 3 5 11 -7 854 25 3 -7 4 -3
Sample output
3 6 -1 4 6 5 3
2
1 4
0 0 0 0
0 1 2 3
0 1 2 3
-4 45 2 0 3 5 11 -7 854 25 3 -7 4 -3
7 11
8
Input The input will begin with a single line containing T , the number of test cases to follow. The remaining lines contain the T sequences, one line per sequence. Each of these lines contains the values in the sequence. Each such value is separated from the next by at least one space.
Output For each sequence given as input, there should be four lines of output. The first line echos the given sequence. The second line indicates the positions at which the minimum value occurs. The third line indicates the positions at which the maximum value occurs. The fourth line is blank.
Java Program
import java.util.Scanner; //Import until scanner package for
scanner class
class MinMaxSeq //set class name
{
public static void main(String args[]) //Declared main
function
{
Scanner sc =new Scanner(System.in); //create object sc of
scanner class
int i,len=0;
int seq[]=new int[100]; //initialize array to store all
numbers
System.out.println("\n Enter Sequence ");
while(sc.hasNextInt()) //to take integer input from user work
until user input non-integer value
{
seq[len]=sc.nextInt();
len++;
}
System.out.println();
for(i=0;i<len;i++). // To print entered series
{
System.out.print(seq[i]+" ");
}
int min=seq[0],max=seq[0]; //set minimum and maximum as first
number
for(i=1;i<len;i++) //find minimum and maximum number among
sequence
{
if(min>seq[i])
min=seq[i];
else if(max<seq[i])
max=seq[i];
}
System.out.println();
for(i=0;i<len;i++) // display index number of minimum
number
{
if(min==seq[i])
System.out.print(i+" ");
}
System.out.println();
for(i=0;i<len;i++) // display index number of maximum
number
{
if(max==seq[i])
System.out.print(i+" ");
}
}
}
________________________________________________________________
OUTPUT:-