# DSA

Data structure & algorithm using swift language.

## Count all prime numbers

[Question] Count all prime numbers from given nExample Input: n = 10Output: 4Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.Solution: using Sieve of Eratosthenes AlgoThe Sieve of Eratosthenes is an ancient and efficient algorithm for finding all prime numbers up to a specified integer \( n \). It […]

## Logarithms Explained

In this article I will explain about how log values is being calculated Remember Log 2 8 = 3 which means 2 3 = 8 Example : Log 10 1000 = 4 which means 10 4 = 10,000

## How to generate parentheses

[Question]: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.Example: – n = 2. —   [“(())”, “()()”] Approach: We can solve using backtracking / recursion

## How to Generate all the binary strings of N bits?

[Question]: Given a positive integer number N. The task is to generate all the binary strings of N bits. These binary strings should be in ascending order.Example: – Input: 2Output:0 00 11 01 1

## How to Remove all occurrence of certain number ?

[Question]: Given an integer array nums and an integer k, remove all occurrences of k in nums in-place. The order of the elements may be changed. Then return the count after removal Approach: We can declare a variable which holds index initial it’s 0 now move elements which are not matched with k

## How to Reverse a Stack using Recursion ?

[Question] Write a program to reverse a stack using recursion.Input: elements present in stack from top to bottom 1 2 3 4 Output: 4 3 2 1

## How to Sort a Stack using Recursion

[Question]: Given a stack, the task is to sort it using recursion.Input: elements present in stack from top to bottom -3 14 18 -5 30Output: 30 18 14 -3 -5Explanation: The given stack is sorted know 30 > 18 > 14 > -3 > -5

## How to find longest Substring Without Repeating Characters ?

[Question]: Given a string s, find the length of the longest substring without repeating characters. Example 1: Input: s = “abcabxy” Output: 5 Explanation: The answer is “abcxy”, with the length of 5

## How to flatten a linked list who has down & next pointer

[Question]: Given a linked list where every node represents a linked list and contains two pointers of its type:  Condition: All linked lists are sorted and the resultant linked list should also be sorted

## How to rotate linked list with K-th places ?

[Question]: Given the head of a linked list, rotate the list to the right by k places.Example: – Input: head = [1,2,3,4,5], k = 2 Output: [4,5,1,2,3] #Approach Step: 1 Create tail node which moves till endsStep: 2 Create curr the tail node is the (len-k)-th node (1st node is head)Step: 3 Reorder linked list