Job Roles: Backend Developer, Frontend Developer
Avg CTC: 22.4LPA
Apply here: Current Job Openings
Given a string s, find the length of the longest substring without repeating characters.
Constraints:
Examples:
Input 1: s = "abcabcbb"
Output 1: 3
Explanation: The answer is "abc", with the length of 3.
Input 2: s = "bbbbb"
Output 2: 1
Explanation: The answer is "b", with the length of 1.
Input 3: s = "pwwkew"
Output 3: 3
Explanation: The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
Return Probability of Palindromes for 'n' dice throws where each throw can give result 1 to 6.
For Ex. if n=3 then (111),(121),(131),(141),(151),(161),(212),...(666) = 36; total permutations is 216 hence probability is 36/216
Find number of sub array combinations A[i],A[j] in array A and an int X with the following conditions,
i*j is divisible by X
we use 1-index here;
Example:
A={2,2,2}, X=2
Combinations if i,j are {1,2},{1,3} and {2,3}
Divisible by X are {1,2} and {2,3}
Hence number of combinations = 2.
Overview
Solution
Code
ll n;
cin >> n;
string s;
cin >> s;
map<char, ll> mp;
for (ll i = 0; i < n; i++)
{
    mp[s[i]] = -1; // initialise with -1
}
ll ans = 0;
ll last_visisted_index = 0;
for (ll i = 0; i < n; i++)
{
    last_visisted_index = max(last_visisted_index, mp[s[i]] + 1); // update the last visited vertex
    ans = max(ans, i - last_visisted_index + 1);                  // update the answer
    mp[s[i]] = i;                                                 // update the most recent index for a charater
}
cout << ans << endl; // print the answer
return;
Let’s maintain a hashtable,count[v][f], which would store the count of how many indexes having a certain value v is divisible by f.
Now, let's iterate, and then from each index i, the other index should be divisible by - X/gcd(X, i), therefore, we will increment the count by count[A[i]][X/gcd(X, i)]. After counting from each index, we will just update the table and that requires each of us to iterate on all factors of the current index and just increment by 1 count[A[i]][factor].
Updating requires sqrt(n) time and the table could be represented by an ordered or unordered map, and the final time complexity should be O(n*sqrt(n)) or with an extra log with the ordered map.
    int ans = 0;
    map<int, map<int, int>> count;
    for (int i = 0; i < n; i++) {
        if (count.find(arr[i]) != count.end()) {
            int q = x / __gcd(x, i + 1);
            ans += count[arr[i]][q];
        }
        else {
            map<int, int>mm;
            count[arr[i]] = mm;
        }
        addDivisors(i + 1, count[arr[i]]);         //sqrt(n) operation
    }
    cout << ans << endl;
Q1. very simple standard Sliding window question
approach:
maintain two pointer start and end
traverse through string and hash elements (expand window as we want largest substring) and whenever any charcater frequency is greater than 1 Shrink window
and update our answer in each step as max(ans,end-start+1)
thats it :)
n dice throws where each throw can give results 1 to 6.n digit numbers is 6n.n/2 digits match with the last n/2 digits in reverse order.n/2 digits and then duplicate them to form the rest of n/2 digits so we can choose n/2 digits.(n-1)/2 digits and then duplicate them to form the rest of (n-1)/2 digits so we can choose (n+1)/2 digits.n digit number is a palindrome is 6ceil(n/2) / 6n or 1 / 6floor(n/2)Time Complexity: O(N), N is the number of dice throws.
Memory Complexity: O(1)
Neat idea!
Kudos on passing all the test cases ! :)