algorithms – Make two lists equal by using only few operations

This question was in one of the online judges(Chinese) and the question was as follows

“Consider you are given two $unequal$ $arrays$ $a$ and $b$ of $integers$ and you need to make the two arrays equal by using following operations. Select a $integer$ (by integer i mean both positive and negative) $x$ and select a $subset$ of $numbers$ from $array$ $a$ and you can $add$ or $multiply$ each $element $ with $x$ and place back them in their previous positions in $array$ $a$ . Find minimum number of operations to make $a=b$

It was given that $|a|$$<$$6$ and $|a|=|b|$ .

Now when $|a|=1$ then $answer$ will be $0$ or $1$.(it is simple for $|a|=1$)

When $|a|=2$ then worst case answer is $2$ and when they are equal answer is $0$ and if elements have $common$ $difference$ or $common$ $factors$ then answer is $1$.

But i am stuck from case $|a|>2$ for $|a|=3$ the for answer being $0$,$1$ or $3$ i could find ,criteria for answer being $2$ i have no idea and for $|a|>3$ i have no idea how to proceed.

Could anybody give me a hint.( only a hint, i am solving it from $3$ days)

Example:

$a=(3,4,5)$ and $b=(10,13,16)$ then answer is $2$ .first multiply whole array with $3$ and then add $1$ to whole array

spring – Flatten nested lists and reduce the complexity in java

I’m developing a complex app with spring boot and mongodb. I have a Video class like following.(All getters setters are omitted)

class Video{
    ObjectId _id;
    private List<OReelOCategoryLGroup> listOfReelIdCategoryIdAndGroups;
    //some other fields    
}

public class OReelOCategoryLGroup {
    ObjectId reelId;
    ObjectId categoryId;
    List<Group> groups;
}

public class Group {
    ObjectId _id;
    Boolean isMandatory=false;
    String dueDate;
}

From the front-end, I’m receiving a structure

{
    "reelId":"5eda6be5b31e96f58b3a7c65",
    "groupId":"5eda6bed217c01319701aea0",
    "category":(
        {
            "categoryId":"5eda6bf48ebcb19e031f2aa2",
            "videoId":("5eda6bff1230f45ae4b7e54a","5eda6c0464ba746e243a4d6d","5eda6c0eed64b31232bf1095")

        },
        {
            "categoryId":"5eda6bf48ebcb19e031f2aa2",
            "videoId":("5eda6c14214ca295b6473c78")

        }
    )
}

The representation of the class is

public class OReelLCategoryOGroup {
    ObjectId reelId;
    ObjectId groupId;
    List<OIdOVideoId> category;

}

public class OIdOVideoId {

    ObjectId categoryId;
    List<ObjectId> videoId;

}

I want to update OReelLCategoryOGroup to each and every videoId what I receive from front-end. So I coded

public String addVideoToCurrentGroupFromAnotherReel(OReelLCategoryOGroup list) {

    Set<ObjectId> notFoundIds = new HashSet<>();

    for (OIdOVideoId cat : list.getCategory()) {

        for (ObjectId vId : cat.getVideoId()) {
            Optional<Video> video = videoRepository.findBy_id(vId);
            if (video.isPresent()) {
                Video v=video.get();

                OReelOCategoryLGroup flat = new OReelOCategoryLGroup();

                Group group = new Group();
                group.set_id(list.getGroupId());
                group.setIsMandatory(false);

                flat.setReelId(list.getReelId());
                flat.setCategoryId(cat.getCategoryId());
                flat.setGroups(Arrays.asList(group));

                v.getListOfReelIdCategoryIdAndGroups().add(flat);
                save(v);
            } else {
                notFoundIds.add(vId);
            }

        }
    }

    if (notFoundIds.size() > 0) {
        return "Few videos successfully added to group, but " + notFoundIds.toString() + " are not found in database";
    }
    return "Videos successfully added to group";
}

Here the complicity is O(n to the power of 2). But if listOfReelIdCategoryIdAndGroups contains reelId==list.getReelId() && categoryId==cat.getCategoryId() then I have to add the group to that list of Groups. If I’m going to check it, there will be another loop inside the nested loop. So complexicity is O(n to the power of 3).

for (OIdOVideoId cat : list.getCategory()) {
    for (ObjectId vId : cat.getVideoId()) {
        if (video.isPresent()) {

            for(OReelOCategoryLGroup g:v.getListOfReelIdCategoryIdAndGroups()){
                if(g.getReelId()==list.getReelId() && g.getCategoryId()==cat.getCategoryId()){
                    g.getGroups().add(group);
                }else{
                    // insert
                }
            }
        }
    }
}

I feel its inefficient, Is there any way to overcome to this problem with java 8, Stream APIs?

database – Organizing Data with Specific Group in Objects and Lists

So recently I have been working with a large-scale app that has a large number of individual objects with specific group attributes. For example, I have a list with objects such as

{Name: "Todd", Desc: "Todd likes basketball", Team: "#23"}

This list is has about 10k of these objects in it with about 1,000 different obj.Team values, ranging from Team: #1 -> Team: #1000.Throughout my app, I will be using the full list of objects for certain calculations as well as just viewing all objects with a certain obj.Team value.

I will be using a Redux State w/ React to store these values and I was wondering what the best method would be in terms of performance, better practice, readability, etc. Would it be preferably to have two lists where one list is the full 10k objects while another list is a list of lists based on the Team value, where the same obj data is copied to two lists (with just different format)? Or would it be best to simply have a single list of these 10k objects and when I want to use some calculation on a specific obj.Team value, I just loop through and find the appropriate objects?

function construction – ReplaceAll with pattern matching not working with list of lists

When you did

ReplaceAll({vec1,vec2}, {x_List, c_} :> c x )
(* {{-3, 6}, {8, -20}} *)

Then it took x=vec1 and c=vec2 which is not what you want. So it did vec1 vec2 which is

vec1*vec2
(* {{-3,6},{8,-20}} *)

You can instead thread over the input

(ReplaceAll(#, {x_List, c_} :> c x) &) /@ {vec1, vec2}
(*  {{4, -8}, {-3, {2, -5}}}  *)

One way to find this out, is to actually insert Print statements, on the right side, like this

ReplaceAll({vec1, vec2}, {x_List, c_} :> (Print("x=", x, " c=", c); c x))

The print does not affect the replacement, but useful to see what is being replaced. Then you would see the following

enter image description here

In addition, if you do Attributes(ReplaceAll) you would see that Listable is not given as attribute of ReplaceAll

plotting – ListPlot and comparisons between lists

Here is an example with made up data:

Clear(y, z, k)
x = Range(20);
{y, z, k} = RandomInteger({-10, 10}, {3, 20});

ListPlot(
  MapThread(Style({#1, #2}, If(#3 > #4, Red, Black)) &, {x, y, z, k}),
  PlotRange -> All,
  PlotStyle -> PointSize(0.02)
)

formatted points


For your second part (“Additional issue: for the first observations, the list k is empty, so I want to plot a red dot for such observations.”), you should really provide a sample of your data. Any answer would depend on its precise structure.

Creating a view which lists all taxnomy terms and the number they appear in a content type?

So i have a taxonomy called Product Warnings, and it has around 10 terms. This taxonomy is referenced in several content types (Small products, Large products, etc), and can be selected multiple times (so a user can select in the content type 1 or up to all 10 terms from the Taxonomy).

I want to create a view which lists all terms from the given taxonomy (that’s the easy part), and also count the number of times each terms is referenced in a specific content type (i.e. Small products)

So it would look like:

Eye damage: 44
Skin irritation: 13
Hair lose: 1 
.
.
.

Now, this is fairly easy to do with a db_query and custom code, but the trick is to do it in a view (if it’s possible)