Suppose we have one 2D matrix, that is representing one image. We have to rotate this image to 90 degrees clockwise. So if the image is like157963213Then the output will be291165337To solve this, we will follow these steps −Consider temp_mat = [], col := length of matrix – 1for col in range 0 to length of matrixtemp := []for row in range length of matrix – 1 down to -1add matrix[row, col] in tempadd temp into temp_matfor i in range 0 to length of matrixfor j in range 0 to length of matrixmatrix[i, j] := temp_mat[i, j]Example(Python)Let us see the ... Read More
Suppose we have a collection of distinct integers; we have to find all possible permutations. Now if the array stores the duplicate elements, then ignore that state which is looking similar. So if the array is like [1, 1, 3], then the result will be [[1, 1, 3], [1, 3, 1], [3, 1, 1]]To solve this, we will follow these steps −We will use the recursive approach, this will make the list, index. Index is initially 0if index = size of the list then insert list into res array, and returnfor i in range index to length of given list ... Read More
Suppose we have one 9x9 Sudoku board. We have to check whether that is valid or now. Only the filled cells need to be validated according to the following rules −Each row must contain the digits from 1-9 without repetition.Each column must contain the digits from 1-9 without repetition.Each of the 9 (3x3) sub-boxes of the grid must contain the digits from 1-9 without repetition.Suppose the Sudoku grid is like −537619598686348317266284195879This is valid.To solve this, we will follow these steps −for i in range 0 to 8create some empty dictionary called row, col and block, row_cube := 3 * (i ... Read More
Suppose we have an array of integers A. This is sorted in ascending order, we have to find the starting and ending position of a given target value. When the target is not found in the array, return [-1, -1]. So if the array is like [2, 2, 2, 3, 4, 4, 4, 4, 5, 5, 6], and target is 4, then the output will be [4, 7]To solve this, we will follow these steps −Initially res := [-1, -1], set low := 0, high := length of array Awhile low < highmid := low + (high – low)/2if A[mid] ... Read More
Consider we have an array sorted in ascending order, and that is rotated at some pivot unknown to you beforehand. For example, [0, 1, 2, 4, 5, 6, 7] might become [4, 5, 6, 7, 0, 1, 2]. We have given a target value to the search. If we can get it in the array, then return its index, otherwise return -1. We can assume no duplicate exists in the array. So if the array is like [4, 5, 6, 7, 0, 1, 2], then the output will be 4. as the index of this element is present at index ... Read More
Suppose we have two integers dividend and divisor. We have to divide two integers without using multiplication, division, and mod operator. Return the quotient after dividing the dividend by divisor. The integer division should truncate toward zero. Both of the inputs are integersSo if the given inputs are dividend = 7, divisor = -3, then output will be -2.To solve this, we will follow these steps −Taking two arguments x and y it indicates x divides yif x < -Infinity and y = 1, then return infinitya := |x|, b := |y| and ans := 0while a – b >= ... Read More
Consider we have a linked list. We have to swap every two adjacent nodes and return its head. The constraint is that we cannot modify the value of the nodes, only the node itself can be changed. So if the list is like [1, 2, 3, 4], then the resultant list will be [2, 1, 4, 3]To solve this, we will follow these steps −if head is not present, then return headfirst := head, second := next of head, dummy is one new node with value -1next of dummy := first, and prev := dummywhile second is not nulltemp := ... Read More
Suppose we have a value n. We have to generate all possible well-formed parentheses where n number of opening and closing parentheses are present. So if the value of n = 3, then the parentheses set will be ["()()()", "()(())", "(())()", "(()())", "((()))"]To solve this, we will follow these steps −Define method called genParenthesisRec(). This takes left, right, temp string and result array. initially result array is emptyThe function genParenthesisRec, will work like belowif left = 0 and right := 0, then insert temp into result, and returnif left > 0getParenthesisRec(left – 1, right, temp + “(”, result)if right > ... Read More
Suppose we have a linked list. We have to remove the Nth node from the end of the list, then return its head. So if the list is like [1, 2, 3, 4, 5, 6] and n = 3, then the returned list will be [1, 2, 3, 5, 6].To solve this, we will follow these steps −If there is no node after head, then return Nonefront := head, back := head, counter := 0 and fount := falsewhile counter
Java 9 introduced an interactive REPL (Read-Evaluate-Print-Loop) tool: JShell, and it allows us to execute code snippets and get an immediate result. A snippet is an instruction that can use standard Java syntax. It represents a single expression, statement, or declaration.Below are some of the rules we need to follow while using the JShell tool.Rules for JShell tool:A snippet is like import declarations, class declarations, method declarations, interface declarations, field declarations, statements, and primary expressions.The package declarations are not allowed. JShell code is placed under the transient JShell package.The access modifiers: public, protected, and private, and the modifiers: final and static are not allowed in ... Read More