Found 26504 Articles for Server Side Programming

Program to find length of longest anagram subsequence in Python

Niharikaa Aitam
Updated on 01-Sep-2025 11:59:28

356 Views

In Python, a string is 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. To solve this, we need to count how many characters appear an even number of times and at most one character can appear an odd number of times. An anagram is a word or phrase formed by rearranging the letters of another word or phrase ... Read More

Program to return number of smaller elements at right of the given list in Python

Niharikaa Aitam
Updated on 01-Sep-2025 11:58:58

406 Views

In Python, a list is a ordered and mutable data structure where elements are enclosed in square braces []. In some scenarios, we may need to count how many elements to the right of each element in the list are smaller than it. Using Binary Search with bisect_left() Function The bisect_left() Function of the bisect method is used locate the insertion point for an element in a sorted list to maintain the list's sorted order. The insertion point returned by bisect_left() is the index where the element can be inserted without violating the sort order by placing it before any ... Read More

Program to find which element occurs exactly once in Python

Niharikaa Aitam
Updated on 01-Sep-2025 11:58:21

463 Views

In Python, datastructures such as string, list etc., may contain duplicates of a value. In few scenarios, we may need to find which element appears only once while all others elements appear multiple times. Let's go through different methods to find which element occurs exactly once in Python. Using Bitwise XOR The XOR is abbrivated as Exclusive OR is a bitwise operator which returns 1 if the corresponding bits of two operands are different otherwise, it returns 0 if they are same. In Python, we can use the ^ between two operands to perform Exclusive OR operation. Example Following is ... Read More

Program to partition color list in Python

Arnab Chakraborty
Updated on 10-Oct-2020 11:37:39

596 Views

Suppose we have a a list of color strings, these may contain "red", "green" and "blue", we have to partition the list so that the red come before green, and green come before blue.So, if the input is like colors = ["blue", "green", "blue", "red", "red"], then the output will be ['red', 'red', 'green', 'blue', 'blue']To solve this, we will follow these steps −green := 0, blue := 0, red := 0for each string in strs, doif string is same as "red", thenstrs[blue] := "blue"blue := blue + 1strs[green] := "green"green := green + 1strs[red] := "red"red := red + ... Read More

Program to check whether list can be split into sublists of k increasing elements in C++

Arnab Chakraborty
Updated on 10-Oct-2020 11:31:20

144 Views

Suppose we have a list of numbers called nums, and another number k, we have to check whether the list can be split into lists where each list contains k values and the values are consecutively increasing.So, if the input is like nums = [4, 3, 2, 4, 5, 6], k = 3, then the output will be True, as we can split the list into [2, 3, 4] and [4, 5, 6]To solve this, we will follow these steps −Define one mapfor each key it in mincrease m[it] by 1ok := truewhile (size of m is not 0 and ... Read More

Program to create linked list to binary search tree in Python

Arnab Chakraborty
Updated on 10-Oct-2020 11:24:02

1K+ Views

Suppose we have a sorted linked list node of size n, we have to create a binary search tree by Taking the value of the k = floor of (n / 2) the smallest setting it as the root. Then recursively constructing the left subtree using the linked list left of the kth node. And recursively constructing the right subtree using the linked list right of the kth node.So, if the input is like [2, 4, 5, 7, 10, 15], then the output will beTo solve this, we will follow these steps−Define a method solve(), this will take nodeif node ... Read More

Program to find tree level that has minimum sum in C++

Arnab Chakraborty
Updated on 10-Oct-2020 11:18:23

288 Views

Suppose we have a binary tree, the level of its root is 1, the level of its children is 2, and so on.We have to find the smallest level X such that the sum of all the values of nodes at level X is minimum. So if the tree is like −Output will be 2 as the sum is 4 – 10 = -6, which is minimum.To solve this, we will follow these steps −level := 1, sum := value of r, ansLevel := level, ansSum := sumdefine a queue q, insert given node r into qwhile q is not ... Read More

Program to perform level order traversal of binary tree in C++

Arnab Chakraborty
Updated on 10-Oct-2020 11:10:22

261 Views

Suppose we have a binary tree. We have to traverse this tree using level order traversal fashion. So if the tree is likeThe traversal sequence will be like: [1, 2, 3, 5, 4]To solve this, we will follow these steps −define queue que to store nodesinsert root into the que.while que is not empty, doitem := item present at front position of queueprint the value of itemif left of the item is not null, then insert left of item into queif right of the item is not null, then insert right of item into quedelete front element from queLet us ... Read More

Program to convert level order binary tree traversal to linked list in Python

Arnab Chakraborty
Updated on 10-Oct-2020 11:02:09

417 Views

Suppose we have a binary search tree, we have to convert it to a singly linked list using levelorder traversal.So, if the input is likethen the output will be [5, 4, 10, 2, 7, 15, ]To solve this, we will follow these steps −head := a new linked list nodecurrNode := headq := a list with value rootwhile q is not empty, docurr := delete first element from qif curr is not null, thennext of currNode := a new linked list node with value of currcurrNode := next of currNodeinsert left of curr at the end of qinsert right curr ... Read More

Program to traverse binary tree level wise in alternating way in Python

Arnab Chakraborty
Updated on 10-Oct-2020 10:54:23

142 Views

Suppose we have binary tree, we have to show the values of each level by alternating from going left-to-right and right-to-left.So, if the input is likethen the output will be [5, -10, 4, -2, -7, 15]To solve this, we will follow these steps −if root is null, thenreturn a new lists1 := a list initially insert roots2 := a new listres := a new listwhile s1 is not empty or s2 is not empty, dowhile s1 is not empty, donode := delete last element from s1if left of node is not null, theninsert left of node at the end of ... Read More

Advertisements