[Hacker Earth] IRCTCRead more
[Hacker Earth] Little Jhool and his PrimesRead more
[Hacker Earth] Sherlock and MagicRead more
[LeetCode] Minimum Moves to Pick K Ones

3086. Minimum Moves to Pick K Ones

You are given a 0-indexed binary array nums of length n, a positive integer k and a non-negative integer maxChanges.

Dylan Smith plays a game, where the goal is for Dylan to pick up k ones from nums using the minimum number of moves. When the game starts, Dylan picks up any index dylanIndex in the range [0, n - 1] and stands there. If nums[dylanIndex] == 1 , Dylan picks up the one and nums[dylanIndex] becomes 0(this does not count as a move). After this, Dylan can make any number of moves (including zero) where in each move Dylan must perform exactly one of the following actions:

  • Select any index j != dylanIndex such that nums[j] == 0 and set nums[j] = 1. This action can be performed at most maxChanges times.
  • Select any two adjacent indices x and y (|x - y| == 1) such that nums[x] == 1, nums[y] == 0, then swap their values (set nums[y] = 1 and nums[x] = 0). If y == dylanIndex, Dylan picks up the one after this move and nums[y] becomes 0.

Return the minimum number of moves required by Dylan to pick exactly k ones.

Read more
[LeetCode] Minimum Deletions to Make String K-Special

3085. Minimum Deletions to Make String K-Special

You are given a string word and an integer k.

We consider word to be k-special if |freq(word[i]) - freq(word[j])| <= k for all indices i and j in the string.

Here, freq(x) denotes the frequency of the character x in word, and |y| denotes the absolute value of y.

Return the minimum number of characters you need to delete to make word k-special\.

Read more
[LeetCode] Count Substrings Starting and Ending with Given Character

3084. Count Substrings Starting and Ending with Given Character

You are given a string s and a character c. Return the total number of substrings of s that start and end with c.

Read more
[LeetCode] Existence of a Substring in a String and Its Reverse

3083. Existence of a Substring in a String and Its Reverse

Given a string s, find any substring of length 2 which is also present in the reverse of s.

Return true if such a substring exists, and false otherwise.

Read more
[LeetCode] Find the Sum of the Power of All Subsequences

3082. Find the Sum of the Power of All Subsequences

  • User Accepted:924
  • User Tried:1995
  • Total Accepted:987
  • Total Submissions:4186
  • Difficulty:**Hard**

You are given an integer array nums of length n and a positive integer k.

The power of an array of integers is defined as the number of subsequences with their sum equal to k.

Return the sum of power of all subsequences of nums.

Since the answer may be very large, return it modulo 109 + 7.

Read more
[LeetCode] Replace Question Marks in String to Minimize Its Value

3081. Replace Question Marks in String to Minimize Its Value

You are given a string s. s[i] is either a lowercase English letter or '?'.

For a string t having length m containing only lowercase English letters, we define the function cost(i) for an index i as the number of characters equal to t[i] that appeared before it, i.e. in the range [0, i - 1].

The value of t is the sum of cost(i) for all indices i.

For example, for the string t = "aab":

  • cost(0) = 0
  • cost(1) = 1
  • cost(2) = 0
  • Hence, the value of "aab" is 0 + 1 + 0 = 1.

Your task is to replace all occurrences of '?' in s with any lowercase English letter so that the value of s is minimized.

Return a string denoting the modified string with replaced occurrences of '?'. If there are multiple strings resulting in the minimum value, return the lexicographically smallest one.

Read more
[LeetCode] Mark Elements on Array by Performing Queries

3080. Mark Elements on Array by Performing Queries

You are given a 0-indexed array nums of size n consisting of positive integers.

You are also given a 2D array queries of size m where queries[i] = [indexi, ki].

Initially all elements of the array are unmarked.

You need to apply m queries on the array in order, where on the ith query you do the following:

  • Mark the element at index indexi if it is not already marked.
  • Then mark ki unmarked elements in the array with the smallest values. If multiple such elements exist, mark the ones with the smallest indices. And if less than ki unmarked elements exist, then mark all of them.

Return an array answer of size m where answer[i] is the sum of unmarked elements in the array after the ith query.

Read more