Minimum Time To Scan Vivek has two scanners in hi...
Creato il: 30 luglio 2025
Creato il: 30 luglio 2025
Minimum Time To Scan
Vivek has two scanners in his shop to scan the documents of a customer. When a customer comes to his shop, they give X number of documents to scan. Viyek knows T1 and T2, the time taken by scanner1 and scanner2 to scan a single document, respectively. Your task is to write a program to calculate the minimum time required to scan all the x documents using the two scanners, where the documents can be distributed optimally between the two scanners.
Input Format:
A single line containing three space-separated integers:
X,T1
and
T2
x is the number of documents to be scanned.
T2 is the time taken by scanner2 to scan a single
document.
Output Format:
A single integer representing the minimum time required to scan
all the x documents using the two scanners.
Constraints:
x,T1, and
T2
are positive integers.
Example
Input: 5 2 3
Output:
6
write code in java
Below is a concise O(log (X · min(T1, T2))) solution that uses binary search on time.
At any candidate time t we can compute how many documents each scanner could finish ( ⌊t / T1⌋ and ⌊t / T2⌋ ).
The least t for which the two totals reach X is the answer.
javaimport java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long X = sc.nextLong(); // number of documents long T1 = sc.nextLong(); // time per doc on scanner 1 long T2 = sc.nextLong(); // time per doc on scanner 2 sc.close(); // ensure T1 is the faster (or equal) scanner; makes the search bounds tighter if (T1 > T2) { long tmp = T1; T1 = T2; T2 = tmp; } long low = 0; // time definitely insufficient long high = T1 * X; // upper bound: slowest‐case, all docs on faster scanner while (low < high) { long mid = (low + high) >>> 1; // candidate time long done = mid / T1 + mid / T2; // docs both scanners can finish by 'mid' if (done >= X) { // enough docs ⇒ can try a smaller time high = mid; } else { // not enough ⇒ need more time low = mid + 1; } } System.out.println(low); // minimum time required } }
t
, scanner 1 completes ⌊t/T1⌋ docs and scanner 2 completes ⌊t/T2⌋ docs.t
is sufficient, all larger times are also sufficient; hence binary search applies.T1 × X
— allocating every document to the faster scanner always finishes by this time, so the answer lies ≤ that.The algorithm runs in logarithmic time and uses only O(1) additional space.
possible Path Count
Samuel has invited a couple of friends to his pizza party. All of them are programmers. There came a moment when only one slice of pizza was left and it was dfficult to figure out who will get it. So, Samuel created a problem statement, whoever resolves it, will get
the final slice of pizza.
Below is the problem statement:
There is an arrangement of a Christmas tree with an unlimited number of integers present at different levels as shown in "Fig-1".
Fig 1: Christmas Tree of numbers
1
2 3
4 5 6
7 8 9 10
The above is a Christmas tree structure where each node is labeled with a unique integer starting rom 1. You can only move downwards in the tree, either to the left child or to the right child of a node, but never upwards. For example, from node 2, you can move to either 4 or 5. Your task is to determine the number of different paths from a S
starting node S
to an ending node E
Note
If S is greater than E return O.
code im java
Below is a self‑contained Java program that reads the start node S and end node E, then prints the number of distinct downward paths in the infinite triangular (“Christmas‑tree”) grid.
javaimport java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long S = sc.nextLong(); // start node label long E = sc.nextLong(); // end node label sc.close(); // If labels are out of order, a downward path is impossible if (S > E) { System.out.println(0); return; } // Convert the labels to (row, col) coordinates Coord start = toCoord(S); Coord end = toCoord(E); // End node must be on or below the start node’s row int downSteps = end.row - start.row; if (downSteps < 0) { System.out.println(0); return; } int rights = end.col - start.col; // how many right moves are needed if (rights < 0 || rights > downSteps) { System.out.println(0); return; } // Number of paths equals C(downSteps, rights) BigInteger paths = binom(downSteps, rights); System.out.println(paths); } /** Holds 1‑based (row, col) inside the triangular grid. */ private record Coord(int row, int col) {} /** * Converts a label x (>=1) to its (row, col). * Row r has r labels; first label of row r is 1+ r*(r‑1)/2. */ private static Coord toCoord(long x) { // Row can be found from the quadratic formula for triangular numbers long r = (long) ((Math.sqrt(8.0 * x + 1) - 1) / 2); while (r * (r + 1) / 2 < x) ++r; // adjust if rounded down int row = (int) r; int col = (int) (x - r * (r - 1) / 2); // 1‑based within the row return new Coord(row, col); } /** Returns C(n, k) as a BigInteger (0 ≤ k ≤ n). */ private static BigInteger binom(int n, int k) { if (k < 0 || k > n) return BigInteger.ZERO; if (k == 0 || k == n) return BigInteger.ONE; k = Math.min(k, n - k); // exploit symmetry BigInteger res = BigInteger.ONE; for (int i = 1; i <= k; ++i) { res = res.multiply(BigInteger.valueOf(n - k + i)) .divide(BigInteger.valueOf(i)); } return res; } }
Coordinate mapping
Path characterization
r₁
to row r₂
requires t = r₂ − r₁
moves.k = c₂ − c₁
of those moves must be rights.Edge cases
k < 0
or k > t
), the answer is 0.S == E
, the algorithm naturally yields 1.The program uses BigInteger
so it works even for very deep rows where the path count exceeds 64‑bit range.