Problem Statement:
You are given an array of integers and a number d. Select a group of three elements where the difference between the largest and smallest values in the group is at most d.
Return the total number of such groups, modulo (10^9 + 7).
Example 1:
Input: arr = [-3, -2, -1, 0], d = 2
Output: 2
Explanation:
All Valid Triplets:
Elements: [-3, -2, -1] Maximum Difference: (-1) - (-3) = 2
Elements: [-2, -1, 0] Maximum Difference: 0 - (-2) = 2
Example 2:
Input: arr = [2, 1, 3, 4], d = 3
Output: 4
Explanation:
All Valid Triplets:
Elements: [2, 1, 3] Maximum Difference: 3 - 1 = 2
Elements: [2, 1, 4] Maximum Difference: 4 - 1 = 3
Elements: [2, 3, 4] Maximum Difference: 4 - 2 = 2
Elements: [1, 3, 4] Maximum Difference: 4 - 1 = 3
Problem Statement:
A palindrome is a string that reads the same forwards and backwards, such as 121 or tacocat. A substring is a continuous sequence of characters within a string.
Given a string s, how many unique substrings of s are palindromes?
Example:
s = "mokkori"
Some of its substrings are [m, o, k, r, i, mo, ok, mok, okk, kk, okko].
There are 7 distinct palindromes: [m, o, k, r, i, kk, okko].
Function Description:
Complete the function palindrome in the editor.
palindrome has the following parameter(s):
string s: a string
Returns:
int: the number of distinct palindromes
Problem1 (Triplets Within Range) Solution -:
Topics -: Sorting,Sliding Window / Two Pointers, Combinatorics (nCr)
Sorting the array first allows us to easily lock in the smallest element of our potential triplet and search for valid maximum boundaries. By using a sliding window, we can efficiently find exactly how many subsequent numbers fall within the allowed difference d from our starting number. Once that boundary is found, choosing the remaining two triplet members from the available numbers is just a simple combinatorial math calculation.
Approach
1. Sorting and the Sliding Window Because we are picking a group of three numbers where the order doesn't matter, we should sort the array first. We iterate through the array, treating each number arr[i] as the absolute smallest number in our triplet. We then push a second pointer, j, forward until the difference between arr[j] and arr[i] exceeds d.
2. Calculating the Combinations When the j pointer stops, every number strictly between index i and index j - 1 is a valid candidate for the triplet. If we have count numbers available in this window, we just need to pick exactly 2 of them to complete the triplet with arr[i]. The mathematical formula to choose 2 items from a pool is count * (count - 1) / 2. We add this to our total sum, applying the modulo at each step to prevent overflow.
Time Complexity
Space: O(U) - Where U is the number of unique wallets participating in transactions that must be held in memory for sorting and returning.
Problem2
Sub-Palindrome Solution
Topics Involved / Prerequisites
Overview
A palindrome perfectly mirrors itself from its center, meaning we can find them all by treating every character and the space between characters as a midpoint.
As we expand outwards from each midpoint, we extract the valid palindromic strings and store them in a hash set.
Because hash sets automatically filter out duplicates, the final size of the set gives us the exact count of uniquely distinct palindromes.
Approach
1. Expanding Around the Center
There are 2N - 1 possible centers in a string (every single character can be a center for odd-length palindromes, and the space between every pair of adjacent characters can be a center for even-length palindromes). We iterate through the string and use two pointers (left and right) to expand outwards from these centers as long as the characters match.
2. Extracting and Storing
Every time we successfully expand and find matching characters, the string between left and right is a valid palindrome! We extract that substring and insert it directly into a Hash Set. If we find the exact same palindrome later in the string, the Hash Set will simply ignore it. Once we check all centers, the number of elements in the Hash Set is our final answer.
Time Complexity
Code Implementation (Pseudocode)