Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Given an integer array nums
, return the number of elements that have both a strictly smaller and a strictly greater element appear in nums
.
Example 1:
Input: nums = [11,7,2,15]
Output: 2
Explanation: The element 7 has the element 2 strictly smaller than it and the element 11 strictly greater than it.
Element 11 has element 7 strictly smaller than it and element 15 strictly greater than it.
In total there are 2 elements having both a strictly smaller and a strictly greater element appear in nums.
Example 2:
Input: nums = [-3,3,3,90]
Output: 2
Explanation: The element 3 has the element -3 strictly smaller than it and the element 90 strictly greater than it.
Since there are two elements with the value 3, in total there are 2 elements having both a strictly smaller and a strictly greater element appear in nums.
Constraints:
1 <= nums.length <= 100
-105 <= nums[i] <= 105
class Solution {
public:
int countElements(vector<int>& nums) {
int M = *max_element(nums.begin(), nums.end());
int m = *min_element(nums.begin(), nums.end());
int res = 0;
for(int i = 0; i < nums.size(); i++){
if(nums[i] > m && nums[i] < M) res++;
}
return res;
}
};
class Solution {
public int countElements(int[] nums) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int i = 0; i < nums.length; ++i) {
min = Math.min(min, nums[i]);
max = Math.max(max, nums[i]);
}
int count = 0;
for (int i = 0; i < nums.length; ++i) {
if (nums[i] > min && nums[i] < max) {
count++;
}
}
return count;
}
}
class Solution:
def countElements(self, nums: List[int]) -> int:
M = max(nums)
m = min(nums)
return sum(1 for i in nums if m<i<M)
class Solution:
def countElements(self, nums: List[int]) -> int:
mi = min(nums)
ma = max(nums)
check = Counter(nums)
ans = max(0,len(nums)-check[mi]-check[ma])
return ans
You are given a 0-indexed integer array nums
of even length consisting of an equal number of positive and negative integers.
You should rearrange the elements of nums
such that the modified array follows the given conditions:
nums
is preserved.Return the modified array after rearranging the elements to satisfy the aforementioned conditions.
Example 1:
Input: nums = [3,1,-2,-5,2,-4]
Output: [3,-2,1,-5,2,-4]
Explanation:
The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4].
The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4].
Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions.
Example 2:
Input: nums = [-1,1]
Output: [1,-1]
Explanation:
1 is the only positive integer and -1 the only negative integer in nums.
So nums is rearranged to [1,-1].
Constraints:
2 <= nums.length <= 2 * 105
nums.length
is even1 <= |nums[i]| <= 105
nums
consists of equal number of positive and negative integers.vector<int> rearrangeArray(vector<int>& nums) {
vector<int> ans(n,0);
int indexpos = 0, indexneg=1;
for(auto num: nums){
if(num>0){
ans[indexpos] = num;
indexpos+=2;
}
if(num<0){
ans[indexneg] = num;
indexneg += 2;
}
}
return ans;
}
class Solution:
def rearrangeArray(self, nums: List[int]) -> List[int]:
a=[0]*len(nums)
p=0
n=1
for i in nums:
if i>0:
a[p]=i
p+=2
else:
a[n]=i
n+=2
return a
class Solution {
public int[] rearrangeArray(int[] nums) {
int [] out = new int [nums.length];
int j = 0;
int k = 1;
for(int i=0;i<nums.length;i++){
if(nums[i] >= 0){
out[j] = nums[i];
j += 2;
}else {
out[k] = nums[i];
k += 2;
}
}
return out;
}
}
class Solution:
def rearrangeArray(self, nums: List[int]) -> List[int]:
l1=[0]*len(nums)
a=1
b=0
for i in nums:
if "-" in str(i):
l1[a]=i
a+=2
else:
l1[b]=i
b+=2
return(l1)
var rearrangeArray = function (nums) {
let n = nums.length;
let pos = 0;
let neg = 0;
let cur = -1;
while (pos < n && nums[pos] < 0) pos++;
while (neg < n && nums[neg] > 0) neg++;
for (let i = 0; i < n; i++) {
cur = cur === -1 ? 1 : -1;
if (cur === 1 && nums[i] > 0) {
pos++;
while (pos < n && nums[pos] < 0) pos++;
continue;
}
if (cur === -1 && nums[i] < 0) {
neg++;
while (neg < n && nums[neg] > 0) neg++;
continue;
}
let index = pos === i ? neg : pos;
let tmp = nums[index];
for (let k = index; k > i; k--) {
nums[k] = nums[k - 1];
}
nums[i] = tmp;
neg = i + 1;
pos = i + 1;
while (neg < n && nums[neg] > 0) neg++;
while (pos < n && nums[pos] < 0) pos++;
}
return nums;
};
You are given an integer array nums
. A number x
is lonely when it appears only once, and no adjacent numbers (i.e. x + 1
and x - 1)
appear in the array.
Return all lonely numbers in nums
. You may return the answer in any order.
Example 1:
Input: nums = [10,6,5,8]
Output: [10,8]
Explanation:
- 10 is a lonely number since it appears exactly once and 9 and 11 does not appear in nums.
- 8 is a lonely number since it appears exactly once and 7 and 9 does not appear in nums.
- 5 is not a lonely number since 6 appears in nums and vice versa.
Hence, the lonely numbers in nums are [10, 8].
Note that [8, 10] may also be returned.
Example 2:
Input: nums = [1,3,5,3]
Output: [1,5]
Explanation:
- 1 is a lonely number since it appears exactly once and 0 and 2 does not appear in nums.
- 5 is a lonely number since it appears exactly once and 4 and 6 does not appear in nums.
- 3 is not a lonely number since it appears twice.
Hence, the lonely numbers in nums are [1, 5].
Note that [5, 1] may also be returned.
Constraints:
1 <= nums.length <= 105
0 <= nums[i] <= 106
class Solution:
def findLonely(self, nums: List[int]) -> List[int]:
m = Counter(nums)
return [n for n in nums if m[n] == 1 and m[n - 1] + m[n + 1] == 0]
vector<int> findLonely(vector<int>& nums) {
unordered_map<int, int> m;
vector<int> res;
for (int n : nums)
++m[n];
for (const auto [n, cnt] : m)
if (cnt == 1 && m.count(n + 1) == 0 && m.count(n - 1) == 0)
res.push_back(n);
return res;
}
public List<Integer> findLonely(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
List<Integer> res = new ArrayList<>();
for (int num : map.keySet()) {
// for (int num : nums) {
if (map.get(num) == 1 && !map.containsKey(num - 1) && !map.containsKey(num + 1)) {
res.add(num);
}
}
return res;
}
var findLonely = function(nums) {
let countMap = new Map();
let result = [];
for (let num of nums) {
countMap.set(num, (countMap.get(num) || 0) + 1);
}
for (let num of nums) {
if (!countMap.has(num - 1) && !countMap.has(num + 1) && countMap.get(num) === 1) {
result.push(num);
}
}
return result;
};
public class Solution
{
public IList<int> FindLonely(int[] nums)
{
var dict = nums.GroupBy(x => x).ToDictionary(g => g.Key, g => g.Count());
return nums.Where(x => dict[x] == 1 && !dict.ContainsKey(x - 1) && !dict.ContainsKey(x + 1)).ToList();
}
}
class Solution:
def findLonely(self, nums: List[int]) -> List[int]:
counter=defaultdict(int)
ans=[]
for i in nums:
counter[i]+=1
for i in nums:
if counter[i-1] or counter [i+1] or counter[i]>1: continue
ans.append(i)
return ans
There are two types of persons:
You are given a 0-indexed 2D integer array statements
of size n x n
that represents the statements made by n
people about each other. More specifically, statements[i][j]
could be one of the following:
0
which represents a statement made by person i
that person j
is a bad person.1
which represents a statement made by person i
that person j
is a good person.2
represents that no statement is made by person i
about person j
.Additionally, no person ever makes a statement about themselves. Formally, we have that statements[i][i] = 2
for all 0 <= i < n
.
Return the maximum number of people who can be good based on the statements made by the n
people.
Example 1:
Input: statements = [[2,1,2],[1,2,2],[2,0,2]]
Output: 2
Explanation: Each person makes a single statement.
- Person 0 states that person 1 is good.
- Person 1 states that person 0 is good.
- Person 2 states that person 1 is bad.
Let's take person 2 as the key.
- Assuming that person 2 is a good person:
- Based on the statement made by person 2, person 1 is a bad person.
- Now we know for sure that person 1 is bad and person 2 is good.
- Based on the statement made by person 1, and since person 1 is bad, they could be:
- telling the truth. There will be a contradiction in this case and this assumption is invalid.
- lying. In this case, person 0 is also a bad person and lied in their statement.
- Following that person 2 is a good person, there will be only one good person in the group.
- Assuming that person 2 is a bad person:
- Based on the statement made by person 2, and since person 2 is bad, they could be:
- telling the truth. Following this scenario, person 0 and 1 are both bad as explained before.
- Following that person 2 is bad but told the truth, there will be no good persons in the group.
- lying. In this case person 1 is a good person.
- Since person 1 is a good person, person 0 is also a good person.
- Following that person 2 is bad and lied, there will be two good persons in the group.
We can see that at most 2 persons are good in the best case, so we return 2.
Note that there is more than one way to arrive at this conclusion.
Example 2:
Input: statements = [[2,0],[0,2]]
Output: 1
Explanation: Each person makes a single statement.
- Person 0 states that person 1 is bad.
- Person 1 states that person 0 is bad.
Let's take person 0 as the key.
- Assuming that person 0 is a good person:
- Based on the statement made by person 0, person 1 is a bad person and was lying.
- Following that person 0 is a good person, there will be only one good person in the group.
- Assuming that person 0 is a bad person:
- Based on the statement made by person 0, and since person 0 is bad, they could be:
- telling the truth. Following this scenario, person 0 and 1 are both bad.
- Following that person 0 is bad but told the truth, there will be no good persons in the group.
- lying. In this case person 1 is a good person.
- Following that person 0 is bad and lied, there will be only one good person in the group.
We can see that at most, one person is good in the best case, so we return 1.
Note that there is more than one way to arrive at this conclusion.
Constraints:
n == statements.length == statements[i].length
2 <= n <= 15
statements[i][j]
is either 0
, 1
, or 2
.statements[i][i] == 2
class Solution {
public:
int n, ans = 0;
int maximumGood(vector<vector<int>>& S) {
n = size(S);
string cur = ""; cur.reserve(n);
dfs(S, cur, 0, 0);
return ans;
}
void dfs(vector<vector<int>>& S, string& cur, int i, int cnt) {
if(i == n) {
// if valid, update ans to store maximum good person found till now
if(valid(S, cur)) ans = max(ans, cnt);
return;
}
cur.append(1, '0');
dfs(S, cur, i+1, cnt); // assuming ith person is bad
cur.back() = '1';
dfs(S, cur, i+1, cnt + 1); // assuming ith person is good
cur.pop_back();
}
bool valid(vector<vector<int>>& S, string& cur) {
for(int i = 0; i < n; i++)
if(cur[i] == '1')
for(int j = 0; j < n; j++)
if(S[i][j] != 2 && S[i][j] != cur[j] - '0') return false;
return true;
}
};
class Solution:
def maximumGood(self, S):
n, ans = len(S), 0
def valid(cur):
for i in range(n):
if cur[i]:
for j in range(n):
if S[i][j] != 2 and S[i][j] != cur[j]: return False
return True;
def dfs(cur, i, cnt):
nonlocal ans
if i == n:
if valid(cur): ans = max(ans, cnt)
return
cur.append(0)
dfs(cur, i+1, cnt)
cur[-1] = 1
dfs(cur, i+1, cnt+1)
cur.pop()
dfs([], 0, 0)
return ans
class Solution {
static int BAD= 0, GOOD= 1, UNKNOWN= 2;
public int maximumGood(int[][] statements) {
int n= statements.length;
int max= 0, combos= 1<<n;
int[] roles= new int[n];
// iterate through all combinations of good/bad people using bit masks
for(int mask=1; mask<combos; mask++){
int count= apply(roles, mask);
if(possible(statements, roles))
max= Math.max(max, count);
}
return max;
}
// convert bitmask to role array, count good people in this mask
// (this part is optional, you can operate on bitmasks directly, but I like it this way to make the checking function's code easy)
int apply(int[] roles, int mask){
int count= 0, n= roles.length;
for(int i=0; i<n; i++){
count+= roles[i]= mask & GOOD;
mask >>= 1;
}
return count;
}
// verify the n x n statement matrix against the current combination of roles
boolean possible(int[][] statements, int[] roles){
int n= statements.length;
for(int i=0; i<n; i++){
if(roles[i]==BAD) continue;
// only check statements of good people
for(int j=0; j<n; j++){
if(statements[i][j]==UNKNOWN) continue;
// statement of a good person contradicts the assigned role in this combination
if(statements[i][j]!=roles[j]) return false;
}
}
return true;
}
}
In our experience, we suggest you solve this Weekly Contest 277 LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the Weekly Contest 277 LeetCode Solution
I hope this Weekly Contest 277 LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>