Skip to main content

Posts

A- Yes or Yes (Problem 1703A) Codeforces Round 120 (Div. 4) (C++ Code and Explanation)

  A - Yes or Yes? (Problem 1703A) - Codeforces Round 806 (Div.4) Problem-   https://codeforces.com/contest/1703/problem/A Approach- Since we have to check just three letters, so we can just match them individually. Check whether the first letter is 'Y' or 'y' similarly to 'E' and 'e' and also for 'S' and 's' . It is already given that the length of the string is 3 so we do not have to worry about any further cases. Below is the detailed code in C++ exactly matching the explanation above. Code - string s; cin>>s; if((s[0]=='y'||s[0]=='Y')&&(s[1]=='e'||s[1]=='E')&&(s[2]=='s'||s[2]=='S')) cout<<"YES"<<endl; else cout<<"NO"<<endl; Other Problems from this Contest- https://contestsolver.blogspot.com/2022/07/round806.html Disclaimer- Everything is published after the actual contest gets over.

Leetcode 67 - Add Binary (Easy) - C++ Code With Explanation

Leetcode 67 - Add Binary (Easy) Problem-   https://leetcode.com/problems/add-binary/ Approach- In normal addition, we start from the least significant position i.e. from right to left. So to perform this in the strings we have to reverse both the strings.  After the reversal, we have to make their length equal by adding the character '0' to the smaller string. Now perform the addition process starting from the left-hand side in the reversed and equal lengthed strings using the carry method. Initially carry is 0 and the further cases are explained in the code. In binary addition sum of 1 and 1 gives 0 with carry 1. And the sum of 1 and 1 with carry one gives 1 as result along with carry 1. Another method that we can use is to convert both the strings in decimal form and then add them and return the binary form string of the resultant addition in decimal form. Below is the code for the same. Code- string addBinary(string a, string b) { reverse(a.begin(), a.end());

Leetcode Problemset C++ Code and Solutions

Leetcode 67      Add Binary                  Easy Leetcode 905    Sort Array By Parity    Easy

Leetcode 905 - Sort Array by Parity (Easy) - C++ Code With Explanation

             Leetcode 905 - Sort Array by Parity (Easy) Problem-   https://leetcode.com/problems/sort-array-by-parity Approach- This can be solved using two methods of brute force that use extra space and the other one is by using two pointers.  For the first method we just have to take an empty vector and iterate the given array twice, in the first traversal push back all the even numbers to our final array and then in the second traversal push back all the odd numbers. For the second method take two pointers i and j at the starting and the ending indices of the given array respectively. Now there can be four cases. Case 1 nums[i] is odd and nums[j] is odd Just decrease j as it has the correct element i.e. an odd element at the end side of the array.  Case 2 nums[i] is odd and nums[j] is even Swap nums[i] and nums[j]. Now both are at correct sides, so do i++ and j--. Case 3 nums[i] is even and nums[j] is even Both are at correct sides, so do i++ and j--. Just increase i as it has t

Codechef Post Contest Solutions and Code C++

   Solutions with explanation and C++ code of these contests are available here. All these solutions and editorials are published after the actual contest gets over and are only aimed for editorial purposes.   Codechef Long Challenges - 1. Codechef January 2022 Long Challenge-1 Codechef Starters - Codechef Cookoffs - Codechef Lunchtimes - Other Rated Contests -

A- Construct a Rectangle Codeforces Educational Round 120 (C++ Code and Explanation)

 A- Construct a Rectangle Codeforces Educational Round 120 Problem-   https://codeforces.com/contest/1622/problem/A Approach- The idea is to take the given lengths in increasing order. And the solution is only possible when the difference of the largest and the smallest value is equal to the middle value, so that the largest one can be broken in two pieces. Or if any two lengths are equal and the third one is divisible by 2 (to get integer length on cutting), the answer is yes. Only these four cases will have a positive answer. Below is the detailed code in C++ exactly matching with the explanation above. Code - #include <bits/stdc++.h> using namespace std; #define ll long long #define f(ii, n) for (ll ii = 0; ii < n; ii++) int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);           ll testcase=1;     cin >> testcase;     f(i, testcase)     {         ll n=3;         ll a[n];         f(i,n)         cin>>a[i];         sort(a, a+n);         if(a[2]

B- Berland Music Codeforces Educational Round 120 (C++ Code and Explanation)

B- Berland Music Codeforces Educational Round 120 Problem-   https://codeforces.com/contest/1622/problem/B Approach- The base idea for solving this problem is to separate the liked and disliked elements and sort them separately. Since, all the disliked elements should have less rating than those of all the liked ones, the idea is to make a change in disliked elements first and then in liked elements of the array. All the disliked elements should be numbered increasingly from 1 in a sorted manner to satisfy the given condition of minimum deviation from older values.  To accomplish this task we take disliked and liked elements separately in two vectors of pair and then sort them. Now sort both of them and iterate over the disliked vector first making changes at indexes of the elements in the original array. Then iterate over the liked vector elements. Below is the detailed code in C++ exactly matching with the explanation above. Code - #include <bits/stdc++.h> using namespace std;

C- Set or Decrease Codeforces Educational Round 120 (C++ Code and Explanation)

  C- Set or Decrease Codeforces Educational Round 120 Problem-   https://codeforces.com/contest/1622/problem/C Approach- Since, we have been asked to find the minimum number of operations, the first thought that should come to our mind should be of using Binary Search. And yes, half of the task is done. But how to make the check function for binary search. So, lets say we have some operations available to us ( totaloperations ), now from this we can do two types of operations, be it the subtract operation ( ai=ai-1 ) or the replace operation ( aj=ai ). So, we should check for every possible value between 0 and maximum possible replaceable elements i.e n-1 using a for loop. So, for each value ( replace ) from 0 to min( totaloperations , n-1 ) we get subtracted value as  totaloperations-replace. For these two values, check if the current sum of elements is <= k or not. Prefix Sum is used to reduce the time complexity for calculating the sum of array elements. Below is the detailed co