Problem:
Design and implement an algorithm to find the maximum in a set and the position where it first occurs and where it last occurs.
Solution:
package array.programs;
import java.util.Scanner;
public class FirstAndLastOccurencesOfMaximumNumber {
int[] numbers;
int max;
int counter;
int first;
int last;
public static void main(String[] args) {
FirstAndLastOccurencesOfMaximumNumber maximumNumber = new FirstAndLastOccurencesOfMaximumNumber();
maximumNumber.getTheNumbers();
maximumNumber.findMaximum();
maximumNumber.findTheMaximumValueOccurences();
maximumNumber.printTheResult();
}
public void getTheNumbers(){
Scanner scanner = new Scanner(System.in);
System.out.println("how many numbers");
numbers = new int[scanner.nextInt()];
for (int i = 0; i < numbers.length; i++) {
System.out.println("enter number");
numbers[i] = scanner.nextInt();
}
scanner.close();
}
public void findMaximum(){
max = numbers[0];
for (int i = 0; i < numbers.length; i++) {
if(numbers[i] > max){
max = numbers[i];
}
}
}
public void findTheMaximumValueOccurences(){
for (int i = 0; i < numbers.length; i++) {
if(max == numbers[i]){
if(counter == 0){
first = i + 1;
}
counter++;
last = i + 1;
}
}
}
public void printTheResult(){
System.out.println("Maximum value " + max + " first occurence is at " + first + " and last occurence is at " + last);
}
}
Output:
how many numbers
5
enter number
123
enter number
44
enter number
322
enter number
344
enter number
333
Maximum value 344 first occurence is at 4 and last occurence is at 4
Design and implement an algorithm to find the maximum in a set and the position where it first occurs and where it last occurs.
Solution:
package array.programs;
import java.util.Scanner;
public class FirstAndLastOccurencesOfMaximumNumber {
int[] numbers;
int max;
int counter;
int first;
int last;
public static void main(String[] args) {
FirstAndLastOccurencesOfMaximumNumber maximumNumber = new FirstAndLastOccurencesOfMaximumNumber();
maximumNumber.getTheNumbers();
maximumNumber.findMaximum();
maximumNumber.findTheMaximumValueOccurences();
maximumNumber.printTheResult();
}
public void getTheNumbers(){
Scanner scanner = new Scanner(System.in);
System.out.println("how many numbers");
numbers = new int[scanner.nextInt()];
for (int i = 0; i < numbers.length; i++) {
System.out.println("enter number");
numbers[i] = scanner.nextInt();
}
scanner.close();
}
public void findMaximum(){
max = numbers[0];
for (int i = 0; i < numbers.length; i++) {
if(numbers[i] > max){
max = numbers[i];
}
}
}
public void findTheMaximumValueOccurences(){
for (int i = 0; i < numbers.length; i++) {
if(max == numbers[i]){
if(counter == 0){
first = i + 1;
}
counter++;
last = i + 1;
}
}
}
public void printTheResult(){
System.out.println("Maximum value " + max + " first occurence is at " + first + " and last occurence is at " + last);
}
}
how many numbers
5
enter number
123
enter number
44
enter number
322
enter number
344
enter number
333
Maximum value 344 first occurence is at 4 and last occurence is at 4
how many numbers
5
enter number
123
enter number
2
enter number
34
enter number
44
enter number
123
Maximum value 123 first occurence is at 1 and last occurence is at 5
No comments:
Post a Comment