Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static ArrayList<Integer> merge_one_into_another(ArrayList<Integer> arr1, ArrayList<Integer> arr2) {
- int i = arr1.size()-1;
- int j = arr1.size()-1;
- int idx = arr2.size()-1;
- // sorting from right side
- while(i >= 0 && j >= 0) {
- if(arr1.get(i) >= arr2.get(j)) {
- arr2.set(idx, arr1.get(i));
- arr1.set(i, 0);
- i--;
- idx--;
- } else {
- arr2.set(idx, arr2.get(j));
- arr2.set(j, 0);
- j--;
- idx--;
- }
- }
- // put remaining elements in arr1 to arr2
- while(i >= 0)
- {
- arr2.set(idx, arr1.get(i));
- arr1.set(i, 0);
- i--;
- idx--;
- }
- return arr2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement