# Daily Coding Challenge #46

This is a series of Daily Coding Challenge. Each day I show a few solutions written in C++. The questions are from coding practice/contest sites such as HackerRank, LeetCode, Codeforces, Atcoder and etc.

``````/*
LeetCode - Average Salary Excluding the Minimum and Maximum Salary

Given an array of unique integers salary where salary[i] is the salary of the employee i.

Return the average salary of employees excluding the minimum and maximum salary.

Example 1:

Input: salary = [4000,3000,1000,2000]
Output: 2500.00000
Explanation: Minimum salary and maximum salary are 1000 and 4000 respectively.
Average salary excluding minimum and maximum salary is (2000+3000)/2= 2500
Example 2:

Input: salary = [1000,2000,3000]
Output: 2000.00000
Explanation: Minimum salary and maximum salary are 1000 and 3000 respectively.
Average salary excluding minimum and maximum salary is (2000)/1= 2000
Example 3:

Input: salary = [6000,5000,4000,3000,2000,1000]
Output: 3500.00000
Example 4:

Input: salary = [8000,9000,2000,3000,6000,1000]
Output: 4750.00000

Constraints:

3 <= salary.length <= 100
10^3 <= salary[i] <= 10^6
salary[i] is unique.
Answers within 10^-5 of the actual value will be accepted as correct.
*/

class Solution {
public:
double average(vector<int>& salary) {
int i=0, ans=0, mx=0, mn=1e9, n=(int)salary.size();
for(int i=0;i<n;i++){
// find the min, max, and total value
mx=max(mx,salary[i]);
mn=min(mn,salary[i]);
ans+=salary[i];
}
// find the avg excluding min & max value
return (double)(ans-mn-mx)/(n-2);
}
};
``````

``````/*
LeetCode - The kth Factor of n

Given two positive integers n and k.

A factor of an integer n is defined as an integer i where n % i == 0.

Consider a list of all factors of n sorted in ascending order, return the kth factor in this list or return -1 if n has less than k factors.

Example 1:

Input: n = 12, k = 3
Output: 3
Explanation: Factors list is [1, 2, 3, 4, 6, 12], the 3rd factor is 3.
Example 2:

Input: n = 7, k = 2
Output: 7
Explanation: Factors list is [1, 7], the 2nd factor is 7.
Example 3:

Input: n = 4, k = 4
Output: -1
Explanation: Factors list is [1, 2, 4], there is only 3 factors. We should return -1.
Example 4:

Input: n = 1, k = 1
Output: 1
Explanation: Factors list is , the 1st factor is 1.
Example 5:

Input: n = 1000, k = 3
Output: 4
Explanation: Factors list is [1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 125, 200, 250, 500, 1000].

Constraints:

1 <= k <= n <= 1000
*/

class Solution {
public:
int kthFactor(int n, int k) {
// since the constraints are quite low so this solution would work
for(int i=1;i<=n/2;i++){
if(n%i==0){
// found a factor
k--;
// found the k-th one
if(k==0) return i;
}
}
// if k is 1, the last divisor is n itself
return k==1?n:-1;
}
};
``````

``````/*
LeetCode - Longest Subarray of 1's After Deleting One Element

Given a binary array nums, you should delete one element from it.

Return the size of the longest non-empty subarray containing only 1's in the resulting array.

Return 0 if there is no such subarray.

Example 1:

Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:

Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:

Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:

Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:

Input: nums = [0,0,0]
Output: 0

Constraints:

1 <= nums.length <= 10^5
nums[i] is either 0 or 1.
*/

class Solution {
public:
int longestSubarray(vector<int>& arr) {
// approach:
// count contiguous 1s before every index from the left & right side
// find the max of the sum of l[i] & r[i]
int n=arr.size();
int l[n],r[n];
int one=0,res=0;
for (int i=0; i<n; i++){
l[i]=one;
if (arr[i]==1) one++;
else one = 0;
}
one = 0;
for (int i=n-1; i>=0; i--) {
r[i] = one;
if (arr[i]==1) one++;
else one = 0;
}
for (int i=0;i<n;i++)
res = max(res,l[i]+r[i]);

return res;
}
};

//     0 1 1 1 0 1 1 0 1
// l   0 0 1 2 3 0 1 2 0
// r   3 2 1 0 2 1 0 1 0
// --------------------------
//     3 3 3 3 5 2 2 3 1
``````

The source code is available in corresponding repo below. Star and watch for timely updates!

## wingkwong / atcoder

### Discussion   