I have an object from this Class: And I would like to write the JAVA method: private List<List> extractNames(Person ancestor) which gives back all the names of each branch of the tree: Do you have an idea, how I could do it ? Answer Update: Removed the dependency to lombok The relevant algorithm part is in the method extractNamesAlternative in
Tag: data-structures
Function to check for perfect square not working for large number
I came across an algorithm to tell if a given number is perfect square or not in O(logN) time. Here is the implementation(JAVA) of the idea. This works fine for numbers like 256, 808201 , etc But fails for numbers like 999966000289. I cannot figure out why? Answer As mentioned in the comments, the problem is that the intermediate mid*mid
Longest common prefix in binary representation
We are given a undirected tree with N (1 to N) nodes rooted at node 1. Every node has a value assigned with it, represented by array – A[i] where i:[1:N]. We need to answer Q queries of type : -> V X : longest length of the common prefix between value V and any ancestor of node X including
Count nodes that are bigger than all their sons
I have to make a programm that counts the nodes bigger than their sons without including the leaf nodes for example: This is my approach: But I can’t see the error Answer Assuming info is an int, this could work for you. I haven’t tested the code, so let me know if there are issues.
Simple Linked List: getting an ‘error – found cycle’ in code
I have this reverseList function: But I get an error that says ‘found cycle in the LinkedList’ when iterating specifically through ‘curr.next = prev’ and ‘prev = curr’. Any ideas on why this might be occuring? Thanks Answer As far as I can tell you are testing your code with some kind of judge since there is no “found cycle”
Couldn’t implement binary search on linked list
i am a cse student who takes data structures course. Trying to implement binary search algorithm to my SinglyLinkedList class, somehow i’ve failed. Could you check it what’s wrong please ? The related method; I’ve debugged and it just enters the loops this side: else if(temp.getElement() > target) All class for better understanding; And the main method; It returns; Answer
Making a island large leetcode – spotting mistake Java
I am working on LeetCode problem 827. Making A Large Island: You are given an n x n binary matrix grid. You are allowed to change at most one 0 to be 1. Return the size of the largest island in grid after applying this operation. An island is a 4-directionally connected group of 1s. Example 1: Input: grid =
Maximum number of tasks to be performed
I’m stucking in a problem. I know dp can be applied here, but not getting it. Consider a part of the positive number line starting at 0 and ending at 10^9. You start at 0 and there are N tasks can be performed. The ith task is at l[i] and requires t[i] time to be performed. To perform ith task,
print numbers from 1 to 1000 digit number – interview question
I had an interview and I’ve been asked to print numbers from 1 to a 1000 digit number – 1, 2, 3, . . . ., 999999999999999999999999999999999999999999999999…….. I couldn’t solve it but I’m still looking for the best way to do it, because obviously, you cant use integers or floats in a case like this and because it’s a job
What is the quickest way to determine if two elements are in the same row or column in a 2D array?
As part of one of my programs, I’m trying to check if two elements in a 2D array are part of the same row or column. I know there might be many ways to go about this, mainly with loops and by iterating through the array, but I was just wondering: is there any quick way to determine this? Let’s