Make Two Arrays Equal by Reversing Subarrays | Easy | Leetcode 1460 | codestorywithMIK
Вставка
- Опубліковано 12 вер 2024
- Whatsapp Community Link : www.whatsapp.c...
This is the 50th Video of our Playlist "Leetcode Easy : Popular Interview Problems" by codestorywithMIK
In this video we will try to solve an easy Array Problem : Make Two Arrays Equal by Reversing Subarrays | Easy | Leetcode 1460 | codestorywithMIK
I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Also, please note that my Github solution link below contains both C++ as well as JAVA code.
Problem Name : Make Two Arrays Equal by Reversing Subarrays | Easy | Leetcode 1460 | codestorywithMIK
Company Tags : will update soon
My solutions on Github(C++ & JAVA) : github.com/MAZ...
Leetcode Link : leetcode.com/p...
My DP Concepts Playlist : • Roadmap for DP | How t...
My Graph Concepts Playlist : • Graph Concepts & Qns -...
My Recursion Concepts Playlist : • Introduction | Recursi...
My GitHub Repo for interview preparation : github.com/MAZ...
Instagram : / codestorywithmik
Facebook : / 100090524295846
Twitter : / cswithmik
Subscribe to my channel : / @codestorywithmik
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
Summary :
Approach 1: Sorting
Time Complexity: O(n log n)
Space Complexity: O(1)
Description:
Both arrays (target and arr) are sorted.
The sorted arrays are then compared element by element.
If all corresponding elements in the sorted arrays are equal, the method returns true; otherwise, it returns false.
Advantages:
Simple and straightforward.
Does not require additional space beyond the input arrays.
Disadvantages:
Sorting both arrays can be less efficient for larger datasets due to O(n log n) time complexity.
Approach 2: Using HashMap
Time Complexity: O(n)
Space Complexity: O(n)
Description:
A HashMap is used to count the frequency of each element in the arr array.
The target array is then iterated through, and the frequency of each element is checked against the HashMap.
If an element in target does not exist in the HashMap or its frequency does not match, the method returns false.
If all elements match, the method returns true.
Advantages:
More efficient for large datasets as it runs in linear time.
Effectively handles cases where elements need to be compared based on frequency.
Disadvantages:
Requires additional space proportional to the number of unique elements in the arrays.
✨ Timelines✨
00:00 - Introduction
#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 #newyear2024
I was trying to sort every possible subarray to get target array and when I was writing logic for edge case where if any of the array contains different elements, there I got an idea that if both arrays have same elements then they will definitely going to be equal.
@prathameshpalkar514 did similar but it passed 105/108 test cases and failed for 3 test cases .
Eg:- target = {1,2,3} and arr={2,2,2} , the sum of the elements of both the array are equal but the two arrays are not equal.
so it will give wrong answer.
I code the similar you write. Already submitted both solution in the morning. Here to watch your solution.
After reading the question only i got the idea that we can always make the array equal if they have same element by swapping adjacent like in bubble sort 😅
It's a bubble sort intuition, you can make the arrays equal if you are allowed to make adjacent swaps. Just check whether same element is present or not
i solved the problem using the same way as discussed in the video.
done on my own 🥳😌
done on my own
Bhaiya plz solve weekly and biweekly contest 3 and 4th problem😢 nhi hota mujhse
Thanks a lot bhaiya ❤️❤️
Best Explanation !!
bro can you please make a video for 4th question of today's biweekly leetcode contest
return collections.Counter(arr) == collections.Counter(target) just write 1 line and see magic🤣😂
what if we need to determine min no of moves to make it equal???
agar target aur arr array ko sort kar diya jaye aur dono array ke element match ho jaye to true nahi to false but maine ye logic bataya vo question me jis tarah kaha gaya hai us tarah nahi hai mik bhaiya bata rahe vo sahi logic hai dono me answer same aayega lekin sahi tarika mik bhaiya ka hai😁😁
dusara vala
Completed in the morning 🙌
Thanks 🙂
Python code for the first approach:
class Solution:
def canBeEqual(self, target: List[int], arr: List[int]) -> bool:
return sorted(target) == sorted(arr)
class Solution {
public:
bool iszero(vector&count){
for(auto &x:count){
if(x!=0){
return false;
}
}
return true;
}
bool canBeEqual(vector& target, vector& arr) {
vectorcount(1001,0);
for(auto &x:target){
count[x]++;
}
for(auto &x:arr){
count[x]--;
}
if(iszero(count)){
return true;
}
return false;
}
};
leetcode 214 plese....
Here is my solution
bool canBeEqual(vector& target, vector& arr) {
unordered_mapmp;
for(int i = 0;i < target.size();i++){
mp[target[i]]++;
mp[arr[i]]--;
}
for(auto i : mp){
if(i.second > 0){
return false;
}
}
return true;
}
return sorted(arr) == sorted(target)
C++ Code :
class Solution {
public:
bool canBeEqual(vector& target, vector& arr) {
int n = target.size();
unordered_map mp;
for(auto &num : target) mp[num]++;
for(auto &num : arr){
if(mp.find(num) == mp.end()) return false;
else{
mp[num]--;
if(mp[num] == 0) mp.erase(num);
}
}
return mp.size() == 0;
}
};
bro please timeline or provide kr diya kro
bhaiya apne c++ and java ki naming in github sahi ni kari h
working fine:
class Solution {
public boolean canBeEqual(int[] target, int[] arr) {
int n = target.length;
Arrays.sort(target);
Arrays.sort(arr);
for (int i = 0; i < n; i++) {
if (target[i] != arr[i])
return false;
}
return true;
}
}
the interviewer might reject this as we should not touch target
Hey is segment tree playlist completed??
i don't think so, 2-3 he to ques hue h
sir i solved this question like this
class Solution {
public:
bool canBeEqual(vector& target, vector& arr) {
int n = arr.size();
vectorfir(1001,0);
vectorsec(1001,0);
for(int i=0;i
please solve leetcode 3225
Bhaiya leetcode - 888 kriye na please 😑 and koi bhi kr skta hai... Jisse bane wo approach cmt kr dena 💀
public boolean canBeEqual(int[]target,int[]arr){
int[]freq=new int[1001];
for(int i=0;i
@dayashankarlakhotia4943 constraints kam h so this will work , but agar jyada hote to space limit exceeded deta , magar subarrays ke question me length of the array kam h hota h
Count steps pls