Get Spiral Traversal of Matrix
[Question]: Given a Matrix, print the given matrix in spiral order.Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [1,2,3,6,9,8,7,4,5]
[Question]: Given a Matrix, print the given matrix in spiral order.Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [1,2,3,6,9,8,7,4,5]
[Question] : Given a 2D matrix, your task is to rotate the matrix 90 degrees clockwise.Input: – [[1, 2, 3], [4, 5, 6], [7, 8, 9]]Output – [[7, 4, 1], [8, 5, 2], [9, 6, 3]]
[Question]: Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence. You must write an algorithm that runs in O(n) time.Example: I/P = [200,4,2,1,3,100].. Output-> 4 because 1,2,3,4 is largest consecutive. Now a similar problem which gives consecutive array
[Question 1] .Given an array arr, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1 Example 1:: Input: arr = [17,18,5,4,6,1] Output: [18,6,6,6,1,-1] Explanation: – index 0 –> the greatest element to the right of index 0 is index 1 (18). – index 1 –> …
[Question]: An given array contains equal number of positive and negative elements. Without altering the relative order of positive and negative elements, you must return an array of alternately positive and negative values.Example: —Input: {1,2,-4,-5}Output: 1 -4 2 -5 Approach #1 As positive index is on even numbers so create variable named positiveIndex with 0 …
[Question]: In an array of prices where prices[i] is the price of a given stock on an ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this …
[Question]: Find max sum in an given array
[Question] Find the Majority Element in an given array that occurs more than N/2 times Approach #1 Brute-force Use two loop and compare each elements with rest if it matches increment the counter Approach #2 : Optimal Approach: Moore’s Voting Algorithm Basically, we are trying to keep track of the occurrences of the majority element …
Find the Majority Element that occurs more than N/2 times Read More »
[Question]: How to sort an array which has 0s, 1s and 2s Approach #1 Using Dutch National Flag I used three pointers low mid & high The sorting of 0,1,2 is as per below steps arr[0….low-1] contains 0. [most left part] arr[low….mid-1] contains 1arr[high+1….n-1] contains 2. [most right part],So we swap according to the left(0) …
Sort an array of 0s, 1s and 2s || Dutch National Flag solution Read More »
[Question] : Given an array of integers nums and an integer target, find indices of the two numbers which addition equals to target.Example: Input: nums = [2,7,11,15], target = 13 Output: [0,2] Explanation: Because nums[0] + nums[2] == 13, we return [0, 2]. Approach #1 : Using Two pointers: – Step 1: First sort array thenStep 2: If arr[left] …