Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 195 of 377

Beautiful Arrangement in C++

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

Suppose we have N integers from 1 to N. We will define a beautiful arrangement as an array that is constructed by these N numbers completely if one of the following is true for the ith position (1

Read More

Random Pick with Weight in C++

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

Suppose we have an array w of positive integers, were w[i] describes the weight of index i, we have to define a function pickIndex() which randomly picks an index in proportion to its weight.So if the input is like [1, 3], call pickIndex() five times, then the answer may come as − 0, 1, 1, 1, 0.To solve this, we will follow these steps −Define an array v, Through the initializer, initialize asn := w[0]for i in range 1 to size of ww[i] := w[i] + w[i – 1]n := w[i]v = wThe pickIndex() will work as follows −Take a ...

Read More

Remove Zero Sum Consecutive Nodes from Linked List in C++

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

Suppose we have given the head of a linked list; we have to repeatedly delete consecutive sequences of nodes that sum to 0 until there are no such sequences. So after doing so, we have to return the head of the final linked list. So if the list is like [1, 2, -3, 3, 1], then the result will be [3, 1].To solve this, we will follow these steps −Create a node called dummy, and store 0 into it, set next of dummy := headcreate one map m, store dummy for the key 0 into m, set sum = 0while ...

Read More

Next Permutation in Python

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

Suppose we want to implement the next permutation method, that method rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, this method will rearrange it as the lowest possible order (That is actually, sorted in ascending order). The replacement must be in-place and do not use any extra memory. For example, if the Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.1, 2, 3 → 1, 3, 2 3, 2, 1 → 1, 2, 3 1, 1, 5 → 1, 5, 1Let us see the steps −found ...

Read More

Combination Sum in Python

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

Suppose we have a set of candidate numbers (all elements are unique) and a target number. We have to find all unique combinations in candidates where the candidate numbers sum to the given target. The same repeated number may be chosen from candidates unlimited number of times. So if the elements are [2, 3, 6, 7] and the target value is 7, then the possible output will be [[7], [2, 2, 3]]Let us see the steps −We will solve this in recursive manner. The recursive function is named as solve(). This takes an array to store results, one map to ...

Read More

Combination Sum II in C++

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

Suppose we have a set of candidate numbers (all elements are unique) and a target number. We have to find all unique combinations in candidates where the candidate numbers sum to the given target. The same number will not be chosen from candidates more than once. So if the elements are [2, 3, 6, 7, 8] and the target value is 8, then the possible output will be [[2, 6], [8]]Let us see the steps −We will solve this in recursive manner. The recursive function is named as solve(). This takes index, an array a, the integer b and another ...

Read More

Jump Game in Python

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

Suppose we have an array of non-negative integers; we are initially positioned at the first index of the array. Each element in the given array represents out maximum jump length at that position. We have to determine if we are able to reach the last index or not. So if the array is like [2, 3, 1, 1, 4], then the output will be true. This is like jump one step from position 0 to 1, then three step from position 1 to the end.Let us see the steps −n := length of array A – 1for i := n ...

Read More

Merge Intervals in Python

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

Suppose we have a collection of intervals, we have to merge all overlapping intervals. So if the intervals are like [[1, 3], [2, 6], [8, 10], [15, 18]], then the intervals after merging will be [[1, 6], [8, 10], [15, 18]]. This is because there were two intervals those are overlapping, the intervals are [1, 3] and [2, 6], these are merged to [1, 6]Let us see the steps −if interval list length is 0, then return a blank listsort the interval list using quicksort mechanismstack := an empty stack, and insert intervals[0] into the stackfor i in range 1 ...

Read More

Spiral Matrix II in Python

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

Suppose we have a positive integer n, we have to generate a square matrix with n2 elements in spiral order. So if n = 5, then the matrix will be −12341213145111615610987Let us see the steps −set (row1, col1) := (0, 0) and (row2, col2) := (n, n), and create one matrix called res, then fill it with 0s, and set num := 1while num n2, then breakfor i in range row1 + 1 to row2, res[i, col2-1] = num, incase num by 1if num > n2, then breakfor i in range col2 – 2 down to col1 – 1, ...

Read More

Unique Paths in Python

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

Suppose there is a robot is located at the top-left corner of a n x m grid (n rows and m columns). The robot can only move either down side or right side at any point in time. The robot wants to reach the bottom-right corner of the grid (marked 'END in the diagram below). So we have to find how many possible unique paths are there? For example if m = 3 and n = 2, then the grid will be like below −RoboENDThe output will be 3, So there are total 3 different ways to reach from start ...

Read More
Showing 1941–1950 of 3,768 articles
« Prev 1 193 194 195 196 197 377 Next »
Advertisements