Relative Sort Array | Sort an array according to the other | Hashing | Leetcode DSA series | Hindi
Vložit
- čas přidán 27. 05. 2021
- This is the video under the series of DATA STRUCTURE & ALGORITHM in a HASHING Playlist. We are going to solve the problem "Relative Sort Array" from Leetcode which is solved by using a vector, map in c++.
Join My Telegram channel for more Updates: telegram.me/helloworldbyprince
Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 are also in arr1.
Sort the elements of arr1 such that the relative ordering of items in arr1 is the same as in arr2. Elements that don't appear in arr2 should be placed at the end of arr1 in ascending order.
Input: arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6]
Output:
[2,2,2,1,4,3,3,9,6,7,19]
We also Provide courses on Competitive Programming and Data structure and Algorithms. Please see our Full Playlist on our Channel.
----------------------------------------------------------------------------------------
Relative Sort Array: leetcode.com/problems/relativ...
Sort an array according to the other: practice.geeksforgeeks.org/pr...
Home Work ( Custom Sort String ): leetcode.com/problems/custom-...
code in This Video: github.com/Prince-1501/Hello_...
Pdf in this video: github.com/Prince-1501/Hello_...
----------------------------------------------------------------------------------------
*Follow me *
LinkedIn► / iamprince
Facebook► / helloworldofficials
Instagram► / helloworldbyprince
Twitter► / prince_king_
Telegram► telegram.me/helloworldbyprince
----------------------------------------------------------------------------------------
►Our Playlists on:-
►Competitive Programming: • How to start Competiti...
►C++ Full Course : • L-01 || Introduction a...
►Algorithms: • L-01 || Prefix Sum Arr...
►Data Structure: • Data Structures with C...
------------------------------------------------------------------------
🌟 Please leave a LIKE ❤️ and SUBSCRIBE for more AMAZING content! 🌟
✨ Tags ✨
how to solve 1122. Relative Sort Array
Sort an array according to the other
How to solve 791. Custom Sort String
question asked in Google
how to crack Goldman Sachs online test
how to crack Google Interview
off-campus placement
how to learn to code for beginners
Practice Hashing data structure
hashing in data structure
Best Telegram channel for Off-campus Placement drive
hashing in a data structure in Hindi
unordered_map
#hashing #geeksforgeeks #programming
Literally you're doing the great work!
And all those who are here and reading my comment, this series of hasing is going to help a lot in placements!!
✌🏻✌🏻
Thanks Devesh for your love and support
By seeing the question I thought mujhse nhi hoga lekin phir aapne bola ki try kro to thodi sharm aa gyi and I triied aur ho gya. Thanks bro
BHUT MAST PADHTEY HO .... MERE KO HASHING MEI BHUT CONFIDENCE AH GYA HA. Following the golden rule of this channel ..phele khudse!!!.
Thanks a lot buddy 😀😊😊😊
Thank you Prince Bhaijaan
Solved by own. Thank you so much bhaiyya.
I tried on my own and reached almost 90% problem , but was not able to push the remaining elements of array 1 to ans vector in ascending order , great video Prince bhai
Well done
same problem
Custom Sorting Question was done easily after I understood this question!! Thanks for the video man !!
Glad it helped!
biklul esa hi kiya tha , thanks for your efforts,HW done
I have done this before watching the video and submitted it with 0 ms. All thanks to you bhaiya💕
Great 👍
you are the best.......masum.
Thanks a lot
leetcode problem 791 is done and thanks for u encourage to first slove problem itself and then watch ur video and i'm not have that habits but now yes
keep learning like this and pls keep sharing
class Solution {
public:
string customSortString(string order, string s) {
unordered_map A;
string s1;
for(int i = 0 ; i< s.size();i++)
{
A[s[i]]++;
}
for(int i = 0 ; i< order.size();i++)
{
if(A.find(order[i])!=A.end())
{
for(int j = 0 ; j< A[order[i]];j++)
{
s1=s1+order[i] ;
}
A.erase(order[i]);
}
}
for(auto x : A)
{
for(int i = 0 ; i
Amazing Garima
whyyyyyyy.........tell us inplace or better approach. We need to change the vector arr1 itself, not use extra vector. Why take extra space................
Bro doing great job 🙏🙏
Thanks a lot binod
bhaiya what is the best way to solve dsa problems ... easy then meduim and then hard or mix ?
maza aa gaya bhaiya
Thanks brother 😀😀
Homework question solve ho gya bhaiya
nice work uday
Bhaiya bohot achha video tha ye ....maza aa gaya ..Ly ♥️
Thanks a ton
Sir , I have done your homework . Thank You
Great
i have a doubt . in a map all elements stored are in ascending order then in first loop we will get 1 first hence in output overall arr 2 elements will also be in ascending order since its a map. so how is ans correct?
4:33
YE BADI ACHI BAAT KAHI AAPNE (Paresh raaval)
Thanks ☺️☺️
Kabhi kabhi kuch jyada hi dialogue de dete hai
Tried it myself,as soon as it clicked ,I was over the moon.
Wohoo 😄😄
thank you bhai
welcome brother
great bro
Thanks buddy ❤️
5:02
Bhaiya your lecture is very very good...
Thanks bhai
15/18 done (11.12.22)
My approach
vector relativeSortArray(vector& arr1, vector& arr2) {
map mp;
int n = arr1.size();
int m = arr2.size();
vector ans;
for (int i=0 ; isecond;
while (v != 0){
ans.push_back(x);
v--;
}
}
return ans;
}
You are going great
@@HelloWorldbyprince thank you bhai
class Solution {
public:
vector relativeSortArray(vector& arr1, vector& arr2) {
map mp;
vector ans;
vectortemp;
int m = arr1.size();
int n = arr2.size();
for(int i=0; i first);
}
}
}
return ans;
}
};
vector relativeSortArray(vector& arr1, vector& arr2) {
sort(arr1.begin(),arr1.end());
mapumap;
for(int i=0;i
Amazing buddy ❤️
code is not working in GFG because of sorting
*******************GFG Code***************************
vector sortA1ByA2(vector A1, int N, vector A2, int M)
{unordered_mapumap;
vectorans;
for(int i=0;i
awesome
keep sharing these channel
I done it
nice
I solved it using unordered map
sir constraints dekhiye ..isko hm counting sort se bhi kar skte hai
//logic pehele hi a gaya tha but map looping ke indentation me thora problem a raha tha , but apne itne bar bola ki khud se try karo khud se solve karo , khud se dhoka matt karo, to mai ne bhi than liya ab to khud se hi karna hai and boom , first pass me ho gaya .sukriya dat neke liye
class Solution {
public:
vector relativeSortArray(vector& arr1, vector& arr2) {
mapmpp;
for(auto it:arr1){
mpp[it]++;
}
vectorv;
for(int i=0;i
Love ❤️ the attitude bro
class Solution {
public:
vector relativeSortArray(vector& arr1, vector& arr2) {
vector ans;
vector temp;
unordered_map mp;
for(int i=0;i
Amazing code bro
Hello bhaiya , i first found the size of the array and then applied the loop
int arr1_size, arr2_size;
arr1_size = sizeof(arr1)/sizeof(arr1[0]);
arr2_size = sizeof(arr2)/sizeof(arr2[0]);
for(int i =0;i
amazing code bro good work
done all
nice Please, share this channel in your college, groups, or LinkedIn bcoz it's cost nothing to you 😀
Bhaiya mujhe khud se question samj nii aata h but CZcams oe question samjne k baad me khud se jyadatar questions solve kar leta hu toh koi problem toh nhi h na
Placement m v yahi kroge kya
LEET CODE SOLUTION FOR 100 % fast result
vector temp;
map hash;
for(int i=0;i
Amazing 🤩🤩
Thanks for creating the video.
Easy to understand Solution:
---------------------------------------------
class Solution {
public:
vector relativeSortArray(vector& arr1, vector& arr2) {
map map;
vector res;
// Get the frequency of each letter of arr1 in a map
for(auto a: arr1)
map[a]++;
// For all the elements in arr2, get the actual freq
// and add it in the result, remove them from the map
for(auto a: arr2){
for(int i=0; i
keep learning and please share my channel
class Solution {
public:
string customSortString(string order, string s) {
unordered_mapmp;
string ans;
for(auto i:s)
{
mp[i]++;
}
for(auto i:order)
{
if(mp.find(i)!=mp.end())
{
auto x=mp.find(i);
int count=x->second;
char found=x->first;
while(count>0)
{
ans=ans+found;
count--;
}
mp.erase(i);
}
}
for(auto i:mp)
{
int count=i.second;
char found=i.first;
while(count>0)
{
ans=ans+found;
count--;
}
}
return ans;
}
};
good job
In Java use the TreeMap class and implement SortedMap interface
class Solution{
public static int[] sortA1ByA2(int A1[], int N, int A2[], int M)
{
SortedMap map = new TreeMap();
for(int i : A1){
map.put(i,map.getOrDefault(i,0)+1);
}
int index = 0; //start from the first index
for(int j : A2){
if(map.containsKey(j)){
int count = map.get(j);
while(count-- > 0){
A1[index++] = j;
}
map.remove(j);
}
}
for(int j:map.keySet()){
int count = (Integer)map.get(j);
while(count-- > 0){
A1[index++] = j;
}
}
return A1;
}
}
amazing bro
HOMEWORK: it was literally the same question LOL
class Solution {
public:
string customSortString(string order, string s) {
string ans;
//step 1
map mpp;
for(auto it: s)
mpp[it]++;
//step 2
for(int i=0;i0){
ans.push_back(order[i]);
count--;}
}
mpp.erase(order[i]);
}
//step 3
for(auto it: mpp){
int count=it.second;
while(count>0){
ans.push_back(it.first);
count--;}
}
return ans;
}
};
yhi to kah rahe the sab same hota hai basic samjhne ke baad
Please, if possible then share this channel in your college, groups, or LinkedIn bcoz it's cost nothing to you 😀
Yarr isme aapne jo second example liya h usme aur question me contradiction lag raha h kyoki question ne bola h ki arr2 ke sare elements arr1 me honge.
Home Work Solution :-
class Solution {
public:
string customSortString(string order, string s) {
// map the s string
// traverse order string if order char is present in map then push into ans
// now traverse s and push remain char into ans
map mp;
string ans = "";
for(int i=0; i
good work bro keep it up
Can you make video in english please really appreciate it
Enjoy the journey, while chasing the destination??
HOMEWORK QUESTION DONE
class Solution {
public:
string customSortString(string order, string s) {
mapmp;
string ans="";
//traverse in s
for(auto x:s){
mp[x]++;
}
//traverse in order
for(auto y:order){
if(mp.find(y)!=mp.end()){
auto it=mp.find(y);
int freq=it->second;
char ele=it->first;
string curr(freq,ele);
ans.insert(ans.end(),curr.begin(),curr.end());
mp.erase(y);
}
}
//traverse in mp for rest elements;
for(auto it:mp){
int freq=it.second;
char ele=it.first;
string curr(freq,ele);
ans.insert(ans.end(),curr.begin(),curr.end());
}
return ans;
}
};
Waoo
good work
home work prob done sir
Good 👍
TC: O(N)
SC: O(1)(constant)
public static int[] sortA1ByA2(int A1[], int N, int A2[], int M)
{
int []freq=new int[1000001];
for(int i: A1){
freq[i]++;
}
int ans[]=new int[N];
int k=0;
for(int i: A2){
while(freq[i]-->0){
ans[k++]=i;
}
}
for(int i=1;i0){
ans[k++]=i;
}
}
return ans;
}
Python solution(1122. Relative Sort Array)
from collections import defaultdict
d = defaultdict(int)
for i in arr1:
d[i] += 1
ans = []
for i in arr2:
if i in d:
ans.extend( [i] * d[i] )
del d[i]
for k,v in sorted(d.items()):
ans.extend( [k] * v )
return ans
Tree ke sawal kb krwaoge aap??
Ab Playlist start ho gyi hai
banda tabadtod hay
thanks Vinay keep learning and keep sharing my channel with your friends and in colleges
Relative Sort Array:-
class Solution {
public:
vector relativeSortArray(vector& arr1, vector& arr2) {
map mp;
vector ans;
// map arr1 array with their count value
for(int i=0; isecond;
while(value>0){
ans.push_back(key);
value--;
}
}
return ans;
}
};
Bina dekhe code likh diya vmro.....
class Solution {
public:
vector relativeSortArray(vector& arr1, vector& arr2) {
map mpp;
for(auto i:arr1)
mpp[i]++;
vector ans;
for(int i=0;i0){
ans.push_back(arr2[i]);
count--;}
mpp.erase(arr2[i]);
}
for(auto it:mpp){
int count=it.second;
while(count>0){
ans.push_back(it.first);
count--;}
}
return ans;
}
};
waooo yaar
Please, share this channel in your college, groups, or LinkedIn bcoz it's cost nothing to you 😀
bro pls explain in simple manner many time in this vedio u try to explain what we are doing in this line and then u forget to explain that line and u jump to next line for explaining
Which point you didn’t get ?
Koi bta skta hai kya ki java me kaise hoga?
Kaash Mai bataa paata
Yaar iska solution Java me mil Jayega please ek baar Google kar lijiye aap
@Anand Pandey Thankyou