kyunki humko second array se hi compare karna hai na.... aur resultant changes i variable ko represent karte hai . aur humne final result bhi wahin store karna hai.
dear sister, I am unable to catch the coding logic without seeing the code in video or other tutorial but i understand what to do but cant implement the code .this giving me much stress, I am a beginner .I have solved 9 easy problem but in every problem i have to take help..what should i do?where should i focus on.can I be a good programmer?
Cause both are already sorted and it will increase the time complexity of the solution, first you will put each element in the array then start using any of the sorting algorithm
in above example, m is equal to 3, means 3rd position. But the index in Array starts from 0 and position starts from 1. So (i =m-1) means that In above example, when the position is 3 then the index will be 2. That is the reason that we subtract 1 from the position of Array to get the index. Hope u understand...
arrays are fixed in nature if you don't initialised it with 0s what you will put there other than zero to make it size of m i.e 6 here 2nd thing if you use scanner class also arrays will get initialised with zeros only for int values.
Didi you don't explain why we have to place i, j or k pointers at that position only 😢 . Why can't we start our pointers from somewhere else . You should explain also as it will help with thought process
it is best and simple than above class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { for (int j = 0, i = m; j < n; j++) { nums1[i] = nums2[j]; i++; } Arrays.sort(nums1); } }
Hi.. The code will not fail..As mentioned in the problem, m=0 means there are no elements in the nums 1 arrays so the output would be all elements of nums2 array.. Please try the code..I have tried and submItted ,it's working fine