Maximum Count of Positive Integer and Negative Integer | Leetcode 2529 | codestorywithMIK

preview_player
Показать описание


Hi Everyone, this is the 8th video of our Playlist "Solving only using C++ STLs/JAVA JCF".
Now we will be solving a very easy problem - Maximum Count of Positive Integer and Negative Integer | Learn Something New | Leetcode 2529 | codestorywithMIK

Since this is a very easy problem, I have explained how to do it using STLs/JCF only. We will learn a lot of things from this video.

Problem Name : Maximum Count of Positive Integer and Negative Integer | Learn Something New | Leetcode 2529 | codestorywithMIK
Company Tags : will update later

╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

Video Summary :
Approach 1 (Using STL count_if)
This approach counts positive and negative numbers separately using the count_if function with lambda expressions. It then returns the maximum of the two counts, ensuring we find the dominant sign in the array.

Approach 2 (Using lower_bound)
This approach leverages binary search to find the first occurrence of 1 (for positives) and 0 (for non-negatives). Using these indices, it determines the count of positive and negative numbers efficiently and returns the maximum of the two.

✨ Timelines✨
00:00 - Introduction
0:18 - Motivation
0:40 - Problem Explanation
1:34 - C++ count_if()
5:14 - JAVA stream and filters
12:49 - Coding C++ & Java using count
16:29 - Follow Up using Binary Search
18:19 - C++ lowerbound
23:15 - Java apnaLowerBound
27:03 - Coding C++ Java follow up

#MIK #mik #Mik
#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #coding #programming #100daysofcode #developers #techjobs #datastructures #algorithms #webdevelopment #softwareengineering #computerscience #pythoncoding #codinglife #coderlife #javascript #datascience #leetcode #leetcodesolutions #leetcodedailychallenge #codinginterview #interviewprep #technicalinterview #interviewtips #interviewquestions #codingchallenges #interviewready #dsa #hindi #india #hindicoding #hindiprogramming #hindiexplanation #hindidevelopers #hinditech #hindilearning #helpajobseeker #jobseekers #jobsearchtips #careergoals #careerdevelopment #jobhunt #jobinterview #github #designthinking #learningtogether #growthmindset #digitalcontent #techcontent #socialmediagrowth #contentcreation #instagramreels #videomarketing #codestorywithmik #codestorywithmick #codestorywithmikc #codestorywitmik #codestorywthmik #codstorywithmik #codestorywihmik #codestorywithmiik #codeistorywithmik #codestorywithmk #codestorywitmick #codestorymik #codestorwithmik
Рекомендации по теме
Комментарии
Автор

thanks
FYI predicate means it returns a boolean either true or false
and we can use lambda exp in java like this only for functional interfaces, i.e. interfaces having only one abstract method....

class Solution {
public int maximumCount(int[] nums) {
int pos = -> x > 0).count();
int neg = -> x < 0).count();

return Math.max(pos, neg);
}
}

aizadiqbal
Автор

Every day, there is something great to learn from your videos. The way you explain things is unmatched...

PriyanshiSingh-zd
Автор

I solved myself, 100% beats. Thank U MIK😇

hareesh_sureddy
Автор

apke wajh se 2 min me solve ho gaya khud se still watching the video to learn other approaches ❤

ItsMeCaliber
Автор

thank you sir!! pehle khud solve kia, fir apke video pe ayi hu. apka video dekhna toh ab default setting hogya

s..
Автор

Mai to khush hogaya tha itna easy qn dekh kar. here this legend is teaching to solve this in different ways 🥲🥲🥲

coderbanda-ps
Автор

Thank you bhiya even i solve the question but every time i learn new thing from your videos new concepts, new technique and from 2 years i follow your channel and every single day i learn new thing, i learn how to think differently how to approach a problem i learn even an easy problem can help to learn new thing thank you bhaiya and keep on going 🔥🔥🔥.

vihal-qp
Автор

Easy Qns me bhi kuch naya sikha dete ho aap.
This is Legend MIK 🔥🔥🔥🔥

gui-codes
Автор

Bhaiya recently I watched your count of substrings having vowels and k consonants video. The way you did dry run of your logic, it was literally mind blowing. I think you should be awarded as the best DSA teacher on the planet ❤🙌🙌😊

jeehub
Автор

Bhai ajj mere se phali bar 2min me solve hua h i am shocked 😯😯😯😯

anuragtiwari
Автор

Solved it on my own using custom binary search approach. All thanks to MIK, I have started to build my own intuition in questions🔥🔥. Below is the solution:

class Solution {
public:

int solve(vector<int>& nums, int n)
{
int low = 0, high = n - 1, start = -1, end = -1;

while(low <= high)
{
int mid = low + (high - low) / 2;

if(nums[mid] > 0)
{
start = mid;
if(end == -1 || end <= high)
{
end = high;
}
high = mid - 1;
}
else
{
low = mid + 1;
}
}

return (start == -1 && end == -1) ? -1 : end - start + 1;
}

int solve2(vector<int>& nums, int n)
{
int low = 0, high = n - 1, start = -1, end = -1;

while(low <= high)
{
int mid = low + (high - low) / 2;

if(nums[mid] < 0)
{
end = mid;
if(start == -1 || start >= low)
{
start = low;
}
low = mid + 1;
}
else
{
high = mid - 1;
}
}

return (start == -1 && end == -1) ? -1 : end - start + 1;
}

int maximumCount(vector<int>& nums) {
int pos = 0, neg = 0, n = nums.size();

pos = solve(nums, n);
neg = solve2(nums, n);
return (pos == -1 && neg == -1) ? 0 : max(pos, neg);
}
};

aviralkaushal
Автор

Binary search approach came out of nowhere(I know its obvious, still I missed out the fact that the array is sorted. It highlights the fact that we must read the Q carefully). Thanks Mik❤️

souviksarkar
Автор

sir mene khud se binary search implement kr diya tha, btw stl function new tha mere liye, thank u

bhupendrakalal
Автор

class Solution {
public:
int maximumCount(vector<int>& nums) {
unordered_map<int, int> countmap;

for(int num:nums){
if(num<0){
countmap[-1]++;
}else if(num>0){
countmap[1]++;
}
}
return max(countmap[-1], countmap[1]);
}
}; solve it using maps ----> beats 100% runtime

Arcgamerz
Автор

today's (13th march) POTD I have solved it using line sweep, as you teach earlier, thanks

varunpalsingh
Автор

class Solution {
public int maximumCount(int[] arr) {
int n=arr.length;
int zero=0;
for(int i=0;i<n;i++){
if(arr[i]==0) zero++;
}
int po=0;
int ne=0;
int lo=0;
int hi=n-1;
while(lo<=hi){
int mid=lo+(hi-lo)/2;
if(arr[mid]>0 || arr[mid]==0){
po=po+((hi-mid)+1);
hi=mid-1;
}else if(arr[mid]<0){
ne=ne+((mid-lo)+1);
lo=mid+1;

}
}
return Math.max((po-zero), ne);

}
}

arabindaparida
Автор

Day 27 First time submitted in first go🤓

ManaswiRane
Автор

since the array is sorted it is obvious they want u to use binary search, we have to only count the last position of negative and first position of positive to count the frequency of these +ve and -ve numbers and return the max of it.
class Solution {
public int maximumCount(int[] nums) {
return sol(nums);
}
public int sol(int[] arr){
int l=0, r=arr.length-1;
while(l<=r){
int m=(l+r)/2;
if(arr[m]<=0){
l=m+1;

}
else{
r=m-1;
}

}
int pos=arr.length-l;
l=0;r=arr.length-1;
while(l<=r){
int m=(l+r)/2;
if(arr[m]>=0){
r=m-1;

}
else{
l=m+1;
}

}
int neg=l;
return Math.max(neg, pos);
}
}

sakakibara-mu
Автор

pos, neg = 0, 0
l, r = 0, len(nums) - 1
while l <= r:
mid = (l+r)//2
if nums[mid] < 0:
neg += (mid-l+1)
l = mid + 1
elif nums[mid] > 0:
pos += (r-mid+1)
r = mid - 1
else:
if nums[mid-1] < 0:
neg += (mid-l)
l = mid + 1
elif nums[mid-1] == 0:
l = mid + 1
elif nums[mid+1] > 0:
pos += (r-mid+1)
r = mid - 1
elif nums[mid+1] == 0:
r = mid - 1
return max(neg, pos)

nikhilnayak
Автор

Thank you for explaining things so clearly! This question may be simple, but the way you break down each concept is truly commendable and greatly appreciated. Is it acceptable to use STL in interviews? Is it considered good or bad practice?

lokeshpatidar
join shbcf.ru