Matrix Diagonal Difference using Kotlin

Problem Description: Given a square matrix, calculate the absolute difference between the sums of its diagonals: Mathematical Expression: If matrix[i][j] represents the element at the i-th row and j-th column: Goal: Compute: Diagonal Difference = | Sum of Primary Diagonal−Sum of Secondary Diagonal | Example Input: 11 2 4 4 5 6 10 8 -12 Calculations: Output: 15 Kotlin Solution Below is … Read more

Cycle Detection in Linked Lists

Problem Description: A cycle in a linked list occurs when a node’s next pointer points back to one of the previous nodes, creating a loop. This means traversing the linked list will not end; it will keep looping indefinitely. Detecting such cycles is an important task in linked list operations, particularly when dealing with dynamic … Read more

0/1 knapsack problem recursive

The 0/1 Knapsack problem, as discussed, seeks to maximize the total value of items that can be included in a knapsack without exceeding its weight capacity. Each item has a specified weight and value, and the objective is to determine the optimal subset of items to include. Unlike the dynamic programming approach, solving the 0/1 … Read more

0/1 Knapsack using DP

The 0/1 Knapsack problem is a fundamental problem in combinatorial optimization. Given a set of items, each with a weight and a value, and a knapsack with a maximum weight capacity, the objective is to maximize the total value of items that can be included in the knapsack without exceeding its weight capacity. Each item … Read more

Sum of Subset using Recursion

The “Sum of Subset” problem is a classic example of a combinatorial problem that can be solved using recursion. The objective is to determine whether a subset of the given set of numbers can be found that sums up to a specific target value. This problem is significant in computer science, particularly in the fields … Read more

Search a element in Sorted Matrix in Kotlin

To search for an element in a matrix, you can use various search algorithms depending on the properties of the matrix. One common and efficient algorithm to search for an element in a sorted matrix is the “Binary Search” algorithm. The binary search algorithm can be applied in a matrix if the matrix is sorted … Read more

find the difference between two strings

To find the difference between two strings where one string is a permutation of another string with one additional character, you can use bit manipulation in Kotlin. This approach leverages the XOR operator to cancel out pairs of identical characters, leaving only the additional character as the result. Here’s how you can implement a function … Read more

Finding the missing number in an array

Finding the missing number in an array containing n distinct numbers taken from 0, 1, 2, …, n is a classic problem that can be efficiently solved using bit manipulation. The idea is to use the XOR operation, which has two useful properties for this problem: Given these properties, you can XOR all the numbers … Read more