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 46 of 377
Program to find minimum total distance between house and nearest mailbox in Python
Suppose we have an array called houses and have another value k. Here houses[i] represents the location of the ith house along a street, we have to allocate k mailboxes in the street, and find the minimum total distance between each house and its nearest mailbox. So, if the input is like houses = [6, 7, 9, 16, 22] and k = 2, then the output will be 9 because if we place mailbox at 7 and 18, then minimum total distance from each house is |6-7|+|7-7|+|9-7|+|16-18|+|22-18| = 1+0+2+2+4 = 9. Algorithm To solve this problem, we ...
Read MoreProgram to find minimum cost for painting houses in Python
Suppose there is an array of size m representing m houses in a small city. Each house must be painted with one of the n colors (labeled from 1 to n). Some houses are already painted, so no need to paint them again. Houses colored with the same adjacent color form a neighborhood. We have the array houses, where houses[i] represents the color of house i. If the color value is 0, the house is not colored yet. We have another array called cost, which is a 2D array where cost[i][j] represents the cost to color house i with ...
Read MoreProgram to find where the ball lands in a grid box in Python
Suppose we are given an m x n grid box, where each cell has a board that is positioned either from the top-right to bottom-left, or from the top-left to the bottom-right. A ball is dropped from each top cell, and we need to determine where each ball lands at the bottom of the box. The grid is represented as a matrix where 1 means the diagonal board spans from top-left to bottom-right, and -1 means it spans from top-right to bottom-left. If a ball gets stuck or goes out of bounds, we return -1 for that position. ...
Read MoreProgram to implement a queue that can push or pop from the front, middle, and back in Python
A flexible queue is a data structure that allows insertion and removal of elements from the front, middle, and back positions. This is useful when you need dynamic access to different parts of your queue. Understanding the Queue Operations Our queue supports six main operations ? push_from_front() − Insert element at the beginning push_from_middle() − Insert element at the middle position push_from_back() − Insert element at the end pop_from_front() − Remove and return first element pop_from_middle() ...
Read MoreProgram to determine if two strings are close in Python
Suppose we have two strings, s and t, we have to check whether s and t are close or not. We can say two strings are close if we can attain one from the other using the following operations − Exchange any two existing characters. (like abcde to aecdb) Change every occurrence of one existing character into another existing character, and do the same with the other characters also. (like aacabb → bbcbaa (here all a's are converted to b's, and vice versa)) We can use the operations on either ...
Read MoreProgram to find out the vertical area between two points where no point lies and is the widest in Python
Suppose, we are given n number of points as (x, y). A vertical area is an area that is extended infinitely along the y-axis. We have to find out the vertical area between two points such that no other point is inside the area and is the widest. So, if the input is like pts = [[10, 9], [11, 11], [9, 6], [11, 9]], then the output will be 1. ...
Read MoreProgram to find out the inheritance order in a family in Python
In family inheritance systems, we need to track the order of succession when the head of the family dies. This program implements a family inheritance tracker that manages births, deaths, and determines the inheritance order using a depth-first search approach. Problem Overview The inheritance follows these rules: The eldest living member is the head of the family When the head dies, their direct descendants inherit in order Children inherit before siblings The inheritance order follows a depth-first traversal of the family tree ...
Read MoreProgram to find minimum jumps to reach home in Python
Suppose there is an array called forbidden, where forbidden[i] indicates that the bug cannot jump to the position forbidden[i], and we also have three values a, b, and x. A bug's home is at position x on the number line. It is at position 0 initially. It can jump by following rules − Bug can jump exactly a positions to the right. Bug can jump exactly b positions to the left. Bug cannot jump backward twice in a row. Bug cannot jump ...
Read MoreProgram to find out the minimum rotations needed to maximize the profit from a Ferris wheel in Python
Suppose there is a Ferris wheel with four cabins and each cabin can contain four passengers. The wheel rotates counter-clockwise, and for each rotation, it costs run amount of money. We have an array cust that contains n items where each item i signifies the number of people waiting to get into the Ferris wheel before the i-th rotation. To board the wheel, each customer pays an amount board for one anti-clockwise rotation. The people waiting in line should not wait if there is any vacant seat available in any cabin. ...
Read MoreProgram to find minimum deletions to make string balanced in Python
Suppose we have a string s with only two characters 's' and 't'. We can delete any number of characters of s to make the string balanced. We can say, s is balanced when there is no pair of indices (i, j) such that i < j and s[i] = 't' and s[j]= 's'. We have to find the minimum number of deletions needed to make s balanced. So, if the input is like s = "sststtst", then the output will be 2 because we can either remove the characters at indices 2 and 6 ("sststtst" to "sssttt"), or ...
Read More