Weekly-Contest-360
01. Furthest Point From Origin
The problem can be found at the following link: Question Link
My Approach
This is the very basic question of counting values, so the intution is check is which side we can move if we take consider all the left and right movement and then add the remaining blank space on that side.
Count the number of left , right and the blank space
Find the absolute difference between left and right
Add remaining blank space to that absolute difference
Time and Auxiliary Space Complexity
Time Complexity:
O(n)
Auxiliary Space Complexity:
O(1)
Code (C++)
02. Find the Minimum Possible Sum of a Beautiful Array
The problem can be found at the following link: Question Link
My Approach
This is the leetcode medium question, the approch to this question is because we can add only distint value and so need to keep track of the values we added in the answer . So we use a set and add the current value which we are adding in the answer , and we can do this as:
Initialise set of an integer , current value to 1 and the answer variable to 0
Define a while loop in which we check if the current value is present in set or not
If current value is not in the set the add current value to answer else we increase current
Insert current value and (target - current) in the set .
Time and Auxiliary Space Complexity
Time Complexity:
O(n)
Auxiliary Space Complexity:
O(1)
Code (C++)
03. Minimum Operations to Form Subsequence With Target Sum
The problem can be found at the following link: Question Link
My Approach
This is the leetcode Hard question. The intution for solution is that, we have given all the value in the array as power of 2 . So we try to think in the terms of bit and check for set bits in target and the bits set in the array .
There are 2 condition that can be possible -
We find the target sum without any operation .
We can not find the sum directly - In this case we check for bits which are set and which are not set in target .
Suppose we find a bit in not set in our array and is set in the target . Then we try to set that bit using remaing number present on the left side of the array . Because 2^i = 2^(i-1) +1
And if we can not able to set the bit from remaining numbers then we find first non zero nuber inright of array and keep breaking it untill we set that bit .
Algorithm
Certainly, here's a point-wise algorithm for the minOperations
function:
Initialize a vector
freq
of size 31 to keep track of the frequency of each bit position.Iterate through each number
i
in thenums
vector and create a frequency array for each numberInitialize two variables:
carry
andoperations
, both initially set to 0.carry
will be used to track the remaining bits after using them to reach the target, andoperations
will count the number of operations performed.Iterate through each bit position from least significant to most significant (from 0 to 30):
Check if the
i
-th bit in thetarget
is not set (i.e.,(target & (1 << i)) == 0
):If true, add the product of the frequency of the
i
-th bit and2^i
tocarry
.Continue to the next iteration.
If the
i
-th bit in thetarget
is set:Check if there are any numbers in the
nums
vector that have a1
in thei
-th bit (i.e.,freq[i] > 0
):If true, use one of these numbers and subtract one from its frequency.
Add the product of the remaining frequency of the
i
-th bit and2^i
tocarry
.Continue to the next iteration.
If neither of the above conditions is met, check if the
carry
is greater than or equal to2^i
:If true, subtract
2^i
fromcarry
.Continue to the next iteration.
If none of the above conditions are met:
Initialize a boolean variable
found
tofalse
.Iterate from the next bit position
j
(fromi+1
to 30):Check if there are any numbers in the
nums
vector with a1
in thej
-th bit (i.e.,freq[j] > 0
):If true, set
found
totrue
.Perform a series of operations to borrow bits from higher positions:
Iterate from
k
starting fromj
and moving down toi+1
:Decrement the frequency of the
k
-th bit by 1.Increment the frequency of the
k-1
-th bit by 2.Increment the
operations
counter by 1 for each operation.
Break out of the loop.
If
found
is stillfalse
after the loop in step 7, return-1
as it's not possible to reach the target.Repeat steps 4 to 8 for each bit position, moving from least significant to most significant.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the rishabhv12/Daily-Leetcode-Solution repository.
Last updated