Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 277 of 377

Find the closest and smaller tidy number in C++

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

Suppose we have a number n, we have to find the closest and smaller tidy number of n. So a number is called tidy number, if all of its digits are sorted in non-decreasing order. So if the number is 45000, then the nearest and smaller tidy number will be 44999.To solve this problem, we will traverse the number from end, when the tidy property is violated, then we reduce digit by 1, and make all subsequent digit as 9.Example#include using namespace std; string tidyNum(string number) {    for (int i = number.length()-2; i >= 0; i--) {     ...

Read More

Duplicate Zeros in Python

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

Suppose we have a fixed length array of integers, we have to duplicate each occurrence of zero, shifting the remaining elements to the right side.Note that elements beyond the length of the original array are not written.So suppose the array is like [1, 0, 2, 3, 0, 4, 5, 0], then after modification it will be [1, 0, 0, 2, 3, 0, 0, 4]To solve this, we will follow these steps −copy arr into another array arr2, set i and j as 0while i < size of arr −if arr2[i] is zero, thenarr[i] := 0increase i by 1if i < ...

Read More

Find the count of numbers that can be formed using digits 3 and 4 only and having length at max N in C++

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

Given a number N. We have to find the count of such numbers that can be formed using digit 3 and 4. So if N = 6, then the numbers will be 3, 4, 33, 34, 43, 44.We can solve this problem if we look closely, for single digit number it has 2 numbers 3 and 4, for digit 2, it has 4 numbers 33, 34, 43, 44. So for m digit numbers, it will have 2m values.Example#include #include using namespace std; long long countNumbers(int n) {    return (long long)(pow(2, n + 1)) - 2; } int main() {    int n = 3;    cout

Read More

Defanging an IP Address in Python

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

Suppose we have a valid IPv4 IP address. We have to return the Defanged version of the IP address. A Defanged IP address is basically replace every period “.” by “[.]” So if the IP address is “192.168.4.1”, the output will be “192[.]168[.]4[.]1”To solve this, we will follow these steps −We will split the string using dot, then put each element separated by “[.]”ExampleLet us see the following implementation to get better understanding −class Solution(object):    def defangIPaddr(self, address):       address = address.split(".")       return "[.]".join(address) ob1 = Solution() print(ob1.defangIPaddr("192.168.4.1"))Input"192.168.4.1"Output"192[.]168[.]4[.]1"

Read More

Find the count of Strictly decreasing Subarrays in C++

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

Suppose we have an array A. And we have to find the total number of strictly decreasing subarrays of length > 1. So if A = [100, 3, 1, 15]. So decreasing sequences are [100, 3], [100, 3, 1], [15] So output will be 3. as three subarrays are found.The idea is find subarray of len l and adds l(l – 1)/2 to result.Example#include using namespace std; int countSubarrays(int array[], int n) {    int count = 0;    int l = 1;    for (int i = 0; i < n - 1; ++i) {       if ...

Read More

Find the compatibility difference between two arrays in C++

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

Consider there are two friends and now they want to test their bonding. So they will check, how much compatible they are. Given the numbers n, numbered from 1..n. And they are asked to rank the numbers. They have to find the compatibility difference between them. The compatibility difference is basically the number of mismatches in the relative ranking of the same movie given by them. So if A = [3, 1, 2, 4, 5], and B = [3, 2, 4, 1, 5], then the output will be 2. The compatibility difference is 2, as first ranks movie 1 before ...

Read More

Find the count of substrings in alphabetic order in C++

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

Suppose we have a string of length n. It contains only uppercase letters. We have to find the number of substrings whose character is occurring in alphabetical order. Minimum size of the substring will be 2. So if the string is like: “REFJHLMNBV”, and substring count is 2, they are “EF” and “MN”.So to solve this, we will follow these steps −Check whether str[i] + 1 is same as the str[i+1], if so, then increase the result by 1, and iterate the string till next character which is out of alphabetic order, otherwise continue.Example#include using namespace std; int countSubstr(string main_str) ...

Read More

Find the fractional (or n/k – th) node in linked list in C++

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

Suppose we have a singly linked list and the number k. We have to write a function to find the (n/k)th element, where n is the number of elements in the list. For decimals, we will choose the ceiling values. So if the list is like 1, 2, 3, 4, 5, 6, and k = 2, then output will be 3, as n = 6 and k = 2, then we will print n/k th node so 6/2 th node = 3rd node that is 3.To solve this we have to follow some steps like below −Take two pointers called ...

Read More

Find the next identical calendar year in C++

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

Suppose we have an year Y. Find next identical calendar year to Y. So the calendar of 2017 is identical with 2023.A year X is identical to given previous year Y if it matches these two conditions.x starts with the same day as year, If y is leap year, then x also, if y is normal year, then x also normal year.The idea is to check all years one by one from next year. We will keep track of number of days moved ahead. If there are total 7 moved days, then current year begins with same day. We also ...

Read More

Find the second last node of a linked list in single traversal in C++

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

Now we will see how to get the second last element in the linked list. Suppose there are few elements like [10, 52, 41, 32, 69, 58, 41], second last element is 58.To solve this problem, we will use two pointers one will point to current node, and another will point to previous node of the current position, then we will move until the next of current is null, then simply return the previous node.Example#include using namespace std; class Node {    public:       int data;       Node *next; }; void prepend(Node** start, int new_data) { ...

Read More
Showing 2761–2770 of 3,768 articles
« Prev 1 275 276 277 278 279 377 Next »
Advertisements