Found 33676 Articles for Programming

Program to reverse the directed graph in Python

Arnab Chakraborty
Updated on 20-Oct-2020 10:53:25

1K+ Views

Suppose we have a directed graph, we have to find its reverse so if an edge goes from u to v, it now goes from v to u. Here input will be an adjacency list, and if there are n nodes, the nodes will be (0, 1, ..., n-1).So, if the input is likethen the output will beTo solve this, we will follow these steps −ans := a list of n different lists, where n is number of verticesfor each index i, and adjacent list l in graph, dofor each x in l, doinsert i at the end of ans[x]return ... Read More

Program to reverse a linked list in Python

Arnab Chakraborty
Updated on 20-Oct-2020 10:51:14

543 Views

Suppose we have a linked list, we have to reverse it. So if the list is like 2 -> 4 -> 6 -> 8, then the new reversed list will be 8 -> 6 -> 4 -> 2.To solve this, we will follow this approach −Define one procedure to perform list reversal in recursive way as solve(head, back)if head is not present, then return headtemp := head.nexthead.next := backback := headif temp is empty, then return headhead := tempreturn solve(head, back)Let us see the following implementation to get better understanding −Exampleclass ListNode:    def __init__(self, data, next = None):   ... Read More

Program to find string after removing consecutive duplicate characters in Python

Arnab Chakraborty
Updated on 20-Oct-2020 10:48:27

422 Views

Suppose we have a string s, we repeatedly delete the first consecutive duplicate characters. We have to find the final string.So, if the input is like s = "xyyyxxz", then the output will be "z", as "yyy" are the first consecutive duplicate characters which will be deleted. So we have "xxxz". Then "xxx" will be deleted to end up with "z".To solve this, we will follow these steps −stack := a new stacki := 0while i < size of s, doif stack is not empty and top of stack is same as s[i], thenx := delete last element from stackwhile ... Read More

Program to count minimum invalid parenthesis to be removed to make string correct in Python

Arnab Chakraborty
Updated on 20-Oct-2020 10:45:21

446 Views

Suppose we have a string of parentheses; we have to write a function to compute the minimum number of parentheses to be removed to make the string correct (each open parenthesis is eventually closed).So, if the input is like "(()))(", then the output will be 2, as the correct string is "(())", remove ")(".To solve this, we will follow these steps −total := 0, temp := 0for each p in s, doif p is same as "(", thentotal := total + 1otherwise when p is same as ")" and total is not 0, thentotal := total - 1otherwise, temp := ... Read More

Program to remove sublist to get same number of elements below and above k in C++

Arnab Chakraborty
Updated on 20-Oct-2020 10:43:04

169 Views

Suppose we have a list of numbers called nums and another number k, we can remove any sublist at most once from the list. We have to find the length of the longest resulting list such that the amount of numbers strictly less than k and strictly larger than k is the same.So, if the input is like nums = [6, 10, 8, 9, 3, 5], k = 6, then the output will be 5, as if we remove the sublist [9] then we will get [6, 10, 8, 3, 5] and there are two numbers [3, 5] which are ... Read More

Program to find minimum number of intervals to be removed to remove overlaps in C++

Arnab Chakraborty
Updated on 20-Oct-2020 10:37:18

492 Views

Suppose we have a set of intervals; we have to find the minimum number of intervals that should be removed to make the rest of the intervals non-overlapping. So if the intervals are [[8, 10], [3, 5], [6, 9]], then the output will be 1, as we have to remove [6, 9] to make all others are non-overlapping.To solve this, we will follow these steps −n := size of arrayif n is 0, then return 0count := 1sort the array based on the end time of the intervalsend := end date of the first intervalfor i in range 1 to ... Read More

Program to remove duplicate entries in a linked list in Python

Arnab Chakraborty
Updated on 20-Oct-2020 10:34:42

213 Views

Suppose we have a linked list of numbers, we have to remove those numbers which appear multiple times in the linked list (hold only one occurrence in the output), we also have to maintain the order of the appearance in the original linked list.So, if the input is like [2 -> 4 -> 6 -> 1 -> 4 -> 6 -> 9], then the output will be [2 -> 4 -> 6 -> 1 -> 9].To solve this, we will follow these steps −if node is not null, thenl := a new settemp := nodeinsert value of temp into lwhile ... Read More

Program to update elements in a given range in Python

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

338 Views

Suppose we have a list of numbers called nums and a list of operations. Here each operation has three fields [L, R, X], this indicated that we should increment by X all the elements from indices L to R (inclusive). We have to apply all operations and return the final list.So, if the input is like nums = [8, 4, 2, -9, 4] operations = [ [0, 0, 3], [1, 3, 2], [2, 3, 5] ], then the output will be [11, 6, 9, -2, 4], as the initial list was [8, 4, 2, -9, 4].Performing first operation [0, 0, ... Read More

Program to find how many total amount of rain we can catch in Python

Arnab Chakraborty
Updated on 20-Oct-2020 07:45:17

138 Views

Suppose we have an array of n non-negative integers. These are representing a height where the width of each bar is 1, we have to compute how much water it is able to catch after raining. So the map will be like −Here we can see there are 8 blue boxes, so the output will be 8.To solve this, we will follow these steps −Define a stack st, water := 0 and i := 0while i < size of heightif is stack is empty or height[stack top] >= height[i], then push i into stack, increase i by 1otherwisex := stack ... Read More

Program to find maximum product of contiguous subarray in Python

Arnab Chakraborty
Updated on 20-Oct-2020 07:43:33

1K+ Views

Suppose we have an array called nums, we have to find the product of elements of a contiguous subarray within an array (containing at least one number) which has the largest product. So if the array is [1, 9, 2, 0, 2, 5], the output will be 18, as contiguous subarray [1, 9, 2] has max product.To solve this, we will follow these steps −max_list := list of size nums, and fill with 0min_list := list of size nums, and fill with 0min_list := list of size nums, and fill with 0for i in range 1 to length of numsmax_list[i] ... Read More

Advertisements