"Problem Statement: The Fibonacci sequence is defined as F(n) = F(n-1) + F(n-2) with F(0) = 1 and F(1) = 1.
The solution is given in the problem statement itself.
If the value of n = 0, return 1.
If the value of n = 1, return 1.
Otherwise, return the sum of data at (n - 1) and (n - 2).
Explanation: The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, typically starting with 0 and 1.
Java Solution:
public static int fib(int n"
Rishi G. - "Problem Statement: The Fibonacci sequence is defined as F(n) = F(n-1) + F(n-2) with F(0) = 1 and F(1) = 1.
The solution is given in the problem statement itself.
If the value of n = 0, return 1.
If the value of n = 1, return 1.
Otherwise, return the sum of data at (n - 1) and (n - 2).
Explanation: The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, typically starting with 0 and 1.
Java Solution:
public static int fib(int n"See full answer
"Binary Search on the array and after than compare the numbers at low and the high pointers whichever is closest is the answer. Because after the binary search low will be pointing to a number which is immediate greater than x and high will be pointing to a number which is immediate lesser than x.
int low = 0;
int high = n-1;
while(low <= high){
int mid = (low + high) / 2;
if(x == arr[mid]) return mid; //if x is already present then it will be the closest
else if(x < arr[mid]) high"
Shashwat K. - "Binary Search on the array and after than compare the numbers at low and the high pointers whichever is closest is the answer. Because after the binary search low will be pointing to a number which is immediate greater than x and high will be pointing to a number which is immediate lesser than x.
int low = 0;
int high = n-1;
while(low <= high){
int mid = (low + high) / 2;
if(x == arr[mid]) return mid; //if x is already present then it will be the closest
else if(x < arr[mid]) high"See full answer
"class Solution:
def missingNumber(self, nums: list[int]) -> int:
Sorting approach
n = len(nums)
s = n*(n+1)//2
r = s - sum(nums)
return self.r
l = [3,0,1]
print(missingNumber(l))"
Rohit B. - "class Solution:
def missingNumber(self, nums: list[int]) -> int:
Sorting approach
n = len(nums)
s = n*(n+1)//2
r = s - sum(nums)
return self.r
l = [3,0,1]
print(missingNumber(l))"See full answer
Software Engineer
Data Structures & Algorithms
+1 more
🧠 Want an expert answer to a question? Saving questions lets us know what content to make next.
"A much better solution than the one in the article, below:
It looks like the ones writing articles here in Javascript do not understand the time/space complexity of javascript methods.
shift, splice, sort, etc... In the solution article you have a shift and a sort being done inside a while, that is, the multiplication of Ns.
My solution, below, iterates through the list once and then sorts it, separately. It´s O(N+Log(N))
class ListNode {
constructor(val = 0, next = null) {
th"
Guilherme F. - "A much better solution than the one in the article, below:
It looks like the ones writing articles here in Javascript do not understand the time/space complexity of javascript methods.
shift, splice, sort, etc... In the solution article you have a shift and a sort being done inside a while, that is, the multiplication of Ns.
My solution, below, iterates through the list once and then sorts it, separately. It´s O(N+Log(N))
class ListNode {
constructor(val = 0, next = null) {
th"See full answer
"To determine if a graph is not a tree, you can check for the following conditions:
Presence of cycles: A graph is not a tree if it contains cycles. In a tree, there should be exactly one unique path between any two vertices. If you can find a cycle in the graph, it cannot be a tree.
Insufficient number of edges: A tree with N vertices will have exactly N-1 edges. If the graph has fewer or more than N-1 edges, then it is not a tree.
Disconnected components: A tree is a connected graph, m"
Vaibhav C. - "To determine if a graph is not a tree, you can check for the following conditions:
Presence of cycles: A graph is not a tree if it contains cycles. In a tree, there should be exactly one unique path between any two vertices. If you can find a cycle in the graph, it cannot be a tree.
Insufficient number of edges: A tree with N vertices will have exactly N-1 edges. If the graph has fewer or more than N-1 edges, then it is not a tree.
Disconnected components: A tree is a connected graph, m"See full answer
"Example:
bucket A: 3 liters capacity
bucket B: 5 liters capacity
goal: 4 liters
You are asked to print the logical sequence to get to the 4 liters of water in one bucket.
Follow up:
How would you solve the problem if you have more than 2 buckets of water?"
B. T. - "Example:
bucket A: 3 liters capacity
bucket B: 5 liters capacity
goal: 4 liters
You are asked to print the logical sequence to get to the 4 liters of water in one bucket.
Follow up:
How would you solve the problem if you have more than 2 buckets of water?"See full answer
"function areSentencesSimilar(sentence1, sentence2, similarPairs) {
if (sentence1.length !== sentence2.length) return false;
for (let i=0; i (w1 === word1 && !visited.has(w2)) || (w2 === word1 && !visited.has(w1)));
if (!edge) {
"
Tiago R. - "function areSentencesSimilar(sentence1, sentence2, similarPairs) {
if (sentence1.length !== sentence2.length) return false;
for (let i=0; i (w1 === word1 && !visited.has(w2)) || (w2 === word1 && !visited.has(w1)));
if (!edge) {
"See full answer
"Implemented the Java code to find the largest island. It is similar to count the island. But in this we need to keep track of max island and compute its perimeter."
Techzen I. - "Implemented the Java code to find the largest island. It is similar to count the island. But in this we need to keep track of max island and compute its perimeter."See full answer
"function knapsack(weights, values, cap) {
const indicesByValue = Object.keys(weights).map(weight => parseInt(weight));
indicesByValue.sort((a, b) => values[b]-values[a]);
const steps = new Map();
function knapsackStep(cap, sack) {
if (steps.has(sack)) {
return steps.get(sack);
}
let maxOutput = 0;
for (let index of indicesByValue) {
if (!sack.has(index) && weights[index] <= cap) {
maxOutput ="
Tiago R. - "function knapsack(weights, values, cap) {
const indicesByValue = Object.keys(weights).map(weight => parseInt(weight));
indicesByValue.sort((a, b) => values[b]-values[a]);
const steps = new Map();
function knapsackStep(cap, sack) {
if (steps.has(sack)) {
return steps.get(sack);
}
let maxOutput = 0;
for (let index of indicesByValue) {
if (!sack.has(index) && weights[index] <= cap) {
maxOutput ="See full answer
"A red-black tree is a self-balancing binary search tree. The motivation for this is that the benefits of O(logN) search, insertion, and deletion that a binary tree provides us will disappear if we let the tree get too "imbalanced" (e.g. there are too many nodes on one side of the tree or some branches have a depth that is way out of proportion to the average branch depth). This imbalance will occur if we don't adjust the tree after inserting or deleting nodes, hence our need for self-balancing c"
Alex M. - "A red-black tree is a self-balancing binary search tree. The motivation for this is that the benefits of O(logN) search, insertion, and deletion that a binary tree provides us will disappear if we let the tree get too "imbalanced" (e.g. there are too many nodes on one side of the tree or some branches have a depth that is way out of proportion to the average branch depth). This imbalance will occur if we don't adjust the tree after inserting or deleting nodes, hence our need for self-balancing c"See full answer