Skip to main content

Posts

Showing posts with the label Competitive Programming

Competitive Programming: Calculate the square of a number without using *, / and pow()

  Hello everyone ...  Here we are going to solve a  Competitive Programming  problem related to  Bit Manipulation. Problem Statement Calculate the square of a number without using *, / and pow() Examples Input : N = 4 Output : 16 Explanation : 4*4 = 16 Constraints 1 ≤ N ≤ 10 8 Expectations Expected Time Complexity:  O(logN) Expected Space Complexity:  O(1) Solution Approach  C++ Code Time Complexity Since we are only iterating through the bits of  N  in its binary representation, this solution's time complexity is  O(LogN) .  Space Complexity Since we are using constant space, this solution's space complexity is  O(1) . Hooray! We successfully solved this problem with the expected time and space complexity. Hopefully, you enjoyed this blog post and learned something new. If you want to share anything or if you have any doubts or suggestions please comment below.  Thank you, guys.  Have a nice day an...

Competitive Programming: Count total set bits in all numbers from 1 to N

 Hello everyone ...  Here we are going to solve a  Competitive Programming  problem related to  Bit Manipulation. Problem Statement Find the total count of set bits for all numbers from 1 to N (both inclusive). Examples Input : N = 4 Output : 5 Explanation : For numbers from 1 to 4. For 1: 0 0 1 = 1 set bits For 2: 0 1 0 = 1 set bits For 3: 0 1 1 = 2 set bits For 4: 1 0 0 = 1 set bits Therefore, the total set bits is 5. Constraints 1 ≤ N ≤ 10 8 Expectations Expected Time Complexity:  O(logN) Expected Space Complexity:  O(1) Problem Link :  https://practice.geeksforgeeks.org/problems/count-total-set-bits-1587115620/1 Solution Approach  C++ Code Time Complexity Since we are only iterating through the bits of N in its binary representation, this solution's time complexity is  O(LogN) .  Space Complexity Since we are using constant space, this solution's space complexity is  O(1) . Hooray! We successfully solved this pr...

Competitive Programming: Find the two non-repeating elements

 Hello everyone ...  Here we are going to solve a  Competitive Programming  problem related to  Bit Manipulation. Problem Statement Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other 2 numbers occur exactly once and are distinct . Find the two non-repeating numbers. Examples Input: N = 2 arr[] = {1, 2, 3, 2, 1, 4} Output: 3 4 Explanation: 3 and 4 occur exactly once. Input: N = 1 arr[] = {2, 1, 3, 2} Output: 1 3 Explanation: 1 3 occur exactly once. Constraints 1 <= Length of array <= 10 6  1 <= Elements in array <= 5 * 10 6 Expectations Expected Time Complexity:  O(N) Expected Space Complexity:  O(1) Problem Link :  https://practice.geeksforgeeks.org/problems/finding-the-numbers0215/1 Solution The first idea that might come to your mind is to use Sorting and comparing adjacent elements in the sorted array to find the non-repeating elements. This solution's time compl...

Competitive Programming: Count Set Bits in an Integer

Hello everyone ...  Here we are going to solve a Competitive Programming problem related to Bit Manipulation. Problem Statement Given a positive integer N , print count of set bits in it. Examples Input: N = 6 Output: 2 Explanation: Binary representation is '110' So the count of the set bit is 2. Input: 8 Output: 1 Explanation: Binary representation is '1000' So the count of the set bit is 1. Problem Link:  https://practice.geeksforgeeks.org/problems/set-bits0143/1  Solution Approach 1 In this approach, we will loop through all the bits of a given integer and check if the bit is set. If it is, then we will increment the count. To loop through the bits of an integer, we will iterate from LSB (Least Significant Bit) to MSB(Most Significant Bit), i.e, from Right to Left. In each iteration, we will increment the count by 1 if LSB (i.e. last bit) is set and then we will perform the Right Shift operation on the given integer by 1 time. C++ Code Time Complexi...