Write a C++ function to show all the INNOVATORS using one dimensional an•ay. An item value which is store in array is Innovator if it is greater than all the items to its left side. And the left most item is always an Innovator. For example, in the array {11, 21, 19, 33, 79, 41}, Innovator are 79,
21 and 11.
#include <iostream>
#include <limits.h>
using namespace std;
//Function to print all elements which are greater than all elements present to their right
void findInnovators(int arr[], int n)
{
//Store the max value encountered so far when looping
int maxSoFar = INT_MIN;
//Traverse the array from left to right
for(int i = 0; i <= n; i++)
{
// f the current element is greater than the max so far print it and update maxSoFar variable
if (arr[i] > maxSoFar)
{
maxSoFar = arr[i];
cout<<arr[i]<<" ";
}
}
}
int main(void)
{
int arr[] = { 11, 21, 19, 33, 79, 41 };
int n = sizeof(arr)/sizeof(arr[0]);
findInnovators(arr, n);
return 0;
Comments
Leave a comment