Mathematics Problems
Mathematics Problems
Proof: We know that if a number D divides both A and B, it will also divide
abs(A-B)
Now, if B = A+1, let's assume D to be a number that divides both
A and B.
D should also divide abs(A-B) = abs(A - (A+1)) = 1
But the only number to perfectly divide 1 is 1 itself, hence D has
to be 1
Divisor game
Scooby has 3 three integers A, B and C. Scooby calls a positive integer special if it is
divisible by B and it is divisible by C. You need to tell number of special integers less
than or equal to A. C
onstraints:
1<=A,B,C<=1000000000
Input:
A positive integer A
A positive integer B
A positive integer C
Examples: I nput:
12
2
Output:
Explanation:
The two integers divisible by 2 and 3 and less than or equal to 12 are 6,12.
if(b == 0) return a;
Repeated Subtraction(time complexity)
Observe that finally the numbers will reduce to their gcd (can be proven
mathematically)
My solution:
int Solution::getFinal(int A, int B) {
if(A==B)
return A+B;
else if(A==0 || B==0)
return A+B;
if(B>A)
{
swap(A,B);
if(A%B==0)
return B*2;
}
int temp=__gcd(A,B);
return temp;
}
It is getting not completed in given time
Time complexity is high
For the above solution efficiency is not their as shown when submitted
Partially Correct Answer.
You got 100.0/200 points!
ok
Q Max Chunks To Make Sorted II
Yesterday Homework
Can you explain this?
https://leetcode.com/articles/max-chunks-to-make-sorted-ii/
Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals
(merge if necessary). Y ou may assume that the intervals were initially sorted
according to their start times. E xample 1: Given intervals [ 1,3],[6,9] insert and
merge [2,5] would result in [ 1,5],[6,9]. E xample 2: Given
[1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [ 4,9] would result in
[1,2],[3,10],[12,16]. T his is because the new interval [4,9] overlaps with
[3,5],[6,7],[8,10]. M ake sure the returned intervals are also sorted.
Sir pls provide some hints to solve it.
[1, 3]
[2, 5]
[6, 9]
Max Chunks To Make Sorted II
Given an array of integers (not necessarily distinct) A
, if we split the array into some
number of "chunks" (partitions), and individually sort each chunk. After
concatenating them, the result equals the sorted array.
Input 1:
A = [3, 2, 3, 4, 0]
Output 1:
1
Input 2:
A = [2, 0, 1, 2]
Output 2:
2
https://leetcode.com/articles/max-chunks-to-make-sorted-ii/
Input Format
The first argument given is the integer matrix A.
The second argument given is the integer array B.
The third argument given is the integer array C.
The fourth argument given is the integer array D.
The fifth argument given is the integer array E.
(B[i], C[i]) represents the top left corner of the i'th query.
(D[i], E[i]) represents the bottom right corner of the i'th query.
Output Format
Return the submatrix sum (% 10^9 + 7) for each query in the form of an integer array.
Constraints
1 <= N, M <= 1000
-100000 <= A[i] <= 100000
1 <= Q <= 100000
1 <= B[i] <= D[i] <= N
1 <= C[i] <= E[i] <= M
For Example
Input 1:
A = [ [1, 2, 3]
[4, 5, 6]
[7, 8, 9] ]
B = [1, 2]
C = [1, 2]
D = [2, 3]
E = [2, 3]
Output 1:
[12, 28]
Input 2:
A = [ [5, 17, 100, 11]
[0, 0, 2, 8] ]
B = [1, 1]
C = [1, 4]
D = [2, 2]
E = [2, 4]
Output 2:
[22, 19]
Yes sir got it but i have raised doubt raju sir didn’t ans ? there is no option
Okay sir thank you sir