Skip to content

Instantly share code, notes, and snippets.

View gauravbhasin87's full-sized avatar
🏠
Working from home

gauravbhasin87

🏠
Working from home
View GitHub Profile
@gauravbhasin87
gauravbhasin87 / SherlockandAnagrams.java
Created March 13, 2016 03:22
Sherlock and Anagrams
/*
Given a string SS, find the number of "unordered anagrammatic pairs" of substrings.
Input Format
First line contains TT, the number of testcases. Each testcase consists of string SS in one line.
Constraints
1≤T≤101≤T≤10
2≤length(S)≤1002≤length(S)≤100
String SS contains only the lowercase letters of the English alphabet.
/*
You are given two strings, AA and BB. Find if there is a substring that appears in both AA and BB.
Input Format
Several test cases will be given to you in a single file. The first line of the input will contain a single integer TT, the number of test cases.
Then there will be TT descriptions of the test cases. Each description contains two lines. The first line contains the string AA and the second line contains the string BB.
Output Format
/*
An English text needs to be encrypted using the following encryption scheme.
First, the spaces are removed from the text. Let LL be the length of this text.
Then, characters are written into a grid, whose rows and columns have the following constraints:
⌊L√⌋≤rows≤column≤⌈L√⌉⌊L⌋≤rows≤column≤⌈L⌉, where ⌊x⌋⌊x⌋ is floor function and ⌈x⌉⌈x⌉ is ceil function
For example, the sentence if man was meant to stay on the ground god would have given us roots after removing spaces is 5454 characters long, so it is written in the form of a grid with 7 rows and 8 columns.
ifmanwas
meanttos
/*
Given a 2D array of digits, try to find the occurrence of a given 2D pattern of digits. For example, consider the following 2D matrix:
1234567890
0987654321
1111111111
1111111111
2222222222
Assume we need to look for the following 2D pattern:
/*
Little Bob loves chocolate, and he goes to a store with $N$N in his pocket. The price of each chocolate is $C$C. The store offers a discount: for every MM wrappers he gives to the store, he gets one chocolate for free. How many chocolates does Bob get to eat?
Input Format:
The first line contains the number of test cases, TT.
TT lines follow, each of which contains three integers, NN, CC, and MM.
Output Format:
Print the total number of chocolates Bob eats.
/*
You are given NN sticks, where the length of each stick is a positive integer. A cut operation is performed on the sticks such that all of them are reduced by the length of the smallest stick.
Suppose we have six sticks of the following lengths:
5 4 4 2 2 8
Then, in one cut operation we make a cut of length 2 from each of the six sticks. For the next cut operation four sticks are left (of non-zero length), whose lengths are the following:
3 2 2 6
The above step is repeated until no sticks are left.
/*
Calvin is driving his favorite vehicle on the 101 freeway. He notices that the check engine light of his vehicle is on, and he wants to service it immediately to avoid any risks. Luckily, a service lane runs parallel to the highway. The length of the service lane is NN units. The service lane consists of NN segments of equal length and different width.
Calvin can enter to and exit from any segment. Let's call the entry segment as index ii and the exit segment as index jj. Assume that the exit segment lies after the entry segment (i≤ji≤j) and 0≤i0≤i. Calvin has to pass through all segments from index ii to index jj (both inclusive).
Paradise Highway
Calvin has three types of vehicles - bike, car, and truck - represented by 11, 22 and 33, respectively. These numbers also denote the width of the vehicle.
You are given an array widthwidth of length NN, where width[k]width[k] represents the width of the kkth segment of the service lane. It is guaranteed that while servicing he can pass through at most 10001
/*
Given an integer, NN, traverse its digits (dd1,dd2,...,ddn) and determine how many digits evenly divide NN (i.e.: count the number of times NN divided by each digit ddi has a remainder of 00). Print the number of evenly divisible digits.
Note: Each digit is considered to be unique, so each occurrence of the same evenly divisible digit should be counted (i.e.: for N=111N=111, the answer is 33).
Input Format
The first line is an integer, TT, indicating the number of test cases.
The TT subsequent lines each contain an integer, NN.
/*
The Utopian Tree goes through 2 cycles of growth every year. Each spring, it doubles in height. Each summer, its height increases by 1 meter.
Laura plants a Utopian Tree sapling with a height of 1 meter at the onset of spring. How tall will her tree be after NN growth cycles?
Input Format
The first line contains an integer, TT, the number of test cases.
TT subsequent lines each contain an integer, NN, denoting the number of cycles for that test case.
@gauravbhasin87
gauravbhasin87 / SherlockAndBeast.java
Created March 13, 2016 03:12
Sherlock and The Beast
/*
Sherlock Holmes suspects his archenemy, Professor Moriarty, is once again plotting something diabolical. Sherlock's companion, Dr. Watson, suggests Moriarty may be responsible for MI6's recent issues with their supercomputer, The Beast.
Shortly after resolving to investigate, Sherlock receives a note from Moriarty boasting about infecting The Beast with a virus; however, he also gives him a clue—a number, NN. Sherlock determines the key to removing the virus is to find the largest Decent Number having NN digits.
A Decent Number has the following properties:
Its digits can only be 3's and/or 5's.
The number of 3's it contains is divisible by 5.
The number of 5's it contains is divisible by 3.