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 56 of 377
Program to find minimum changes required for alternating binary string in Python
Suppose we have a binary string s. Let us consider an operation where we can flip one bit. The string s is called alternating string if no two adjacent characters are same. We have to find the minimum number of operations needed to make s alternating. So, if the input is like s = "11100011", then the output will be 3 because if we flip bits at position 1, 4 and 7, then, it will be "10101010", then all are alternating. Approach The key insight is that there are only two possible alternating patterns: Pattern ...
Read MoreProgram to find sum of unique elements in Python
Suppose we have an array nums with few duplicate elements and some unique elements. We have to find the sum of all the unique elements present in nums. So, if the input is like nums = [5, 2, 1, 5, 3, 1, 3, 8], then the output will be 10 because only unique elements are 8 and 2, so their sum is 10. Approach To solve this, we will follow these steps − count := a dictionary holding all unique elements and their frequency ans := 0 ...
Read MoreProgram to find maximum number of balls in a box using Python
Suppose we have a ball factory where we have n balls numbered from l to r (both inclusive) and have an infinite number of boxes numbered from 1 to infinity. We put each ball in the box with a number equal to the sum of digits of the ball's number. For example, ball number 123 will be put in box number 1 + 2 + 3 = 6. Given two values l and r, we need to find the maximum number of balls in any single box. Example If the input is l = 15 and r = ...
Read MoreProgram to find latest valid time by replacing hidden digits in Python
Suppose we have a string representing time in the format hh:mm. Some digits are hidden (represented by ?). Considering a 24-hour clock, valid times are between 00:00 and 23:59. We need to find the latest valid time by replacing the hidden digits. For example, if the input is "1?:?5", the output will be "19:55" as we want the latest possible time. Algorithm To solve this problem, we need to consider the constraints of a 24-hour time format ? First digit of hour: can be 0, 1, or 2 Second digit of hour: depends on the ...
Read MoreProgram to find the highest altitude of a point in Python
Suppose there is a biker going on a road trip through n different points at various altitudes. The biker starts from point 0 with altitude 0. Given a sequence called gain with n elements, gain[i] represents the net gain in altitude between points i and i + 1. We need to find the highest altitude reached during the trip. For example, if gain = [-4, 2, 6, 1, -6], the altitudes at each point would be [0, -4, -2, 4, 5, -1], making the highest altitude 5. Algorithm To solve this problem, we follow these steps ? ...
Read MoreProgram to find number of rectangles that can form the largest square in Python
Suppose we have an array called rect where rect[i] has two elements [len_i, wid_i], where len_i and wid_i are representing the length and width of ith rectangle respectively. Now we can cut the ith rectangle to form a square whose side length is of k if both k
Read MoreProgram to recover decode XORed array in Python
Suppose we have a hidden array arr with n non-negative integers. This array is encoded into another array enc of length n-1, where enc[i] = arr[i] XOR arr[i+1]. Given the encoded array and the first element of the original array, we need to recover the original array. So, if the input is like enc = [8, 3, 2, 7], first = 4, then the output will be [4, 12, 15, 13, 10]. Algorithm To solve this, we will follow these steps − arr := an array with only one element first for i in range ...
Read MoreProgram to find total amount of money we have in bank in Python
Suppose you put 1Rs in a bank on the first day (Monday). Each subsequent day of the week, you put in 1Rs more than the previous day. On every new Monday, you start with 1Rs more than the previous Monday. This creates a pattern where the amount increases both daily and weekly. For example, if n = 17 days: Week 1: 1Rs (Mon) + 2Rs (Tue) + 3Rs (Wed) + 4Rs (Thu) + 5Rs (Fri) + 6Rs (Sat) + 7Rs (Sun) = 28Rs Week 2: 2Rs (Mon) + 3Rs (Tue) + 4Rs (Wed) + 5Rs (Thu) + 6Rs ...
Read MoreProgram to find maximum units that can be put on a truck in Python
Suppose we have a set of boxes represented as a 2D array called boxTypes, where boxTypes[i] contains two elements [number of boxes of type i, number of units per box of type i]. Now we also have another value k, which is the maximum number of boxes that can be put on that truck. We can select any boxes to put on the truck as long as the number of boxes does not cross k. We have to find the maximum total number of units that can be put on the truck. Problem Understanding So, if the input ...
Read MoreProgram to check whether String Halves Are Alike in Python
Suppose we have a string s whose length is even. We have to split this string into two different halves of same lengths. So consider 'a' is the first half and 'b' is the second half. We say two strings are alike when they have the same number of vowels (uppercase or lowercase). We have to check whether 'a' and 'b' are alike or not. So, if the input is like s = "talent", then the output will be True because two halves are "tal" and "ent", they are alike because they have only one vowel each. Algorithm ...
Read More