Count Elements in Two Arrays | GFG Solution | Searching and Sorting
Vložit
- čas přidán 9. 06. 2021
- Please consume this content on nados.pepcoding.com for a richer experience. It is necessary to solve the questions while watching videos, nados.pepcoding.com enables that.
NADOS also enables doubt support, career opportunities and contests besides free of charge content for learning. Question Statement:
Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[].
Topic: #BinarySearch #GFG #SearchingAndSorting
Used #DataStructure: #Array
#TimeComplexity: O(nlog(m)) , where n is the size of first array and m is the size of second array.
#SpaceComplexity: O(1)
----------------------------------------------------------------
For detailed information and other exercises, VISIT: www.pepcoding.com
Have a look at our result: www.pepcoding.com/placements
Follow us on our FB page: / pepcoding
Follow us on Instagram: / pepcoding
Follow us on LinkedIn: / pepcoding-education
----------------------------------------------------------------
#Array #leetcode #GFG #SearchingAndSorting #BinarySearch
For a better experience and more exercises, VISIT: www.pepcoding.com/resources/
Have a look at our result: www.pepcoding.com/placements
Follow us on our CZcams page: / pepcoding
Follow us on our FB page: / pepcoding
Follow us on Instagram: / pepcoding
Follow us on LinkedIn: / pepcoding-education
Follow us on Pinterest: / _created
Follow us on Twitter: home
.
.
.
Happy Programming !!! Pep it up 😍🤩
.
.
.
#pepcoding #code #coder #codinglife #programming #coding #java #freeresources #datastrucutres #pepcode #competitive #competitiveprogramming #softwareengineer #engineering #engineer
The Prefix Sum and Frequency technique is quite intuitive. Thanks for the explanation.
Quality explanation mam cleared all the concepts.
More solution vids from Manish Ma'am 🙌🙌🙌🙌🙌🙌
Great explanation ma'am ❤❤
super madam
My mind was blown away by second approach
Best👌👌
Glad you liked it!
Keep learning.
And for better experience and well curated content explore nados.pepcoding.com
Thank you so much maam
Most welcome 😊
NEXT LVL
I am glad you liked it. I also hope that you are watching till end and trying to understand the what, how and especially why of the problem.
If you like our efforts, we request a review
g.page/Pepcoding/review?rc
You can subscribe to our channel here
czcams.com/users/Pepcodingabout?view_as=subscriber
in binary search can we find the last occurance of the given el and then add (last occ + 1) in count
we don't know the ele exist or not ,we will find last occ of
binary search wali approach me Arrays.sort ka (mlog(m)) bhi count hoga
it will be O(nlogm + mlogm) and we will consider higher one
Just another approach :
vector getCount(vector A, vector B)
{
vector C=A;
sort(A.begin(),A.end());
sort(B.begin(),B.end());
int i=0, j=0, m=A.size(), n=B.size();
unordered_map mp;
while(i
Mam or kitni videos aaegi iss series me ?
More questions atleast 50 more
Ma'am, would Freq array method work if both the arrays contains negative integers ?
No it won't work
Man aapake videos ki special playlist banaye
maam just becos of ur teaching i jv subscribed this channel
keep motivating, keep learning and keep loving Pepcoding😊
Mam we can also take psa array's length as max value of arr1. because we can search for that value at max in psa. Plz tell if I'm wrong
yes we can take psa length as max value from arr1 or arr2(whichever is max) + 1(for index handling)
More Optimized Approch
.
.
int[] ans = new int[arr1.length];
int max1 = Integer.MIN_VALUE;
for(int i : arr1){
max1 = Math.max(max1,i);
}
int max2 = Integer.MIN_VALUE;
for(int i : arr2){
max2 = Math.max(max2,i);
}
int omax = Math.max(max1,max2);
int[]pre = new int[omax + 1];
for(int i = 0 ; i < arr2.length ; i++){
int key = arr2[i];
pre[key] += 1;
}
for(int i = 1 ; i < pre.length; i++){
pre[i] += pre[i-1];
}
for(int i = 0 ; i < ans.length ; i++){
ans[i] = pre[arr1[i]];
}
return ans;
.
.
.