Leetcode with dani – Telegram
Leetcode with dani
1.31K subscribers
196 photos
14 videos
56 files
240 links
Join us and let's tackle leet code questions together: improve your problem-solving skills
Preparing for coding interviews
learning new algorithms and data structures
connect with other coding enthusiasts
Download Telegram
Forwarded from Leetcode with dani
another interview question

You are given a 0-indexed integer array nums.
Swaps of adjacent elements are able to be performed on nums.
A valid array meets the following conditions:
The largest element (any of the largest elements if there are multiple) is at the rightmost position in the array.
The smallest element (any of the smallest elements if there are multiple) is at the leftmost position in the array.
Return the minimum swaps required to make nums a valid array.

Example 1:
Input: nums = [3,4,5,5,3,1]
Output: 6
Explanation: Perform the following swaps:
- Swap 1: Swap the 3rd and 4th elements, nums is then [3,4,5,3,5,1].
- Swap 2: Swap the 4th and 5th elements, nums is then [3,4,5,3,1,5].
- Swap 3: Swap the 3rd and 4th elements, nums is then [3,4,5,1,3,5].
- Swap 4: Swap the 2nd and 3rd elements, nums is then [3,4,1,5,3,5].
- Swap 5: Swap the 1st and 2nd elements, nums is then [3,1,4,5,3,5].
- Swap 6: Swap the 0th and 1st elements, nums is then [1,3,4,5,3,5].
It can be shown that 6 swaps is the minimum swaps required to make a valid array.

Example 2:
Input: nums = [9]
Output: 0
Explanation: The array is already valid, so we return 0.




Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^5
Forwarded from typing.......
You are given a 0-indexed string s consisting of only lowercase English letters, and an integer count. A substring of s is said to be an equal count substring if, for each unique letter in the substring, it appears exactly count times in the substring.

Return the number of equal count substrings in s.

A substring is a contiguous non-empty sequence of characters within a string.



Example 1:

Input: s = "aaabcbbcc", count = 3
Output: 3
Explanation:
The substring that starts at index 0 and ends at index 2 is "aaa".
The letter 'a' in the substring appears exactly 3 times.
The substring that starts at index 3 and ends at index 8 is "bcbbcc".
The letters 'b' and 'c' in the substring appear exactly 3 times.
The substring that starts at index 0 and ends at index 8 is "aaabcbbcc".
The letters 'a', 'b', and 'c' in the substring appear exactly 3 times.
Example 2:

Input: s = "abcd", count = 2
Output: 0
Explanation:
The number of times each letter appears in s is less than count.
Therefore, no substrings in s are equal count substrings, so return 0.
Example 3:

Input: s = "a", count = 5
Output: 0
Explanation:
The number of times each letter appears in s is less than count.
Therefore, no substrings in s are equal count substrings, so return 0
Forwarded from Leetcode with dani
You are given a sorted array nums of size n and an integer target. 
Your task is to find the pair of numbers in the array whose sum is less than or equal to the target and has the largest possible sum.

Return the maximum sum of such a pair. If no valid pair exists, return -1.

Example 1:
Input: nums = [2, 3, 5, 8, 13], target = 10
Output: 10
l
[2, 3, 5, 8, 13],
r
[7,8 , 10]
Explanation: The pair (2, 8) has a sum of 10, which is the largest possible sum less than or equal to 10.

Example 2:
Input: nums = [1, 1, 1, 1], target = 1
Output: -1
Explanation: No pair has a sum less than or equal to 1.

Constraints:
1<=n<=10⁵
Nums is sorted in non-decreasing order.



def targetSum(nums , target):
left = 0
max_ = -1
right = len(nums)-1
while left < right:
sum = nums[left] + nums[right]
if sum == target:
return sum
elif sum > target:
right -=1
elif sum < target:
left += 1
max_ = max(max_,sum)
return max_
Forwarded from Leetcode with dani
"""
Given a binary array nums, return the maximum number of consecutive 1's in the array.



Example 1:
Input: nums = [1,1,0,1,1,1]
Output: 3
Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.

Example 2:
Input: nums = [1,0,1,1,0,1]
Output: 2


Constraints:

1 <= nums.length <= 105
nums[i] is either 0 or 1.

"""
Forwarded from Maki Asrat
The is the interview question I just had:

Given a binary array nums, return the maximum number of consecutive 1's in the array if you can flip at most one 0.

Example 1:
Input: nums = [1,0,1,1,0]
Output: 4
Explanation:
- If we flip the first zero, nums becomes [1,1,1,1,0] and we have 4 consecutive ones.
- If we flip the second zero, nums becomes [1,0,1,1,1] and we have 3 consecutive ones.
The max number of consecutive ones is 4.

max_cont= 4
max_count= 3
max_count =4
[1,0,1,1,0]

Example 2:
Input: nums = [1,0,1,1,0,1]
Output: 4
Explanation:
- If we flip the first zero, nums becomes [1,1,1,1,0,1] and we have 4 consecutive ones.
- If we flip the second zero, nums becomes [1,0,1,1,1,1] and we have 4 consecutive ones.
The max number of consecutive ones is 4.


Constraints:

1 <= nums.length <= 105
nums[i] is either 0 or 1.
7
Forwarded from ALX Ethiopia
🚨 Only 1 Day Left to Register!
Compete on Codeforces in Ethiopia’s National Competitive Programming Challenge!

💰 PRIZE POOL: 🥇 20,000 | 🥈 15,000 | 🥉 10,000 ETB 🏅 4th: 5,000 | 5th: 3,000 | Top 15: 1,000 Each

Note: All applications will be reviewed and only qualified candidates will receive an Official Acceptance Email to participate.

🗓 ቅዳሜ, ኅዳር 27 2018 (Dec 6, 2025)
3፡00 – 11፡00 ሰአት (9 AM – 5 PM)
📍 CapStone ALX Tech Hub, Lideta
🔗 Register: https://luma.com/9pobz8sd

⚠️ Each team member must register individually.

#ALXEthiopia #CodeLeagueEthiopia #CompetitiveProgramming #Codeforces #DSA #DoHardThings #ALXAfrica
You tube recap😂 I posted the good ones
But the 4th ranked music should be first
3😁3❤‍🔥1
If u want to share ur A2SV G7 interview questions u can send to me direct message by clicking the message icon on bottom right
screenshot.png
3
/* 
You have some apples and a basket that can carry up to 5000 units of weight.
Given an integer array weight where weight[i] is the weight of the ith apple,
return the maximum number of apples you can put in the basket.

Example 1:
Input: weight = [100,200,150,1000]
Output: 4
Explanation: All 4 apples can be carried by the basket since their sum of weights is 1450.

Example 2:
Input: weight = [900,950,800,1000,700,800]
Output: 5
Explanation: The sum of weights of the 6 apples exceeds 5000 so we choose any 5 of them.

*/

#include<bits/stdc++.h>
using namespace std;

int maxApples(vector<int> weight, int limit){

sort(weight.begin(), weight.end());
int count = 0, sum = 0, n = weight.size();


for(int i=0; i<n; i++){
sum+=weight[i];
if(sum>limit)break;
count++;
}

return count;
}


int main(){
vector<int> weight = {100,200,150,1000};
cout << maxApples(weight, 1450) << endl;

return 0;}
1
There are n windows open numbered from 1 to n, we want to simulate using alt + tab to navigate between the windows.
You are given an array windows which contains the initial order of the windows (the first element is at the top and the last one is at the bottom).
You are also given an array queries where for each query, the window queries[i] is brought to the top.
Return the final state of the array windows.

Example 1:
Input: windows = [1,2,3], queries = [3,3,2]
Output: [2,3,1]
Explanation:
Here is the window array after each query:
Initial order: [1,2,3]
After the first query: [3,1,2]
After the second query: [3,1,2]
After the last query: [2,3,1]
Example 2:
Input: windows = [1,4,2,3],

= [4,1,3]
Output: [3,1,4,2]
Explanation:
Here is the window array after each query:
Initial order: [1,4,2,3]
After the first query: [4,1,2,3]
After the second query: [1,4,2,3]
After the last query: [3,1,4,2]

Constraints:
1 <= n == windows.length <= 103
windows is a permutation of [1, n].
1 <= queries.length <= 103
1 <= queries[i] <= n

"""
Problem:
A string can be shortened by replacing any number of non-adjacent, non-empty substrings with their lengths (without leading zeros).

Examples of valid abbreviations:

"i12n" → (i mplementatio n)

"imp4n5n" → (imp leme n tatio n)

"14" → (implementation)

"implemetation" → (no substrings replaced)

Invalid abbreviations:

"i57n" → adjacent substrings replaced

"i012n" → leading zeros

"i0mplementation" → empty substring replaced

Your Task:

Given a word and an abbreviation abbr, return true if abbr correctly abbreviates word, otherwise return false.

A substring = a contiguous non-empty sequence of characters.

Examples

1.
Input: word = "apple", abbr = "a3e"
Output: true

2.
Input: word = "international", abbr = "i9l"
Output: false

3.
Input: word = "abbreviation", abbr = "abbreviation"
Output: true

Constraints

1 <= word.length <= 100

word contains only lowercase letters

1 <= abbr.length <= 100

abbr contains lowercase letters and digits

All digit substrings fit in a 32-bit integer #Leetcode_408_Premium
😨1