先讨论出现次数大于n/2的数字,如果这样的数字存在,那么这个数出现的次数大于其他数出现的次数的总和. 在数组A中,我们定义两个数据集合a1,a2.a1为出现次数大于n/2的数的集合,a2为其余数组成的集合.对于数组 A中元素a.b,假设a不等于b,那么有两种情况,分别为:a属于a1,b属于a2:a属于a2,b属于a2.对于这两种情况,如 果把a.b从数组A中去掉,集合a1的size依旧是大于a2的.按照这个思路,我们有如下代码: int m; ; for (auto num : nums) {…
#include <bits/stdc++.h> using namespace std; const int N = 1e6,INF = 0x3f3f3f3f; int a[N]; int n,x; void fun(int n) { int left = 0, right = n-1; while(left < right) { int ans=a[left] + a[right]; if(ans == x) { cout<<a[left]<<' '<&…
题目原文 Selection in two sorted arrays. Given two sorted arrays a[] and b[], of sizes n1 and n2, respectively, design an algorithm to find the kth largest key. The order of growth of the worst case running time of your algorithm should be logn, where n…