Ali ELJALAOUI
Embedded Software Engineer
- Report this post
๐ **Tutorial: Bit Manipulation in C**Are you intrigued by the art of bit manipulation in C? Let's embark on a journey to understand the fundamentals of tinkering with individual bits within variables. This knowledge can be invaluable for low-level operations, data compression, and creating masks for various tasks.**1. Bitwise Logical Operations:**Bitwise operators like AND (`&`), OR (`|`), XOR (`^`), and NOT (`~`) allow us to perform operations on specific bits within operands.Example:```cunsigned int a = 5;// 0101 in binaryunsigned int b = 3;// 0011 in binaryunsigned int result_and = a & b;// 0001 (bitwise AND)unsigned int result_or = a | b;// 0111 (bitwise OR)unsigned int result_xor = a ^ b;// 0110 (bitwise XOR)```**2. Bit Shifting:**Shifting operations (`<<` and `>>`) enable us to move bits left or right within a variable.Example:```cunsigned int value = 10;// 1010 in binaryunsigned int shifted_left = value << 2;// 101000 in binary (shifted left by 2)unsigned int shifted_right = value >> 1; // 0101 in binary (shifted right by 1)```**3. Manipulating Specific Bits:**Crafting masks and applying them to specific bits can be a game-changer. For instance, consider `(1 << 2)` as a mask to manipulate the 3rd bit.Example (Clearing the 3rd bit):```cunsigned int value = 12;// 1100 in binaryunsigned int mask = ~(1 << 2);// Creates a mask for the 3rd bit (0010)unsigned int result = value & mask;// Clears the 3rd bit (result: 1000, decimal 8)```Remember these core principles when delving into bit manipulation:- Understand sign behavior during shifts.- Master proper masking and unmasking techniques.- Consider endianness and its implications.- Utilize standardized types (`<stdint.h>`) for consistency.- Strike a balance between optimization and readability.- Ensure code portability and safety across platforms.Whether you're working in C or C++, these principles remain steadfast. Cultivate your curiosity and explore the universe of bit manipulationโit's a fundamental tool for efficient programming!#CProgramming #BitManipulation #CodingTips #LinkedInLearning---Feel free to tailor the post according to your preferences and audience. Happy learning and coding!
1
To view or add a comment, sign in
More Relevant Posts
-
Aman Pandey
Coder | Programmer | Student (2nd yr)C++, JAVA, PHP| DSA, OPPS | Learning Web Development
- Report this post
Hello ๐ connections, it's day no. 26 โ of 100daysDsaChallenge#100daysofleetcode and here is the problem that I solved today๐Problem:Valid ParenthesesProblem link: https://bit.ly/34kxPaqIntution:Here we used a stack data structure to check if a given string of parentheses is valid or not. The basic idea is to iterate through the string, and every time an opening parenthesis is encountered, it is pushed onto the stack. When a closing parenthesis is encountered, the code checks if there is a corresponding opening parenthesis at the top of the stack. If there is, it means that the parentheses are properly nested, and the opening parenthesis is popped from the stack. If not, or if there is no opening parenthesis in the stack when a closing parenthesis is found, the string is considered invalid.Here's a step-by-step breakdown:Initialize an empty stack to keep track of opening parentheses.Iterate through each character in the input string.If the character is an opening parenthesis ('(', '{', '['), push it onto the stack.If the character is a closing parenthesis (')', '}', ']'), check if the stack is not empty. If it's not empty, pop the top element from the stack and check if it matches the corresponding opening parenthesis for the current closing parenthesis. If it matches, continue; otherwise, return false.If the stack is empty after processing all characters, return true; otherwise, return false.Algorithmic Approach:The algorithm uses a stack to keep track of the opening parentheses, ensuring that each closing parenthesis has a corresponding opening parenthesis. It efficiently handles different types of parentheses and returns true if the parentheses are valid; otherwise, it returns false.Time Complexity:The time complexity of this code is O(n), where n is the length of the input string. This is because it iterates through each character in the string exactly once, and each operation inside the loop takes constant time.Space Complexity:The space complexity is O(n), where n is the length of the input string. In the worst case, the stack can have all the opening parentheses from the string.
4
Like CommentTo view or add a comment, sign in
-
Mohamed Tarek
Embedded Software Engineer @ SEITech Solutions
- Report this post
Different Types of pointers===============wild pointerNull pointervoid pointerdangling pointer1.wild pointer:pointer that is unintialized is known as a wild pointer . performing any deferencing operation orpointer arithmetic will result in undefined behaviour.#include<stdio.h>int main(){int a=10;int *ptr;//wild pointerprintf("%d\n",*ptr);//undefined behaviour or value}2.Null pointer==========pointer that points to NULL is known as a NULL pointer ..performing any dereferncing or pointer arithmetic results in segmentation fault.basically NULL is a macro whose value is 0 defined already ..where ever NULL is written at the preprocessing stage it will be replaced with 0.#include<stdio.h>int main(){int a=10;int *ptr=NULL;//int *ptr=0;//NULL pointerprintf("%d\n",*ptr);//segmentation fault}Dangling pointer=============pointer pointing to a freed memory location or an unused memory location is termed as a dangling pointer.performing any pointer dereferncing or pointer arithmetic results in the existing value or undefined behaviour#include<stdio.h>int* fun();intmain(){int *ptr=fun();//dangling pointerprintf("%d\n",*ptr);//undefined behaviour}int* fun(){int a=10;int *p=&a;return p;}void pointer=========void pointer is also known as a generic pointer that can point to any type of datatype.but whenever we are performing any operations on void pointer we need to do the type convertion or type casting.type casting is nothing but changing the data from one type to another type.syntax===void *pointer_name;#include<stdio.h>int main(){int a=10;void *vptr=&a;printf("%d\n",*(int *)vptr);char ch='m';vptr=&ch;printf("%c\n",*(char *)vptr);float f=9.8;vptr=&f;printf("%f\n",*(float *)vptr);double d=789.09;vptr=&d;printf("%lf\n",*(double *)vptr);}
13
2 Comments
Like CommentTo view or add a comment, sign in
-
purnendu kumar
Senior Technical leader
- Report this post
Different Types of pointers===============wild pointerNull pointervoid pointerdangling pointer1.wild pointer:pointer that is unintialized is known as a wild pointer . performing any deferencing operation orpointer arithmetic will result in undefined behaviour.#include<stdio.h>int main(){int a=10;int *ptr;//wild pointerprintf("%d\n",*ptr);//undefined behaviour or value}2.Null pointer==========pointer that points to NULL is known as a NULL pointer ..performing any dereferncing or pointer arithmetic results in segmentation fault.basically NULL is a macro whose value is 0 defined already ..where ever NULL is written at the preprocessing stage it will be replaced with 0.#include<stdio.h>int main(){int a=10;int *ptr=NULL;//int *ptr=0;//NULL pointerprintf("%d\n",*ptr);//segmentation fault}Dangling pointer=============pointer pointing to a freed memory location or an unused memory location is termed as a dangling pointer.performing any pointer dereferncing or pointer arithmetic results in the existing value or undefined behaviour#include<stdio.h>int* fun();intmain(){int *ptr=fun();//dangling pointerprintf("%d\n",*ptr);//undefined behaviour}int* fun(){int a=10;int *p=&a;return p;}void pointer=========void pointer is also known as a generic pointer that can point to any type of datatype.but whenever we are performing any operations on void pointer we need to do the type convertion or type casting.type casting is nothing but changing the data from one type to another type.syntax===void *pointer_name;#include<stdio.h>int main(){int a=10;void *vptr=&a;printf("%d\n",*(int *)vptr);char ch='m';vptr=&ch;printf("%c\n",*(char *)vptr);float f=9.8;vptr=&f;printf("%f\n",*(float *)vptr);double d=789.09;vptr=&d;printf("%lf\n",*(double *)vptr);}
266
15 Comments
Like CommentTo view or add a comment, sign in
-
Chandan Agrawal
SDE @Microsoft | Ex-Infosys | 130K+ on LinkedIn | 2000+ rated on LeetCode | Code, Content and Memes
- Report this post
๐๐ข๐ญ ๐๐๐ง๐ข๐ฉ๐ฎ๐ฅ๐๐ญ๐ข๐จ๐ง ๐ข๐ง ๐๐๐, is very important and sometimes people fears as well ( including me ) so here are essential tips and tricks to unravel the magic of bit manipulation, which will definately gonna help you in Data Structures and Algorithms. ๐ป๐ ๐๐ก๐๐๐ค๐ข๐ง๐ ๐ข๐ ๐๐ฎ๐ฆ๐๐๐ซ ๐ ๐ข๐ฌ ๐๐ฏ๐๐ง/๐๐๐- If `A & 1` equals 0, then A is even.- If `A & 1` equals 1, then A is odd.๐ ๐๐ฎ๐ฅ๐ญ๐ข๐ฉ๐ฅ๐ฒ (๐๐๐๐ญ ๐๐ก๐ข๐๐ญ) ๐ ๐๐ฒ ๐ฉ๐จ๐ฐ๐๐ซ๐ฌ ๐จ๐ ๐- A = `A << k`.๐๐๐ข๐ฏ๐ข๐๐ (๐๐ข๐ ๐ก๐ญ ๐๐ก๐ข๐๐ญ) ๐ ๐๐ฒ ๐ฉ๐จ๐ฐ๐๐ซ๐ฌ ๐จ๐ ๐- A = `A >> k`.๐ ๐๐๐ญ๐ญ๐ข๐ง๐ ๐ ๐ฃ'๐ญ๐ก ๐๐ข๐ญ- A = A | (1 << j).๐ ๐๐ง๐ฌ๐๐ญ๐ญ๐ข๐ง๐ ๐ ๐ฃ'๐ญ๐ก ๐๐ข๐ญ- A = A & (~(1 << j)).๐ ๐๐จ๐ ๐ ๐ฅ๐ข๐ง๐ (๐ ๐ฅ๐ข๐ฉ) ๐ฃ'๐ญ๐ก ๐๐ข๐ญ- A = A ^ (1 << j).๐ ๐๐ก๐๐๐ค๐ข๐ง๐ ๐ข๐ ๐ฃ-๐ญ๐ก ๐๐ข๐ญ ๐ข๐ฌ ๐๐๐ญ ๐ข๐ง ๐- If `(A & (1 << j)) > 0`, then the j-th bit in A is set.๐ ๐๐ก๐๐๐ค๐ข๐ง๐ ๐ข๐ ๐ฃ-๐ญ๐ก ๐๐ข๐ญ ๐ข๐ฌ ๐๐ง๐ฌ๐๐ญ ๐ข๐ง ๐- If `(A & (1 << j)) == 0`, then the j-th bit in A is unset.๐ ๐๐ง๐ฏ๐๐ซ๐ญ๐ข๐ง๐ ๐๐ฅ๐ฅ ๐๐ข๐ญ๐ฌ ๐ข๐ง ๐- (A = ~A).๐ ๐๐๐ฅ๐ฎ๐ ๐จ๐ ๐๐๐๐ฌ๐ญ ๐๐ข๐ ๐ง๐ข๐๐ข๐๐๐ง๐ญ ๐๐ข๐ญ ๐ญ๐ก๐๐ญ ๐ข๐ฌ ๐๐ง ( ๐๐ข๐ซ๐ฌ๐ญ ๐๐ซ๐จ๐ฆ ๐ซ๐ข๐ ๐ก๐ญ)- T = (A & (-A))๐ ๐๐๐ฅ๐ฎ๐ ๐จ๐ ๐๐๐๐ฌ๐ญ ๐๐ข๐ ๐ง๐ข๐๐ข๐๐๐ง๐ญ ๐๐ข๐ญ ๐ญ๐ก๐๐ญ ๐ข๐ฌ ๐๐๐ ( ๐๐ข๐ซ๐ฌ๐ญ ๐๐ซ๐จ๐ฆ ๐ซ๐ข๐ ๐ก๐ญ)- T = (~A & (A + 1)).๐๐๐ฎ๐ซ๐ง ๐๐ง ๐๐ฅ๐ฅ ๐๐ข๐ญ๐ฌ ๐ข๐ง ๐ ๐๐๐ญ ๐จ๐ ๐๐ข๐ณ๐ ๐ง- A = (1 << n) - 1๐ ๐๐ญ๐๐ซ๐๐ญ๐ ๐๐ก๐ซ๐จ๐ฎ๐ ๐ก ๐๐ฅ๐ฅ ๐๐ฎ๐๐ฌ๐๐ญ๐ฌ ๐จ๐ ๐ ๐๐๐ญ ๐จ๐ ๐๐ข๐ณ๐ ๐ง- for ( x = 0; x < (1 << n); ++x )Share your interesting tricks for bit manipulation; I'd love to learn, and others will benefit too. Let's uncover the magic together! ๐ปโจFollow Chandan Agrawal for more ๐๐#meme #dsa #interview #coding #fun #weekend #leetcode #bit #lc #gfg #cp #maang #google #amazon
603
7 Comments
Like CommentTo view or add a comment, sign in
-
Arijit Ghosh
LinkedIn Content Creator || Discord moderator @scaler || DSA @python || Full stack Web Developer(LAMP)
- Report this post
๐ฎ๐ฑ ๐บ๐ผ๐๐ ๐ถ๐บ๐ฝ๐ผ๐ฟ๐๐ฎ๐ป๐ ๐ฎ๐น๐ด๐ผ๐ฟ๐ถ๐๐ต๐บ๐ ๐๐ผ๐ ๐บ๐๐๐ ๐ป๐ผ๐ ๐ฎ๐๐ผ๐ถ๐ฑ1. ๐๐ค๐ง๐ฉ๐๐ฃ๐ ๐ผ๐ก๐๐ค๐ง๐๐ฉ๐๐ข๐จ ๐ช:๐น Explore various techniques to arrange data in specific orders, including Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, and QuickSort2. ๐๐๐๐ง๐๐๐๐ฃ๐ ๐ผ๐ก๐๐ค๐ง๐๐ฉ๐๐ข๐จ ๐: ๐น Master methods to efficiently locate specific elements within data collections, such as Linear Search and Binary Search.3. ๐๐๐ฃ๐ ๐๐ ๐๐๐จ๐ฉ๐จโ๏ธ:๐น Build a foundation in non-contiguous memory allocation by understanding linked list structures with operations like insertion, deletion, and reversal within linked lists.4. ๐๐ฉ๐๐๐ ๐จ ๐๐ฃ๐ ๐๐ช๐๐ช๐๐จ๐ข๏ธ:๐น Master stacks (pancake towers) and queues (orderly lines) built with arrays or linked lists โจ5. ๐๐ง๐๐๐จ ๐ด:๐น Explore hierarchical data structures with a focus on binary trees.Know about traversal techniques (inorder, preorder, postorder) to navigate and process tree data.6. ๐๐ง๐๐ฅ๐๐จ ๐:๐น Represent connections and relationships between entities using graphs. Implement graph traversal algorithms like Depth-First Search (DFS) and Breadth-First Search (BFS) for various applications.7. ๐๐๐๐ช๐ง๐จ๐๐ค๐ฃ ๐:๐น Understand the concept of self-referential functions and apply it to problems like factorial calculation and Fibonacci series generation.8. ๐ฟ๐ฎ๐ฃ๐๐ข๐๐ ๐๐ง๐ค๐๐ง๐๐ข๐ข๐๐ฃ๐ ๐ข:๐น Break down complex problems into overlapping subproblems for efficient solutions. Explore examples like Fibonacci calculation using dynamic programming and the 0/1 Knapsack problem.9. ๐๐๐จ๐๐๐ฃ๐ ๐:๐น Implement hash tables for efficient storage and retrieval of data using key-value pairs. Practice hash table operations, including insertion, deletion, and search.10. ๐๐๐๐ฅ๐จ โฐ๏ธ:๐น Build a specialized tree-based data structure that maintains the heap property (parent nodes always greater/smaller than children). Learn Heapify and Heap Sort algorithms for efficient sorting.11. ๐ฝ๐๐ฉ ๐๐๐ฃ๐๐ฅ๐ช๐ก๐๐ฉ๐๐ค๐ฃ โ๏ธ:๐น Manipulate individual bits within data using bitwise operators (AND, OR, XOR, Shifts). Apply bit manipulation techniques for tasks like data compression and encryption.----------------------------------------------------------------FollowArijit Ghoshfor more insightful shares ๐1๏ธโฃTelegram link to join ongoing activity and access many other coding resourceshttps://lnkd.in/dy2Hkh2m2๏ธโฃ Like and share with your friends ๐_______________________________________#dsa #algorithms #datastructuresandalgorithms #internship #hiring #graphs #sorting #stacks
68
11 Comments
Like CommentTo view or add a comment, sign in
-
Jeffrey Dean Kelly
President
- Report this post
Here's an interesting, useful and novel update to our brute-force variable subset selection (BFVSS) technique!Recently, we showed how a simple brute-force approach to perform parallelized and sparse data regression (PSDREG) via IMPL-DATA(c)'s variable subset selection was effective at finding multiple linear regression (MLR) models to the Dow Data Challenge Problem (DDCP) better than previously reported in the following papers:1. Braun et. al., IFAC, (2020),2. Qin et. al., CACE, (2021) and3. Barton & Lennox, Dig. Chem. Eng., (2022).As previously mentioned, the DDCP is a 44 x 1 MISO problem and supplies both the raw training and testing data.The past studies above employed several different and well-known machine learning methods where the best Q2 found was 0.698 (i.e., R2 for the testing data). In our previous two posts, we found a Q2 = 0.716 with eight (8) regressors using a mathematical programming VSS approach and Q2 = 0.718 with five (5) predictors using our BFVSS with N = 1000 number of shuffles, selections, samples, scenarios or situations varying the number of explanatory variables from 1 to 12 in parallel with the same random seed.However, here we apply something different using IMPL-DATA's semantic of "coefficient-setups" which includes or excludes an independent variable's regression coefficient i.e., zero (0) to exclude and one (1) to include. In our last post, we highlighted the Green (1977) algorithm to perform a "k" out of "m" shuffle or select which exogenously determines the coefficient-setups.Each coefficient-setup sample (n = 1..N) corresponds to a single MLR run. Below we detail two sets of Q2 results when N = 1000 and k = 2..13 (M = 44) where all runs use the same random seed.k= 2: Q2=0.625 -> Q2=0.641k= 3: Q2=0.669 -> Q2=0.693k= 4: Q2=0.687 -> Q2=0.711k= 5: Q2=0.682 -> Q2=0.723*k= 6: Q2=0.691 -> Q2=0.709k= 7: Q2=0.692 -> Q2=0.703k= 8: Q2=0.723 -> Q2=0.719*k= 9: Q2=0.701 -> Q2=0.723k=10: Q2=0.681 -> Q2=0.720k=11: Q2=0.699 -> Q2=0.705k=12: Q2=0.683 -> Q2=0.693k=13: Q2=0.689 -> Q2=0.695Now here's the difference, when we run the first set of Q2's, we fit or estimate a MLR by regressing the zero-one coefficient-setups w.r.t. to the mean squared error (MSE) of the testing data.By sorting their respective coefficients from negative to positive or smallest to largest, we arbitrarily record the top 22 (say 44 / 2 or any other subset) and only include the top subset when generating the coefficient-setups for the next set of samples for a particular k.As the results show for the second Q2's after "->" above, these Q2's are improved except for k = 8 and as the number of k's increase the Q2 values rise then fall where for k = 4..11, they are all better than the best Q2 = 0.698 found by others ;-)#industrial #algorithms #nonlinear #dynamic #optimization #control #industrialai #machinelearning
7
2 Comments
Like CommentTo view or add a comment, sign in
-
Sanyam Goyal
SDE 2 @ Ecom Express | Logistics | Fintech, Ex: Stashfin | IIIT Delhi
- Report this post
๐ Harnessing Hashing: Unlocking Key-Value Pairs' Secrets๐๏ธHash Tables: A Powerful Data StructureHash tables are a fundamental data structure that is used in a wide variety of applications. They are a type of associative array that maps keys to values. Hash tables are efficient for searching, inserting, and deleting elements.๐ฏWhat are Hash Tables?A hash table is a data structure that uses a hash function to map keys to values. The hash function takes a key and produces a hash value, which is an integer. The hash value is used to determine the location of the key in the hash table.โป How are Hash Tables Used?Hash tables are used in a wide variety of applications, including:Symbol tables:Hash tables can be used to implement symbol tables,which are data structures that map names to values.๐ณ Caches:Hash tables can be used to implement caches,which are data structures that store frequently accessed data.Databases:Hash tables can be used to implement databases,which are data structures that store data in a structured format.๐Collision Resolution: Collisions occur when two different keys have the same hash value. There are a number of different collision resolution techniques that can be used to handle collisions. Some of the most common techniques include:โถ Separate chaining:Separate chaining uses a linked list to store the values that have the same hash value.โถ Linear probing:Linear probing probes the next available slot in the hash table until an empty slot is found.โถ Quadratic probing:Quadratic probing probes the hash table using a quadratic formula.๐Average Time Complexity:The average time complexity for searching, inserting, and deleting elements in a hash table is O(1). This means that the time it takes to perform these operations is independent of the size of the hash table.ConclusionHash tables are a powerful and versatile data structure that is used in a wide variety of applications. They are efficient for searching, inserting, and deleting elements. If you are not already familiar with hash tables, I encourage you to learn more about them.for more details please check out the: link: https://lnkd.in/dCZut4vY#hashtables #algorithms
2
Like CommentTo view or add a comment, sign in
-
Anshul Dohare
Software Developer | Java | C++ | Android Application Development
- Report this post
๐ Exploring the Basics: ๐๐๐ซ๐ ๐ ๐๐จ๐ซ๐ญ ๐Hello, LinkedIn friends!๐ Let's have a look at a reliable and efficient sorting algorithm : Selection Sort!๐ฃ๏ธ๐๐๐ซ๐ ๐ ๐๐จ๐ซ๐ญ is a popular and efficient sorting algorithm that follows the divide and conquer strategy. It divides the unsorted list into n sub-lists, each containing one element, and then repeatedly merges these sub-lists to produce new sorted sub-lists until there is only one sub-list remaining, which is the sorted list.๐๐ปWithout going into coding, here's a quick look:๐ ๏ธ ๐๐จ๐ฐ ๐ข๐ญ ๐๐จ๐ซ๐ค๐ฌ:-> Divide : Divide the unsorted list into sub-lists, each containing one element.-> Conquer : Repeatedly merge sub-lists to produce new sorted sub-lists.-> Combine : Continue merging sub-lists until there is only one sub-list remaining, which is the sorted list.๐ฏ ๐๐ก๐ฒ ๐๐๐ซ๐ ๐ ๐๐จ๐ซ๐ญ?-> Merge Sort is a reliable choice for general purpose sorting and a strong algorithm with a variety of uses. Merge Sort can help you with sorting large datasets, external sorting problems, and consistent performance all around. ๐โฐ ๐๐ข๐ฆ๐ ๐๐จ๐ฆ๐ฉ๐ฅ๐๐ฑ๐ข๐ญ๐ฒ:-> Best case : O(nlog n)-> Average case : O(nlog n)-> Worst case : O(nlog n)๐ ๐๐ซ๐จ๐ฌ:-> ๐๐๐ต๐ข๐ฃ๐ญ๐ฆ ๐๐ฐ๐ณ๐ต๐ช๐ฏ๐จ : Maintains the relative order of equal elements in the sorted output.-> โณ๐๐ณ๐ฆ๐ฅ๐ช๐ค๐ต๐ข๐ฃ๐ญ๐ฆ ๐๐ฆ๐ณ๐ง๐ฐ๐ณ๐ฎ๐ข๐ฏ๐ค๐ฆ : It has a consistent and predictable performance of O(n log n) in the worst, average, and best cases, making it efficient for large data sets.-> ๐ช๐๐น๐ต๐ฆ๐ณ๐ฏ๐ข๐ญ ๐๐ฐ๐ณ๐ต๐ช๐ฏ๐จ : Ideal for situations where data exceeds main memory capacity.-> ๐๐๐ข๐ณ๐ข๐ญ๐ญ๐ฆ๐ญ๐ช๐ป๐ข๐ต๐ช๐ฐ๐ฏ : The divide and conquer nature of Merge Sort allows for easy parallelization, making it suitable for parallel processing and multi-core systems.๐ซ ๐๐จ๐ง๐ฌ:-> ๐๐๐ฑ๐ข๐ค๐ฆ ๐๐ฐ๐ฎ๐ฑ๐ญ๐ฆ๐น๐ช๐ต๐บ : The space complexity of Merge sort is O(n) because it requires additional space proportional to the size of the input array for the temporary storage of merged sub-lists. This can be a concern for large data sets with limited memory.-> ๐๐๐ฐ๐ต ๐๐ฅ๐ข๐ฑ๐ต๐ช๐ท๐ฆ : Even if the list is sorted, the merge sort goes through the entire process.-> ๐๏ธ๐๐ฐ๐ต ๐ช๐ฏ ๐๐ญ๐ข๐ค๐ฆ : Merge sort is not in-place sorting algorithm, as it needs additional space for the merging process. This can be a drawback in situations where memory usage is a critical factor.-> ๐ข๐๐ฐ๐ต ๐ฑ๐ณ๐ฆ๐ง๐ฆ๐ณ๐ข๐ฃ๐ญ๐ฆ ๐ง๐ฐ๐ณ ๐ด๐ฎ๐ข๐ญ๐ญ ๐ฅ๐ข๐ต๐ข๐ด๐ฆ๐ต : While the time complexity is efficient at O(nlog n), the constant factors involved can make it slower for small datasets compared to simpler algorithms like Quick Sort and Insertion Sort. Follow Anshul Dohare for more such a knowledgeable content.#mergesort #dsa #datastructuresandalgorithms #algorithms #jobs #sorting #keepgrowing #computerscience #engineering #engineer #computersciencestudents #softwareengineer #programming #development #softwaredevelopment #learning #student #internships #success
11
Like CommentTo view or add a comment, sign in
-
Suresh Chelani
CP enthusiast || 3โญ @LEETCODE || Institute Rank ๐๏ธ1st @GFG || Former Technical J. Secretary - Student Council - ITJ, ICFAI University Jaipur
- Report this post
๐๐๐ฎ๐ฌ๐ญ ๐๐จ๐ฅ๐ฏ๐๐ ๐ ๐๐จ๐๐ข๐ง๐ ๐๐ก๐๐ฅ๐ฅ๐๐ง๐ ๐! ๐ (With 4 Approaches)๐งฉ ๐๐ซ๐จ๐๐ฅ๐๐ฆ: "Find the Duplicate Number" (Medium)๐ ๐๐จ๐ฉ๐ข๐๐ฌ: Algorithms, Arrays๐ข ๐๐จ๐ฆ๐ฉ๐๐ง๐ข๐๐ฌ: Various๐ ๐๐๐ฌ๐๐ซ๐ข๐ฉ๐ญ๐ข๐จ๐ง:You are given an array of integers nums containing n + 1 integers, where each integer is in the range [1, n] inclusive. There's only one repeated number in nums, and your task is to find and return this repeated number.Here's the catch: you must solve the problem without modifying the nums array, and you can only use constant extra space.๐๐ฉ๐ฉ๐ซ๐จ๐๐๐ก ๐:- ๐๐ข๐ฆ๐ ๐๐จ๐ฆ๐ฉ๐ฅ๐๐ฑ๐ข๐ญ๐ฒ: O(n log n)- ๐๐ฉ๐๐๐ ๐๐จ๐ฆ๐ฉ๐ฅ๐๐ฑ๐ข๐ญ๐ฒ: O(1)This approach employs sorting to solve the problem. It sorts the given array of integers and then checks for consecutive duplicates. If found, it returns the duplicate number. While this solution works, it has a time complexity of O(n log n) due to sorting, which may not be optimal for larger input sizes.---๐๐ฉ๐ฉ๐ซ๐จ๐๐๐ก ๐:- ๐๐ข๐ฆ๐ ๐๐จ๐ฆ๐ฉ๐ฅ๐๐ฑ๐ข๐ญ๐ฒ: O(n)- ๐๐ฉ๐๐๐ ๐๐จ๐ฆ๐ฉ๐ฅ๐๐ฑ๐ข๐ญ๐ฒ: O(n)Approach 2 uses a frequency array to count the occurrences of each number in the input array. It then scans this frequency array to identify the repeated number. This approach ensures a linear time complexity, but it uses additional space proportional to the size of the input array, making it less memory-efficient.---๐๐ฉ๐ฉ๐ซ๐จ๐๐๐ก ๐:- ๐๐ข๐ฆ๐ ๐๐จ๐ฆ๐ฉ๐ฅ๐๐ฑ๐ข๐ญ๐ฒ: O(n^2)- ๐๐ฉ๐๐๐ ๐๐จ๐ฆ๐ฉ๐ฅ๐๐ฑ๐ข๐ญ๐ฒ: O(1)Approach 3 employs a nested loop to compare each pair of elements in the array, searching for duplicates. Unfortunately, this approach results in a time complexity of O(n^2), which can lead to a "Time Limit Exceeded" error for larger input sizes, making it less efficient than other solutions.---๐๐ฉ๐ฉ๐ซ๐จ๐๐๐ก ๐:- ๐๐ข๐ฆ๐ ๐๐จ๐ฆ๐ฉ๐ฅ๐๐ฑ๐ข๐ญ๐ฒ: O(n)- ๐๐ฉ๐๐๐ ๐๐จ๐ฆ๐ฉ๐ฅ๐๐ฑ๐ข๐ญ๐ฒ: O(1)Approach 4 uses a clever algorithm known as Floyd's Tortoise and Hare (Cycle Detection) to find the duplicate number efficiently. It has a time complexity of O(n) and constant space complexity, making it the most optimal solution among the presented approaches.---These different approaches provide various trade-offs in terms of time and space complexity. Approach 4, with its linear time complexity and constant space usage, stands out as the recommended solution for efficiently solving this problem. However, it's valuable to explore and understand different approaches to problem-solving in order to enhance your coding skills.Approach1_sol: https://lnkd.in/dzV83SGAApproach2_sol: https://lnkd.in/dRFsuvMTApproach3_sol: https://lnkd.in/dKzX5R2pApproach4_sol: https://lnkd.in/dSAhimCx#CodingChallenge #LeetCode #Algorithms #Programming #ProblemSolving #ConstantSpace #FollowUpQuestions #array
15
Like CommentTo view or add a comment, sign in
-
Sujeet Kumar
Senior Software Engineer | Generative AI | AWS & Azure Certified | Microservices & API Architect | .NET Expert | Python
- Report this post
๐DSA Series - ๐จ๐ป๐น๐ผ๐ฐ๐ธ๐ถ๐ป๐ด ๐๐ถ๐ป๐ธ๐ฒ๐ฑ ๐๐ถ๐๐ ๐ ๐๐๐๐ฒ๐ฟ๐ถ๐ฒ๐ ๐๐ถ๐๐ต ๐๐๐๐ฒ๐ป๐๐ถ๐ฎ๐น ๐ง๐ฒ๐ฐ๐ต๐ป๐ถ๐พ๐๐ฒ๐๐Core set of algorithms and techniques to effectively tackle a broad range of linked list problems in technical interviews and software development. ๐ญ. ๐๐น๐ผ๐๐ฑโ๐ ๐ง๐ผ๐ฟ๐๐ผ๐ถ๐๐ฒ ๐ฎ๐ป๐ฑ ๐๐ฎ๐ฟ๐ฒ (๐๐๐ฐ๐น๐ฒ ๐๐ฒ๐๐ฒ๐ฐ๐๐ถ๐ผ๐ป)๐ช๐ต๐ ๐๐'๐ ๐๐๐๐ฒ๐ป๐๐ถ๐ฎ๐น: Vital for identifying cycles in a linked list, determining the cycle's starting node, and assessing the cycle's length. Its applications go beyond cycle detection, including problems where detecting repetition or loops in data structures is required.๐ฎ. ๐ฅ๐ฒ๐ฐ๐๐ฟ๐๐ถ๐ผ๐ป๐ช๐ต๐ ๐๐'๐ ๐๐๐๐ฒ๐ป๐๐ถ๐ฎ๐น: Many linked list problems, such as reversing a list, can be elegantly solved using recursion. Recursion simplifies code for operations that involve backtracking or decomposing the problem into smaller, similar subproblems.๐ฏ. ๐ ๐ฒ๐ฟ๐ด๐ฒ ๐ฆ๐ผ๐ฟ๐ ๐ผ๐ป ๐๐ถ๐ป๐ธ๐ฒ๐ฑ ๐๐ถ๐๐๐๐ช๐ต๐ ๐๐'๐ ๐๐๐๐ฒ๐ป๐๐ถ๐ฎ๐น: Sorting is a common problem, and Merge Sort is particularly well-suited to linked lists due to its efficient divide-and-conquer approach, which does not require random access to elements. Understanding how to merge two sorted lists is also crucial for solving various problems that involve combining or rearranging data in sorted order.๐ฐ. ๐ง๐๐ผ-๐ฃ๐ผ๐ถ๐ป๐๐ฒ๐ฟ ๐ง๐ฒ๐ฐ๐ต๐ป๐ถ๐พ๐๐ฒ๐๐ช๐ต๐ ๐๐'๐ ๐๐๐๐ฒ๐ป๐๐ถ๐ฎ๐น: Beyond cycle detection, two-pointer techniques are invaluable for finding elements at a certain position from the end, detecting palindromes, and solving problems that require simultaneous traversal of a list at different rates or starting points.๐๐ฑ๐ฑ๐ถ๐๐ถ๐ผ๐ป๐ฎ๐น ๐ง๐ฒ๐ฐ๐ต๐ป๐ถ๐พ๐๐ฒ๐ ๐ฎ๐ป๐ฑ ๐๐น๐ด๐ผ๐ฟ๐ถ๐๐ต๐บ๐:-๐ฅ๐ฒ๐๐ฒ๐ฟ๐๐ฒ ๐ฎ ๐๐ถ๐ป๐ธ๐ฒ๐ฑ ๐๐ถ๐๐: Both iterative and recursive solutions.-๐๐ฎ๐๐ ๐ฎ๐ป๐ฑ ๐ฆ๐น๐ผ๐ ๐ฃ๐ผ๐ถ๐ป๐๐ฒ๐ฟ ๐ง๐ฒ๐ฐ๐ต๐ป๐ถ๐พ๐๐ฒ: For finding the middle element, detecting cycles, and more.-๐๐๐บ๐บ๐ ๐๐ฒ๐ฎ๐ฑ ๐ก๐ผ๐ฑ๐ฒ: Useful for simplifying operations on the head of the list, such as insertions or deletions.-๐๐ป๐๐ฒ๐ฟ๐น๐ฒ๐ฎ๐๐ถ๐ป๐ด ๐ฎ๐ป๐ฑ ๐ฆ๐ฝ๐น๐ถ๐๐๐ถ๐ป๐ด: For problems that require rearranging nodes or separating a list into components based on certain criteria.If you found this post helpful, show your support below please! ๐๐ Like ๐ Share ๐ฌ Comment๐ค Follow Sujeet Kumar๐ Stay tuned for more updates#softwareengineering #softwaredevelopment #dsa #interview
5
Like CommentTo view or add a comment, sign in
669 followers
- 26 Posts
View Profile
FollowExplore topics
- Sales
- Marketing
- Business Administration
- HR Management
- Content Management
- Engineering
- Soft Skills
- See All