Given an array of size n, find the majority element. The majority element is the element that appears more than b n c times.You may assume that the array is non-empty and the majority 2 element always exist in the array.
INPUT:
Line 1: the length of array.
Line 2: the all elements in array and split by spaces OUTPUT:
Line 1: A single integer that is the majority element.
#include<stdio.h> #include<map> using namespace std; map<int, int> m; int main() { int n; scanf("%d", &n); int MAX = 0, ans =0; while(n--) { int a; scanf("%d", &a); m[a]++; if(m[a] > MAX){ MAX = m[a]; ans = a; } } printf("%d\n", ans); }
B:
Given a 2d m * n grid map of ’1’s (land) and ’0’s (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
INPUT:
Line 1: m and n.
Line 2: ’1’ or ’0’ in grid and split by spaces OUTPUT: