Filtering

At a toy company there is a job to filter good and bad toy, job is given to you, to find out good and bad toy based on their requirements. The requirements are described in the int[] toys and the String result. If character t in result is ‘G’, then all objects of size toys[i] must be accepted good toy. If character t in result is ‘B’, then all objects of size toys[i] must be rejected bad toy.

If an object’s size does not appear in toys, then it doesn’t matter if it is accepted or rejected. You can only design filters of one specific kind called (I, J)-filters. Each such filter is characterized by two integers I and J. It accepts an object if and only if its size is between I and J, inclusive. You have excellent (I, J)-filter construction skills, so you can construct any such filter where 1 <= I <= J. If it is possible to construct an (I, J)-filter that fulfills all the requirements described in toys and result, return a int[] containing the filter’s parameters, where element 0 is I and element 1 is J. If there are several appropriate filters, choose the one that minimizes J – I. If there are no suitable filters, return an empty int[].

Toys will contain between 1 and 50 elements, inclusive.
Each element of toys will be between 1 and 100, inclusive, all elements of toys will be distinct.
Each character in result will be ‘G’ or ‘B’, at least one ‘G’ character.
result will contain the same number of characters as the number of elements in toys.
Examples
1) Input => {3, 4, 5, 6, 7} , “GGGGG” => Returns: {3, 7 }
2) Input => {3, 4, 5, 6, 7}, “GGBGG” = > Returns: { }
3) Input => {3, 4, 5, 6, 7}, “BGGGG” = > Returns: {4, 7}
4) Input => {68,57,7,41,76,53,43,77,84,52,34,48,27,75,36}, “BGBBBGBBBGBGBBB” = > Returns: {48,57}
5) Input => {26,81,9,14,43,77,55,57,12,34,29,79,40,25,50}, “GBGGGBBGBGBGBGG” = > Returns: {}
import java.util.*;
public class Filtering {
    private int[] designFilter(int[] sizes, String outcome) {
        int iarr[] = new int[2];
        int ismall = 101;
        int ibig =-1;
        String sarr[] =new String[outcome.length()];
        for (int k=0;k<outcome.length();k++)
            sarr[k] =outcome.substring(k,k+1);
        for (int i=0;i<outcome.length();i++) {
            if (sarr[i].equals("A")) {
                if (sizes[i]<ismall)  ismall = sizes[i];
                if (sizes[i]>ibig) ibig = sizes[i];
            }
        }
        boolean baccept =true;
        for (int i=0;i<outcome.length();i++) {
            if (sarr[i].equals("R")) {
                if (sizes[i]>ismall && sizes[i]<ibig)
                    baccept =false;
            }
        }
        if (baccept)  {
                iarr[0] = ismall;
                iarr[1] = ibig;
        } else {
            int[] sizes1 = {};
            iarr = sizes1;
        }
        return iarr;
    }

    public static void main(String[] args) {
        Filtering fil = new Filtering();
        int[] sizes = {3, 4, 5, 6, 7};
        String outcome = "AAAAA";
        System.out.println(Arrays.toString(sizes) + " - " + outcome+ " - " + Arrays.toString(fil.designFilter(sizes, outcome)));

        sizes = new int[]{3, 4, 5, 6, 7};
        outcome = "AARAA";
        System.out.println(Arrays.toString(sizes) + " - " + outcome+ " - " + Arrays.toString(fil.designFilter(sizes, outcome)));

        sizes = new int[]{3, 4, 5, 6, 7};
        outcome = "RAAAA";
        System.out.println(Arrays.toString(sizes) + " - " + outcome+ " - " + Arrays.toString(fil.designFilter(sizes, outcome)));

        sizes = new int[]        {68,57,7,41,76,53,43,77,84,52,34,48,27,75,36};
        outcome = "RARRRARRRARARRR";
        System.out.println(Arrays.toString(sizes) + " - " + outcome+ " - " + Arrays.toString(fil.designFilter(sizes, outcome)));

        sizes = new int[]        {26,81,9,14,43,77,55,57,12,34,29,79,40,25,50};
        outcome = "ARAAARRARARARAA";
        System.out.println(Arrays.toString(sizes) + " - " + outcome+ " - " + Arrays.toString(fil.designFilter(sizes, outcome)));

        sizes = new int[]        {9};
        outcome = "A";
        System.out.println(Arrays.toString(sizes) + " - " + outcome+ " - " + Arrays.toString(fil.designFilter(sizes, outcome)));

        sizes = new int[]        {11, 42};
        outcome = "AR";
        System.out.println(Arrays.toString(sizes) + " - " + outcome+ " - " + Arrays.toString(fil.designFilter(sizes, outcome)));

        sizes = new int[]        {35, 15};
        outcome = "AR";
        System.out.println(Arrays.toString(sizes) + " - " + outcome+ " - " + Arrays.toString(fil.designFilter(sizes, outcome)));

        sizes = new int[]        {4, 3};
        outcome =  "AA"     ;
        System.out.println(Arrays.toString(sizes) + " - " + outcome+ " - " + Arrays.toString(fil.designFilter(sizes, outcome)));

        sizes = new int[]        {10, 32, 65};
        outcome =  "AAA"     ;
        System.out.println(Arrays.toString(sizes) + " - " + outcome+ " - " + Arrays.toString(fil.designFilter(sizes, outcome)));

        sizes = new int[]        {4, 73, 40};
        outcome =  "ARA"     ;
        System.out.println(Arrays.toString(sizes) + " - " + outcome+ " - " + Arrays.toString(fil.designFilter(sizes, outcome)));

    } }
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: