Zoblik International operates a vast network of automated sorting facilities. One of their most critical systems, the 'QuantumSort' facility, is responsible for efficiently distributing millions of items into standard batches. Recently, a rare cosmic event damaged QuantumSort's primary arithmetic processing unit. As a result, the system can now only perform addition and subtraction operations.
You are tasked with developing a fallback algorithm for QuantumSort. Given a total quantity of items (a) and a fixed batch size (b), your algorithm must determine the maximum number of full batches that can be formed from the total quantity. This is equivalent to finding the integer quotient of a divided by b.
The crucial constraint is that your solution cannot use the multiplication, division, or modulo operators. You must achieve the result using only addition, subtraction, and bitwise operations.
The first line contains an integer T, denoting the number of test cases.
For each test case, a single line follows containing two space-separated integers, a and b, representing the total quantity of items and the batch size, respectively.
For each test case, output a single integer: the quotient of a divided by b, calculated without using multiplication, division, or modulo operators. The result should truncate towards zero, consistent with standard integer division behavior in most programming languages (e.g., 15 / 4 = 3, 15 / -4 = -3).
1 <= T <= 10000-10^9 <= a, b <= 10^9b != 01 <= T <= 10000; -10^9<= a,b <= 10^9; b != 0
Example 1: Basic Distribution
Input: a: 15 (Total items) b: 4 (Batch size) Output: 3 Explanation: You can form 3 full batches of 4 items from 15 items (3 * 4 = 12). 3 items would be remaining. Since we only care about full batches, the quotient is 3.Example 2: Perfect Fit
Input: a: 6 (Total items) b: 2 (Batch size) Output: 3 Explanation: You can form 3 full batches of 2 items from 6 items (3 * 2 = 6). No items would be remaining. The quotient is 3.Example 3: Negative Batch Size
Input: a: 15 (Total items) b: -4 (Batch size, representing a reverse process) Output: -3 Explanation: When dividing 15 by -4, the mathematical result is -3.75. Standard integer division (truncating towards zero) yields -3.
Math & Bit Manipulation
Microsoft
Start coding and your submissions will appear here.