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
Server Side Programming Articles
Page 293 of 2109
Program to design a queue that moves the most recently used element to the end of it in Python
We need to design a queue that moves the most recently used element to the end. The queue is initialized with integers 1 to n, and we implement a function that moves an element from a given position to the end while returning that element's value. For example, with n = 5 (queue contains [1, 2, 3, 4, 5]) and move operations at positions 5, 2, 3, and 1, the output will be 5, 2, 4, 1 respectively. Algorithm Steps To solve this efficiently, we use a square root decomposition approach − Find the correct ...
Read MoreProgram to find out the sum of the maximum subarray after a operation in Python
Given an array of integers, we can perform one operation where we replace any element array[i] with its squared value array[i] * array[i]. Our goal is to find the maximum possible subarray sum after performing this operation. So, if the input is like array = [4, 1, -2, -1], then the output will be 17. If we replace the value in array[0] with its squared value, the array becomes [16, 1, -2, -1]. The maximum subarray from this is [16, 1], which has the sum 16 + 1 = 17. Algorithm We use dynamic programming with ...
Read MoreProgram to restore the array from adjacent pairs in Python
Suppose we have a 2D array called adPair of size n-1 where each adPair[i] has two elements [ui, vi] representing that the elements ui and vi are adjacent in an array called nums. In nums there are n unique elements. We have to find the array nums. So, if the input is like adPair = [[3, 2], [4, 5], [4, 3]], then the output will be [2, 3, 4, 5]. Algorithm To solve this, we will follow these steps ? my_map := an empty map to store adjacency list for different ...
Read MoreProgram to find out distance between two nodes in a binary tree in Python
Suppose, we are given a binary tree and are asked to find the distance between two nodes in the binary tree. We find out the edges between the two nodes like in a graph and return the number of edges or the distance between them. A node of a tree has the structure as below − data : right : left : So, if the input is like 5 ...
Read MoreProgram to find k-th largest XOR coordinate value in Python
Suppose we have one m x n matrix and another value k. The value of coordinate (a, b) of the matrix is the XOR of all matrix[i, j] where i is in range (0 to a) and j is in range (0 to b). We have to find the k-th largest value (1-indexed) of all the coordinates of the matrix. Problem Example Consider the following matrix: 5 2 1 6 If k = 1, then the output will be 7 because the value of coordinate (0, 1) is 5 XOR ...
Read MoreProgram to change minimum characters to satisfy one of three conditions in Python
Suppose we have two strings s and t with only lowercase letters. In one operation, we can change any character in s or t to any lowercase letter. We have to satisfy one of the following three conditions ? Every letter in s is strictly smaller than every letter in t in the alphabet. Every letter in t is strictly smaller than every letter in s in the alphabet. Both s and t consist of only one distinct letter. We have to find the minimum number of ...
Read MoreProgram to find decode XORed permutation in Python
Suppose we have an array enc that represents an encoded permutation. There is an array perm that is a permutation of the first n (odd) positive integers. This list is encoded into array enc of length n-1, such that enc[i] = perm[i] XOR perm[i+1]. We need to find the original array perm. So, if the input is like enc = [2, 5, 6, 3], then the output will be [7, 5, 0, 6, 5]. Here [7 XOR 5, 5 XOR 0, 0 XOR 6, 6 XOR 5] = [2, 5, 6, 3]. Algorithm To solve this, we ...
Read MoreProgram to find minimum number of people to teach in Python
Suppose we have a number n, an array called languages, and an array called friendships. There are n languages numbered from 1 through n, languages[i] represents a set of languages the ith user knows, and friendships[i] holds a pair [ui, vi] denoting a friendship between users ui and vi. We can select one language and teach it to some users so that all friends can communicate with each other. We have to find the minimum number of users required to teach. Note that friendships are not transitive, so if x is a friend of y and y is a ...
Read MoreProgram to find minimum cost to hire k workers in Python
Suppose we have an array called quality for each different worker, and have another array called wages and a value K. The i-th worker has a quality[i] and a minimum wage expectation wage[i]. We want to hire K workers to form a paid group. When we are hiring a group of K workers, we must pay them according to the following rules: Each worker in the paid group should be paid in the ratio of their quality by comparing with others in the paid group. Every worker in the paid ...
Read MoreProgram to check a string can be split into three palindromes or not in Python
Given a string, we need to check whether it can be split into exactly three palindromic substrings. A palindrome reads the same forwards and backwards. So, if the input is like s = "levelpopracecar", then the output will be True because we can split it like "level", "pop", "racecar" − all are palindromes. Algorithm To solve this, we will follow these steps − Create a 2D DP table to store which substrings are palindromes Fill the DP table using dynamic programming Try all possible ways to split the string into three parts Check if all ...
Read More