Leetcode-editorials
Contribute
  • Leetcode question of the day
  • 08-August
    • 26. Maximum Length of Pair Chain
    • 28. Implement stack using queues
    • 29. Minimum Penalty for a Shop
    • 30. Minimum Replacements to Sort the Array
    • 31. Minimum Number of Taps to Open to Water a Garden
  • 09-September
    • 01. Counting Bits
    • 02. Extra Characters in a String
    • 03. Unique Paths
    • 04. Linked List Cycle
    • 05. Copy List with Random Pointer
    • 06. Split Linked List in Parts
    • 07. Reverse Linked List II
    • 08. Pascal's Triangle
    • 09. Combination Sum IV
    • 10. Count All Valid Pickup and Delivery Options
    • 11. Group the People Given the Group Size They Belong To
    • 12. Minimum Deletions to Make Character Frequencies Unique
    • 13. Candy
    • 14. Reconstruct Itinerary
    • 15. Min Cost to Connect All Points
    • 16. Path With Minimum Effort
    • 17. Shortest Path Visiting All Nodes
    • 18. The K Weakest Rows in a Matrix
    • 19. Find the Duplicate Number
    • 20. Minimum Operations to Reduce X to Zero
    • 21-Median of Two Sorted Arrays
    • 22- Is Subsequence
    • 23- Longest String Chain
    • 24- Champagne Tower
    • 25- Find the Difference
    • 26- Remove Duplicate Letters
    • 27- Decoded String at Index
    • 28- Sort Array By Parity
    • 29- Monotonic Array
    • 30- 132 Pattern
  • 10-October
    • 01. Reverse Words in a String III
    • 02. Remove Colored Pieces if Both Neighbors are the Same Color
    • 03. Number of Good Pairs
    • 04. Design HashMap
    • 05. Majority Element II
    • 06. Integer Break
    • 07. Build Array Where You Can Find The Maximum Exactly K Comparisons
  • 11-November
    • 01. Find Mode in Binary Search Tree
    • 02. Count Nodes Equal to Average of Subtree
    • 03. Build an Array With Stack Operations
    • 04. Last Moment Before All Ants Fall Out of a Plank
    • 07. Eliminate Maximum Number of Monsters
  • Leetcode Contests
    • Weekly Contest
      • Weekly-Contest-360
Powered by GitBook
On this page
  • 01. Furthest Point From Origin
  • 02. Find the Minimum Possible Sum of a Beautiful Array
  • 03. Minimum Operations to Form Subsequence With Target Sum

Was this helpful?

Edit on GitHub
  1. Leetcode Contests
  2. Weekly Contest

Weekly-Contest-360

PreviousWeekly Contest

Last updated 1 year ago

Was this helpful?

01. Furthest Point From Origin

The problem can be found at the following 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.

  1. Count the number of left , right and the blank space

  2. Find the absolute difference between left and right

  3. Add remaining blank space to that absolute difference

Time and Auxiliary Space Complexity

  • Time Complexity: O(n)

  • Auxiliary Space Complexity: O(1)

Code (C++)


int furthestDistanceFromOrigin(string moves) {

    int left =0 , right =0, blank =0;
    int ans=0;

    for(int i=0;i<moves.size();i++){

        if(moves[i]=='L'){
            left++;
        }
        else if(moves[i]=='R'){
            right++;
        }
        else blank++;
    }

    ans = abs(left-right)+blank;
    return ans;
}

02. Find the Minimum Possible Sum of a Beautiful Array

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:

  1. Initialise set of an integer , current value to 1 and the answer variable to 0

  2. Define a while loop in which we check if the current value is present in set or not

  3. If current value is not in the set the add current value to answer else we increase current

  4. 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++)


long long minimumPossibleSum(int n, int target) {
        set<long long> st; 
        int i=0, current=1;
        long long ans = 0;

        while(i<n){
            if(st.find(current) == st.end()){

                ans += current;
                st.insert((abs(target-current)));
                st.insert(current);

                i++;
                if(i==n) break;
            } 
            current++;
        }
        return ans;
    }

03. Minimum Operations to Form Subsequence With Target Sum

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 -

  1. We find the target sum without any operation .

  2. 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:

  1. Initialize a vector freq of size 31 to keep track of the frequency of each bit position.

  2. Iterate through each number i in the nums vector and create a frequency array for each number

  3. Initialize two variables: carry and operations, both initially set to 0. carry will be used to track the remaining bits after using them to reach the target, and operations will count the number of operations performed.

  4. Iterate through each bit position from least significant to most significant (from 0 to 30):

    • Check if the i-th bit in the target is not set (i.e., (target & (1 << i)) == 0):

      • If true, add the product of the frequency of the i-th bit and 2^i to carry.

      • Continue to the next iteration.

  5. If the i-th bit in the target is set:

    • Check if there are any numbers in the nums vector that have a 1 in the i-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 and 2^i to carry.

      • Continue to the next iteration.

  6. If neither of the above conditions is met, check if the carry is greater than or equal to 2^i:

    • If true, subtract 2^i from carry.

    • Continue to the next iteration.

  7. If none of the above conditions are met:

    • Initialize a boolean variable found to false.

    • Iterate from the next bit position j (from i+1 to 30):

      • Check if there are any numbers in the nums vector with a 1 in the j-th bit (i.e., freq[j] > 0):

        • If true, set found to true.

        • Perform a series of operations to borrow bits from higher positions:

          • Iterate from k starting from j and moving down to i+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.

  8. If found is still false after the loop in step 7, return -1 as it's not possible to reach the target.

  9. Repeat steps 4 to 8 for each bit position, moving from least significant to most significant.

Code (C++)


int minOperations(vector<int>& nums, int target) {
        vector<long long> freq(31);
        
        for(auto i:nums){
            int pwr = 0;
            while(!(i&1)){
                pwr++;
                i >>= 1;
            }
            freq[pwr]++;
        }
        
        long long carry =0, operations =0;
        for(int i=0;i<31;i++){
            // Check if the ith bit in the target is set or not
            if((target & (1<<i)) == 0){
                carry += freq[i] * (1<<i);
                continue;
            }
            
            // If it is set then we use one value and add remaining value to carry 
            if(freq[i] > 0){
                freq[i]--;
                carry += freq[i]* (1<<i);
                continue;
            }
            if(carry >= (1<<i)){
                carry -= (1<<i);
                continue;
            }
            
            bool found = false;
            for(int j=i+1;j<31;j++){
                if(freq[j] == 0) continue;
                found = true;
                
                for(int k=j;k>i;k--){
                    freq[k]--;
                    freq[k-1] +=2;
                    operations++;
                }
                break;
            }
            if(!found) return -1;
            i--;
        }
        return operations;
    }

Contribution and Support

The problem can be found at the following link:

The problem can be found at the following link:

For discussions, questions, or doubts related to this solution, please visit our . 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 repository.

Question Link
Question Link
Question Link
discussion section
rishabhv12/Daily-Leetcode-Solution