Skip to content
Advertisement

Tag: algorithm

Add 2 list of objects and remove duplicate(all but one field different) elements from the final list

I have a object class like I have 2 lists 1st one is like {d1,m1,active}, {d2,m2,active},{d3,m3,acticve} 2nd one {d2,m2,paused},{d4,m4,paused} i want my final list to be like {d1,m1,active},{d2,m2,paused},{d3,m3,active},{d4,m4,paused} My approach was to make a list of commonName{d2} then add both the list{d1,d2,d2,d3,d4} into a total list and then iterate through the total list and if the common deviceName(d2.contains(i.getName())) matches then

How to implement Breadth First Search and Depth First Search for a SimpleWeightedGraph in Java

I need to implement Breadth First Search and Depth First Search for a SimpleWeightedGraph which contains String Vertices in Java Code below is my SimpleWeightedGraph where i implemented the following: declared SimpleWeightedGraph created vertices added vertices to the graph created default weighted edges, added edges and set Weights got and printed the shortest path from vertex “a” to vertex “e”

Moving character efficiently on a row

Given input (items = 6, position = 3) creates a row of 6 items and a character positioned on item 3 {0,1,2,[3],4,5} A call to left() moves the character two positions to the left and the item at position 3 is removed {0,[1],2,4,5} The next call to right() moves the character two positions to the right and the item at

Find the first Recurring letter in Java

I wrote the code as follows but it doesn’t return the first recurring letter properly. Example: In the word “statistics” the recurring letters are s, t, and i. But the letter t recurs sooner than the letter s and i, but my program returns s instead of t. What do I need to do for it to return t, using

Time limit exceeded in hackerearth exercise

I’m trying to solve this exercise in HackerEarth. But I have an error of time limit exceeded. This is the code that I wrote : Answer Your solution is a little bit slow because of the bad implementation. I rewrote your solution in better implementation with the same logic and time complexity of your solution and get Accepted and none

How does parameters of a method get stored in stack during a recursive call?

I was doing a leetcode question https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree-ii/ and I am confused on how parameters for a method get stored during a recursive call. If a node gets visited, I wanted to save it’s state that it was visited. When I send two variables, when the stack is getting unwinded the updated variable values are being lost. But if I send

Check if array is sorted using recursion

I am getting true as answer even for unsorted(isNonDescending) arrays. Where is the bug? I want to break the array into smaller problem from the start of the array only. //Check for isNonDescending. Answer Instead of passing the size of the array as a parameter, which makes no sense anyway, because you can simply call arr.length, you should pass a

How to merge multiple Arrays containing the same value in Java

I’m struggling to find the best way to merge arrays (or create new ones) by looking at their shared value. this is my “dictionary” filled with arrays of 2 words, for example it contains arrays: I need to merge them by values they share, so for example the finished “dictionary” (or completely new list) would look like this: Also, the

Advertisement