Given a positive integer N, we need to check if we can reorder its digits to form a power of 2. The reordered number must have no leading zeros. For example, if N = 812, we can rearrange it to 128, which is 2^7, so the answer is True. Algorithm To solve this problem, we will follow these steps − Start with i = 1 (which is 2^0) While i ≤ 1000000000, do the following: Convert i to string and sort its digits Convert N to string and sort its digits If both sorted ... Read More
Given a binary string, we can apply two types of operations any number of times to maximize its numeric value: Replace substring "00" with "10" Replace substring "10" with "01" We need to find the maximum possible binary string after applying these operations. Understanding the Problem The key insight is that we want to move as many 1s as possible to the left (higher positions) while keeping at least one 0 in the string. The operations allow us to: "00" → "10": Creates a 1 to the left of a 0 "10" ... Read More
Suppose we have a list of classes where classes[i] = [pass_i, total_i] represents the number of students who passed and the total number of students in the ith class respectively. We also have extra brilliant students guaranteed to pass any exam they're assigned to. We need to assign these extra students to maximize the average pass ratio across all classes. The pass ratio of a class is passed_students / total_students. The average pass ratio is the sum of all class ratios divided by the number of classes. Algorithm Approach We use a greedy approach with a max-heap ... Read More
Suppose we have an array customers, where customers[i] = [arrival_i, time_i]. Here arrival_i is the arrival time of the ith customer and time_i is the time needed to prepare the order. The cook processes orders sequentially and can only prepare one order at a time. We need to find the average waiting time of all customers. The waiting time for each customer is the total time from arrival until their order is completed. Problem Understanding Let's understand with an example. If the input is customers = [[7, 2], [8, 4], [10, 3], [20, 1]], then ? ... Read More
A star graph is an undirected graph with n nodes where one central node connects to all other n-1 nodes. In this tutorial, we'll learn how to identify the center node of a star graph represented as a list of edges. Understanding the Problem Given a star graph with n nodes labeled from 1 to n, we need to find the center node. The center node appears in every edge since it connects to all other nodes. ... Read More
Suppose we have an undirected weighted connected graph with n nodes labeled from 1 to n. A restricted path is a special path from node 1 to node n where the distance to the destination decreases at each step. Specifically, for a path [z0, z1, z2, ..., zk], we need dist(zi) > dist(zi+1) where dist(x) is the shortest distance from node x to node n. We need to find the number of restricted paths from node 1 to node n, returning the result modulo 10^9 + 7. For example, if we have this graph: ... Read More
Suppose we have an array called nums and another value k. We are at index 0. In one move, we can jump at most k steps right without going outside the boundaries of the array. We want to reach the final index of the array. For jumping we get score, that is the sum of all nums[j] for each index j we visited in the array. We have to find the maximum score we can get. So, if the input is like nums = [1, -2, -5, 7, -6, 4] and k = 2, then the output will be ... Read More
When working with arrays that have element constraints, we often need to find the minimum number of elements to add to achieve a target sum. This problem involves an array where each element's absolute value is bounded by a limit, and we need to reach a specific goal sum. Problem Statement Given an array nums and two values limit and goal, where |nums[i]| ≤ limit for all elements, we need to find the minimum number of elements to insert so that the array sum equals the goal. The inserted elements must also satisfy the limit constraint. Example ... Read More
Suppose we have an array called nums (with positive values only) and we want to erase a subarray containing unique elements. We will get a score that is the sum of subarray elements. We have to find the maximum score we can get by erasing exactly one subarray. So, if the input is like nums = [6, 3, 2, 3, 6, 3, 2, 3, 6], then the output will be 11, because the optimal subarray is either [6, 3, 2] or [2, 3, 6], so the sum is 11. Algorithm To solve this, we will follow these ... Read More
Given a string, we need to find the sum of beauty of all its substrings. The beauty of a string is the difference between the frequencies of the most frequent and least frequent characters. For example, if the string is "abaacc", the frequency of 'a' is 3 and 'b' is 1, so beauty = 3 - 1 = 2. Problem Example If the input string is "xxyzy", the substrings with non-zero beauty are: "xxy" → beauty = 2 - 1 = 1 "xxyz" → beauty = 2 - 1 = 1 "xxyzy" → beauty = ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance