Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 159 of 377

Palindrome Linked List in Python

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

A palindrome linked list is a linked list that reads the same forwards and backwards. For example, [1, 2, 3, 2, 1] forms a palindrome because it's identical when reversed. The most efficient approach uses the two-pointer technique with partial reversal to check palindrome in O(n) time and O(1) space. Algorithm Steps Use fast and slow pointers to find the middle of the linked list Reverse the first half while traversing to the middle Compare the reversed first half with the second half Return True if all elements match, False otherwise Implementation ...

Read More

Find Numbers with Even Number of Digits in Python

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

Given a list of numbers, we need to count how many numbers have an even number of digits. For example, if the array is [12, 345, 2, 6, 7896], the output will be 2, since 12 (2 digits) and 7896 (4 digits) have even digit counts. Approach To solve this problem, we will follow these steps − Convert each integer to a string to easily count digits Check if the length of the string is even using modulo operation Increment counter for numbers with ...

Read More

Prime Arrangements in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 374 Views

We need to find the number of permutations of numbers 1 to n where prime numbers are placed at prime indices (1-indexed). Since the result can be large, we return it modulo 10^9 + 7. For example, if n = 5, the output is 12. One valid permutation is [1, 2, 5, 4, 3], while [5, 2, 3, 4, 1] is invalid because 5 (prime) is at index 1 (not prime). Algorithm The key insight is that we need to arrange prime numbers in prime positions and non-prime numbers in non-prime positions separately ? Count ...

Read More

Day of the Year in Python

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

Sometimes we need to find which day of the year a specific date represents. For example, February 10th, 2019 is the 41st day of the year. Python provides several ways to calculate this using date manipulation. Algorithm Approach To calculate the day of the year manually, we follow these steps − Create an array of days in each month: [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] Parse the date string to extract year, month, and day Check if the year is a leap year and adjust February days to 29 ...

Read More

Number of Equivalent Domino Pairs in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 382 Views

When working with domino pairs, we need to find equivalent dominos where one can be flipped to match another. Two dominos D[i] = [a, b] and D[j] = [c, d] are equivalent if a = c and b = d, or a = d and b = c (since dominos can be reversed). We need to count all pairs (i, j) where 0 ≤ i < j < length of dominos list. For example, with dominos [[1, 2], [2, 1], [3, 4], [6, 5]], we have one equivalent pair: [1, 2] and [2, 1]. Algorithm To solve ...

Read More

Relative Sort Array in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 850 Views

Suppose we have two arrays arr1 and arr2, where elements of arr2 are unique, and all elements in arr2 are also present in arr1. We need to sort the elements of arr1 such that the relative ordering of items follows the sequence in arr2. Elements not present in arr2 should be placed at the end in ascending order. For example, if arr1 is [2, 3, 1, 3, 2, 4, 6, 7, 9, 2, 19] and arr2 is [2, 1, 4, 3, 9, 6], the result will be [2, 2, 2, 1, 4, 3, 3, 9, 6, 7, 19]. ...

Read More

Distribute Candies to People in Python

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

The candy distribution problem involves giving candies to people in increasing amounts across multiple rounds. In the first round, person 1 gets 1 candy, person 2 gets 2 candies, and so on. In subsequent rounds, the candy count continues incrementally from where it left off. Problem Understanding Given candies total candies and n people, we distribute candies in rounds ? Round 1: Give 1, 2, 3, ..., n candies Round 2: Give n+1, n+2, n+3, ..., 2n candies Continue until candies run out The last person gets remaining candies (if any) Algorithm Steps ...

Read More

Occurrences After Bigram in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 222 Views

When working with text analysis, you might need to find words that appear after a specific bigram (a sequence of two consecutive words). This problem involves finding all words that come immediately after a given pair of words in a text. For example, in the text "lina is a good girl she is a good singer", if we're looking for words that follow the bigram "a good", the result would be ["girl", "singer"]. Algorithm To solve this problem, we follow these steps: Split the text into individual words Create an empty result list Iterate through ...

Read More

Last Stone Weight in Python

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

The Last Stone Weight problem simulates repeatedly smashing the two heaviest stones together until at most one stone remains. When two stones with weights x and y collide (where x ≤ y), they either both get destroyed if equal, or the lighter stone is destroyed and the heavier one becomes y - x. Problem Rules If x = y, then both stones are totally destroyed If x ≠ y, the stone of weight x is destroyed, and the stone of weight y becomes y - x For example, with stones [2, 7, 4, 1, 8, ...

Read More

Partition Array Into Three Parts With Equal Sum in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 536 Views

Given an array of integers, we need to determine if we can partition it into three non-empty parts with equal sums. This means finding two partition points that divide the array into three contiguous subarrays where each subarray has the same sum. Formally, we need to find indices i and j where i+1 < j such that: Sum of elements from index 0 to i equals Sum of elements from index i+1 to j-1 equals Sum of elements from index j to end For example, with input [0, 2, 1, -6, 6, -7, 9, 1, ...

Read More
Showing 1581–1590 of 3,768 articles
« Prev 1 157 158 159 160 161 377 Next »
Advertisements