An array A consisting of N integers is given. The dominator of array A is the value that occurs in more than half of the elements of A.
For example, consider array A such that
A[0] = 3 A[1] = 4 A[2] = 3 A[3] = 2 A[4] = 3 A[5] = -1 A[6] = 3 A[7] = 3The dominator of A is 3 because it occurs in 5 out of 8 elements of A (namely in those with indices 0, 2, 4, 6 and 7) and 5 is more than a half of 8.
Write a function
class Solution { public int solution(int[] A); }
that, given an array A consisting of N integers, returns index of any element of array A in which the dominator of A occurs. The function should return −1 if array A does not have a dominator.
For example, given array A such that
A[0] = 3 A[1] = 4 A[2] = 3 A[3] = 2 A[4] = 3 A[5] = -1 A[6] = 3 A[7] = 3the function may return 0, 2, 4, 6 or 7, as explained above.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [0..100,000];
- each element of array A is an integer within the range [−2,147,483,648..2,147,483,647].
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int n = A.Lenght;
int size =0;
Stack<int> s = new Stack<int>;
for (int i=0; i<n; i++)
{
if(size ==0)
{
size +=1;
s.Push(A[i]);
}
else
{
if (s.Peek(A[i]) != A[i]) size -=1;
else size +=1;
}
}
int candidate = -1;
int count =0;
int leader= -1;
for (int i=0; i<n; i++)
{
if (A[i] == candidate) count +=1;
if (count > n/2) leader = candidate;
}
return leader;
}
}
Compilation failed: 1 error(s), 0 warnings Solution.cs(13,37): error CS1525: Unexpected symbol `;', expecting `(', `[', or `{'
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int n = A.Lenght;
int size =0;
Stack<int> s = new Stack<int>();
for (int i=0; i<n; i++)
{
if(size ==0)
{
size +=1;
s.Push(A[i]);
}
else
{
if (s.Peek(A[i]) != A[i]) size -=1;
else size +=1;
}
}
int candidate = -1;
int count =0;
int leader= -1;
for (int i=0; i<n; i++)
{
if (A[i] == candidate) count +=1;
if (count > n/2) leader = candidate;
}
return leader;
}
}
Compilation failed: 4 error(s), 0 warnings Solution.cs(11,19): error CS1061: Type `int[]' does not contain a definition for `Lenght' and no extension method `Lenght' of type `int[]' could be found. Are you missing an assembly reference? /opt/codility-mono/lib/mono/4.5/mscorlib.dll (Location of the symbol related to previous error) Solution.cs(13,9): error CS0246: The type or namespace name `Stack' could not be found. Are you missing `System.Collections.Generic' using directive? Solution.cs(20,17): error CS0841: A local variable `s' cannot be used before it is declared Solution.cs(24,21): error CS0841: A local variable `s' cannot be used before it is declared
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int n = A.Lenght;
int size =0;
Stack<int> s = new Stack<int>();
for (int i=0; i<n; i++)
{
if(size ==0)
{
size +=1;
s.Push(A[i]);
}
else
{
if (s.Peek(A[i]) != A[i]) size -=1;
else size +=1;
}
}
int candidate = -1;
int count =0;
int leader= -1;
for (int i=0; i<n; i++)
{
if (A[i] == candidate) count +=1;
if (count > n/2) leader = candidate;
}
return leader;
}
}
Compilation failed: 2 error(s), 0 warnings Solution.cs(11,19): error CS1061: Type `int[]' does not contain a definition for `Lenght' and no extension method `Lenght' of type `int[]' could be found. Are you missing an assembly reference? /opt/codility-mono/lib/mono/4.5/mscorlib.dll (Location of the symbol related to previous error) Solution.cs(24,23): error CS1501: No overload for method `Peek' takes `1' arguments /opt/codility-mono/lib/mono/4.5/System.dll (Location of the symbol related to previous error)
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int n = A.Length;
int size =0;
Stack<int> s = new Stack<int>();
for (int i=0; i<n; i++)
{
if(size ==0)
{
size +=1;
s.Push(A[i]);
}
else
{
if (s.Peek(A[i]) != A[i]) size -=1;
else size +=1;
}
}
int candidate = -1;
int count =0;
int leader= -1;
for (int i=0; i<n; i++)
{
if (A[i] == candidate) count +=1;
if (count > n/2) leader = candidate;
}
return leader;
}
}
Compilation failed: 1 error(s), 0 warnings Solution.cs(24,23): error CS1501: No overload for method `Peek' takes `1' arguments /opt/codility-mono/lib/mono/4.5/System.dll (Location of the symbol related to previous error)
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int n = A.Length;
int size =0;
Stack<int> s = new Stack<int>();
for (int i=0; i<n; i++)
{
if(size ==0)
{
size +=1;
s.Push(A[i]);
}
else
{
if (s.Peek(A[i]) != A[i]) size -=1;
else size +=1;
}
}
int candidate = -1;
int count =0;
int leader= -1;
for (int i=0; i<n; i++)
{
if (A[i] == candidate) count +=1;
if (count > n/2) leader = candidate;
}
return leader;
}
}
Compilation failed: 1 error(s), 0 warnings Solution.cs(24,23): error CS1501: No overload for method `Peek' takes `1' arguments /opt/codility-mono/lib/mono/4.5/System.dll (Location of the symbol related to previous error)
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int n = A.Length;
int size =0;
Stack<int> s = new Stack<int>();
for (int i=0; i<n; i++)
{
if(size ==0)
{
size +=1;
s.Push(A[i]);
}
else
{
if (s.Peek() != A[i]) size -=1;
else size +=1;
}
}
int candidate = -1;
int count =0;
int leader= -1;
for (int i=0; i<n; i++)
{
if (A[i] == candidate) count +=1;
if (count > n/2) leader = candidate;
}
return leader;
}
}
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int n = A.Length;
int size =0;
int value=0;
Stack<int> s = new Stack<int>();
for (int i=0; i<n; i++)
{
if(size ==0)
{
size +=1;
s.Push(A[i]);
}
else
{
if (s.Peek() != A[i]) size -=1;
else size +=1;
}
}
int candidate = -1;
if (size>0) candidate = s.Peek();
int count =0;
int leader= -1;
for (int i=0; i<n; i++)
{
if (A[i] == candidate) count +=1;
if (count > n/2) leader = candidate;
}
return leader;
}
}
example test
got 3, but element is not a dominator, value 2 has only 1 occurences (n=8)
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int n = A.Length;
int size =0;
int value=0;
Stack<int> s = new Stack<int>();
for (int i=0; i<n; i++)
{
if(size ==0)
{
size +=1;
s.Push(A[i]);
}
else
{
if (s.Peek() != A[i]) size -=1;
else size +=1;
}
}
int candidate = -1;
if (size>0) candidate = s.Peek();
int count =0;
int leader= -1;
for (int i=0; i<n; i++)
{
if (A[i] == candidate) count +=1;
if (count > n/2) leader = candidate;
}
return Array.IndexOf(A, leader);
}
}
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int n = A.Length;
int size =0;
int value=0;
Stack<int> s = new Stack<int>();
for (int i=0; i<n; i++)
{
if(size ==0)
{
size +=1;
s.Push(A[i]);
}
else
{
if (s.Peek() != A[i]) size -=1;
else size +=1;
}
}
int candidate = -1;
if (size>0) candidate = s.Peek();
int count =0;
int leader= -1;
for (int i=0; i<n; i++)
{
if (A[i] == candidate) count +=1;
if (count > n/2) leader = candidate;
}
return Array.IndexOf(A, leader);
}
}
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int n = A.Length;
int size =0;
int value=0;
Stack<int> s = new Stack<int>();
for (int i=0; i<n; i++)
{
if(size ==0)
{
size +=1;
s.Push(A[i]);
}
else
{
if (s.Peek() != A[i]) size -=1;
else size +=1;
}
}
int candidate = -1;
if (size>0) candidate = s.Peek();
int count =0;
int leader= -1;
for (int i=0; i<n; i++)
{
if (A[i] == candidate) count +=1;
if (count > n/2) leader = candidate;
}
return Array.IndexOf(A, leader);
}
}
The solution obtained perfect score.