[LeetCode] Cycle Length Queries in a Tree

2509. Cycle Length Queries in a Tree

You are given an integer n. There is a complete binary tree with 2n - 1 nodes. The root of that tree is the node with the value 1, and every node with a value val in the range [1, 2n - 1 - 1] has two children where:

  • The left node has the value 2 * val, and
  • The right node has the value 2 * val + 1.

You are also given a 2D integer array queries of length m, where queries[i] = [ai, bi]. For each query, solve the following problem:

  1. Add an edge between the nodes with values ai and bi.
  2. Find the length of the cycle in the graph.
  3. Remove the added edge between nodes with values ai and bi.

Note that:

  • A cycle is a path that starts and ends at the same node, and each edge in the path is visited only once.
  • The length of a cycle is the number of edges visited in the cycle.
  • There could be multiple edges between two nodes in the tree after adding the edge of the query.

Return an array answer of length m where answer[i] is the answer to the ith query.

Read more
[LeetCode] Add Edges to Make Degrees of All Nodes Even

2508. Add Edges to Make Degrees of All Nodes Even

There is an undirected graph consisting of n nodes numbered from 1 to n. You are given the integer n and a 2D array edges where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi. The graph can be disconnected.

You can add at most two additional edges (possibly none) to this graph so that there are no repeated edges and no self-loops.

Return true if it is possible to make the degree of each node in the graph even, otherwise return false.

The degree of a node is the number of edges connected to it.

Read more
[LeetCode] Smallest Value After Replacing With Sum of Prime Factors

2507. Smallest Value After Replacing With Sum of Prime Factors

You are given a positive integer n.

Continuously replace n with the sum of its prime factors.

  • Note that if a prime factor divides n multiple times, it should be included in the sum as many times as it divides n.

Return the smallest value n will take on.

Read more
[LeetCode] Count Pairs Of Similar Strings

2506. Count Pairs Of Similar Strings

You are given a 0-indexed string array words.

Two strings are similar if they consist of the same characters.

  • For example, “abca” and “cba” are similar since both consist of characters ‘a’, ‘b’, and ‘c’.
  • However, “abacba” and “bcfd” are not similar since they do not consist of the same characters.

Return the number of pairs (i, j) such that 0 <= i < j <= word.length - 1 and the two strings words[i] and words[j] are similar.

Read more
[CodeChef] Mod EqualityRead more
[CodeChef] Find A, B, CRead more
[CodeChef] Chef and Array and KRead more
[CodeChef] Bytelandian RobotsRead more
[CodeChef] From Rational to BinaryRead more
[AtCoder] C - Max DotRead more