Given n courses, labeled from 0 to n – 1 and an array prerequisites[] where prerequisites[i] = [x, y] indicates that we need to take course y first if we want to take course x.
Find if it is possible to complete all tasks. Return true if all tasks can be completed, or false if it is impossible.
Examples:
Input: n = 4, prerequisites = [[2, 0], [2, 1], [3, 2]] Output: true Explanation: To take course 2, you must first finish courses 0 and 1. To take course 3, you must first finish course 2. All courses can be completed, for example in the order [0, 1, 2, 3] or [1, 0, 2, 3].
Input: n = 3, prerequisites = [[0, 1], [1, 2], [2, 0]] Output: false Explanation: To take course 0, you must first finish course 1. To take course 1, you must first finish course 2. To take course 2, you must first finish course 0. Since each course depends on the other, it is impossible to complete all courses.
3 Sum – Triplet Sum Closest to Target
Given an array arr[] of n integers and an integer target, find the sum of triplets such that the sum is closest to target. Note: If there are multiple sums closest to target, print the maximum one.
Examples:
Input: arr[] = [-1, 2, 2, 4], target = 4 Output: 5 Explanation: All possible triplets
[-1, 2, 2], sum = (-1) + 2 + 2 = 3
[-1, 2, 4], sum = (-1) + 2 + 4 = 5
[-1, 2, 4], sum = (-1) + 2 + 4 = 5
[2, 2, 4], sum = 2 + 2 + 4 = 8
Triplet [-1, 2, 2], [-1, 2, 4] and [-1, 2, 4] have sum closest to target, so return the maximum one, that is 5.
Input: arr[] = [1, 10, 4, 5], target = 10 Output: 10 Explanation: All possible triplets
[1, 10, 4], sum = (1 + 10 + 4) = 15
[1, 10, 5], sum = (1 + 10 + 5) = 16
[1, 4, 5], sum = (1 + 4 + 5) = 10
[10, 4, 5], sum = (10 + 4 + 5) = 19
Triplet [1, 4, 5] has sum = 10 which is closest to target.
Word Search in a 2D Grid of characters
Given a 2D grid m*n of characters and a word, the task is to find all occurrences of the given word in the grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match in this direction (not in zig-zag form). The 8 directions are, Horizontally Left, Horizontally Right, Vertically Up, Vertically Down and 4 Diagonal directions.
Note: The returning list should be lexicographically smallest. If the word can be found in multiple directions starting from the same coordinates, the list should contain the coordinates only once.