Pots of Gold Game – Dynamic Programming

Two-player X and Y are playing Pots of Gold Game where pots of gold arranged in a  line, each containing some gold coins. Both players will get an alternating chance in which the player can pick a pot from one of the ends of the line. The winner is the player who has a higher number of coins at the end. The objective is to … Continue reading Pots of Gold Game – Dynamic Programming

Minimum cost to reach destination – Dynamic Programming

Give n*n matrix find minimum cost required to reach from source to destination. Lets consider source is Array [0][0] and destination is Array [n-1][ n-1] the each elements also represent as cost You can only move down, right and diagonally lower elements from a given element. For example: The path of minimum cost is 1 ->5->1 so minimum cost to reach destination is 7. Dynamic … Continue reading Minimum cost to reach destination – Dynamic Programming

Edit Distance Problem (Levenshtein distance) – Dynamic Programming

There are two strings source and destination. The goal of the problem is to convert source to destination by applying minimum edit operations on string str1. The edit operations are following – Insert Remove Replace For example: We need only one operation to replace char ‘e’ with char ‘o’ Using Recursion Using Dynamic Programming Continue reading Edit Distance Problem (Levenshtein distance) – Dynamic Programming

Longest Increasing Subsequence Problem – Dynamic Programming

Given an array find a subsequence of a given array in which the array’s elements are in sorted order, lowest to highest, and in which the subsequence is as long as possible Example: From below give array a longest increasing subsequence is This subsequence has length six; the input sequence has no seven-member increasing subsequences. The longest increasing subsequence in this example is not the … Continue reading Longest Increasing Subsequence Problem – Dynamic Programming

Subset Sum Problem – Dynamic Programming

Given a set of positive integers and a value sum write program to determine if there is a subset of the given set with sum equal to given sum. For e.g. subset {4,12,18} has the sum 34 Solution Using Dynamic Programming Approach We will find whether if there is exists any subset of with provided sum 1 if it’s existed store it in array and … Continue reading Subset Sum Problem – Dynamic Programming

Maximum sub array problem – Kadane’s Algorithm

Given a sequence of n real numbers A(1) … A(n), program to find a contiguous subarray with the largest sum For example, var arr = new int[] {-2, 1,-3, 4,-1, 2, 1,-5, 4 }; the contiguous sub array with the largest sum is [4, −1, 2, 1], with sum 6. Solution Solution via iterating over all array elements repeatedly, try each key elements Execute above … Continue reading Maximum sub array problem – Kadane’s Algorithm

Balanced Brackets –  HackerRank Problem Solution

Question: A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and (). A matching pair of brackets is not balanced if the set of brackets it encloses … Continue reading Balanced Brackets –  HackerRank Problem Solution

Append and Delete –  HackerRank Problem Solution

Question: You have a string of lowercase English alphabetic letters. You can perform two types of operations on the string: 1.       Append a lowercase English alphabetic letter to the end of the string. 2.       Delete the last character in the string. Performing this operation on an empty string results in an empty string. Given an integer,k , and two strings, s and t, determine whether or not you can convert  to  by performing exactly  k … Continue reading Append and Delete –  HackerRank Problem Solution

Binary Numbers – HackerRank Problem solution

Question: Given a base- 10 integer, n, convert it to binary (base-2). Then find and print the base-10 integer denoting the maximum number of consecutive 1’s in n Input: Output: Explanation Sample Case 1:The binary representation of 5 is 101, so the maximum number of consecutive 1’s is 1. Here is original problem from HackerRank Answer: Continue reading Binary Numbers – HackerRank Problem solution

Migratory Birds – HackerRank Problem Solution

Question: You have been asked to help study the population of birds migrating across the continent. Each type of bird you are interested in will be identified by an integer value. Each time a particular kind of bird is spotted, its id number will be added to your array of sightings. You would like to be able to find out which type of bird is … Continue reading Migratory Birds – HackerRank Problem Solution

Ice Cream Parlor – HackerRank Problem Solution

Question Sunny and Johnny like to pool their money and go to the ice cream parlor. Johnny never buys the same flavor that Sunny does. The only other rule they have is that they spend all of their money. Given a list of prices for the flavors of ice cream, select the two that will cost all of the money they have. For example, they … Continue reading Ice Cream Parlor – HackerRank Problem Solution

Counting Valleys – HackerRank Problem solution

Question: Gary is an avid hiker. He tracks his hikes meticulously, paying close attention to small details like topography. During his last hike he took exactly n steps. For every step he took, he noted if it was an uphill, U, or a downhill, D step. Gary’s hikes start and end at sea level and each step up or down represents a 1 unit change in altitude. We define the following terms: A mountain is … Continue reading Counting Valleys – HackerRank Problem solution

Staircase – HackerRank Problem solution

Question: Consider a staircase of size n=4: Observe that its base and height are both equal to n , and the image is drawn using # symbols and spaces. The last line is not preceded by any spaces. Write a program that prints a staircase of size n  input: Output: Here is actual problem HackerRank. Answer: Solution Logic Take variable which stores value of white space, initialize it with array … Continue reading Staircase – HackerRank Problem solution

Sock Merchant – HackerRank Problem Solution

Question: John works at a clothing store. He has a large pile of socks that he must pair by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are. For example, there are n=7  socks  with colors  ar = [1,2,1,2,1,3,2]. There is one pair of color 1 and one of color 2 . There … Continue reading Sock Merchant – HackerRank Problem Solution

Alternating Characters – HackerRank Problem Solution

Question: You are given a string containing characters and only. Your task is to change it into a string such that there are no matching adjacent characters. To do this, you can delete zero or more characters in the string. Your task is to find the minimum number of required deletions. For example, given the string s=AABAAB, remove an A at positions 0 and 3  … Continue reading Alternating Characters – HackerRank Problem Solution

Bot Saves Princess – HackerRank Problem Solution

Question: Princess Peach is trapped in one of the four corners of a square grid. You are in the center of the grid and can move one step at a time in any of the four directions. Can you rescue the princess? Input format The first line contains an odd integer N (< 100) denoting the size of the grid. This is followed by an … Continue reading Bot Saves Princess – HackerRank Problem Solution