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 Niharikaa Aitam
77 articles
Program to find longest equivalent sublist after K increments in Python
The longest equivalent sublist after K increments is the longest contiguous portion of a list where we can make all elements equal by performing at most K increment operations. This problem uses a sliding window technique on a sorted array to find the optimal solution. Given a list nums and integer k, we need to find the maximum length of a sublist where all elements can be made equal using at most k increment operations on individual elements. Problem Example Consider this input scenario where we can increment 9 once and 6 four times to get the ...
Read MoreProgram to find length of longest distinct sublist in Python
A distinct sublist is a continuous portion of a list in which all the elements are different from each other. The goal is to find the maximum length of such a sublist within a given list of elements. In Python, a list is an ordered and mutable data structure in which sequence of elements are enclosed in square brackets []. The elements in a list can be of any datatype such as integer, float, string etc., and each element in a list has a unique index position which starts from 0. Using Sliding Window with set() ...
Read MoreProgram to find length of longest anagram subsequence in Python
In Python, a string is an immutable data structure in which sequence of characters are enclosed in double("") or single quotes(''). In some cases, we need to find the length of the longest subsequence of characters that can be rearranged to form a palindrome. This type of subsequence is referred to as a palindromic anagram subsequence. An anagram is a word or phrase formed by rearranging the letters of another word or phrase by using all the original letters exactly once. In other words, two strings are said to be anagrams of each other if they contain the same ...
Read MoreProgram to return number of smaller elements at right of the given list in Python
In Python, a list is an ordered and mutable data structure where elements are enclosed in square brackets []. In some scenarios, we may need to count how many elements to the right of each element in the list are smaller than it. For example, given the list [5, 2, 6, 1], for element 5 at index 0, there are 2 smaller elements (2 and 1) to its right. For element 2 at index 1, there is 1 smaller element (1) to its right. Using Binary Search with bisect_left() Function The bisect_left() function from the bisect module ...
Read MoreProgram to find which element occurs exactly once in Python
In Python, data structures such as strings, lists, etc., may contain duplicate values. In some scenarios, we may need to find which element appears only once while all other elements appear multiple times. Let's explore different methods to find which element occurs exactly once in Python. Using Bitwise XOR The XOR (Exclusive OR) is a bitwise operator that returns 1 if the corresponding bits of two operands are different, otherwise it returns 0 if they are the same. In Python, we use the ^ operator to perform XOR operations. The XOR approach works because of these properties: ...
Read MoreTo print all elements in sorted order from row and column wise sorted matrix in Python
In Python, we may work with matrices that are sorted in a particular order. A common case is a matrix where each row and each column is sorted in increasing order, but the entire matrix is not necessarily sorted. In such cases, we need to extract all elements and print them in a fully sorted order. Understanding Row and Column-wise Sorted Matrix A row and column-wise sorted matrix means each row is sorted from left to right, and each column is sorted from top to bottom. For example, consider the following matrix − matrix ...
Read MorePython - Check if all elements in a List are same
In Python, a list is a collection of ordered and mutable elements enclosed in square brackets []. Each element in a list has a unique position index, starting from 0. It can accept duplicate elements. In some cases, we may need to check if all the elements in a list are identical or not to detect a special condition in algorithms. Let's go through the different methods to check if all elements in a list are the same or not. Using set() Function The Python set() function (built-in) creates an unordered collection of unique elements. To check ...
Read MoreGenerate two output strings depending upon occurrence of character in input string in Python
In Python, a string is a sequence of characters enclosed within single quotes '' or double quotes "". When processing strings, we often need to analyze character occurrence patterns to generate different outputs based on frequency. To generate two output strings based on character occurrence in Python, we follow these steps: Count the frequency of each character in the input string using a dictionary or collections.Counter Separate characters into two categories: ...
Read MoreBasic calculator program using Python program
In this tutorial, we are going to build a basic calculator in Python. A calculator provides multiple arithmetic operations to users, allowing them to select one option and perform the respective calculation. Following are the arithmetic operations that we can perform using a basic calculator − Addition Subtraction Multiplication Division Steps in Developing the Basic Calculator Following are the steps involved in creating a basic calculator in Python − Defining Arithmetic Functions First, we define all the arithmetic operations that can be performed by using a basic calculator ? def ...
Read MoreCheck if both halves of the string have same set of characters in Python
In Python, a string is one of the data structures that is a sequence of characters enclosed within single quotes '' or double quotes "". It is immutable, i.e., once a string is created, it cannot be changed. When we want to check if both halves of the string have the same set of characters in Python, we can follow the steps below − First, based on the length of the string, we have to split the string into two halves. Next, convert each half into a set of characters using ...
Read More