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 12 of 377
Program to find out the maximum points collectable in a game in Python
Suppose we are playing a game of cards. We are given several cards arranged linearly with a number on each of them. The numbers on the cards are randomly distributed; and at the beginning and the end of the cards, two cards are inserted with the number 1 on them. Now, in the game, we have to collect the maximum points by picking up the given cards. The cards are represented in an array 'cards' where the elements in the array represent the number of cards[i]. When we pick up card i, we collect points cards[i - 1] * cards[i] ...
Read MoreProgram to find out if we win in a game in Python
Suppose we are playing a two-player game where there are n number of marbles and in each round, a player has to take a positive square number of marbles. If a player can't take that square number of marbles, he/she loses. So, given a number n, we have to find out if we can win the game or not. We always make the first turn and select an optimal number of marbles. So, if the input is like 14, then the output will be True. Because at the first turn, we take 9 marbles. That leaves 5 marbles from ...
Read MoreProgram to find out the minimum number of intercountry travels in a road trip in Python
In road trip planning, we often need to find the shortest path between cities while minimizing intercountry travels. This problem combines graph traversal with country border crossing penalties to find both the minimum border crossings and total cost. Problem Understanding We have a list of roads R where each element is (x, y, cost) representing a road from city x to city y with given cost. We also have a list C where each element contains cities belonging to the same country. Given a starting city s and destination city e, we need to find the minimum intercountry ...
Read MoreProgram to find out the conversion rate of two currencies in Python
Currency conversion is a common problem in financial applications. Given three arrays containing source currencies, destination currencies, and their conversion rates, we need to find the conversion rate between any two currencies using the available exchange paths. The problem provides curr_a (source currencies), curr_b (destination currencies), and conv_rate (conversion rates). We need to find the conversion rate from a source currency to a destination currency, returning 0 if no path exists. Problem Analysis If the input is src = "INR", dest = "JPY", curr_a = ["INR", "GBP", "EUR"], curr_b = ["GBP", "EUR", "JPY"], conv_rate = [0.009, 1.17, ...
Read MoreProgram to find dot product of run length encoded vectors in Python
Suppose we have two lists nums1 and nums2. Each of these two lists represents a vector in run-length encoded form. For example, a vector [1, 1, 1, 2, 2, 2, 2] is represented as [3, 1, 4, 2] (because there are 3 ones and 4 twos). We need to find the dot product of these two vectors, which is the sum of element-wise multiplication of items present in two vectors. So, if the input is like nums1 = [2, 7, 5, 3] and nums2 = [3, 5, 4, 2], then the output will be 109 because: The vectors ...
Read MoreProgram to check there is any common reachable node in a graph or not in Python
Suppose we have an edge list of a directed graph with n nodes numbered from 0 to n-1. Given two integer values a and b, we need to check whether there exists a node c such that we can reach both a and b from c. 0 1 2 3 4 ...
Read MoreProgram to find maximum coins we can get from disappearing coins matrix in Python
When we have a 2D matrix where each cell represents coins, we need to find the maximum coins we can collect with specific constraints. When we pick coins from matrix[r, c], all coins in adjacent rows (r-1 and r+1) and adjacent columns (c-1 and c+1) disappear. This problem can be solved using dynamic programming by treating it as two separate "House Robber" problems − first for each row, then for the resulting row sums. Problem Understanding Given the input matrix ? 2876 101042 5923 We can pick cells with coins 8, 6, 9, ...
Read MoreProgram to check all tasks can be executed using given server cores or not in Python
Suppose we have two lists: cores and tasks. The cores[i] indicates the number of cores available in the ith server, and tasks[i] indicates the number of cores needed to execute that task. Each task must be run on only one server, and a server may have multiple tasks to run. We need to check whether it's possible to run all tasks with the given server cores. For example, if cores = [10, 7] and tasks = [7, 3, 2, 2, 1], the output will be True because we can assign tasks[0] (7 cores) and tasks[1] (3 cores) to the ...
Read MoreProgram to check there is any forward path in circular cyclic list or not in Python
Suppose we have a circular list called nums where the first and last elements are neighbors. Starting from any index i, we can move nums[i] number of steps forward if nums[i] is positive, or backwards if it's negative. We need to check whether there is a cycle with length greater than one that moves only in one direction (either all forward or all backward). For example, if the input is nums = [-1, 2, -1, 1, 2], the output will be True because there is a forward path: [1 → 3 → 4 → 1]. Algorithm Steps ...
Read MoreProgram to count number of characters in each bracket depth in Python
Sometimes we need to count characters at different bracket depths in a string. This problem involves parsing a string with balanced brackets "(", ")" and characters "X", then counting how many "X" characters appear at each depth level. So, if the input is like s = "(XXX(X(XX))XX)", then the output will be [5, 1, 2] Bracket Depth Visualization ( X X X ( X ( X X ) ...
Read More