Question

public static int[] interleaveArray(int[] a, int[] b) Given two arrays, return the array which interleaves the...

public static int[] interleaveArray(int[] a,
                                    int[] b)

Given two arrays, return the array which interleaves the elements of the two arrays. The two arrays do not have to be the same length. For example, given a = [1, 2, 3] and b = [4, 5, 6, 7, 8], the method returns c = [1, 4, 2, 5, 3, 6, 7, 8]

Parameters:

a - given array

b - given array

Returns:

the array which interleaves the elements of the two arrays.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public static int[] interleaveArray(int[] a,
                                    int[] b){
    int arr[] = new int[a.length+b.length];
    int i = 0, k =0;
    while(i<a.length && i<b.length){
        arr[k++] = a[i];
        arr[k++] = b[i];
        i++;
    }

    while(i<a.length){
        arr[k++] = a[i++];
    }

    while(i<b.length){
        arr[k++] = b[i++];
    }
    return arr;
}
Add a comment
Know the answer?
Add Answer to:
public static int[] interleaveArray(int[] a, int[] b) Given two arrays, return the array which interleaves the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java: Write a class ArrayIntersection that implements the method below. public static int[] get(int[] one, int[]...

    Java: Write a class ArrayIntersection that implements the method below. public static int[] get(int[] one, int[] two) Given two arrays, it returns a new array with all values that are in both arrays. The returned array has a size fitting its elements exactly and without duplicates. The order of values does not matter. For example, given {1,5,5,1} and {5,3}, the method returns {5}.

  • /** Given an int array, return true if the array contains duplicate values. duplicateInts({3}) -> false...

    /** Given an int array, return true if the array contains duplicate values. duplicateInts({3}) -> false duplicateInts({1, 2}) -> false duplicateInts({7, 7}) -> true duplicateInts({1, 2, 3, 4, 5}) -> false duplicateInts({1, 2, 3, 2, 4, 5}) -> true **/ public static boolean duplicateInts(int[] numbers) { //your code here return false; }//end duplicateInts /** Given a String array, return true if the array contains duplicate values. Note: Capital letters count duplicateStrings({"a"}) -> false duplicateStrings({"a", "b", "c", "d"}) -> false duplicateStrings({"a",...

  • 1. Write a complete program based on public static int lastIndexOf (int[] array, int value) {...

    1. Write a complete program based on public static int lastIndexOf (int[] array, int value) { for (int i = array.length - 1; i >= 0; i--) { if (array [i] == value) { return i; } } return -1; write a method called lastindexof that accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. the method should return -1 if the value is...

  • collatz public static int[] collatz(int start, int numIterations) Given integer start and integer numIterations, return an...

    collatz public static int[] collatz(int start, int numIterations) Given integer start and integer numIterations, return an array containing the Collatz sequence beginning with start up to numIterations. The Collatz function is defined by: 3n + 1 if n is odd n/2 if n is even Given start = 7 and numIterations = 3, this method returns [7, 22, 11, 34] Parameters: start - starting integer numIterations - how long to compute the Collatz sequence for Returns: an array containing the...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • /**    Given a String and an array of two Strings,    return a three String...

    /**    Given a String and an array of two Strings,    return a three String array containing the strings in alphabetical order.    Note: Capital letters count    sort3Strings("wallace", {"washington", "irving"}) -> {"irving", "wallace", "washington"}    sort3Strings("wallace", {"washington", "Irving"}) -> {"Irving", "wallace", "washington"}    sort3Strings("Washington", {"irving", wallace"}) -> {"Washington", "irving", "wallace"}    sort3Strings("washington", {"washington", "Washington"}) -> {"Washington", "washington", "washington"} **/ public static String[] sort3Strings(String stringValue, String[] stringArray) {    //your code here    return new String[1]; }//end sort3Strings   ...

  • A method called linearSearch(), which takes as parameters an array of int followed by three values...

    A method called linearSearch(), which takes as parameters an array of int followed by three values of type int, and returns a value of type int. The first int parameter represents a key, the second int parameter represents a starting position, and the third int parameter represents an end position. If the key occurs in the array between the start position (inclusive) and the end position (exclusive), the method returns the position of the first occurrence of the key in...

  • /** Given an int array, return an int array with duplicate ints removed if the array...

    /** Given an int array, return an int array with duplicate ints removed if the array contains duplicate values. <br> <br> removeDuplicateInts({3}) -> {3} <br> removeDuplicateInts({1, 2}) -> {1, 2} <br> removeDuplicateInts({7, 7}) -> {7} <br> removeDuplicateInts({1, 7, 1, 7, 1}) -> {1, 7} <br> removeDuplicateInts({1, 2, 3, 4, 5}) -> {1, 2, 3, 4, 5}) <br> removeDuplicateInts({1, 2, 3, 2, 4, 2, 5, 2}) -> {1, 2, 3, 4, 5} <br> @param numbers int[] an array of integers. @return...

  • Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating...

    Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating elements from both array lists. If one array list is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer array list. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 then merge returns the array list 1 9 4 7 9 4 16 9 11...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT