Posts

Showing posts from September, 2023

array m&h

Image
Q In this qustion we have to make the four points using this we can store all the values from the matrix. important to make two conditions 1)top<= bottom is required to avoid the repeated row when we have only one row, 2) left<=right required when when in last we have only one element left.   Q Similar as the matrix 1 only difference is to use the number and increment in next step.   Q

revice easy array

Image
 Q Approach- note-> 1) we can use the outer loop for take the lenngth of indices 2) we use the [i][0] in first loop to increment the cols by making constant row 3)[i][1] use to change the row by making constant cols 2nd way->  create two boolean array for row and col with row col size  row[indices[i][0]] ^=true;  same with col using [1]  then count r and c values ; use following formulae; Q sol-> first run loop till length  after that take the sum from primary and secondary side  if length is odd then we have to - the middle element because it comes twice    Q approch 1->while(0<nums[i]) - count++  nums[i]/=10; approch2-> Math.log10(nums[i])+1;  it produce odd value for even and even for odd so we have to add 1 to it; approch3-> we set the limit like >9 && < =99 || like wise; Q ans-> Use the new 2D ans for [col][row] because if matrix size is 2*4 the after transpose it becomes 4*2 thats why we g...

Quick Sort Algorithm

Image
  Quick Sort Algorithm Problem Statement:   Given an array of n integers, sort the array using the  Quicksort  method. Examples: Example 1: Input: N = 5 , Arr[] = {4,1,7,9,3} Output: 1 3 4 7 9 Explanation: After sorting the array becomes 1, 3, 4, 7, 9 Example 2: Input: N = 8 , Arr[] = {4,6,2,5,7,9,1,3} Output: 1 2 3 4 5 6 7 9 Explanation: After sorting the array becomes 1, 3, 4, 7, 9 Solution Disclaimer :  Don’t jump directly to the solution, try it out yourself first.  Problem Link . Intuition: Quick Sort is a divide-and-conquer algorithm like  the Merge Sort . But unlike Merge sort, this algorithm does not use any extra array for sorting(though it uses an auxiliary stack space). So, from that perspective, Quick sort is slightly better than Merge sort. This algorithm is basically a repetition of two simple steps that are the following: Pick a pivot and place it in its correct place in the sorted array. Shift smaller elements(i.e. Smaller ...

Merge Sort Algorithm

Image
  Merge Sort Algorithm Problem :  Given an array of size n, sort the array using  Merge Sort . Examples: Example 1: Input: N=5, arr[]={4,2,1,6,7} Output: 1,2,4,6,7, Example 2: Input: N=7,arr[]={3,2,8,5,1,4,23} Output: 1,2,3,4,5,8,23 Solution Disclaimer :  Don’t jump directly to the solution, try it out yourself first. Solution :   Intuition:  Merge Sort is a  divide and conquers algorithm , it divides the given array into equal parts and then merges the 2 sorted parts.  There are 2 main functions : merge():  This function is used to merge the 2 halves of the array. It assumes that both parts of the array are sorted and merges both of them. mergeSort():  This function divides the array into 2 parts. low to mid and mid+1 to high where, low = leftmost index of the array high = rightmost index of the array mid = Middle index of the array We recursively split the array, and go from top-down until all sub-arrays size becomes 1. Appr...