Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 355 of 377

Sort an Array in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Apr-2020 523 Views

Suppose we have an array of integers; we have to sort them in ascending order. So if the array is like [5, 2, 3, 1], then the result will be [1, 2, 3, 5]To solve this, we will follow these steps −Make one method called partition, this will take array, low and highset pivot := lowfor i in range low to high – 1if nums[i] < nums[high], then swap(nums[i] and nums[pivot]), increase pivot by 1swap nums[pivot] and nums[high]Define a method called sortArr(), this will take array, low and highif low >= high, then returnpartitionIndex := partition(nums, low, high)sortArr(nums, low, partitionIndex ...

Read More

Possible Bipartition in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Apr-2020 277 Views

Suppose we have a set of N people (they are numbered 1, 2, ..., N), we would like to split everyone into two subgroups of any size. Now each person may dislike some other people, and they should not go into the same group. So, if dislikes[i] = [a, b], it indicates that it is not allowed to put the people numbered a and b into the same group. We have to find if it is possible to split everyone into two groups in this way.So if the input is like N = 4 and dislike = [[1, 2], [1, ...

Read More

Maximum Level Sum of a Binary Tree in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Apr-2020 338 Views

Suppose we have the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on. We have to return the smallest level X such that the sum of all the values of nodes at level X is maximal. So if the tree is like −Then the output will be 2, The level 1 sum = 1, level 2 sum is 7 + 0 = 7, level 2 sum is 7 + (-8) = -1, so max is of level 2, so output is 2.To solve this, we will follow ...

Read More

Can I Win in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Apr-2020 321 Views

Suppose in a game called "100 games, " two players take turns adding, to a running total, any integer from 1 through 10. The player who first causes the running total to reach or exceed 100, he/she wins. So what if we change the game so that players cannot re-use integers?For example, if two players take turns drawing from a common pool of numbers of 1..15 without replacement until they reach a total >= 100.So suppose given an integer maxChoosableInteger and another integer desired total, determine if the first player to move can force a win, assuming both players play ...

Read More

Find Right Interval in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Apr-2020 494 Views

Suppose we have a of intervals, for each of the interval i, check whether there exists an interval j whose start point is bigger than or equal to the endpoint of the interval i, which can be called that j is on the "right" of i. For any interval i, we have to store the minimum interval j's index, which indicates that the interval j has the minimum start point to build the "right" relationship for interval i. When the interval j doesn't exist, then store -1 for the interval i. And finally, we need output the stored value of ...

Read More

Delete Leaves With a Given Value in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Apr-2020 217 Views

Suppose we have a binary tree and an integer target, we have to delete all the leaf nodes with value target. We have to keep in mind that once we delete a leaf node with a value target if it's parent node becomes a leaf node and has the value target, it should also be deleted (we need to continue doing that until we can't). So if the tree is like below, and the target is 2, then the final tree will be like the last one −To solve this, we will follow these steps −Define a recursive method called ...

Read More

Valid Palindrome in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 28-Apr-2020 901 Views

Suppose we have a string with alphanumeric values and symbols. There are lower case and uppercase letters as well. We have to check whether the string is forming a palindrome or not by considering only the lowercase letters (uppercases will be converted into lower case), other symbols like a comma, space will be ignored.Suppose the string is like “A Man, a Plan, a Canal: Panama”, then by considering these rules, it will be “amanaplanacanalpanama”. This is a palindrome.To solve this, follow these steps −define x = “”read each character c in str −if c is lowercase letter or number, then ...

Read More

Minimum Path Sum in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 28-Apr-2020 300 Views

Suppose we have a m x n matrix filled with non-negative integers, find a path from top left corner to bottom right corner which minimizes the sum of all numbers along its path. Movements can only be either down or right at any point in time. So for example, if the matrix is like below131151421The output will be 7, the path will be 1, 3, 1, 1, 1, this will minimize the sumLet us see the steps −a := number of rows, b := number of columnsi := a – 1, j := b - 1while j >= 0matrix[a, j] ...

Read More

Print Immutable Linked List in Reverse in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 31-Mar-2020 525 Views

Suppose we have an immutable linked list, we have to print out all values of each node in reverse with the help of the following interface −ImmutableListNode − This is an interface of an immutable linked list, we are given the head of the list.We have to use the following functions to access the linked list −ImmutableListNode.printValue() − This will print value of the current node.ImmutableListNode.getNext() −This will return the next node.So if the list is like: [0, -4, -1, 3, -5], then the output will be [-5, 3, -1, -4, 0]To solve this, we will follow these steps −Define ...

Read More

Airplane Seat Assignment Probability in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 31-Mar-2020 328 Views

Suppose n passengers board an airplane with exactly n seats. If the first passenger has lost the ticket and picks a seat randomly. But after that, the rest of passengers will follow these operations −Take their own seat written in the ticket if it is still available, Pick other seats randomly when they find their seat occupiedSo we have to find what is the probability that the n-th person can get his own seat? So if the input is 2, then the output will be 0.5. So the second person has a probability of 0.5 to get the second seat ...

Read More
Showing 3541–3550 of 3,768 articles
« Prev 1 353 354 355 356 357 377 Next »
Advertisements