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 Arnab Chakraborty
Page 48 of 377
Program to arrange linked list nodes based on the value k in Python
Suppose we have a singly linked list and a value k. We need to rearrange the nodes so that all nodes with values less than k come first, followed by nodes equal to k, and finally nodes greater than k. The relative ordering within each group must be preserved. For example, if we have a linked list [4, 3, 6, 6, 6, 10, 8] and k = 6, the output will be [4, 3, 6, 6, 6, 10, 8]. Algorithm To solve this problem, we will follow these steps − Create three ...
Read MoreProgram to find folded list from a given linked list in Python
Suppose we have a linked list. We have to take the first half of the linked list and fold over the second half then merge the intersecting nodes by taking their sum. Finally, we have to return the resulting head of the linked list. So, if the input is like [5, 8, 1, 2, 4, 7, 5], then the output will be [2, 5, 15, 10]. Understanding the Folding Process The folding process works as follows: Split the linked list into two halves Reverse the first half ...
Read MoreProgram to remove last occurrence of a given target from a linked list in Python
Suppose we have a singly linked list and a target value, we have to remove the last occurrence of the target from the given list. So, if the input is like [5, 4, 2, 6, 5, 2, 3, 2, 4, 5, 4, 7], target = 5, then the output will be [5, 4, 2, 6, 5, 2, 3, 2, 4, 4, 7] Algorithm To solve this, we will follow these steps − Initialize head := node Set k := null, prev := null ...
Read MoreProgram to check whether list of points form a straight line or not in Python
Suppose we have a list of coordinates in a Cartesian plane, we have to check whether the coordinates form a straight line segment or not. So, if the input is like coordinates = [(5, 5), (8, 8), (9, 9)], then the output will be True, as these points are forming a line segment with a slope 1. Algorithm To solve this, we will follow these steps − (x0, y0) := coordinates[0] (x1, y1) := coordinates[1] for i in range 2 to size of ...
Read MoreProgram to find lexicographically smallest non-palindromic string in Python
Given a palindromic string, we need to change exactly one character to make it the lexicographically smallest non-palindromic string possible. The strategy is to iterate through the first half of the string and replace the first non-'a' character with 'a'. If all characters in the first half are 'a', we change the last character to 'b'. Example Let's see how this works with the string "level" ? def find_smallest_non_palindrome(s): # Check first half for non-'a' characters for i in range(len(s) // 2): ...
Read MoreProgram to find minimum number of bricks required to make k towers of same height in Python
Suppose we have a list of tower heights, and a positive value k. We want to select k towers and make them all the same height by adding more bricks, but using as few bricks as possible. We have to find the minimum number of bricks needed to pick k towers and make them the same height. So, if the input is like heights = [4, 7, 31, 14, 40] k = 3, then the output will be 17, as we can select towers with heights [7, 14, 31] and make them all height 31, requiring (31-7) + (31-14) ...
Read MoreProgram to find sum of non-adjacent elements in a circular list in python
Suppose we have a list of numbers representing a circular list. We need to find the largest sum of non-adjacent elements, where the first and last elements are considered adjacent due to the circular nature. For example, if the input is nums = [10, 3, 4, 8], the output will be 14 by selecting elements 10 and 4. We cannot select 10 and 8 because they are adjacent in the circular arrangement. Algorithm Approach Since the list is circular, we need to handle two cases separately: Case 1: Include the first element but exclude the ...
Read MoreProgram to find the sum of largest K sublist in Python
When we have a list of numbers and need to find the maximum sum of a contiguous sublist from the list concatenated k times, we can use Kadane's algorithm with optimization for large k values. The key insight is that we only need to process at most 2 complete iterations of the original list to find the optimal solution, then add the contribution from remaining iterations if beneficial. Algorithm Steps The algorithm works as follows: Initialize sum (s), answer (ans), and lowest prefix sum (lo) to 0 Iterate through the list at most min(k, 2) ...
Read MoreProgram to find area of largest island in a matrix in Python
Suppose we have a binary matrix where 1 represents land and 0 represents water. An island is a group of connected 1s whose perimeter is surrounded by water. We can assume that the edges of the matrix are surrounded by water. We need to find the area of the largest island in the matrix. So, if the input matrix is like ? 0011111 0000000 0111100 0011000 0000011 0000010 Then the output will be 6, as the largest island has 6 connected land cells. Algorithm To solve this problem, we use Depth-First ...
Read MoreProgram to find largest distance pair from two list of numbers in Python
We need to find the maximum value of the expression |a[i] - a[j]| + |b[i] - b[j]| + |i - j| for all valid pairs (i, j) where 0 ≤ i < j < n from two lists A and B of equal length. The key insight is to transform the absolute value expression using the mathematical property that |x| + |y| = max(x+y, x-y, -x+y, -x-y). This allows us to convert the problem into finding maximum and minimum values efficiently. Understanding the Problem Given two lists A and B, we want to maximize: |a[i] - ...
Read More