Found 34494 Articles for Programming

How to Invert Python Tuple Elements?

Pranay Arora
Updated on 02-Nov-2023 12:33:44

129 Views

Python tuples store data in the form of individual elements. The order of these elements is fixed i.e (1, 2, 3) will remain in the same order of 1, 2, 3 always. In this article, we are going to see how to invert python tuple elements or in simple terms how to reverse the order of the elements. Let us 1st see a sample input and output − Input (5, 6, 7, 8) Output (8, 7, 6, 5) Let us now explore the various ways to invert tuple elements. Method 1: Using Tuple Slicing Slicing is ... Read More

Convert Lists into Similar key value lists in Python

Pranay Arora
Updated on 02-Nov-2023 12:31:36

102 Views

Given 2 separate lists, we are going to transform them into a single data structure by mapping them into a key-value data structure namely dictionary. The values of the 1st list will serve as keys and values from the 2nd list will serve as values of the corresponding keys in the dictionary. The relationship can be considered as 1 to 1 or 1 to many i.e. 1 key can have multiple values. Let us now see a sample input and output to better understand how we will be able convert Lists into Similar key value lists in Python in this ... Read More

Three-way partitioning of an Array without changing the relative ordering

Satvik Watts
Updated on 01-Nov-2023 12:55:18

115 Views

In this article, we will do a three-way partitioning of an array containing N integers. The approach is to use three queues. Each of these queues will be used to store the elements of one of the parts. After that, we can get the elements of each part from their respective queues without changing the relative order of the elements Problem Statement Given an array containing N integers and a range [LOW, HIGH], we need to partition the array into three parts such that − Elements less than LOW comes first Elements greater than LOW and lower than HIGH ... Read More

Sum of Array maximums after K operations by reducing max element to its half

Satvik Watts
Updated on 01-Nov-2023 12:53:00

99 Views

In this article, we will calculate the sum of the array maximums after K operations in which we reduce the maximum value of the array to its half. In the first approach, we will implement the brute force solution to this problem. In each iteration, we will use a for loop to find the maximum element in the array. We will then add this element to our answer, then we will reduce the element to its half. We will do this for K iterations as requested. Then, we will return the answer. In the second approach, we will use a ... Read More

Sort a given array which is already sorted based on the absolute value of the element

Satvik Watts
Updated on 01-Nov-2023 12:51:02

115 Views

In this article, we will sort the given array. The given array is already sorted on the basis of absolute value of the elements, we just need to sort the array based on the true values of the elements. In the first approach, we will use a sorting algorithm, like merge sort, bubble sort, insertion sort, quicksort and so on. In this example, we will use the inbuilt sort function to sort our array. In the second approach, we will use a double ended queue. We will push the positive elements in front of the double ended queue, and we ... Read More

Shortest path between two points in a Matrix with at most K obstacles

Satvik Watts
Updated on 01-Nov-2023 12:49:25

110 Views

In this article, we will find the shortest path between two points in a matrix. The matrix contains two types of cells, empty cells and cells which have obstacles. We are given an integer K, which represents that we can remove at most K obstacles to reach our destination. In the approach discussed in this article, we will do a breadth first search (BFS) on the matrix to find the shortest path. We will use a queue data structure, which will store a vector of integers. The vector will have 3 integers, the x coordinate, the y coordinate and the ... Read More

C++ Program to Find Minimum circular rotations to obtain a given numeric string by avoiding a set of strings

Satvik Watts
Updated on 01-Nov-2023 12:44:40

29 Views

In this article, we will find the minimum circular rotations that are needed to obtain a given numeric string, target, by avoiding a given set of strings. The target strings and the strings in the set of string both have a size of N. The initial string will be a string containing all zeroes and the length of the input string will also be N. In the approach discussed in this article, we will use a queue data structure and a set data structure. The queue data structure will hold the strings that we are currently at, i.e., the numeric ... Read More

Maximum count of pairs such that element at position i is included in a[i] pairs

Satvik Watts
Updated on 01-Nov-2023 12:40:29

52 Views

In this article, we will find the number of the pair of indices, such that an index i can be included in at most a[i] number of pairs. In the approach discussed in this article, we will use a priority queue data structure which will contain the elements of the array. The priority queue data structure will be a maximum heap which will allow us to get the current maximum elements of the array on log(N) time. It will also allow us to modify the elements and insert them back in, in the same amount of time. We will ... Read More

Maximum width of a Binary Tree with null values

Satvik Watts
Updated on 01-Nov-2023 12:34:52

103 Views

A binary tree is defined as a tree data structure where each has at most two children. The width of a binary tree for a level is defined as the number of nodes between the rightmost and leftmost nodes of that level, including the NULL nodes that come in between. The maximum width of a binary tree is defined as the maximum of all the widths at each level of the binary tree. In this first approach, we represent the binary tree as an array representation of the Heap data structure. At each level, the width of that level will ... Read More

Longest subsegment of 1’s formed by changing at most k 0’s (Using Queue)

Satvik Watts
Updated on 01-Nov-2023 12:30:09

68 Views

In this article, we will find the longest subsegment of 1’s which can be formed by changing at most k 0’s to 1’s. We will be using queue data structure to solve this problem. In the approach discussed in this article, we will use a queue data structure to find the longest subarray containing only 1’s, which can be formed by changing at most k 0’s into 1’s. The queue data structure will be used to store the indices of 0 elements that have occurred previously. Whenever we encounter a new 0, we will check the size of the queue. ... Read More

Advertisements