Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 45 of 377

Program to find column index where left most 1 is present in a binary matrix in Python?

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

Suppose we have a 2D binary matrix. Here each row is sorted in ascending order with 0s coming before 1s, we have to find the leftmost column index with the value of 1. If there's no such result, return -1.So, if the input is like0001001100110010then the output will be 2, as the second column has left most 1 in entire matrix.To solve this, we will follow these steps:if matrix is empty, thenreturn -1N := row count of matrixM := column count of matrixi := 0, j := M - 1leftmost := -1while i < N and j >= 0, doif ...

Read More

Program to find higher number with same number of set bits as n in Python?\\n

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

Suppose we have a number n; we have to find the smallest next higher number with the same number of 1s as n in binary form.So, if the input is like n = 7, then the output will be 11, as 7 in binary is 0111 and next higher from 7 with three ones would be 11 which is 1011 in binary.To solve this, we will follow these steps:copy := n, zeros := 0, ones := 0while copy is not 0 and copy is even, dozeros := zeros + 1copy = copy / 2while copy is odd, doones := ones ...

Read More

Line Reflection in C++

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

Suppose we have n points on a 2D plane, we have to check whether there is any line parallel to y-axis that reflect the given points symmetrically, in other words, check whether there exists a line that after reflecting all points over the given line the set of the original points is the same that the reflected ones.So, if the input is like points = [[1, 1], [-1, 1]]then the output will be trueTo solve this, we will follow these steps −Define one set okn := size of pointsminVal := infmaxVal := -inffor initialize i := 0, when i < ...

Read More

Program to pruning a given binary tree in C++

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

Suppose we have a binary tree, where every node's value is either a 0 or a 1. We have to find the same tree where every subtree not containing a 1 has been deleted. So if the tree is like −To solve this, we will follow these steps −Define a recursive method solve(), this will take the node. the method will be like −If node is null, then return nullleft of node := solve(left of node)right of node := solve(right of node)if left of node is null and right of node is also null and node value is 0, then ...

Read More

Sort Transformed Array in C++

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

Suppose we have a sorted array of integer nums and integer values a, b and c. We have to apply a quadratic function of the form f(x) = ax^2 + bx + c to each element x in the array. And the final array must be in sorted order.So, if the input is like nums = [-4, -2, 2, 4], a = 1, b = 3, c = 5, then the output will be [3, 9, 15, 33]To solve this, we will follow these steps −Define function f(), that takes x, a, b, c −return ax^2 + bx + cFrom ...

Read More

Program to find starting index of the child who receives last balloon in Python?

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

Suppose we have n children standing in a circle, and they are waiting to get a balloon. The distribution is carried out starting with the kth child (first at index 0), and giving them a balloon they left the circle. Now every kth child gets a balloon going clockwise until there is only one child left that gets a balloon. So if we have n and k, we have to find the starting index of the child that receives the last balloon.So, if the input is like n = 3 k = 2, then the output will be 1, in ...

Read More

Bomb Enemy in C++

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

Suppose we have a 2D grid, here each cell is either a wall 'W', an enemy 'E' or that is empty '0', We have to find the maximum enemies we can kill using one bomb. The bomb kills all the enemies in the same row and column from the planted point until it hits the wall. And we can put bombs only on blank spaces.So, if the input is likethen the output will be 3, as placing bomb at the green place, it will kill three enemies.To solve this, we will follow these steps −ret := 0n := row count ...

Read More

Program to find sum of all elements of a tree in Python

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

Suppose we have a binary tree containing some values, we have to find the sum of all values in the tree.So, if the input is likethen the output will be 14To solve this, we will follow these steps −Define a function recurse() . This will take nodeval := value of nodeif left of node is not null, thenval := val + recurse(left of node)if right of node is not−null, thenval := val + recurse(right of node)return valFrom the main method, do the following −if not root is non−zero, thenreturn 0return recurse(root)Let us see the following implementation to get better understanding ...

Read More

Program to find minimum possible sum by changing 0s to 1s k times from a list of numbers in Python?

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

Suppose we have a list of numbers called nums and another value k. We have to following operation k times: Select any number on the list. In the binary representation of that number, select a bit that is 0 and make it 1. Finally, we have to return the minimum possible sum of all the numbers after performing k operations. If the answer is too high, return result mode 10^9+7.So, if the input is like nums = [4, 7, 3] k = 2, then the output will be 17, as the binary representation of 4 is 100, 3 is 011, ...

Read More

Design Hit Counter in C++

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

Suppose we want to design a hit counter which counts the number of hits received in the past 5 minutes. There will be a function and that accepts a timestamp parameter in the second unit and we may assume that calls are being made to the system in chronological order (so, the timestamp is monotonically increasing). We also assume that the earliest timestamp starts at 1.It is possible that several hits arrive roughly at the same time.So we will call the hit() function to hit and getHits() function to get the count of hits.To solve this, we will follow these ...

Read More
Showing 441–450 of 3,768 articles
« Prev 1 43 44 45 46 47 377 Next »
Advertisements