Question -
Given an sorted array, your task is to find the leader elements from array.
note - an element is said to be leader of the array if element is greater element to all the right side of the elements.
Example -
Input : arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}
Output : 9
Approach -
In a sorted array, last element is the leader element of the array.
Implementation -
//move zero at end of array without chamge order
#include<iostream>
using namespace std;
int findleader(int arr[],int n)
{
return arr[n-1];
}
int main()
{
int n;
cout<<"Enter n : ";
cin>>n;
int arr[n];
cout<<"Enter values : "<<endl;
for(int i=0;i<n;i++)
cin>>arr[i];
cout<<"Leader elemetn is : "<<findleader(arr,n)<<endl;
}
Time complexity - O(1)
Extra space -O(1)
Comments
Post a Comment