Posts

Showing posts with the label CodingNinja

Minimum Bracket Reversal

For a given expression in the form of a string, find the minimum number of brackets that can be reversed in order to make the expression balanced. The expression will only contain curly brackets. If the expression can't be balanced, return -1. Example : Expression : {{{{ If we reverse the second and the fourth opening brackets, the whole expression will get balanced. Since we have to reverse two brackets to make the expression balanced, the expected output will be 2. Expression : {{{ In this example, even if we reverse the last opening bracket, we would be left with the first opening bracket and hence will not be able to make the expression balanced and the output will be -1. Input Format : The first and the only line of input contains a string expression, without any spaces in between.   Output Format : The only line of output will print the number of reversals required to balance the whole expression. Prints -1, otherwise. Note :You don't have to print anything. It has alrea...

Stock Span

Afzal has been working with an organization called 'Money Traders' for the past few years. The organization is into the money trading business. His manager assigned him a task. For a given array/list of stock's prices for N days, find the stock's span for each day. The span of the stock's price today is defined as the maximum number of consecutive days(starting from today and going backwards) for which the price of the stock was less than today's price. For example, if the price of a stock over a period of 7 days are [100, 80, 60, 70, 60, 75, 85], then the stock spans will be [1, 1, 1, 2, 1, 4, 6]. Explanation: On the sixth day when the price of the stock was 75, the span came out to be 4, because the last 4 prices(including the current price of 75) were less than the current or the sixth day's price. Similarly, we can deduce the remaining results. Afzal has to return an array/list of spans corresponding to each day's stock's price. Help him to achi...

Longest Increasing Subsequence

Given an array with N elements, you need to find the length of the longest subsequence in the given array such that all elements of the subsequence are sorted in strictly increasing order. Input Format The first line of input contains an integer N. The following line contains N space separated integers, that denote the value of elements of array. Output Format The first and only line of output contains the length of longest subsequence. Constraints 1 <= N <= 10^3 Time Limit: 1 second Sample Input 1 : 6 5 4 11 1 16 8 Sample Output 1 : 3 Sample Output Explanation Length of longest subsequence is 3 i.e. (5,11,16) or (4,11,16). Sample Input 2 : 3 1 2 2 Sample Output 2 : 2  

Delete node (recursive)

Given a singly linked list of integers and position 'i', delete the node present at the 'i-th' position in the linked list recursively.  Note : Assume that the Indexing for the linked list always starts from 0. No need to print the list, it has already been taken care. Only return the new head to the list.  input format : The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. The first line of each test case or query contains the elements of the singly linked list separated by a single space. The second line of input contains a single integer depicting the value of 'i'. Remember/Consider : While specifying the list elements for input, -1 indicates the end of the singly linked list and hence, would never be a list element Output format : For each test case/query, print the elements of the updated singly linked list. Output for every test case will be printed in a seperate line. C...

Find path in BST

Given a BST and an integer k. Find and return the path from the node with data k and root (if a node with data k is present in given BST) in a list. Return empty list otherwise. Note: Assume that BST contains all unique elements. Input Format : The first line of input contains data of the nodes of the tree in level order form. The data of the nodes of the tree is separated by space. If any node does not have left or right child, take -1 in its place. Since -1 is used as an indication whether the left or right nodes exist, therefore, it will not be a part of the data of any node. The following line of input contains an integer, that denotes the value of k. Output Format : The first line and only line of output prints the data of the nodes in the path from node k to root. The data of the nodes is separated by single space. Constraints: Time Limit: 1 second Sample Input 1: 8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1 2 Sample Output 1: 2 5 8  

Construct BST

Given a sorted integer array A of size n, which contains all unique elements. You need to construct a balanced BST from this input array. Return the root of constructed BST. Note: If array size is even, take first mid as root. Input format: The first line of input contains an integer, which denotes the value of n. The following line contains n space separated integers, that denote the values of array. Output Format: The first and only line of output contains values of BST nodes, printed in pre order traversal. Constraints: Time Limit: 1 second Sample Input 1: 7 1 2 3 4 5 6 7 Sample Output 1: 4 2 1 3 6 5 7  

Elements Between K1 and K2

Given a Binary Search Tree and two integers k1 and k2, find and print the elements which are in range k1 and k2 (both inclusive). Print the elements in increasing order. Input format: The first line of input contains data of the nodes of the tree in level order form. The data of the nodes of the tree is separated by space. If any node does not have left or right child, take -1 in its place. Since -1 is used as an indication whether the left or right nodes exist, therefore, it will not be a part of the data of any node. The following line contains two integers, that denote the value of k1 and k2. Output Format: The first and only line of output prints the elements which are in range k1 and k2 (both inclusive). Print the elements in increasing order. Constraints: Time Limit: 1 second Sample Input 1: 8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1 6 10 Sample Output 1: 6 7 8 10  

Search In BST

Given a BST and an integer k. Find if the integer k is present in the given BST or not. You have to return true, if a node with data k is present, return false otherwise. Note: Assume that BST contains all unique elements. Input Format: The first line of input contains data of the nodes of the tree in level order form. The data of the nodes of the tree is separated by space. If any node does not have left or right child, take -1 in its place. Since -1 is used as an indication whether the left or right nodes exist, therefore, it will not be a part of the data of any node. The following line of input contains an integer, that denotes the value of k. Output Format: The first and only line of output contains a boolean value. Print true, if node with data k is present, print false otherwise. Constraints: Time Limit: 1 second Sample Input 1 : 8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1 2 Sample Output 1 : true Sample Input 2 : 8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1 12 Sample Output 2 : false  

Min Steps To 1

Given a positive integer 'n', find and return the minimum number of steps that 'n' has to take to get reduced to 1. You can perform any one of the following 3 steps: 1.) Subtract 1 from it. (n = n - ­1) , 2.) If n is divisible by 2, divide by 2.( if n % 2 == 0, then n = n / 2 ) , 3.) If n is divisible by 3, divide by 3. (if n % 3 == 0, then n = n / 3 ). Input format : The first and the only line of input contains an integer value, 'n'. Output format : Print the minimum number of steps. Constraints : 1 <= n <= 10 ^ 6 Time Limit: 1 sec Sample Input 1 : 4 Sample Output 1 : 2 Explanation of Sample Output 1 : For n = 4 Step 1 : n = 4 / 2 = 2 Step 2 : n = 2 / 2 = 1 Sample Input 2 : 7 Sample Output 2 : 3 Explanation of Sample Output 2 : For n = 7 Step 1 : n = 7 ­- 1 = 6 Step 2 : n = 6 / 3 = 2 Step 3 : n = 2 / 2 = 1

Rat In a Maze All Paths

You are given a N*N maze with a rat placed at maze[0][0]. Find and print all paths that rat can follow to reach its destination i.e. maze[N-1][N-1]. Rat can move in any direc­tion ( left, right, up and down). Value of every cell in the maze can either be 0 or 1. Cells with value 0 are blocked means rat can­not enter into those cells and those with value 1 are open. Input Format The first line of input contains an integer 'N' representing the dimension of the maze. The next N lines of input contain N space-separated integers representing the type of the cell. Output Format : For each test case, print the path from start position to destination position and only cells that are part of the solution path should be 1, rest all cells should be 0. Output for every test case will be printed in a separate line. Note: You do not need to print anything, it has already been taken care of. Just implement the given function. Constraints: 0 < N < 11 0 <= Maze[i][j] <=1 Time Lim...

Amazing Strings

Given 3 Strings, check whether the 3rd string contains all the characters of strings 1 and 2 in any order. If all the characters are present, print "YES" otherwise print "NO". There should not be any extra character and any missing character. The string s contains uppercase Latin letters only. Input format : Line 1 : First String Line 2 : Second String Line 3 : Third String Output format : YES or NO Constraints : 1 <= n (Length of 1st & 2nd Strings) <= 100 Sample Input 1 : SANTACLAUS DEDMOROZ SANTAMOROZDEDCLAUS Sample Output 1 : YES Sample Input 2 : PAPAINOEL JOULUPUKKI JOULNAPAOILELUPUKKI Sample Output 2 : NO Sample Input 3 : BABBONATALE FATHERCHRISTMAS BABCHRISTMASBONATALLEFATHER Sample Output 3 : NO Sample Output Explanation : Output 1: the letters written in the last line can be used to write the names and there won't be any extra letters left. Output 2: Letter "P" is missing and there's an extra letter "L" Output 3: There...

Special Sum of array

Level EASY Given an array of length N, which contains single digit elements at every index. You need to find and return the sum of all elements of the array. But the final sum should also be single digit. In order to keep the output single digit - you need to keep adding the output number digits till single digit is left. Input Format : Line 1 : An Integer N i.e. size of array Line 2 : N integers which are elements of the array, separated by spaces Output Format : Single digit sum Constraints : 1 <= N <= 10^6 Sample Input 1 : 3 9 9 9 Sample Output 1 : 9 Sample Output Explanation : 9 + 9 + 9 = 27 2 + 7 = 9 Hence, ans is 9. Sample Input 2 : 5 1 7 8 5 9 Sample Output 1 : 3 Sample Output Explanation : 1 + 7 + 8 + 5 + 9 = 30 3 + 0 = 3 Hence, ans is 3

Consecutive elements

Level MEDIUM You are given an array arr of N non-negative integers, you need to return true if the array elements consist of consecutive numbers otherwise return false. For Example: If the given array is [4,3,5] then you should return true as all the array elements are in consecutive order. Input Format: The first line of input contains a single integer T, representing the number of test cases or queries to be run. Then the T test cases follow. The first line of each test case contains an integer 'N', representing the length of the array. The next line contains 'N' single space-separated integers representing elements of the array. Output Format : For each test case, print “True” or “False” in a separate line. Note: You are not required to print the expected output, it has already been taken care of. Just implement the function. Constraints: 1 <= T <= 10 1 <= N <= 10^5 0 <= arr[i] <= 10^9 Time Limit: 1 sec Sample Input 1: 1 4 1 2 4 6 Sample Output ...

Division of 4

Level EASY Given an array, update each element of the array by the value obtained by dividing the element by 4 (take only integer part). If the value obtained by dividing the element by 4 comes out to be 0, then update the element with value -1. Note: Do not return or print the array and make changes in the same array. Input Format : Line 1: An Integer N i.e. size of the array Line 2: N integers which are elements of the array, separated by spaces Output Format : N elements of the array, separated by space Constraints : 1 <= N <= 10^5 0 <= array[i] <= 100 Time Limit: 1 sec Sample Input : 2 3 8 Sample Output : -1 2 Sample Output Explanation : The input list is [ 3, 8 ] , after dividing each element by 4, our list becomes [ 0, 2 ] . So as the first element is 0 so replace it by -1.

Delete node

Image
You have been given a linked list of integers. Your task is to write a function that deletes a node from a given position, 'pos'. Note : Assume that the Indexing for the linked list always starts from 0. If the position is greater than or equal to the length of the linked list, you should return the same linked list without any change. Illustration : The following images depict how the deletion has been performed. Image-I : Image-II : Input format : The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. The first line of each test case or query contains the elements of the linked list separated by a single space. The second line of each test case contains the integer value of 'pos'. It denotes the position in the linked list from where the node has to be deleted.  Remember/Consider : While specifying the list elements for input, -1 indicates the end of the singly linked list and hence, ...

Balanced Paranthesis

For a given a string expression containing only round brackets or parentheses, check if they are balanced or not. Brackets are said to be balanced if the bracket which opens last, closes first. Example: Expression: (()()) Since all the opening brackets have their corresponding closing brackets, we say it is balanced and hence the output will be, 'true'. You need to return a boolean value indicating whether the expression is balanced or not. Note: The input expression will not contain spaces in between. Input Format: The first and the only line of input contains a string expression without any spaces in between.  Output Format: The only line of output prints 'true' or 'false'. Note: You don't have to print anything explicitly. It has been taken care of. Just implement the function. Constraints: 1 <= N <= 10^7 Where N is the length of the expression. Time Limit: 1sec Sample Input 1 : (()()()) Sample Output 1 : true Sample Input 2 : ()()(() Sample Outp...

Popular posts from this blog

MySQL Multi Source Master Slave Replication using GTID

Setting Up PostgreSQL Logical Replication with Docker Compose

Regex 101: An Introduction to Regular Expressions for Developers