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 132 of 377
Find the winner of a game where scores are given as a binary string in Python
When analyzing volleyball match scores represented as binary strings, we need to determine the winner based on specific scoring rules. In this scenario, 1 represents our team scoring a point, while 0 represents the opponent scoring a point. Game Rules The volleyball match follows these conditions: The first team to reach 15 points wins the match Exception: If both teams reach 14 points, the winner must maintain a 2-point lead Algorithm Steps To solve this problem, we follow these steps: Track scores for both teams using a counter array Process each ...
Read MoreMinimum Cuts can be made in the Chessboard such that it is not divided into 2 parts in Python
Suppose we have an A x B chessboard (matrix), we need to calculate the maximum number of cuts that can be made on this board such that it remains connected as one piece and is not divided into 2 separate parts. The key insight is that we can cut along the grid lines between squares. For an A x B chessboard, there are (A-1) horizontal lines and (B-1) vertical lines where cuts can be made. To keep the board connected, we can cut at most (A-1) × (B-1) grid intersections. Understanding the Problem Consider a 2 x ...
Read MoreFind the winner by adding Pairwise difference of elements in the array until Possible in Python
Suppose we have an array A of positive integers with unique elements. Two players P and Q are playing a game where at each move, a player picks two numbers a and b from the array and adds |a - b| to the array if it's not already present. The player who cannot make a move loses the game. We need to find the winner if player P always starts first. So, if the input is like A = [8, 9, 10], then the output will be P. Algorithm To solve this problem, we follow these steps: ...
Read MoreMinimum Cost to cut a board into squares in Python
Suppose we have a board of length p and width q; we have to break this board into p*q number of squares such that cost of breaking is as minimum as possible. Cutting cost for each edge will be given. The key insight is to use a greedy approach: always make the most expensive cut first, because expensive cuts will affect more pieces if made later. Problem Understanding Given: X_slice: costs of horizontal cuts Y_slice: costs of vertical cuts We need to find the minimum total cost to cut the board into unit squares. ...
Read MoreFind the time which is palindromic and comes after the given time in Python
A palindromic time is a time string that reads the same forwards and backwards. Given a time in 24-hour format (HH:MM), we need to find the next closest palindromic time. For example, "12:21" is palindromic, but "12:34" is not. The algorithm checks if we can form a palindrome with the current hour by reversing it to get the minutes. If not, we increment the hour and try again. Algorithm Steps The solution follows these steps − Extract the hour and minute from the input time string Reverse the hour digits to get potential palindromic minutes ...
Read MoreFind the Surface area of a 3D figure in Python
Finding the surface area of a 3D figure represented by a matrix involves calculating the exposed surfaces of each building block. Each cell A[i][j] represents the height of a building at position (i, j). Algorithm The surface area calculation considers ? Top and bottom surfaces ? Each cell contributes 2 units (top and bottom) Side surfaces ? Calculate height differences between adjacent cells Border surfaces ? Add full height for cells at matrix edges Step-by-Step Approach Initialize result = 0 For each cell (i, j), calculate height differences with top and left ...
Read MoreFind the sums for which an array can be divided into subarrays of equal sum in Python
Given an array of integers, we need to find all possible sum values for which the array can be divided into contiguous subarrays where each subarray has the same sum. If no such division is possible, we return -1. For example, if the input array is [2, 4, 2, 2, 2, 4, 2, 6], the output will be [6, 8, 12] because the array can be divided into subarrays with these equal sums: Sum 6: [{2, 4}, {2, 2, 2}, {4, 2}, {6}] Sum 8: [{2, 4, 2}, {2, 2, 4}, {2, 6}] Sum 12: [{2, 4, ...
Read MoreMinimize (max(A[i], B[j], C[k]) – min(A[i], B[j], C[k])) of three different sorted arrays in Python
Given three sorted arrays A, B, and C of different sizes, we need to find the minimum absolute difference between the maximum and minimum values of any triplet (A[i], B[j], C[k]) where each element comes from a different array. The key insight is to use a greedy approach: start from the largest elements of each array and progressively reduce the maximum element to minimize the difference. Algorithm Steps The algorithm works as follows ? Start with pointers at the end of each array (largest elements) Calculate the current difference between max and min of the ...
Read MoreFind the sum of maximum difference possible from all subset of a given array in Python
Given an array of n values, we need to find the sum of maximum differences possible from all subsets. For each subset, we calculate max(subset) - min(subset), then sum these differences across all possible subsets. For example, if we have array [1, 3, 4], the subsets and their max-min differences are: {1}: max=1, min=1, difference=0 {3}: max=3, min=3, difference=0 {4}: max=4, min=4, difference=0 {1, 3}: max=3, min=1, difference=2 {1, 4}: max=4, min=1, difference=3 {3, 4}: max=4, min=3, difference=1 {1, 3, 4}: max=4, min=1, difference=3 Total sum = 0+0+0+2+3+1+3 = 9 Algorithm Approach Instead ...
Read MoreFind the sum of all Truncatable primes below N in Python
A truncatable prime is a prime number that remains prime when digits are successively removed from either the left (left-truncatable) or right (right-truncatable) side. A number must be both left-truncatable and right-truncatable to be considered truncatable. For example, 3797 is truncatable because 3797, 797, 97, 7 (left truncation) and 3797, 379, 37, 3 (right truncation) are all prime numbers. Algorithm We use the Sieve of Eratosthenes to precompute all primes, then check each number for truncatability ? Generate all primes up to a reasonable limit using the sieve For each prime number, check if it's left-truncatable ...
Read More