class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int p1= m-1, p2=n-1; //declare two variables that takes length-1 from 2 arrays
//iterating nums1 in reverse order as it has more elements
for(int i=nums1.length-1; i>=p2; i--){
if (p2 < 0 || p1<0) { // if any of the arrays index is less than zero then break
break;
}
//comparing nums1 and nums2 elements and adding the highest value
if(nums1[p1]>nums2[p2]){
nums1[i]= nums1[p1];
p1--;
}else{
nums1[i]= nums2[p2];
p2--;
}
}
// copying any remaining elements from nums2 into nums1
System.arraycopy(nums2, 0, nums1, 0, p2 + 1);
}
}