My approach was to figure out using the first two unique numbers if the array is increasing or decreasing, if its increasing, that means new numbers must not be less than the last number and if its a decreasing array, then subsequent numbers must not be greater than the previous number. Based on this conditions, we are able to do an early false return for non-monotonic arrays, and if all checks, we return a true
Used the editorial - Editorial
The approach was to find the factors of each num in the nums array, did this by iterating from 1 to sqrt(num), then used a set to store the factors (to avoid repetition in the case of perfect squares), then we check for the number with factors size of 4, and add the sum of all the 4 factors to the res value.
class Solution {
public:
int sumFourDivisors(vector<int>& nums) {
int res = 0;Iterate through the nums array, and using an hashmap, we count the frequency of each number, as soon as we encounter a number with frequency more than 1, we return that number and exit the loop , as that's the non-unique number
class Solution {
public:
int repeatedNTimes(vector& nums) {my intuitive approach was basically to join all the digits together, convert to a number, (using BigInt) to handle big numbers, split the resulting string into characters and map over the array of characters to convert the individual characters to number.
More optimal approach was to do the addition manually, start iteration from the end of the digits array, check if the current digit is less than 9, if yes, add one to the digit and return the entire digits array, if the number is 9, we want to change it to 0 and continue the iteration, till we get a number that is less than 9, to bear the carry. And at the end of the iteration, if we've not exited the function, meaning all digits were 9, we insert a 1 at the beginning of our new digits array, which has just digit zero.
# approach i
var plusOne = function(digits) {Didn't realize at first that each elements in the nums array has to be unique after performing an operation. My first step was to build a mod array to store the least non-negative number that can be formed from each nums element. Then sorted the mod array, if first element in the sorted array isn't 0, then 0 is our smallest mex.
For cases of repeting numbers in our mod array, we build a frequency map, with the frequencies being "0-indexed", then I modifed the mod array elements using their freq. count, (adding value * freq to the mod element).
Then a second sort, to keep the elements in the mod array in increasing order, to make iteration easy.
I assembled every individual increasing set of elements into an array, arr, if all elements in the nums array are increasing, then size of arr will be 1, then we can return the result of the floor division of the length by 2, to give the maximum number of elements in each adjacent increasing group.
For an array with > 1 increasing groups, we iterate over the set of arrays, first we check for the possible adjacent grouping we can get from the current increasing group (max(res, len/2);), then we compare the adjacent increasing groups as well, picking the min of them, to ensure we have the maximum equal number of elemnts from each group.
For each computation, we keep the max value, which at the end gives the maximum possible valaue of elements that satisfy the adjacent incresing group rule.
First step was to first group all increasing elements into their own array, then iterate through the groups, to see if a single group could form adjacent subarrays with increasing elemnts (len / k >= 2), if not, we check with the adjacent group (len >= k && nxt_len >= k) and if neither of these exist across the group arrays, return false
var hasIncreasingSubarrays = function(nums, k) {
let arr = []First step was to sort each word in the words array, and store that in a new array. Then loop through the sorted words array, adding the original word to the result array, then comparing adjacent elements, for elements with same set of characters (anagrams), till we get where the words no longer repeat, meaning we've reached a unique word, and then continue the loop until the end of the words array
class Solution {
public:My approach was to loop through the nums arr and check the equality of the parities of adjacent elements, so it checks if elements at i and i+1 are either both even or both odd
class Solution {
public:
bool isArraySpecial(vector& nums) {