Skip to main content

## Posts

Showing posts with the label programming

### How to find all prime number till N? most efficient solution Sieve of Eratosthenes in c++

How to find all prime number till N? most efficient solution Sieve of Eratosthenes- There are multiple ways to find prime numbers till N. but the sieve of Eratosthenes is the most efficient solution to find prime numbers till n. This efficient solution often used in competitive programming contests. algorithm step by step - step -1 Create a bool vector of size n  and initialize it by false value. step -2  create an empty vector to store prime numbers. step 3-  loop through the visited vector from 2 to n. step 4- if the current value is pointing to unvisited we will push this value to our answer vector and mark visited it's all multiple till n. check below image how it works.                Code for  most efficient solution Sieve of Eratosthenes in c++

### Find Minimum in Rotated Sorted Array II

Find Minimum in Rotated Sorted Array II- Suppose an array of length  n  sorted in ascending order is  rotated  between  1  and  n  times. For example, the array  nums = [0,1,4,4,5,6,7]  might become: [4,5,6,7,0,1,4]  if it was rotated  4  times. [0,1,4,4,5,6,7]  if it was rotated  7  times. Notice that  rotating  an array  [a[0], a[1], a[2], ..., a[n-1]]  1 time results in the array  [a[n-1], a[0], a[1], a[2], ..., a[n-2]] . Given the sorted rotated array  nums  that may contain  duplicates , return  the minimum element of this array . You must decrease the overall operation steps as much as possible.   Example 1: Input: nums = [1,3,5] Output: 1 Example 2: Input: nums = [2,2,2,0,1] Output: 0   Constraints: n == nums.length 1 <= n <= 5000 -5000 <= nums[i] <= 5000 nums  is sorted and rotated between  1  and  n  times.   Follow up:  This problem is similar to  Find Minimum in Rotated Sorted Array , but  nums  may contain  duplicates . Would this affect the runtime comple

### Defanging an IP Address leetcode solution

Defanging an IP Address- Problem Statement- Given a valid (IPv4) IP  address , return a defanged version of that IP address. A  defanged IP address  replaces every period  "."  with  "[.]" .   Example 1: Input: address = "1.1.1.1" Output: "1[.]1[.]1[.]1" Example 2: Input: address = "255.100.50.0" Output: "255[.]100[.]50[.]0"   Constraints: The given  address  is a valid IPv4 address. code-

### How to append char to end of string in C++ || Best way to append char to end of string

How to append char to end of string in C++ || Best way to append char to end of string- There are 2 ways mention below to append char to the end of string in c++. But do you know which one is the best way?  Let's see Method -1  += operator we can use += use to append char at the end of the string but by using this method it makes a copy of the initial string and take O(length of the string ) time to make a copy of a string. Method 2- Push back method by using this method we do not create any copy of original string we only put char to the and of the string so the time complexity of this function is o(1). Obviously, push back is a better method to append char at the end of the string.

### Prime Game February long challenge 2021 solution with code and explanation

Prime Game February long challenge 2021 solution with code and explanation  - Problem statement - Chef and Divyam are playing a game with the following rules: First, an integer  X ! X !  is written on a board. Chef and Divyam alternate turns; Chef plays first. In each move, the current player should choose a positive integer  D D  which is divisible by up to  Y Y  distinct primes and does not exceed the integer currently written on the board. Note that  1 1  is not a prime. D D  is then subtracted from the integer on the board, i.e. if the integer written on the board before this move was  A A , it is erased and replaced by  A − D A − D . When one player writes  0 0  on the board, the game ends and this player wins. Given  X X  and  Y Y , determine the winner of the game. Input The first line of the input contains a single integer  T T  denoting the number of test cases. The description of  T T  test cases follows. The first and only line of each test case contains two space-sepa