Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 170 of 377

Circular Array Loop in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 3K+ Views

Suppose we have a circular array nums of positive and negative integer values. If a number k at an index is a positive number, then move forward k steps. Otherwise, if it's negative (-k), move backward k steps. Since the array is circular, we can assume that the next element of the last element is the first element, and the previous element of the first element is the last element. We have to check whether if there is a loop (or a cycle) in nums. A cycle must start and end at the same index and the cycle's length > ...

Read More

Longest Arithmetic Sequence in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 407 Views

Suppose we have an array A of integers, we have to return the length of the longest arithmetic subsequence in A. As you know that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0

Read More

Wiggle Subsequence in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 340 Views

Suppose we have a sequence of numbers that is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference may be either positive or negative. A sequence with less than two elements is trivially a wiggle sequence. So for example, [1, 7, 4, 9, 2, 5] is a wiggle sequence because if you see, the differences (6, -3, 5, -7, 3) are alternately positive and negative. But, [1, 4, 7, 2, 5] and [1, 7, 4, 5, 5] are not wiggle sequences, the first one because its first two differences are ...

Read More

Most Frequent Subtree Sum in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 369 Views

Suppose we have the root of a tree, we have to find the most frequent subtree sum. The subtree sum of a node is actually the sum of all the node values formed by the subtree rooted at that node (including the node itself). The most frequent subtree sum is actually If there is a tie, return all the values with the highest frequency in any order. So if the tree is like [5, 2, -5], then it will return [2]. This is because 2 happens twice, however -5 only occurs once.To solve this, we will follow these steps −Define ...

Read More

Uncrossed Lines in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 277 Views

Suppose we have written the integers of A and B (in the order they are given) on two separate horizontal lines. Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that −A[i] == B[j];The line we draw that does not intersect any other connecting (non-horizontal) line.We have to keep in mind that connecting lines cannot intersect even at the endpoints − each number can only belong to one connecting line. Find the maximum number of connecting lines. So if the input is like [1, 4, 2] and [1, 2, 4], then the output ...

Read More

Linked List Random Node in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 445 Views

Suppose we have a singly linked list, we have to find a random node's value from the linked list. Here each node must have the same probability of being chosen. So for example, if the list is [1, 2, 3], then it can return random node in range 1, 2, and 3.To solve this, we will follow these steps −In the getRandom() method, do the following −ret := -1, len := 1, v := xwhile v is not nullif rand() is divisible by len, then ret := val of vincrease len by 1v := next of vreturn retExample(C++)Let us see ...

Read More

Find Bottom Left Tree Value in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 278 Views

Suppose we have a binary tree. We have to find the left most value of the last row of that tree. So if the tree is like −Then the output will be 7, as the last row is [7, 4], and left most element is 7.To solve this, we will follow these steps −initially define ans and lvl variable as 0define one method called solve(), this will take the tree node, and level, the level is initially 0. This will act as follows −if node is null, then returnif level > lvl, then ans := value of node and lvl ...

Read More

Minimum Score Triangulation of Polygon in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 406 Views

Suppose we have a value N, consider a convex N-sided polygon with vertices labelled A[0], A[i], ..., A[N-1] are in clockwise order. Now suppose we want to triangulate the polygon into N-2 triangles. For each triangle, the value of that triangle is the product of the labels of the vertices, and the total score of the triangulation will be the sum of these values over all N-2 triangles in the triangulation. We have to find the smallest possible total score that we can achieve with some triangulation of the polygon. So if the input is [1, 2, 3], then the ...

Read More

Elimination Game in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 941 Views

Suppose we have a list of sorted integers from 1 to n. That is starting from left and ending at right, we have to remove the first number and every other number afterward until we reach the end of the list. We will repeat the previous step again, but this time from right to left, remove the right most number and every other number from the remaining numbers. We will repeat the steps again, alternating left to right and right to left, until one single number remains. We have to find the last number that remains starting with a list ...

Read More

Find Largest Value in Each Tree Row in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 239 Views

Suppose we have a binary tree, we have to find the largest elements of each level of that tree. So if the tree is like −Then the output will be [3, 5, 8]To solve this, we will follow these steps −Define an array called ansdefine a recursive function solve(), this will take tree node, and level, the level is initially 0. this method will act like −if node is null, then returnif level = size of ans, then insert node value into ans, otherwise ans[level] := max of ans[level] and node valuecall solve(left subtree of node, level + 1)call solve(right ...

Read More
Showing 1691–1700 of 3,768 articles
« Prev 1 168 169 170 171 172 377 Next »
Advertisements