DSA

Check Sudoku board configuration is valid or not(Using Bitmasking)

Using arrays in the prior method allowed us to monitor which numbers have emerged in every row, column, and sub-matrix. Now, by employing bit manipulation, we can make this more efficient; we represent the existence of numbers with bits in an integer, therefore lowering memory use and enhancing time complexity as well. Apart from verifying and marking visited values in row, column, and sub-matrix, the implementation processes remain the same as above technique. Methodical, step-by-step approach: C++ C Java Python C# Time complexity O(n^2), where n = Sudoku matrix size—that is, 9. This is so as we go across all n*n matrix cells iteratively.O(n) auxiliary space since we track the seen numbers for rows, columns, and sub-matrix using three arrays (rows, cols, subMat), each of size n. Bitwise operations use the integer each array element carries.

Check Sudoku board configuration is valid or not(Using Bitmasking) Read More »

Check Sudoku board configuration is valid or not

Using Array of Fixed Length We can track each number in rows, columns, and sub-matrix using auxiliary arrays rather than any hash table to record seen/visited elements. Arrays allow us to remove the hash overhead. Methodically approaching: C++ C Java Python C# Time complexity O(n^2), where n = Sudoku matrix size—that is, 9. This is so as we go across all nn matrix cells iteratively. O(n^2) auxiliary space resulting from three 2D arrays of size nn rows, cols, subMatrix.

Check Sudoku board configuration is valid or not Read More »

Check Sudoku board configuration is valid or not(Using Hashing)

Though with a trade-off in space complexity, this strategy reduces the time complexity of the prior one. For every row, column, and submatrix we track the values we have already seen or visited using an unordered set. We look for if the current cell value already exists in the matching row, column, or sub-grid as we iterate over the matrix and for each cell. Should a duplicate surface, the Sudoku board is void. The board arrangement is good if, after looking over all the cells, no duplicates are discovered. Important Observation: The sudoku matrix consists of nine 3x 3 submatrices; so, the formula (i / 3) * 3 + j / 3 will help one to determine the index of a given submatrix. C++ JAVA Python C# JavaScript Time Complexity: O(n2), where each operation (findnig and inserting) on unordered set requires O(1) on average, iterating over all n x n cells causes O.Auxiliary Space: O(n2) auxiliary space resulting from three arrays of n unordered sets one for rows, one for columns, and one for submatrices. Every set can save up to n elements, which results in O(n²) overall space use.

Check Sudoku board configuration is valid or not(Using Hashing) Read More »

Check Sudoku board configuration is valid or not(Native Approach)

The aim is to verify whether the provided Sudoku board setup is valid or not considering their configuration. Every row, column, and 3x 3 sub-matrix in a valid configuration must have the digits 1 through 9 without repeating. Input Proposed Approaches Naive Approach Every integer between 1 and 9 shows just once in each row, column, 3X3 sub-matrix of the sudoku. Search every cell in the row, column, and 3X3 sub-matrix when its value is only showing once. C++ C Java Python C# JavaScript Output : True Time Complexity: O(n3) for every cell we must verify duplicacy with row, column, and sub-matrix. There are thus n^2 cells, and for every cell validation n processes are involved.O(n) auxiliary space for storing row, column, and sub-matrix value.

Check Sudoku board configuration is valid or not(Native Approach) Read More »

Search insert position of K in a sorted array

The problem is to find the index of K, if it exists in the sorted array arr[], given N unique numbers and an integer K. Find the index where K has to be added to maintain the array sorted otherwise. Examples Follow the below naive approach to address the issue: C++ C Java Python3 C# JavaScript Output Binary Search is the suggested method to maximize the above one. Use the following procedures to tackle the issue: C++ C Java Python3 C# JavaScript Output

Search insert position of K in a sorted array Read More »

Find first and last positions of an element in a sorted array

Finding indices of the earliest and last occurrences of an element x in a sorted array arr[] with maybe duplicate elements is the goal. Example A naive method: Iteratively on the elements of an array, check given elements in an array and record first and last occurrence of the index of the discovered element. The following outlines the actions to carry out the aforementioned concept: Here is the application of the aforementioned strategy: C++ C Java Python3 C# PHP JavaScript Output A binary search-based effective method: 2. For the last occurrence of a number  C++ C Java Python3 C# PHP JavaScript Output

Find first and last positions of an element in a sorted array Read More »

Search in a sorted and rotated Array

Given a sorted and rotated array arr[] of n unique entries, the goal is to locate the index of specified key in the array. Return -1 should the key not be found in the array. Examples Using Two Binary Searches For instance, the min in {4, 5, 6, 7, 0, 1, 2} is 0 at index 4. Find the pivot point—or index of the min element. And at index 1 the min of 50, 10, 20, 30, 40 equals 10. How to get the min’s index? We have covered in great length here. Once we identify the pivot, we may readily split the provided array into two sorted subarrays with the index of the min. For instance {{4, 5, 6, 7, 0, 1, 2} as {{4, 5, 6, 7}, {1, 2}} and {{50, 10, 20, 30, 40} as {{50}, {10, 20, 30, 40} }. The following are the arising cases: Should the provided key match minimal, we returnShould min index be zero, the entire array is sorted; hence, we use binary search over the complete array.How then should we choose the subarray in other circumstances? To call binary search for both sides, one basic concept is We can save one binary search even though the general time complexity will remain O(Log n). One intends to match the given key with the first element. For instance {{4, 5, 6, 7}, {1, 2}, and with a key = 6. We conduct binary search in the first subarray if key is more than equal to the first element; else, in the second. C++ Java Python C# JavaScript

Search in a sorted and rotated Array Read More »

Next Permutation in C++, Java, Python

The goal is to print the lexicographically next bigger permutation of an n-sized array arr[]. If no further permutation exists, then determine the lexicographically lowest permutation of the particular array. Let us better grasp the issue by first writing all permutations of [1, 2, 4] in lexicographical sequence. [1, 2, 4]; [1, 4, 2], [2, 1, 4], [2, 4, 1], [4, 1, 2] and [4, 2, 1] Should we provide any of the above—except from the last—as input, we must then determine the next one in order. Should we provide last as input, we must revert to the first one. Examples [Naive Approach] Generating All Permutations – O(n!*n*log(n!)) Time and O(n!) Space Our first basic thought is that we would first create and sort all potential permutations of a particular array. Once arranged, we find the current permutation in this list. The next permutation is just the subsequent arrangement in the sorted sequence. Show the first permutation—the smallest permutation—should the present arrangement be the last in the list. Note: This method will only be effective in an input array free of duplicate values. Please see the expected method to manage duplicates here. C++ Java Python Output 2 4 5 0 1 7 Time Complexity: O(n!nlog(n!). n is the total number of elements in the input sequence, so reflecting all conceivable permutation.Auxiliary Space: O(n!). Permutation storage [Expected Approach] Generating Only Next – O(n) Time and O(1) Space Notes on the next permutation: Apply the above observation by following the guidelines below: C++ C Java Python C# JavaScript

Next Permutation in C++, Java, Python Read More »

How to Remove an Element from an Array in JavaScript?

Fundamental JavaScript procedure, removal of members from an array is necessary for data manipulation, filtering, and transformation. This post will investigate several ways to effectively remove elements from an array, therefore improving your knowledge and capacity to manage arrays. 1. Using pop() method The last element of the array is deleted and returned using thepop() method. This operation shrinks the array’s length by 1. JavaScript Output 2. Using shift() Method The first element of the array is eliminated using the shift() technique, therefore shrinking the original array by 1.  JavaScript Output Removed element: shift Remaining elements: splice,filter,pop 3. Using splice() Method Any specific element from an array in JavaScript can be deleted with the JavaScript Array splice() technique. Furthermore, this ability allows one to add or remove multiple elements from an array. Syntax Javascript Output [ ‘C++ ‘, ‘ Java ‘, ‘ Go ‘, ‘ Prolog’ ] 4. Using filter() Method This approach generates a new array from a given array comprising just those elements satisfying a criteria defined by the argument function JavaScript. Output 5. Using Delete Operator Use the Delete Operator to remove elements from a JavaScript array. JavaScript Output Removed: true Remaining elements: lodash,remove,,reset 6. Using _.remove() Method The _.remove() method is used to remove all elements from the array that predicate returns True and returns the removed elements. Output 7. Using Array.prototype.slice() Method One might generate a new array excluding the elements you wish to delete by means of the slice() method While slice() returns a new array rather than changing the original one like splice() does,. Syntax JavaScript Output

How to Remove an Element from an Array in JavaScript? Read More »

Remove Duplicates from Sorted Array In CPP, C, JAVA,PYTHON,C#,JS

With a sorted array arr[] of size n, the aim is to reorganize the array so that every unique element shows at the beginning in sorted sequence. Return also the length of this unique sorted subarray. Note: Since they have no effect on the outcome, the items following the unique ones can be in any sequence and have any value. Example Using Hash Set – Works for Unsorted Also – O(n) Time and O(n) Space C++ Java Python C# JavaScript Output Anticipated Method – O(n) Time and O(1) Space The array is sorted hence we do not have to have a hash set. Every incident of an element would be consecutive. We so mostly have to find whether the current element is exactly the same as the previous one or not. Step by step application: C++ C JAVA Python C# JavaScript Output 12345

Remove Duplicates from Sorted Array In CPP, C, JAVA,PYTHON,C#,JS Read More »