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
Programming Articles
Page 569 of 2547
Check 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 MoreAdding K to each element in a Python list of integers
In this article, we will learn how to add a constant K value to each element in a Python list of integers. A list is a data type in Python that stores a sequence of items separated by commas ? items = [item1, item2, item3...] Suppose we have a list of integers and a constant value "k." We need to add this "k" to each item in the list. For example ? # Input numbers = [5, 10, 15, 20] k = 5 # Output: On adding 5 to each element ...
Read MoreWriting files in the background in Python
In this tutorial, we will learn about multi-threading in Python to perform multiple tasks simultaneously. Python's threading module allows us to write files in the background while executing other operations concurrently. We'll demonstrate this by writing data to a file in the background while calculating the sum of numbers in a list. Here are the key steps involved ? Import the threading module Create a class inheriting from threading.Thread Implement the file writing logic in the run() method Start the background ...
Read MoreSum 2D array in Python using map() function
In this tutorial, we are going to find the sum of a 2D array using map() function in Python. The map() function takes two arguments: function and iterable. It applies the function to every element of the iterable and returns a map object. We can convert the map object into a list or other iterable. Let's see how to find the sum of a 2D array using the map function ? Steps to Sum a 2D Array Initialize the 2D array using nested lists. Pass the sum ...
Read MoreRun Length Encoding in Python
Run-length encoding compresses a string by grouping consecutive identical characters and representing them as character + count. For example, "aaabbc" becomes "a3b2c1". However, the example above shows a different approach - counting total occurrences of each character rather than consecutive runs. Let's explore both approaches. Character Frequency Encoding This approach counts total occurrences of each character ? import collections def character_frequency_encoding(string): # Initialize ordered dictionary to maintain character order count_dict = collections.OrderedDict.fromkeys(string, 0) # Count occurrences of ...
Read More