Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Arnab Chakraborty
Page 154 of 377
4Sum II in Python
The 4Sum II problem asks us to find how many tuples (i, j, k, l) exist such that A[i] + B[j] + C[k] + D[l] equals zero, given four lists A, B, C, D of integers. This is an optimization problem that can be solved efficiently using a hash map approach. Problem Understanding Given four lists of integers with the same length N (0 ≤ N ≤ 500), we need to count tuples where the sum of elements at indices (i, j, k, l) from lists A, B, C, D respectively equals zero. For example, with A = ...
Read MoreHexspeak in Python
Hexspeak is a novelty form of spelling that uses hexadecimal digits to represent words. In Python, we can convert decimal numbers to Hexspeak by first converting to hexadecimal, then replacing specific digits with letters. Understanding Hexspeak Conversion The conversion process involves these steps: Convert decimal number to hexadecimal Replace digit 0 with letter 'O' and digit 1 with letter 'I' Keep hexadecimal letters A-F unchanged If any other digits (2-9) exist, return "ERROR" Valid Hexspeak characters are: {"A", "B", "C", "D", "E", "F", "I", "O"} Implementation Using Built-in hex() Function Here's a ...
Read MoreDiet Plan Performance in Python
A diet plan performance problem tracks a dieter's points based on their calorie consumption over consecutive k days. The dieter gains or loses points depending on whether their total calories fall below a lower bound, above an upper bound, or within the normal range. Problem Description Given an array calories[i] representing daily calorie consumption, we need to evaluate every consecutive sequence of k days and calculate points based on these rules: If total calories < lower bound: lose 1 point If total calories > upper bound: gain 1 point Otherwise: no change in points ...
Read MoreSingle-Row Keyboard in python
A single-row keyboard requires finger movement between keys to type characters. Given a keyboard layout string and a word, we need to calculate the total time to type the word, where time equals the absolute distance between character positions. Problem Understanding Consider a keyboard layout "abcdefghijklmnopqrstuvwxyz" and word "hello": Start at index 0 (position 'a') Move to 'h' (index 7): distance = |0 - 7| = 7 Move to 'e' (index 4): distance = |7 - 4| = 3 Move to 'l' (index 11): distance = |4 - 11| = 7 Stay at 'l' (index 11): ...
Read MoreCheck If a Number Is Majority Element in a Sorted Array in Python
A majority element in an array is an element that appears more than N/2 times in an array of length N. When dealing with a sorted array, we can use binary search to efficiently find the first and last occurrence of the target element and check if its frequency exceeds the majority threshold. Algorithm Overview To solve this problem, we need to: Find the leftmost (first) occurrence of the target using binary search Find the rightmost (last) occurrence of the target using binary search ...
Read MoreLargest Unique Number in Python
In this problem, we need to find the largest number that appears exactly once in a list. If no such number exists, we return -1. Problem Understanding Given a list like [5, 2, 3, 6, 5, 2, 9, 6, 3], we need to ? Count the frequency of each number Find numbers that appear exactly once Return the largest among those unique numbers Method 1: Using Dictionary for Counting We can use a dictionary to count occurrences and then find the largest unique number ? def largest_unique_number(numbers): ...
Read MoreRemove Vowels from a String in Python
Removing vowels from a string is a common string manipulation task in Python. We can accomplish this using several approaches like replace(), list comprehension, or regular expressions. Using replace() Method The simplest approach is to use the replace() method to remove each vowel one by one ? def remove_vowels(s): vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] for vowel in vowels: s = s.replace(vowel, "") return s text = "iloveprogramming" result ...
Read MoreTwo Sum Less Than K in Python
The Two Sum Less Than K problem asks us to find the maximum sum of two distinct elements in an array that is less than a given value K. If no such pair exists, we return -1. Given an array A and integer K, we need to find the maximum sum S where S = A[i] + A[j], i < j, and S < K. Algorithm Steps Here's the approach to solve this problem ? Initialize result as -1 If array has only one element, return -1 (need at least 2 elements) Use nested loops ...
Read MoreIndex Pairs of a String in Python
Finding index pairs of substrings within a text is a common string matching problem. Given a text string and a list of words, we need to find all index pairs [i, j] where the substring text[i:j+1] matches any word in the list. Problem Understanding For example, with text "ababa" and words ["aba", "ab"], we find overlapping matches ? "ab" at positions [0, 1] and [2, 3] "aba" at positions [0, 2] and [2, 4] Algorithm Steps The approach uses nested loops to check all possible substrings ? Initialize an empty result ...
Read MoreFixed Point in Python
A fixed point in an array is an index where the element's value equals its position. Given a sorted array of unique integers, we need to find the smallest index i such that A[i] == i. For example, in the array [-10, -5, 0, 3, 7], index 3 has value 3, making it a fixed point. Algorithm To solve this problem, we follow these steps − Iterate through each index from 0 to length of array If i == A[i], return i as the fixed point If no fixed point is found, return -1 ...
Read More