Mehaboob557
BAN USER- 0of 0 votes
AnswersA non-empty zero-indexed array A consisting of N integers is given. The leader of this array is the value that occurs in more than half of the elements of A.
- Mehaboob557 in India
Write a function:
int arrLeader(int A[], int N);
int arrLeader(NSMutableArray *A);
int arrLeader(const vector<int> &A);
class Solution { int arrLeader(int[] A); }
class Solution { public int arrLeader(int[] A); }
function arrLeader(A);
function arrLeader(A)
function arrLeader($A);
function arrLeader(A : array of longint; N : longint) : longint;
def arrLeader(A)
sub arrLeader { my (@A)=@_; ... }
def arrLeader(a)
Private Function arrLeader ( A As Integer() ) as Integer
that, given a non-empty zero-indexed array A consisting of N integers, returns the leader of array A. The function should return -1 if array A does not contain a leader.
Assume that:
l N is an integer within the range [1..1,000,000];
l each element of array A is an integer within the range [0..2,147,483,647].
For example, given array A consisting of ten elements such that:
A[0] = 4 A[1] = 2 A[2] = 2 A[3] = 3 A[4] = 2 A[5] = 4 A[6] = 2 A[7] = 2 A[8] = 6 A[9] = 4
the function should return -1, because the value that occurs most frequently in the array, 2, occurs 5 times, and 5 is not more than half of 10.
Given array A consisting of five elements such that:
A[0] = 100 A[1] = 1 A[2] = 1 A[3] = 50 A[4] = 1
the function should return 1.
Complexity:
l expected worst-case time complexity is O(N);
l expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified.
// you can also use includes for example:
// #include <algorithm>
int arrLeader ( const vector<int> &A ) {
// write your code here
}| Report Duplicate | Flag | PURGE
Java
Replarryehickl, Associate at ABC TECH SUPPORT
I am a blogger in the Geek system operator . As an editor with a strong background in english and hindi ...