## Problem: Manasa and Stones

Manasa is out on a hike with friends. She finds a trail of stones with numbers on them. She starts following the trail and notices that two consecutive stones have a difference of either a or b. Legend has it that there is a treasure trove at the end of the trail and if Manasa can guess the value of the last stone, the treasure would be hers. Given that the number on the first stone was 0, find all the possible values for the number on the last stone.

Note: The numbers on the stones are in increasing order.

Input Format

The first line contains an integer T, i.e. the number of test cases. T test cases follow; each has 3 lines. The first line contains n (the number of stones). The second line contains a, and the third line contains b.

Constraints

1 <= T <= 10
1 <= n, a, b <= 10^3

Output Format

Space-separated list of numbers which are the possible values of the last stone in increasing order.

Sample Input

2
3
1
2
4
10
100


Sample Output

2 3 4
30 120 210 300


Explanation

All possible series for the first test case are given below:

1. 0,1,2
2. 0,1,3
3. 0,2,3
4. 0,2,4

Hence the answer 2 3 4.

Series with different number of final steps for second test case are the following:

1. 0, 10, 20, 30
2. 0, 10, 20, 120
3. 0, 10, 110, 120
4. 0, 10, 110, 210
5. 0, 100, 110, 120
6. 0, 100, 110, 210
7. 0, 100, 200, 210
8. 0, 100, 200, 300

Hence the answer 30 120 210 300.

Approach:

1. Using backtracking. Generate all possible sequences, get all last values, and put them into a set. This is acceptable with n small (less than 15), since the complexity is O(2^n). Here is the code for backtracking
2. Analyze. Let’s take 2 examples above and find out the pattern. In the first example, n = 3 and there are 3 different ending values, max value is 4 and the difference from the largest to the smallest is 1 (4 -> 3 -> 2) . In the second example, n = 4 and there are 4 different ending values, max value is 300 and difference between pairs is 90. Have you seen the pattern yet? With n, there are n different values, the largest value is max(a,b)*(n-1), the difference is max(a,b) – min(a,b), or a – b, if a is bigger, otherwise b-a. How can we know that this pattern is the one that we’re looking for. First, since there are only two values a,b, assume a is bigger than b (it works the same way if b is bigger than a). Then the biggest possible value you can get is b*(n-1). since the first one is 0. The second biggest is b*(n-1) – (a-b), since this is the second biggest, the last value is b, instead of a. We c0ntinue this as long as the value return after calculate the difference is positive. And this is actually n-1 times, since we already manually calculate the largest.

Source code. The chain function is how you print out all possible stones chain, with complexity O(2^n). Inside main is the code to solve this problem.

def chain(n, arr, options):
for i in options:
arr[n] = arr[n-1] + i
if n == len(arr) - 1:
print(arr)
else:
chain(n+1, arr, options)

def test():
chain(1, [0]*4, (10, 100))

if __name__ == '__main__':
t = int(input().strip())
for _ in range(t):
n = int(input().strip())
a = int(input().strip())
b = int(input().strip())
if b > a:
a, b = b, a
pos_values = [0]*n
pos_values[0] = a*(n-1)
for i in range(1, len(pos_values)):
pos_values[i] = pos_values[i-1] - (a-b)
pos_values = list(set(pos_values))
print(*sorted(pos_values))


## Problem: Check Mirror in N-ary tree

*Difficulty: Medium

Given two n-ary tree’s the task is to check if they are mirror of each other or not.

Example

1                      1
/    \                 /   \
2      3             3     2

Output: 1

1                        1
/  \                    /  \
2    3                2     3

Output: 0

Note: you may assume that root of both the given tree as 1.

Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. The first line of each test case contains two space separated values n and e denoting the no of nodes and edges respectively. Then in the next line two lines are 2*e space separated values u,v denoting an edge from u to v for the both trees .

Output:
For each test case in a new line print 1 if both the trees are mirrors of each other else print 0.

Constraints:
1<=T<=20
1<=n<=15
1<=e<=20

Example:
Input:

2
3 2
1 2 1 3
1 3 1 2
3 2
1 2 1 3
1 2 1 3
Output:
1
0

Approach: Since this is a N-arr tree, that means one node can have multiple child. So the easiest way is to consider it as a graph. Each graph has a list of adjacent nodes. The way to to check  if two threes (or graphs if you will) are mirror of each other or not is:

1. They have the same number of nodes
2. All node have the same number of adjacent nodes
3. The child list of each node in the first tree when reversed will be exactly like the one in the node of second tree and vice versa.

Implementation: Python 3

def is_mirror(tree_1, tree_2):
if len(tree_1) != len(tree_2):
return False
for node in tree_1:
if len(tree_1[node]) != len(tree_2[node]):
return False
if tree_1[node] != list(reversed(tree_2[node])):
return False
return True

def create_tree():
tree = {}
arr = list(map(int, input().strip().split()))
for i in range(0,len(arr),2):
if arr[i] in tree:
tree[arr[i]].append(arr[i+1])
else:
tree[arr[i]] = [arr[i+1]]
return tree

if __name__ == '__main__':
t = int(input().strip())
for _ in range(t):
n, e = list(map(int, input().strip().split()))
tree_1 = create_tree()
tree_2 = create_tree()
# print(tree_1)
# print(tree_2)
print(1 if is_mirror(tree_1, tree_2) else 0)


## Problem:

*Difficulty: Easy

Problem:

ctci-find-the-running-median-english

Approach:
Since the constraints is so high, we need a way to insert and sort the array (or list) that has the complexity equal or lower than O(logn). With n loop and each loop costs O(logn) we have the complexity O(nlogn), which is acceptable. Using linked list and binary search is one of the possible ways. But If you know Python, this problem can be solved in 10 lines of code, using insort method from bisect module.

Implementation: Python 3

import bisect

n = int(input())
l = []
for _ in range(n):
ai = int(input())
bisect.insort(l, ai)
if len(l) % 2 == 0:
print('%.1f' % ((l[len(l)//2-1]+l[len(l)//2])/2))
else:
print('%.1f' % (l[len(l)//2]))


## Problem: Separate the Numbers

*Difficulty: Easy

Problem:
separate-the-numbers-english

Approach:
Here’s how I did it, and I think example is the best way to explain it:
Example:
s = 1011121314
Start with first number = 10, next is 11, next is 12, …..
Eventually, return True
The maximum length of first number will be equal len(s) // 2 (if length s is even), or len(s) // 2 + 1 (length s is odd)
Let’s take another example:
10000001000001
First number: 1, next 0 => False
First number: 10, next 00=> False
First number: 100, next 000=>False
First number: 1000. next 000=>False
….
First number:1000000 next 1000001 => True
Hope you get the idea :>

Implementation: Python 3

def is_beautiful(number):
if number == '' or number[0] == '0' or len(number) <= 2:
return [False]
bound = len(number) // 2
start_length = 1
while start_length <= bound:
first_str = number[:start_length]
first_int = int(first_str)
i = start_length
try:
while i < len(number):
next = next_str(i, first_str, first_int, number)
if next is None:
break
else:
changed = False
next_1, next_2 = next
if int(next_1) == first_int+1:
first_str = next_1
first_int = int(next_1)
i += len(next_1)
changed = True
else:
if next_2:
if int(next_2) == first_int+1:
first_str = next_2
first_int = int(next_2)
i += len(next_2)
changed = True
# cant found next number
if not changed:
break
# check out of bound
if i > len(number):
break
# all numbers statisfy the condition
if i == len(number):
return True, number[:start_length]
except Exception:
pass
start_length += 1
return [False]

def next_str(i, first_str, first_int , number):
# get the next number
# check if it has equal or longer length
# return two next, but only one of them is usable
next_1 = ""
next_2 = ""
if i < len(number):
if number[i] == '0':
return None
else:
if i + len(first_str) <= len(number):
next_1 = number[i:i+len(first_str)]
if i + len(first_str) + 1 <= len(number):
next_2 = number[i:i+len(first_str)+1]
return next_1, next_2
return None

def main():
t = int(input())
for _ in range(t):
number = str(input().strip())
result = is_beautiful(number)
if result[0]:
print('YES', result[1])
else:
print('NO')

if __name__ == '__main__':
main()


## Problem: Making Anagrams

*Difficulty: Easy

Problem:

ctci-making-anagrams-english

Approach: To be able to make anagrams from two string, we have to find their common characters first. For example, if we have abccd and cdcc, then their common characters is c and d. Then we build two string contains only character that is in both string a and string b. In the previous example, string a = ccd and string b = cdcc. The last thing we need to do is make them anagrams. We do that by iterating through either string a or string b, with each character d, if there are more d in string a than string b, we remove d from a to make the number of d equal in both a and b. In our example, string b will remove 1 ‘c’, then a = ccd and b = dcc. Notice that we don’t have to do this on string, we rather do this on dictionary of string. Since the input only contains lower English alphabet, the maximum size of the dictionary will contain only 26 keys and 26 corresponding values. If key ‘c’ in a has higher value than key ‘c’ in b, then set its value to ‘c’ in b.

Implementation: Python 2.7

from collections import Counter

def make_anagrams(a, b):
a_set = set(list(a))
b_set = set(list(b))
a_counter = Counter()
b_counter = Counter()
# get char in common
for c in a:
if c in b_set:
a_counter[c] += 1
for c in b:
if c in a_set:
b_counter[c] += 1
# make anagrams
for key in a_counter.keys():
a_val = a_counter.get(key)
b_val = b_counter.get(key)
if a_val > b_val:
a_counter[key] = b_val
else:
b_counter[key] = a_val
# calculate removed char
a_lost = len(a) - sum([a_counter.get(x) for x in a_counter.keys()])
b_lost = len(b) - sum([b_counter.get(x) for x in b_counter.keys()])
return a_lost + b_lost

if __name__ == '__main__':
a = input().strip()
b = input().strip()
print(make_anagrams(a, b))


## Problem: Arrays Left Rotation

• Difficulty: Easy

Problem description:
Array Left Rotation pdf

All approaching ideas is inspired by Python.
Approach: The first thing come in my mind is using a for loop, using Python list slice notation. But it’s too expensive both in computing muscle and memory usage. With the constraints, it will get TLE. Another approach is using modular arithmetic. Basically, rotate array d times means that we start iterating at index d. We have to use modular arithmetic to wrap around the array in case of reaching the end of array but still haven’t iterated through all elements yet.

Implementation: Python 3

def rotate(arr, d):
count = 0
start = d % len(arr)
while count < len(arr):
print(arr[start], end=' ')
count += 1
start = (start + 1) % len(arr)
print()

if __name__ == '__main__':
n, d = list(map(int, input().strip().split()))
arr = list(map(int, input().strip().split()))
rotate(arr, d)


## Problem: Word Boggle

*Difficulty: Medium

Given a dictionary, a method to do lookup in dictionary and a M x N board where every cell has one character. Find all possible words that can be formed by a sequence of adjacent characters. Note that we can move to any of 8 adjacent characters, but a word should not have multiple instances of same cell.

Example:

Input: dictionary[] = {"GEEKS", "FOR", "QUIZ", "GO"};
boggle[][]   = {{'G','I','Z'},
{'U','E','K'},
{'Q','S','E'}};

Output:  Following words of dictionary are present
GEEKS, QUIZ



Input:
The first line of input contains an integer T denoting the no of test cases . Then T test cases follow. Each test case contains an integer x denoting the no of words in the dictionary. Then in the next line are x space separated strings denoting the contents of the dictinory. In the next line are two integers N and M denoting the size of the boggle. The last line of each test case contains NxM space separated values of the boggle.

Output:
For each test case in a new line print the space separated sorted distinct words of the dictionary which could be formed from the boggle. If no word can be formed print -1.

Constraints:
1<=T<=10
1<=x<=10
1<=n,m<=7

Example:
Input:

1
4
GEEKS FOR QUIZ GO
3 3
G I Z U E K Q S E

Output:
GEEKS QUIZ

*Approach: This problem is almost the same with this.
The mechanism gonna be used here is back tracking. There are only two main parts in the algorithm:

• With each cell in boggle matrix (two for loops)
• Check all possible words that can be generated with that cell
• While generating, if a generated word can be found in dictionary, add to result
• if the length of generated word equal the length of longest word in dictionary, return (base case)
• After getting all words, put them into a set to eliminate duplicate words, then sorted them alphabetically before output

Suppose max_level is the length of longest word in dictionary, as soon as the length of a generated word exceeds max_level, we return. Why? Because if we still continue generating, there is no word will match anything in dictionary.

Implementation: Python 2.7

from __future__ import print_function

def generate_words(x, y, dictionary, boggle, visited_cells, level, max_level, result, word):
visited_cells.append((x, y))
word += boggle[x][y]
#print word
if word in dictionary:
result.append(word)
eight_directions = ((x+1, y), (x-1, y), (x, y+1), (x, y-1),
(x+1, y+1), (x+1, y-1), (x-1, y+1), (x-1, y-1))
eight_directions = [(new_x,new_y) for new_x,new_y in eight_directions
if in_bound(new_x, new_y, boggle) and (new_x, new_y) not in visited_cells]
if level > max_level:
return
for new_x, new_y in eight_directions:
generate_words(new_x, new_y, dictionary, boggle,
visited_cells, level+1, max_level, result, word)
visited_cells.pop()

def find_all_possible_words(dictionary, boggle):
max_level = get_max_level(dictionary)
overall_result = []
for x in xrange(len(boggle)):
for y in xrange(len(boggle[x])):
result = []
visited_cells = []
level = 0
word = ""
generate_words(x, y, dictionary, boggle, visited_cells, level, max_level, result, word)
if result:
overall_result.extend(result)
overall_result = set(overall_result)
overall_result = list(sorted(overall_result))
if overall_result:
print(*overall_result, sep=" ")
else:
print(-1)

def get_max_level(dictionary):
return max(len(x) for x in dictionary)

def in_bound(x, y, boggle):
return (x >= 0 and x < len(boggle)) and (y >= 0 and y < (len(boggle[0])))

if __name__ == '__main__':
t = input()
for _ in range(t):
n = input()
dictionary = raw_input().strip().split()
n, m = map(int, raw_input().strip().split())
boggle = []
temp = raw_input().strip().split()
for i in range(0, len(temp), m):
boggle.append(temp[i:i+m])
#print(dictionary)
#print(boggle)
find_all_possible_words(dictionary, boggle)


## Problem: Longest K unique characters substring

*Difficulty: easy

Given a string you need to print the size of the longest possible substring that has exactly k unique characters. If there is no possible substring print -1.
Example
For the string aabacbebebe and k = 3 the substring will be cbebebe with length 7.
Input:
The first line of input contains an integer T denoting the no of test cases then T test cases follow. Each test case contains two lines . The first line of each test case contains a string s and the next line conatains an integer k.

Output:
For each test case in a new line print the required output.

Constraints:
1<=T<=100
1<=k<=10

Example:
Input:

2
aabacbebebe
3
aaaa
1
Output:
7
4

Implementation: Python 2.7

#code
def all_sub_string(k, string, all_substr):
if k > len(string):
return
elif k == len(string):
all_substr.append(string)
return
else:
for i in range(len(string)):
if i + k > len(string):
break
all_substr.append(string[i:i+k])
all_sub_string(k+1, string, all_substr)

def longest_k_unique_char_substr(all_substr, k):
if k > len(all_substr[-1]):
return -1
max_length = -1
for substr in all_substr:
if len(substr) >= k and len(set(substr)) == k:
if len(substr) > max_length:
max_length = len(substr)
return max_length

def main():
t = input()
for _ in xrange(t):
string = raw_input().strip()
k = input()
all_substr = []
all_sub_string(1, string, all_substr)
print longest_k_unique_char_substr(all_substr, k)

if __name__ == '__main__':
main()


## Problem: Kth Prime factor of a Number

• Difficulty: Easy

Given two numbers n and k, print Kth prime factor among all prime factors of n.

Note: if k > all the count of prime factor of n, then print -1

Input:

The first line of input contains an integer denoting the no of test cases. Then T test cases follow. Each test case contains two space seperated integers n and k respectively.
Output:

For each test case ouput a simple line containing a single integer denoting kth prime factor of n.
Constraints:

1<=T<=100
0<=N<=104
1<=K<=50
Example:

Input:

2
225 2
81 5

Ouput:

3
-1

Explanation:

Test Case 1:  n=225 and k=2

Prime factor of 225 are: 3,3,5,5

Kth prime factor is 3

Test Case 2: n=81 and k=5

Prime factor of 81 is 3,3,3,3

since k is greater than all the count of prime factor, hence we print -1

Approach: This is not the fastest solution, but it’s fairly fast. Enough to paste the biggest test case in less than 100 ms. Using sieve of Eratosthenes to generate first 10000 primes, then use it as a dictionary to find all prime factors of a number. There are multiple ways to solve this problem, since the constraints is not big, so this solution is not only simple but fast.

Implementation: Python 2.7

#code
import math
def sieveOfEratosthenes(n):
sieve = [True]*(n+1)
sieve[0] = sieve[1] = False
for i in range(2, int(math.sqrt(n))):
if sieve[i]:
for j in range(i+i, len(sieve), i):
sieve[j] = False
return [i for i in range(len(sieve)) if sieve[i]]

def primeFactors(number, primes):
factors, i = [], 0
while number > 1:
if number % primes[i] == 0:
factors.append(primes[i])
number /= primes[i]
else:
i += 1
return factors

def main():
t = input()
primes = sieveOfEratosthenes(10000)
for _ in range(t):
number, k = map(int, raw_input().strip().split())
factors = primeFactors(number, primes)
print -1 if k > len(factors) else factors[k-1]

if __name__ == '__main__':
main()


## Problem: A or B

*Difficulty: Medium
Consider four numbers: A, B, C, and K. You must change at most K bits in A and B to form the numbers A’ and B’ satisfying the equation A’ | B’ = C. Here, the | symbol denotes the bitwise OR operation.

Given Q sets of the numbers defined above, find and print the respective values of A’ and B’ on new lines; if no such value exists, print -1 instead. If there are multiple solutions, make A’ as small as possible; if there are still multiple solutions, make B’ as small as possible.

Notes:

• A, B and C are given in Hexadecimal (base 16), and K is given in decimal (base 10).
• If the number of bits changed in A is kA and the number of bits changed in B is kB , then kA + kB must be <= K.

Input Format

The first line contains an integer, Q , denoting the number of queries. The subsequent lines describe each respective query as follows:

• The first line contains a single integer denoting the value of K.
• Each of the next 3 lines contains a Hexadecimal (base 16) number describing the respective values of A, B, and C.

Constraints

Output Format

Print two lines of output for each query:

1. The first line should contain a Hexadecimal (base 16) number denoting the value of A’ .
2. The second line must contain a Hexadecimal (base 16) number denoting the value of B’.

If no valid answer exists, you must instead print one line of output with the integer -1.

Note: The letters in Hexadecimal numbers must be in uppercase.

Sample Input

3
8
2B
9F
58
5
B9
40
5A
2
91
BE
A8


Sample Output

8
58
18
42
-1


Explanation

Query 0:
In this query, K = 8.
Change to A = 2B(16) to A’ = 8(16). 3 bits are changed.

Change B = 9F(16) to B’= 58(16) . 5 bits are changed.

Query 1:
In this query, K = 5.
Change A = B9(16) to A’ = 18(16). 3 bits are changed.
Change B = 40(16) to B’ = 42(16). Only 1 bit is changed.

Query 2:
There is no valid answer, so we print -1.

Approach:

There are two parts of the solution. The first is the one that I figured out, but I sucked at the second part.
Let’s me explain. The solution complexity is O(n).
Part 1: Convert A,B,C to binary, and equalize their length (calculate max length, then append 0 before the ones that are shorter). Traverse through each bit in C.
If C[i] == 0:
+ if A[i] = 1 => A[i] = 0, reduce K by 1
+ if B[i] = 1 => B[i] = 0, reduce K by 1
If C[i] == 1:
+ if A[i] == 0 and B[i] == 0 => B[i] = 1, reduce K by 1
While iterating through C, if K == 0 but still not A’ | B’ = C, then return -1
Note: in case C[i] == 1, we let B[i] = 1 since we want to minimize A first.

Part 2: Since not fully understood the description of the problem (my bad English), I had to see the tutorial, which then made me surprised. The second part when we minimize even further A. If K still bigger than 0.
i -> 0
while K > 0:
if C == [1]: # since if C[i] == 0 we cant change anything
+ if A[i] == 1 and B[i] == 1 => A[i] = 0 #minimize A, reduce K by 1
+ if A[i] == 1 and B[i] == 0 => A[i] = 0 and B[i] = 1, reduce K by 2

That’s it. How silly I am 😦

Implementation: Python 2.7

def figureOut(A, B, C, K):
# convert from base 16 to base 10
# in binary format
A = bin(int(A, 16))[2:]
B = bin(int(B, 16))[2:]
C = bin(int(C, 16))[2:]
maxLength = max(len(A), len(B), len(C))

#equalize A,B,C length
if len(A) < maxLength:
A = '0'*(maxLength-len(A)) + A
if len(B) < maxLength:
B = '0'*(maxLength-len(B)) + B
if len(C) < maxLength:
C = '0'*(maxLength-len(C)) + C
A, B, C = list(A), list(B), list(C)

# 1. found result
result = []
for i in range(len(C)):
if C[i] == '0':
if A[i] == '1':
A[i] = '0'
K -= 1
if B[i] == '1':
B[i] = '0'
K -=1
elif C[i] == '1':
if A[i] == '0' and B[i] == '0':
B[i] = '1'
K -= 1
if K < 0:
return -1

# found result,
# minimize A
i = 0
while K > 0:
if C[i] == '1':
if A[i] == '1' and B[i] == '1':
A[i] = '0'
K -= 1
elif A[i] == '1' and B[i] == '0':
if K > 1:
A[i] = '0'
B[i] = '1'
K -= 2

i += 1
if i == len(C):
break

return '{:X}'.format(int(''.join(A), 2)), '{:X}'.format(int(''.join(B), 2))

def main():
t = input()
for _ in range(t):
K = input()
A = raw_input().strip()
B = raw_input().strip()
C = raw_input().strip()

result = figureOut(A, B, C, K)
if result != -1:
print result[0]
print result[1]
else:
print -1

if __name__ == '__main__':
main()


c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r