Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 187 of 377

Minimum Time Visiting All Points in C++

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

Suppose there are some points given as an array. We have to find the minimum time in seconds to visit all points. There are some conditions.In one second, it can move vertically, horizontally and diagonallyWe have to visit the points in the same order as they appear in the array.So if the points are [(1, 1), (3, 4), (-1, 0)], then output will be 7. If we check the sequence for the shortest route, the sequence will be (1, 1), (2, 2), (3, 3), (3, 4), (2, 3), (1, 2), (0, 1), (-1, 0)To solve this we will just find ...

Read More

Subtract the Product and Sum of Digits of an Integer in C++

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

Suppose we have one number. We have to find the sum of digits and product of digits. After that find the difference between sum and product. So if the number is 5362, then sum is 5 + 3 + 6 + 2 = 16, and 5 * 3 * 6 * 2 = 180. So 180 – 16 = 164To solve this take each digit and add and multiply one by one, then return the difference.ExampleLet us see the following implementation to get better understanding −#include using namespace std; class Solution {    public:       int subtractProductAndSum(int n) {          int prod = 1;          int sum = 0;          for(int t = n;t;t/=10){             sum += t % 10;             prod *= t % 10;          }          return prod - sum;       } }; main(){    Solution ob;    cout

Read More

Element Appearing More Than 25% In Sorted Array in C++

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

Suppose we have an array A. There are few elements. Some elements are common. We have to return an element that is appearing more than 25% spaces in the array. So if A = [1, 2, 4, 4, 4, 4, 5, 5, 6, 6, 7, 7], Here 4 has occurred four times. This is more than 25% of 12 (size of the array)To solve this, we will follow these steps −Read elements and store their respective frequenciesIf the frequency is greater than 25% of the array size, then return the result.ExampleLet us see the following implementation to get better understanding ...

Read More

Ugly Number in C++

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

Ugly numbers are those numbers whose prime factors are 2, 3 or 5. From 1 to 15, there are 11 ugly numbers 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15. The numbers 7, 11, 13 are not ugly because they are prime. The number 14 is not ugly because in its prime factor the 7 will come. So suppose we want to check the 10th ugly number. The value will be 12.Let us see the following algorithm to get an idea −AlgorithmgetUglyNumbers(n)Input − The number of terms.Output − Find nth Ugly numbers.Begin    define array named uglyNum ...

Read More

Missing Number in Python

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

Suppose we have a list of numbers from 0 to n. There is one number that is missing. We have to find the missing number in an efficient approach. So if A = [0, 1, 2, 3, 4, 5, 7, 8, 9], missing number is 6.To solve this, we will use the binary search approach.sort the list in ascending orderhigh = length of A, and low = 0while low < high, domid = low + (high – low)/2if A[mid] > midhigh = midotherwiselow = mid + 1return lowExampleLet us see the following implementation to get a better understanding −class Solution(object): ...

Read More

Find Numbers with Even Number of Digits in Python

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

Suppose we have a list of numbers. We have to count the numbers that has even number of digit count. So if the array is like [12, 345, 2, 6, 7896], the output will be 2, as 12 and 7896 has even number of digitsTo solve this, we will follow these steps −Take the list and convert each integer into stringif the length of string is even, then increase count and finally return the count valueExampleLet us see the following implementation to get better understanding −class Solution(object):    def findNumbers(self, nums):       str_num = map(str, nums)     ...

Read More

Move Zeroes in Python

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

Suppose we have an array to hold some numbers. There are non-zero values as well as zero values. So we have to send all zeros to the right without changing the relative order of other numbers. So if the array is like [0, 1, 5, 0, 3, 8, 0, 0, 9], then the final array will be [1, 5, 3, 8, 9, 0, 0, 0, 0]To solve this, we will follow these steps −Suppose index = 0for i = 0 to the length of Aif A[i] != 0, thenA[index] := A[i]index := index + 1for i = index to the ...

Read More

Replace Elements with Greatest Element on Right Side in C++

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

Suppose we have an array A. We have to replace every element by the greatest element on the right side of this element. And replace the last one by -1. So if A = [5, 17, 40, 6, 3, 8, 2], then it will be [40,40,8,8,8,2,-1]To solve this, we will follow these steps −We will read the array element from right to left.take e := -1for i := n – 1 to 0temp := ee := max between e and array[i]array[i] := tempreturn arrayExampleLet us see the following implementation to get better understanding −#include using namespace std; void print_vector(vector v){    cout

Read More

Range Sum Query - Immutables in C++

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

Suppose we have an array of integers. We have to find the sum of the elements present from index i to j. Two things we have to keep in mind that the array will be immutable, so elements will not be altered, and there will be multiple such queries. So we have to care about the execution time for a large number of queries. Suppose the array is like A = [5, 8, 3, 6, 1, 2, 5], then if query is (A, 0, 3), then it will be 5 + 8 + 3 + 6 = 22.To solve this, ...

Read More

Find N Unique Integers Sum up to Zero in C++

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

Suppose we have an integer n. We have to return any array that contains n unique integers, such that they add up to 0. So if input is n = 5, then one possible output will be [-7, -1, 1, 3, 4]To solve this, we will follow these steps −take an array A as final answer, and take x := 0for i in range 0 to n – 2A[i] = (i + 1)x := x + i + 1A[n – 1] = xreturn AExampleLet us see the following implementation to get better understanding −#include using namespace std; void print_vector(vector v){    cout

Read More
Showing 1861–1870 of 3,768 articles
« Prev 1 185 186 187 188 189 377 Next »
Advertisements