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

КОМЕНТАРІ • 36

  • @prathameshpalkar514
    @prathameshpalkar514 Місяць тому +12

    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.

    • @apurvwaghmare5974
      @apurvwaghmare5974 Місяць тому +1

      @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.

  • @jitDhank04
    @jitDhank04 Місяць тому +4

    I code the similar you write. Already submitted both solution in the morning. Here to watch your solution.

  • @yogeshchauhan9401
    @yogeshchauhan9401 Місяць тому +7

    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 😅

  • @aryansrivastava6692
    @aryansrivastava6692 Місяць тому +2

    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

  • @SonuSharma-fc9hd
    @SonuSharma-fc9hd Місяць тому +2

    i solved the problem using the same way as discussed in the video.

  • @gui-codes
    @gui-codes Місяць тому +4

    done on my own 🥳😌

  • @anshgupta8860
    @anshgupta8860 Місяць тому +4

    done on my own

  • @Infinite_Mainak
    @Infinite_Mainak Місяць тому +1

    Bhaiya plz solve weekly and biweekly contest 3 and 4th problem😢 nhi hota mujhse

  • @gauravbanerjee2898
    @gauravbanerjee2898 Місяць тому +1

    Thanks a lot bhaiya ❤️❤️

  • @priyanshkumar17
    @priyanshkumar17 Місяць тому +1

    Best Explanation !!

  • @RockStar-wc7od
    @RockStar-wc7od Місяць тому +1

    bro can you please make a video for 4th question of today's biweekly leetcode contest

  • @sizuka413
    @sizuka413 Місяць тому +3

    return collections.Counter(arr) == collections.Counter(target) just write 1 line and see magic🤣😂

  • @kartikforwork
    @kartikforwork Місяць тому +1

    what if we need to determine min no of moves to make it equal???

  • @fenilpatel6238
    @fenilpatel6238 Місяць тому +1

    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😁😁

  • @aws_handles
    @aws_handles Місяць тому

    Completed in the morning 🙌

  • @RohitKumar-dz8dh
    @RohitKumar-dz8dh Місяць тому

    Thanks 🙂

  • @sauravchandra10
    @sauravchandra10 Місяць тому

    Python code for the first approach:
    class Solution:
    def canBeEqual(self, target: List[int], arr: List[int]) -> bool:
    return sorted(target) == sorted(arr)

  • @user-vs5rh1oz3w
    @user-vs5rh1oz3w Місяць тому

    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;
    }
    };

  • @KeshavKumar-jl1ub
    @KeshavKumar-jl1ub Місяць тому +1

    leetcode 214 plese....

  • @izanahmad5705
    @izanahmad5705 Місяць тому

    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;
    }

  • @SHIVAMOJHA21
    @SHIVAMOJHA21 Місяць тому

    return sorted(arr) == sorted(target)

  • @priyanshkumar17
    @priyanshkumar17 Місяць тому

    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;
    }
    };

  • @xyzhacker00999
    @xyzhacker00999 Місяць тому

    bro please timeline or provide kr diya kro

  • @dipalisharma4682
    @dipalisharma4682 Місяць тому

    bhaiya apne c++ and java ki naming in github sahi ni kari h

  • @PushpendraKushvaha
    @PushpendraKushvaha Місяць тому

    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;
    }
    }

    • @varunaggarwal7126
      @varunaggarwal7126 Місяць тому

      the interviewer might reject this as we should not touch target

  • @HarshavShah-d3h
    @HarshavShah-d3h Місяць тому

    Hey is segment tree playlist completed??

    • @kartikforwork
      @kartikforwork Місяць тому +1

      i don't think so, 2-3 he to ques hue h

  • @vishwashsoni610
    @vishwashsoni610 Місяць тому

    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

  • @VishalGupta-ft8df
    @VishalGupta-ft8df Місяць тому

    please solve leetcode 3225

  • @AjeetTiwari018
    @AjeetTiwari018 Місяць тому

    Bhaiya leetcode - 888 kriye na please 😑 and koi bhi kr skta hai... Jisse bane wo approach cmt kr dena 💀

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Місяць тому

    public boolean canBeEqual(int[]target,int[]arr){
    int[]freq=new int[1001];
    for(int i=0;i

    • @apurvwaghmare5974
      @apurvwaghmare5974 Місяць тому

      @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

  • @Engineering.Wallah
    @Engineering.Wallah Місяць тому

    Count steps pls