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 103 of 377
Program to convert linked list by alternating nodes from front and back in Python
Given a singly linked list, we need to rearrange it by alternating nodes from the back and front. The pattern is: last node, first node, second-last node, second node, and so on. For example, if the input is [1, 2, 3, 4, 5, 6, 7, 8, 9], the output will be [9, 1, 8, 2, 7, 3, 6, 4, 5]. Algorithm To solve this problem, we follow these steps − Store all node values in a list for easy access from both ends Traverse the linked list again and assign values alternately from the back ...
Read MoreProgram to find number of ways we can arrange symbols to get target in Python?
Suppose we have a list of non-negative numbers called nums and also have an integer target. We have to find the number of ways to arrange + and - signs in front of nums such that the expression equals the target. So, if the input is like nums = [2, 3, 3, 3, 2] target = 9, then the output will be 2, as we can have −2 + 3 + 3 + 3 + 2 and 2 + 3 + 3 + 3 − 2. Algorithm Approach This problem can be transformed into a subset sum ...
Read MoreProgram to find number of arithmetic sequences from a list of numbers in Python?
Finding arithmetic sequences from a list of numbers is a common problem in programming. An arithmetic sequence is a sequence where the difference between consecutive numbers remains constant. We need to count all contiguous arithmetic subsequences of length ≥ 3. For example, in the list [6, 8, 10, 12, 13, 14], we have arithmetic sequences: [6, 8, 10], [8, 10, 12], [6, 8, 10, 12], and [12, 13, 14]. Algorithm Approach The key insight is to use a sliding window approach: Track consecutive elements that form arithmetic sequences When a sequence breaks, calculate how many ...
Read MoreProgram to find start indices of all anagrams of a string S in T in Python
Suppose we have two strings S and T, we have to find all the start indices of S's anagrams in T. The strings consist of lowercase letters only and the length of both strings S and T will not be larger than 20 and 100. So, if the input is like S = "cab" T = "bcabxabc", then the output will be [0, 1, 5] as the substrings "bca", "cab" and "abc" are anagrams of "cab". Using Sliding Window with Character Count We can solve this using a sliding window approach with character frequency counting ? ...
Read MoreProgram to find numbers represented as linked lists in Python
Suppose we have two singly linked lists L1 and L2, each representing a number with least significant digits first. We need to find their sum as a linked list. For example, if L1 = [5, 6, 4] represents 465 and L2 = [2, 4, 8] represents 842, their sum is 1307, which should be returned as [7, 0, 3, 1]. Algorithm To solve this problem, we follow these steps ? Initialize carry = 0 and create a dummy result node Traverse both lists simultaneously while either list has nodes or carry exists For each position, ...
Read MoreProgram to find number of subsequences with i, j and k number of x, y, z letters in Python
When working with string subsequences, we often need to count specific patterns. This problem asks us to find the number of subsequences that contain i number of "x" characters, followed by j number of "y" characters, and then k number of "z" characters, where i, j, k ≥ 1. For example, with the string "xxyz", we can form subsequences like "xyz" (twice) and "xxyz" (once), giving us a total of 3 valid subsequences. Algorithm Approach We use dynamic programming to track the number of valid subsequences ending with each character ? x := count of ...
Read MoreProgram to find next board position after sliding the given direction once in Python
Suppose we have a 2048 game board representing the initial board and a string direction representing the swipe direction, we have to find the next board state. As we know in the 2048 game, we are given a 4 x 4 board of numbers (some of them are empty, represented in here with 0) which we can swipe in any of the 4 directions ("U", "D", "L", or "R"). When we swipe, all the numbers move in that direction as far as possible and identical adjacent numbers are added up exactly once. So, if the input is like ? ...
Read MoreProgram to check whether given list is in valid state or not in Python
Sometimes we need to check if a list can be completely partitioned into valid groups. This problem involves grouping numbers using specific rules to determine if the entire list is in a "valid state". Problem Definition Given a list of numbers, check if every number can be grouped using one of these rules: Contiguous pairs: (a, a) − two identical numbers Identical triplets: (a, a, a) − three identical numbers Consecutive triplets: (a, a+1, a+2) − three consecutive numbers Example For nums = [7, 7, 3, 4, 5], we can group [7, 7] ...
Read MoreProgram to find all upside down numbers of length n in Python
An upside down number (also called a strobogrammatic number) is a number that appears the same when rotated 180 degrees. The digits that remain valid when rotated are: 0, 1, 6, 8, and 9, where 6 becomes 9 and 9 becomes 6 when rotated. So, if the input is like n = 2, then the output will be ['11', '69', '88', '96']. Understanding Valid Digits When rotated 180 degrees ? 0 → 0 1 → 1 6 → 9 ...
Read MoreProgram to check all values in the tree are same or not in Python
Suppose we have a binary tree, we have to check whether all nodes in the tree have the same values or not. This is a common tree traversal problem that can be solved using recursive depth-first search. So, if the input is like ? 5 5 5 5 5 ...
Read More