I'm publicly committing to the 100DaysOfCode Challenge starting today!
Problem : Remove duplicate elements from sorted Array
Statement : Given a sorted array A[] of size N, delete all the duplicated elements from A[]. Modify the array such that if there are X distinct elements in it then the first X positions of the array should be filled with them in increasing order and return the number of distinct elements in the array.
Solution : click here
Problem : First Repeating Element
Statement : Given an array arr[] of size n, find the first repeating element. The element should occur more than once and the index of its first occurrence should be the smallest.
Note:- The position you return should be according to 1-based indexing.
Solution : click here
Problem : Key Pair
Statement : Given an array Arr of N positive integers and another number X. Determine whether or not there exist two elements in Arr whose sum is exactly X.
Solution : click here
Problem : Wave Array
Statement : Given a sorted array arr[] of distinct integers. Sort the array into a wave-like array(In Place).
In other words, arrange the elements into a sequence such that arr[1] >= arr[2] <= arr[3] >= arr[4] <= arr[5].....
If there are multiple solutions, find the lexicographically smallest one.
Solution : click here
Problem : Bubble Sort
Statement : Given an Integer N and a list arr. Sort the array using bubble sort algorithm.
Solution : click here
Problem : Find Transition Point
Statement : Given a sorted array containing only 0s and 1s, find the transition point.
Solution : click here
Problem : Union of Two Sorted Arrays
Statement : Union of two arrays can be defined as the common and distinct elements in the two arrays.
Given two sorted arrays of size n and m respectively, find their union.
Solution : click here
Problem : Search in Linked List
Statement :Given a linked list of n nodes and a key , the task is to check if the key is present in the linked list or not.
Solution : click here
Problem : Insertion Sort
Statement :The task is to complete the insert() function which is used to implement Insertion Sort.
Solution : click here
Problem : Minimum Cost of ropes
Statement :There are given N ropes of different lengths, we need to connect these ropes into one rope. The cost to connect two ropes is equal to sum of their lengths.
The task is to connect the ropes with minimum cost. Given N size array arr[] contains the lengths of the ropes.
Solution : click here
Problem : Level order traversal
Statement :Given a binary tree, find its level order traversal.
Level order traversal of a tree is breadth-first traversal for the tree.
Solution : click here
Problem : Count the triplets
Statement :Given an array of distinct integers. The task is to count all the triplets such that sum of two elements equals the third element.
Solution : click here
Problem : Delete a Node in Single Linked List
Statement :Given a singly linked list and an integer x.Delete xth node from the singly linked list.
Solution : click here
Problem : Move all zeroes to end of array
Statement :Given an array arr[] of N positive integers. Push all the zeros of the given array to the right end of the array while maintaining the order of non-zero elements.
Solution : click here
Problem : Search in a matrix
Statement :Given a matrix mat[][] of size N x M, where every row and column is sorted in increasing order, and a number X is given. The task is to find whether element X is present in the matrix or not.
Solution : click here
Problem : Element with left side smaller and right side greater
Statement :Given an unsorted array of size N. Find the first element in array such that all of its left elements are smaller and all right elements to it are greater than it.
Solution : click here
Problem : Delete Middle of Linked List
Statement :Given a singly linked list, delete middle of the linked list. For example, if given linked list is 1->2->3->4->5 then linked list should be modified to 1->2->4->5.
If there are even nodes, then there would be two middle nodes, we need to delete the second middle element. For example, if given linked list is 1->2->3->4->5->6 then it should be modified to 1->2->3->5->6.
If the input linked list is NULL or has 1 node, then it should return NULL.
Solution : click here
Problem : Selection Sort
Statement : Given an unsorted array of size N, use selection sort to sort arr[] in increasing order.
Solution : click here
Problem : Josephus problem
Statement : Given the total number of persons n and a number k which indicates that k-1 persons are skipped and kth person is killed in circle in a fixed direction.
After each operation, the count will start from k+1th person. The task is to choose the safe place in the circle so that when you perform these operations starting from 1st place in the circle, you are the last one remaining and survive.
Solution : click here
Problem : Minimum element in a sorted and rotated array
Statement : A sorted(in ascending order) array A[ ] with distinct elements is rotated at some unknown point, the task is to find the minimum element in it.
Solution : click here
Problem : Max Sum without Adjacents
Statement : Given an array Arr of size N containing positive integers. Find the maximum sum of a subsequence such that no two numbers in the sequence should be adjacent in the array.
Solution : click here
Problem : Largest subarray of 0's and 1's
Statement : Given an array of 0s and 1s. Find the length of the largest subarray with equal number of 0s and 1s.
Solution : click here
Problem : Count all possible paths from top left to bottom right
Statement : The task is to count all the possible paths from top left to bottom right of a m X n matrix with the constraints that from each cell you can either move only to right or down.
Solution : click here
Problem : Special Stack
Statement : Design a data-structure SpecialStack that supports all the stack operations like push(), pop(), isEmpty(), isFull() and an additional operation getMin() which should return minimum element from the SpecialStack. Your task is to complete all the functions, using stack data-Structure.
Solution : click here
Problem : K distance from root
Statement : Given a Binary Tree of size N and an integer K. Print all nodes that are at distance k from root (root is considered at distance 0 from itself). Nodes should be printed from left to right. If k is more that height of tree, nothing should be printed.
Solution : click here
Problem : Rotate by 90 degree
Statement : Given a square matrix of size N x N. The task is to rotate it by 90 degrees in anti-clockwise direction without using any extra space.
Solution : click here
Problem : Check if Linked List is Palindrome
Statement : Given a singly linked list of size N of integers. The task is to check if the given linked list is palindrome or not.
Solution : click here
Problem : Find the number of islands
Statement : Given a grid of size n*m (n is the number of rows and m is the number of columns in the grid) consisting of '0's (Water) and '1's(Land). Find the number of islands.
Solution : click here
Problem : Lowest Common Ancestor in a Binary Tree
Statement : Given a Binary Tree with all unique values and two nodes value, n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them are present.
Solution : click here
Problem : Next Permutation
Statement : Implement the next permutation, which rearranges the list of numbers into Lexicographically next greater permutation of list of numbers. If such arrangement is not possible, it must be rearranged to the lowest possible order i.e. sorted in an ascending order. You are given an list of numbers arr[ ] of size N.
Solution : click here
Problem : Validate an IP Address
Statement : Write a program to Validate an IPv4 Address. A valid IPv4 Address is of the form x1.x2.x3.x4 where 0 <= (x1, x2, x3, x4) <= 255. Thus, we can write the generalized form of an IPv4 address as (0-255).(0-255).(0-255).(0-255).
Solution : click here
Problem : Total Decoding Messages
Statement : A top secret message containing letters from A-Z is being encoded to numbers using the following mapping:
'A' -> 1
'B' -> 2
...
'Z' -> 26
You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^9 + 7.
Solution : click here
Problem : Maximum of all subarrays of size k
Statement : Given an array arr[] of size N and an integer K. Find the maximum for each and every contiguous subarray of size K.
Solution : click here
Problem : Pythagorean Triplet
Statement : Given an array arr of n integers, write a function that returns true if there is a triplet (a, b, c) from the array (where a, b, and c are on different indexes) that satisfies a^2 + b^2 = c^2, otherwise return false.
Solution : click here
Problem : Connect Nodes at Same Level
Statement : Given a binary tree, connect the nodes that are at same level. You'll be given an addition nextRight pointer for the same. Initially, all the nextRight pointers point to garbage values. Your function should set these pointers to point next right for each node.
Solution : click here
Problem : Implementing Dijkstra Algorithm
Statement : Given a weighted, undirected and connected graph of V vertices and an adjacency list adj where adj[i] is a list of lists containing two integers where the first integer of each list j denotes there is edge between i and j , second integers corresponds to the weight of that edge . You are given the source vertex S and You to Find the shortest distance of all the vertex's from the source vertex S. You have to return a list of integers denoting shortest distance between each node and Source vertex S.
Solution : click here
Problem : Knapsack with Duplicate Items
Statement : Given a set of N items, each with a weight and a value, represented by the array w and val respectively. Also, a knapsack with weight limit W. The task is to fill the knapsack in such a way that we can get the maximum profit. Return the maximum profit.
Solution : click here
Problem : Minimum Spanning Tree
Statement : Given a weighted, undirected and connected graph of V vertices and E edges. The task is to find the sum of weights of the edges of the Minimum Spanning Tree. Given adjacency list adj as input parameters . Here adj[i] contains vectors of size 2, where the first integer in that vector denotes the end of the edge and the second integer denotes the edge weight.
Solution : click here
Problem : Construct Tree from Inorder & Preorder
Statement : Given 2 Arrays of Inorder and preorder traversal. The tree can contain duplicate elements. Construct a tree and print the Postorder traversal.
Solution : click here
Problem : Egg Dropping Puzzle
Statement : You are given N identical eggs and you have access to a K-floored building from 1 to K. There exists a floor f where 0 <= f <= K such that any egg dropped from a floor higher than f will break, and any egg dropped from or below floor f will not break. There are few rules given below.
- An egg that survives a fall can be used again.
- A broken egg must be discarded.
- The effect of a fall is the same for all eggs.
- If the egg doesn't break at a certain floor, it will not break at any floor below.
- If the eggs breaks at a certain floor, it will break at any floor above.
Return the minimum number of moves that you need to determine with certainty what the value of f is.
Solution : click here
Problem : Floyd Warshall
Statement : The problem is to find the shortest distances between every pair of vertices in a given edge-weighted directed graph. The graph is represented as an adjacency matrix of size n*n. Matrix[i][j] denotes the weight of the edge from i to j. If Matrix[i][j]=-1, it means there is no edge from i to j.
Solution : click here
Problem :
Statement : Given a gold mine called M of (n x m) dimensions. Each field in this mine contains a positive integer which is the amount of gold in tons. Initially the miner can start from any row in the first column. From a given cell, the miner can move
- to the cell diagonally up towards the right
- to the right
- to the cell diagonally down towards the right
Solution : click here
Problem : Min distance between two given nodes of a Binary Tree
Statement : Given a binary tree and two node values your task is to find the minimum distance between them. The given two nodes are guaranteed to be in the binary tree and nodes are numbered from 1 to N. Please Note that a and b are not always leaf node.
Solution : click here
Problem : M-Coloring Problem
Statement : Given an undirected graph and an integer M. The task is to determine if the graph can be colored with at most M colors such that no two adjacent vertices of the graph are colored with the same color. Here coloring of a graph means the assignment of colors to all vertices. Print 1 if it is possible to colour vertices and 0 otherwise.
Solution : click here
Problem : Shortest Common Supersequence
Statement : Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Solution : click here
Problem : Smallest distinct window
Statement : Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
Solution : click here
Problem : Word Break
Statement : Given a string A and a dictionary of n words B, find out if A can be segmented into a space-separated sequence of dictionary words.
Solution : click here
Problem : Strongly Connected Components (Kosaraju's Algo)
Statement : Given a Directed Graph with V vertices (Numbered from 0 to V-1) and E edges, Find the number of strongly connected components in the graph.
Solution : click here
Problem : Steps by Knight
Statement : Given a square chessboard, the initial position of Knight and position of a target. Find out the minimum steps a Knight will take to reach the target position.
Solution : click here