Given an array of integers nums, return the number of non-empty subsequences where the mode (the value that appears most frequently) is unique and appears in the middle of the subsequence. The mode must be unique (no tie), and the subsequence must have odd length so that there is a unique middle element. The answer may be large, return it modulo 10^9 + 7.
The key is that the mode must be unique and must be the middle element of the subsequence. For each possible middle position, we can try to build subsequences around it such that the mode is unique and is the middle.
#include<vector>#include<unordered_map>usingnamespace std;
classSolution {
public:int countSubsequences(vector<int>& nums) {
constint MOD =1e9+7;
int n = nums.size();
longlong ans =0;
for (int mid =0; mid < n; ++mid) {
int x = nums[mid];
int left =0, right =0;
for (int i =0; i < mid; ++i) if (nums[i] == x) ++left;
for (int i = mid+1; i < n; ++i) if (nums[i] == x) ++right;
ans = (ans + (1LL<< left) * (1LL<< right) % MOD) % MOD;
}
return ans;
}
};
// Leetcode signature: int countSubsequences(vector<int>& nums);
classSolution {
publicintcountSubsequences(int[] nums) {
int MOD = 1_000_000_007;
int n = nums.length;
long ans = 0;
for (int mid = 0; mid < n; ++mid) {
int x = nums[mid];
int left = 0, right = 0;
for (int i = 0; i < mid; ++i) if (nums[i]== x) ++left;
for (int i = mid+1; i < n; ++i) if (nums[i]== x) ++right;
long ways = pow2(left, MOD) * pow2(right, MOD) % MOD;
ans = (ans + ways) % MOD;
}
return (int)ans;
}
privatelongpow2(int x, int mod) {
long res = 1;
for (int i = 0; i < x; ++i) res = res * 2 % mod;
return res;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
funcountSubsequences(nums: IntArray): Int {
val MOD = 1_000_000_007
var ans = 0Lfor (mid in nums.indices) {
val x = nums[mid]
var left = 0; var right = 0for (i in0 until mid) if (nums[i] == x) left++for (i in mid+1 until nums.size) if (nums[i] == x) right++val ways = pow2(left, MOD) * pow2(right, MOD) % MOD
ans = (ans + ways) % MOD
}
return ans.toInt()
}
funpow2(x: Int, mod: Int): Long {
var res = 1L repeat(x) { res = res * 2 % mod }
return res
}
1
2
3
4
5
6
7
8
9
10
defcountSubsequences(nums):
MOD =10**9+7 n = len(nums)
ans =0for mid in range(n):
x = nums[mid]
left = sum(1for i in range(mid) if nums[i] == x)
right = sum(1for i in range(mid+1, n) if nums[i] == x)
ans = (ans + pow(2, left, MOD) * pow(2, right, MOD)) % MOD
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
pubfncount_subsequences(nums: Vec<i32>) -> i32 {
let modulo =1_000_000_007;
let n = nums.len();
letmut ans =0i64;
for mid in0..n {
let x = nums[mid];
let left = nums[..mid].iter().filter(|&&y| y == x).count();
let right = nums[mid+1..].iter().filter(|&&y| y == x).count();
let ways = mod_pow2(left, modulo) * mod_pow2(right, modulo) % modulo;
ans = (ans + ways) % modulo;
}
ans asi32}
fnmod_pow2(x: usize, modulo: i64) -> i64 {
letmut res =1i64;
for _ in0..x { res = res *2% modulo; }
res
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
functioncountSubsequences(nums: number[]):number {
constMOD=1e9+7;
constn=nums.length;
letans=0;
for (letmid=0; mid<n; ++mid) {
constx=nums[mid];
letleft=0, right=0;
for (leti=0; i<mid; ++i) if (nums[i] ===x) ++left;
for (leti=mid+1; i<n; ++i) if (nums[i] ===x) ++right;
ans= (ans+pow2(left) *pow2(right) %MOD) %MOD;
}
returnans;
}
functionpow2(x: number):number {
letres=1;
for (leti=0; i<x; ++i) res=res*2%1e9_7;
returnres;
}