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 55 of 377
Program to merge strings alternately using Python
Suppose we have two strings s and t. We have to merge them by adding letters in alternating fashion, starting from s. If s and t are not of same length, add the extra letters onto the end of the merged string. So, if the input is like s = "major" and t = "general", then the output will be "mgaejnoerral". Since t is larger than s, we have added extra part "ral" at the end. Algorithm To solve this, we will follow these steps − Initialize i := j := 0 ...
Read MoreProgram to find longest nice substring using Python
A nice substring is one where every letter appears in both uppercase and lowercase. For example, "bBb" is nice because 'b' appears as both 'b' and 'B'. This tutorial shows how to find the longest nice substring in Python. Problem Understanding Given a string, we need to find the longest substring where every letter appears in both uppercase and lowercase forms. If multiple substrings have the same maximum length, return the one that appears first. Algorithm Approach We'll use a nested loop approach to check all possible substrings: For each starting position, build a ...
Read MoreProgram to find largest perimeter triangle using Python
Suppose we have an array nums of positive lengths, we have to find the largest perimeter of a triangle by taking three values from that array. When it is impossible to form any triangle of non-zero area, then return 0. So, if the input is like [8, 3, 6, 4, 2, 5], then the output will be 19. Triangle Inequality Rule For three sides to form a triangle, they must satisfy the triangle inequality: the sum of any two sides must be greater than the third side. For sides a, b, c where a ≥ b ≥ ...
Read MoreProgram to find out the dot product of two sparse vectors in Python
A sparse vector is a vector where most elements are zero. The dot product of two vectors is calculated by multiplying corresponding elements and summing the results. For sparse vectors, we can optimize by skipping zero elements. Problem Statement Given two sparse vectors represented as lists, we need to calculate their dot product efficiently. The vectors are stored as objects with a nums attribute containing the list elements. Example If vector1 = [1, 0, 0, 0, 1] and vector2 = [0, 0, 0, 1, 1], the dot product is ? 1×0 + 0×0 + ...
Read MoreProgram to find out the number of boxes to be put into the godown in Python
Suppose we have two arrays containing integers. One list contains the height of some unit width boxes and another array contains the height of rooms in the godown. The rooms are numbered 0...n, and the height of the rooms is provided in their respective indexes in the array godown. We have to find out the number of boxes that can be pushed into the godown. A few things have to be kept in mind: The boxes can't be put one on another. The order of the boxes can be changed. ...
Read MoreProgram to find out if the strings supplied differ by a character in the same position in Python
Suppose, we are provided an array that contains several strings of the same length. We have to find out if any two of the supplied strings differ by a single character at the same position. If this difference is present, we return True, otherwise we return False. So, if the input is like words = ['pqrs', 'prqs', 'paqs'], then the output will be True. The output is True because all strings differ at index 1 ('q', 'r', 'a'), so any two pairs have a difference in the same position. Algorithm Steps To solve this, we will follow ...
Read MoreProgram to find out the index of the most frequent element in a concealed array in Python
Suppose we are given a class called TestArray that contains a private array which can only contain values 0 or 1, and two public member functions length() and query(). The function length() returns the length of the array and the function query() returns three different values comparing various values in the array. The function takes four indices p, q, r, s as input and works like this: If all four values at the given indices are the same (either all 0s or all 1s), it returns 4 If three values ...
Read MoreProgram to find out the index in an array where the largest element is situated in Python
Finding the index of the largest element in an array is a common problem. This tutorial demonstrates a unique approach where the array is encapsulated in a class and can only be accessed through specific member functions. Problem Statement We have a class called TestArray that contains an array not accessible by other classes. It provides two public member functions: length() − returns the length of the array compare(l, r, x, y) − compares sums of two subarrays The compare() function works as follows: ...
Read MoreProgram to find the diameter of a n-ary tree in Python
An n-ary tree is a tree where each node can have any number of children. The diameter of an n-ary tree is the longest path between any two nodes in the tree. This path doesn't necessarily have to pass through the root node. So, if the input is like ? 14 27 ...
Read MoreProgram to find the root of a n-ary tree in Python
In an n-ary tree, each node can have multiple children. When given nodes of an n-ary tree in an array, we need to find the root node and reconstruct the tree. The root node is the only node that has no parent (in-degree = 0). Algorithm To find the root of an n-ary tree, we use the concept of in-degree: Create an in-degree counter for all nodes For each node, increment the in-degree of its children The node with in-degree 0 is the root Return the root node for tree reconstruction Implementation ...
Read More