loveBabbar / CodeHelp-DSA-Busted-Series

This repo is creating providing students easy access to all the programs taught under Codehelp's DSA Busted Series.
3.37k stars 2.4k forks source link

incorrect c++ code in mergeSortedArray1.cpp #223

Open harshanu11 opened 2 years ago

harshanu11 commented 2 years ago

Please refer https://github.com/loveBabbar/CodeHelp-DSA-Busted-Series/blob/main/Lecture020%20ArrayQuestions/mergeSortedArray1.cpp

line no 25 is incorrect //copy kardo second array k remaining element ko while(j<m) { arr2[k++] = arr2[j++]; }

It should be arr3[k++] = arr2[j++];

piyush168713 commented 2 years ago

Check this one

void mergeArrays_1(int arr1[], int arr2[], int n1, int n2 ){
    int i =0, j=0;

    while (i < n1 && j < n2){
        if (arr1[i] < arr2[j])
        cout<<arr1[i++]<< " ";

        else
        cout<<arr2[j++]<<" ";
    }

    // store remaining elements
    while (i < n1)
    cout<<arr1[i++]<<" ";

    while (j < n2)
    cout<<arr2[j++]<< " ";

}