HashingData Structures

Count Distinct Elements

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...

76.5k
Linked List

Swap Nodes in Pairs

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...

59.9k
Arrays

Search Element in Matrix

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...

98.5k
Arrays

Main Diagonal Print

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...

67.1k
Arrays

Row wise traversal

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...

61.7k
StringRecursionArrays

Introduction to String Advanced

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...

66.9k
Recursion

Sum of Digits of a Number Using Recursion

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...

64.6k
Binary Search TreeRecursion

Convert Sorted Array into BST

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...

114.9k
RecursionBinary Search Tree

Construct a BST from Preorder Traversal

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...

82.6k
Binary Tree

Morris Preorder Traversal: Binary Tree Traversal in O(1) Space

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...

82.2k
Binary Tree

Morris Inorder Traversal: Binary Tree Traversal in O(1) Space

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...

115.8k
Binary Tree

Requirements needed to construct a unique Binary Tree

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...

78.4k