Given an integer array arr[] , return the count of all distinct elements in the array. Input: arr = [10, 20, 20, 10, 30, 10] Output: 3 Explanation: The different values are 10 , 20 , and 30 . 10 an...
Given the head of a singly linked list, swap every two adjacent nodes and return the modified head. Node values must stay inside the original nodes, so the task swaps node links instead of rewritin...
Input: mat = [[1, 2, 3], [7, 6, 8], [9, 2, 5]], X = 6 Output: {1, 1} Explanation: Target value 6 is present at row 1 and column 1 . Input: mat = [[3, 4, 5, 0], [2, 9, 8, 7]], X = 10 Output: {-1, -1...
Input: mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] Output: 1 5 9 Explanation: Main diagonal cells are mat[0][0] , mat[1][1] , and mat[2][2] . Input: mat = [[10, 20, 30], [40, 50, 60]] Output: 10 50 Exp...
Given a matrix with N rows and M columns, print all elements row by row from left to right. tuf-examples-1 Input: mat = [[1, 2, 3], [4, 5, 6]] Output: 1 2 3 4 5 6 Explanation: First row prints as 1...
tuf-examples-1 tuf-plus-ad-1 1. Basic Pattern Matching Problem Suppose a word needs to be searched inside a larger sentence or string. The goal is to find all starting positions where the smaller p...
Given an integer n , return the sum of all digits present in the number using recursion. The negative sign, if present, is not considered a digit. Approach 1 A number can be processed one digit at...
Given an integer array nums sorted in ascending order, convert it into a height-balanced Binary Search Tree and return the root of the tree. A height-balanced BST means that for every node, the hei...
Given an array preorder that represents the preorder traversal of a Binary Search Tree, construct the BST and return its root. In preorder traversal, nodes are visited in this order: Root Left subt...
Morris Preorder Traversal Preorder traversal visits the nodes of a binary tree in the following order: Root → Left Subtree → Right Subtree The recursive method uses the call stack to remember w...
Morris Inorder Traversal Inorder traversal visits the nodes of a binary tree in the following order: Left Subtree → Root → Right Subtree The usual recursive method uses the call stack to rememb...
Requirements to Construct a Unique Binary Tree Constructing a binary tree means creating its nodes and determining exactly which node is the: Root Left child Right child Member of the left subtree...