The Coin Change Problem[hackerRank solution]

This is a C++ Program that Solves Coin Change Problem using Dynamic Programming technique.

Problem:

There are infinite number of coins of x different values. These values are given. Using these coins, you have to make change for Rs. N. In how many ways, you can make this change?

Continue reading “The Coin Change Problem[hackerRank solution]”

Advertisements

AI comes of age

It was only in December that the US’ National Aeronautics and Space Administration (Nasa) announced that a solar system with as many planets as our own had been discovered with the help of its Kepler space telescope and AI.

Closer home, Microsoft and the International Crops Research Institute for the Semi-Arid Tropics are using an AI-based sowing app that is expected to help about 4,000 farmers in Andhra Pradesh and Karnataka during the kharif season. HDFC Bank’s AI chatbot Eva, built by Senseforth AI Research, now works with the Google Assistant in Indian English. It has already answered more than five million user queries with more than 85% accuracy, according to the bank.

Continue reading “Artificial Intelligence is the next new”

Egg Dropping Puzzle[DP][GFG]

Problem:

The following is a description of the instance of this famous puzzle involving n=2 eggs and a building with k=36 floors.

Suppose that we wish to know which stories in a 36-story building are safe to drop eggs from, and which will cause the eggs to break on landing. We make a few assumptions:

Continue reading “Egg Dropping Puzzle[DP][GFG]”

Sparse Arrays[HackerRank Solution]

Problem:

There is a collection of N strings ( There can be multiple occurences of a particular string ). Each string’s length is no more than 20 characters. There are also Q queries. For each query, you are given a string, and you need to find out how many times this string occurs in the given collection of N strings.

Input Format

Continue reading “Sparse Arrays[HackerRank Solution]”

Sherlock and Array[HackerRank Solution]

Problem:

Watson gives Sherlock an array A of length n. Then he asks him to determine if there exists an element in the array such that the sum of the elements on its left is equal to the sum of the elements on its right. If there are no elements to the left/right, then the sum is considered to be zero.

 

Input Format

Continue reading “Sherlock and Array[HackerRank Solution]”

Chocolate Feast[HackerRank Solution]

Problem:

Little Bobby loves chocolate, and he frequently goes to his favorite  store, Penny Auntie, with  dollars to buy chocolates. Each chocolate has a flat cost of  dollars, and the store has a promotion where they allow you to trade in  chocolate wrappers in exchange for  free piece of chocolate.

For example, if  and Bobby has  dollars that he uses to buy  chocolates at  dollar apiece, he can trade in the  wrappers to buy  more chocolates. Now he has  more wrappers that he can trade in for  more chocolate. Because he only has  wrapper left at this point and , he was only able to eat a total of  pieces of chocolate.

Given n ,m ,c and t for  trips to the store, can you determine how many chocolates Bobby eats during each trip?

Continue reading “Chocolate Feast[HackerRank Solution]”

The Maximum Subarray[HackerRank Solution] Dynamic Programming

Problem:

Given an array of N elements, find the maximum possible sum among

  1. all nonempty subarrays.
  2. all nonempty subsequences.

We define a subarray as a contiguous subsequence. Note that empty subarrays/subsequences should not be considered.

Input Format

The first line of input contains a single integer T denoting the number of test cases.

The first line of each test case contains a single integer N . The second line contains N space-separated integers  denoting the elements of A.

Continue reading “The Maximum Subarray[HackerRank Solution]”

Big Sorting[HackerRank Solution]

Problem:

Consider an array of numeric strings, , where each string is a positive number with anywhere from  to  digits. Sort the array’s elements in non-decreasing (i.e., ascending) order of their real-world integer values and print each element of the sorted array on a new line.

Input Format

The first line contains an integer, , denoting the number of strings in .
Each of the  subsequent lines contains a string of integers describing an element of the array.

Constraints

  • Each string is guaranteed to represent a positive integer without leading zeros.
  • The total number of digits across all strings in  is between  and  (inclusive).

Output Format

Print each element of the sorted array on a new line.

Sample Input 0

6
31415926535897932384626433832795
1
3
10
3
5

Sample Output 0

1
3
3
5
10
31415926535897932384626433832795

Code:

#include <bits/stdc++.h>

using namespace std;

bool check(string str1, string str2)
{
 int n1 = str1.length(), n2 = str2.length();
 
 if (n1 < n2)
 return true;
 if (n2 < n1)
 return false;
 for (int i=0; i<n1; i++)
 {
 if (str1[i] < str2[i])
 return true;
 if (str1[i] > str2[i])
 return false;
 }
 
 return false;
}
int main(){
 int n;
 cin >> n;
 vector<string> s;
 for(int i=0;i<n;i++)
 {
 string s1;
 cin>>s1;
 s.push_back(s1);
 } 
 sort(s.begin(),s.end(),check);
 for(int i=0;i<s.size();i++)
 {
 cout<< s[i] <<endl;
 }
 return 0;
}

Passed all test cases!

Monk and Philosopher’s Stone[Hackerearth Solution]

PROBLEM:

Harry Potter wants to get the Philosopher’s stone to protect it from Snape. Monk being the guard of Philosopher’s Stone is very greedy and has a special bag, into which he can add one gold coin at a time or can remove the last gold coin he added. Monk will sleep, once he will have the enough number of gold coins worth amount X. To help Harry, Dumbledore has given a same kind of bag to Harry (as of Monk) with N gold coins each having worth A[i] where i range from 1iN.

Continue reading “Monk and Philosopher’s Stone[Hackerearth Solution]”

Blog at WordPress.com.

Up ↑