Write a linear (O(N)) running time complexity program in Java to find all the dominant elements in the given array of N distinct integer elements. An element is a dominant element if is greater than all the elements to its right side. The rightmost element in the array is always a dominant element. For example, in the array {16, 17, 4, 3, 5, 2}, dominant elements are 17, 5 and 2.

Respuesta :

The program is an illustration of arrays

What are arrays?

Arrays are variables that hold multiple values of the same type in one variable name

The program in Java

The linear (O(N)) running time complexity program in Java, where comments are used to explain each line is as follows

import java.util.*;

public class Main{

   public static void main(String [] args){

       //This initializes an array

       int arr[] = {16, 17, 4, 3, 5, 2};

       //This gets the size of the array

       int size = arr.length;

       //This iterates through the array

       for (int i = 0; i < size; i++){

 //This declares an integer variable

 int j;

 //This iterates through every other element of the array

 for (j = i+1; j < size; j++){

     //This condition breaks the loop

  if (arr[i] <=arr[j])

   break;

 }

 //This conditions prints the dominant element

 if (j == size){

  System.out.print(arr[i] + " ");}

       }

   }

}

Read more about arrays at:

https://brainly.com/question/25083828

ACCESS MORE
EDU ACCESS
Universidad de Mexico