简体   繁体   中英

how to filter an array using a binary representation of an integer argument?

帮手形象 > > A binary representation of a number can be used to select elements from an array. For example,

 n: 88 = 23 + 24 + 26 (1011000) array: 8, 4, 9, 0, 3, 1, 2 indexes: 0 1 2 3 4 5 6 selected * * * result 0, 3, 2 so the result of filtering {8, 4, 9, 0, 3, 1, 2} using 88 would be {0, 3, 2} In the

above, the elements that are selected are those whose indices are used as exponents in the binary representation of 88. In other words, a[3], a[4], and a[6] are selected for the result because 3, 4 and 6 are the powers of 2 that sum to 88. Write a method named filterArray that takes an array and a non-negative integer and returns the result of filtering the array using the binary representation of the integer. The returned array must big enough to contain the filtered elements and no bigger. So in the above example, the returned array has length of 3, not 7 (which is the size of the original array.) Futhermore, if the input array is not big enough to contain all the selected elements, then the method returns null. For example, if n=3 is used to filter the array a = {18}, the method should return null because 3=20+21 and hence requires that the array have at least 2 elements a[0] and a 1 , but there is no a 1 . If you are using Java or C#, the signature of the function is int[ ] filterArray(int[ ] a, int n)

添加图片

I have this question I am trying to solve and I wrote this code

public static int[] filterArray(int[] a, int n) {
    int[] x = new int[a.length];
    int j = 0;
    int count = 0;
    while (n > 0) {
        int digit = n % 2;
        n /= 2;
        x[j] = digit;
        j++;
    }

    System.out.println(Arrays.toString(a));

    System.out.println(Arrays.toString(x));

    for (int k = 0; k < x.length; k++) {
        if (x[k] == 1)
            count++;
    }

    System.out.println("count is " + count);

    int[] z = new int[count];
    for (int i = 0; i < z.length; i++) {
        for (int k = 0; k < x.length; k++) {
            if(x[k] == 1) {
                z[i] = a[k];
            }
        }
    }

    System.out.println(Arrays.toString(z));
    return x;

}

When I try to test is with a test array of

System.out.println(Arrays.toString(filterArray(new int[] { 0, 9, 12, 18, -6 }, 11)));

It is giving me the following output

[18, 18, 18]

the correct output is

[0, 9, 18]

The problem is your nested loops: you're iterating over the entire input array for each element of the output array and keep overwriting the values with the last found element (step through your code with a debugger and you'll see that).

To fix that, swap your loops and keep track of the "next" output index:

  int i = 0;
  for (int k = 0; k < x.length; k++) {
    if(x[k] == 1) {
        z[i] = a[k];
        i++; //advance the output index
    }
  }

That will make your code faster as well since now you don't have O(n 2 ) complexity but just O(n).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM