Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions CompetitiveProgramming/CodeChef/P37_NDIFFPAL.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# A palindrome is a string that reads same in both directions: forwards and backwards. For example,
# the strings radar and noon are palindromes, whereas the string chef is not a palindrome as being read
# backwards is becomes equal to fehc, which is not equal to chef.
#
# Let's say that the pair of indices (i, j) denotes a palindrome in some string S iff i ≤ j and the
# substring starting at the i-th character and ending at the j-th character of S is a palindrome.
#
# Given an integer N. Your task is to construct a string S such that there are exactly N different
# pairs (i, j) that denotes a palindrome.
#
# Input
# The first line of the input contains an integer T denoting the number of test cases. The description
# of T test cases follows.
#
# The first line of each test case contains a single integer N denoting the sought number of pairs that
# denote palindrome.
#
# Output
# For each test case, output a single line containing a string S, consisting of lowecase Latin letters,
# and having exactly N distinct palindrome-denoting pairs. If there's a few such strings, output any one.
#
# If such string S doesn't exist, output -1 instead of it.
#
# Constraints
# 1 ≤ T ≤ 100
# 1 ≤ N ≤ 104
#
# Example
# Input:
# 3
# 6
# 7
# 2
#
# Output:
# noon
# radar
# ab
#
# Explanation:
# Example case 1. In the string "noon", the pairs that denote a palindrome are (1-indexed): (1, 1), (1, 4), (2, 2), (2, 3), (3, 3), (4, 4).
#
# Example case 2. In the string "radar", the pairs that denote a palindrome are (1-indexed): (1, 1), (1, 5), (2, 2), (2, 4), (3, 3), (4, 4), (5, 5).
#
# Example case 3. In the string "ab", the pairs denoting a palindrome are : (1, 1), (2, 2)

for _ in range(int(input())):
n = int(input())
s = 'abcdefghijklmnopqrstuvwxyz'
if (n <= 26):
print(s[:n])
else:
a = n // 26
b = n % 26
c = a * s
c = c + s[:b]
print (c)
62 changes: 62 additions & 0 deletions CompetitiveProgramming/CodeChef/P38_PRINCESS.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# We all know that the princess is very beautiful but one day jealous from her beauty, a person asked a
# question from princess in order to check her wisdom. Since princess is not good at programming you need
# to help her in solving the problem.
# You are given a string of length N. You have to check among all the the substrings that whether a substring
# exist or not which is palindrome and having length greater than 1. If such a substring exists then print
# YES else print NO.
#
# Input
# The first line contains a single integer T, the number of test cases. Each test case is described by a
# single line containing a string.
#
# Output
# For each test case, output a single line containing the YES or NO.
#
# Constraints
# 1 ≤ T ≤ 10
# 1 ≤ N ≤ 100000
#
# Example
# Input:
# 2
# ab
# babba
#
# Output:
# NO
# YES
# Explanation
# Example case 1.The only substring whose length is greater than 1 is ab, and its not a palindrome.
#
# Example case 2.abba is a substring of the string and its a palindrome thus YES.

def manacher(string):

string_with_bounds = '#'.join('^{}$'.format(string))
length = len(string_with_bounds)
P = [0] * length
center = right = 0

for i in range(1, length - 1):
P[i] = (right > i) and min(right - i, P[2 * center - i])

# Attempt to expand palindrome centered at i
while string_with_bounds[i + 1 + P[i]] == string_with_bounds[i - 1 - P[i]]:
P[i] += 1

# If palindrome centered at i expand past R,
# adjust center based on expanded palindrome.
if i + P[i] > right:
center, right = i, i + P[i]

# Find the maximum element in P and return the string
maxLen, centerIndex = max((n, i) for i, n in enumerate(P))
return string[(centerIndex - maxLen)//2: (centerIndex + maxLen)//2]

for _ in range(int(input())):
string = input()
result = manacher(string)
if len(result) > 1:
print('YES')
else:
print('NO')
76 changes: 76 additions & 0 deletions CompetitiveProgramming/CodeChef/P39_ALATE.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Anmol always comes to class when the class is about to end. Frustrated by this behaviour of Anmol, his
# teacher has given him a special question as his homework. We all know that Anmol is very weak at computer
# science thus he came to you for help. Help Anmol in order to solve the problem.
# You are given an array A of length N(1 indexed). You have to process Q queries of two different types:
# 1 x — print func(x)
# 2 x y— change the value of A[x] to y.
# func(x) is defined as ::
#
# func(x)
# {
# sum = 0 ;
# for(i=x;i<=N;i+=x)
# sum = (sum + A[i]*A[i]) ;
# return sum ;
# }
#
# For each query of type 1 print the value of func(x) in a new line.
# Input
# The first line contains a single integer T, the number of test cases.
# Each test case is described as follows :
# The first line contains two numbers N and Q.
# In the next line N space separated numbers denoting the values of the array A.
# Each of the following Q lines contains a query of one of the above mentioned two types.
# Note :: Since the test files are large use scanf/printf for I/O.
#
# Output
# For each test case, For each query of type 1 print the required answer.
#
#
# Since the answer can be very large, output it modulo 1000000007
# Constraints
# 1 ≤ T ≤ 10
# 1 ≤ N ≤ 100000
# 1 ≤ Q ≤ 100000
# 1 ≤ A[i] ≤ 1e9
# 1 ≤ x ≤ N
# 1 ≤ y ≤ 1e9
#
# Subtasks
#
# Subtask #1 (20 points), Time limit : 1 sec
# 1 ≤ T<=10, N<=100
#
#
# Subtask #2 (80 points), Time limit : 1 sec
# 1 ≤ T<=10, N<=100000
#
#
# Example
# Input:
# 1
# 5 3
# 1 2 3 4 5
# 1 1
# 2 2 1
# 1 2
# Output:
# 55
# 17

def func(x):
sum = 0
for i in range(x, int(n) + 1, x):
sum = sum + array[i] * array[i]
return sum

for _ in range(int(input())):
n, q = input().split()
array = [int(i) for i in input().split()]
array.insert(0, 0)
for _ in range(int(q)):
inputs = [int(i) for i in input().split()]
if len(inputs) == 2:
print(func(inputs[1]))
else:
array[inputs[1]] = inputs[2]
46 changes: 46 additions & 0 deletions CompetitiveProgramming/CodeChef/P40_COINS.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# In Byteland they have a very strange monetary system.
#
# Each Bytelandian gold coin has an integer number written on it. A coin n can be exchanged in a bank into
# three coins: n/2, n/3 and n/4. But these numbers are all rounded down (the banks have to make a profit).
#
# You can also sell Bytelandian coins for American dollars. The exchange rate is 1:1. But you can not buy
# Bytelandian coins.
#
# You have one gold coin. What is the maximum amount of American dollars you can get for it?
#
# Input
# The input will contain several test cases (not more than 10). Each testcase is a single line with a number
# n, 0 <= n <= 1 000 000 000. It is the number written on your coin.
#
# Output
# For each test case output a single line, containing the maximum amount of American dollars you can make.
#
# Example
# Input:
# 12
# 2
#
# Output:
# 13
# 2
# You can change 12 into 6, 4 and 3, and then change these into $6+$4+$3 = $13. If you try changing the
# coin 2 into 3 smaller coins, you will get 1, 0 and 0, and later you can get no more than $1 out of them.
# It is better just to change the 2 coin directly into $2.

import sys

list={}
def chk(n):
if n in list.keys():
return list[n]
if n<=2:
ans = n
else:
ans = chk(n//2) + chk(n//3) + chk(n//4)
if ans<n : ans = n
list[n] = ans
return ans

for case in sys.stdin:
n = int(case)
print(chk(n))
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Our Code Monk recently learnt about Graphs and is very excited!
#
# He went over to the Graph-making factory to watch some freshly prepared graphs. Incidentally,
# one of the workers at the factory was ill today, so Monk decided to step in and do her job.
#
# The Monk's Job is to Identify whether the incoming graph is a tree or not. He is given N, the number
# of vertices in the graph and the degree of each vertex.
#
# Find if the graph is a tree or not.
#
# Input:
# First line contains an integer N, the number of vertices.
# Second line contains N space-separated integers, the degrees of the N vertices.
#
# Output:
# Print "Yes" (without the quotes) if the graph is a tree or "No" (without the quotes) otherwise.
#
# Constraints:
# 1 ≤ N ≤ 100
# 1 ≤ Degreei ≤ 1000
#
# SAMPLE INPUT
# 3
# 1 2 1
#
# SAMPLE OUTPUT
# Yes

n = int(input())
degrees = [int(i) for i in input().split()]

# Number of nodes are given thus in a tree number of edges are (n-1) and each edge has two degree
# thus in tree data structure total degree should be 2*(n-1) and this should be equal to sum of given degree

if(2 * (n - 1) == sum(degrees)):
print('Yes')
else:
print('No')
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# The Monk wants to buy some cities. To buy two cities, he needs to buy the road connecting those two cities.
# Now, you are given a list of roads, bought by the Monk. You need to tell how many cities did the Monk buy.
#
# Input:
# First line contains an integer T, denoting the number of test cases. The first line of each test case
# contains an integer E, denoting the number of roads. The next E lines contain two space separated
# integers X and Y, denoting that there is an road between city X and city Y.
#
# Output:
# For each test case, you need to print the number of cities the Monk bought.
#
# Constraint:
# 1 <= T <= 100
# 1 <= E <= 1000
# 1 <= X, Y <= 10000
#
# SAMPLE INPUT
# 1
# 3
# 1 2
# 2 3
# 1 3

for _ in range(int(input())):
roads = int(input())
lst = set([])
for i in range(roads):
node1,node2 = map(int,input().split())
lst.add(node1)
lst.add(node2)
print(len(lst))
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# You are given container full of water. Container can have limited amount of water. You also have
# N bottles to fill. You need to find the maximum numbers of bottles you can fill.
#
# Input:
# First line contains one integer, T, number of test cases.
# First line of each test case contains two integer, N and X, number of bottles and capacity of the container.
# Second line of each test case contains
# N space separated integers, capacities of bottles.
#
# Output:
# For each test case print the maximum number of bottles you can fill.
#
# Constraints:
# 1≤T≤100
# 1≤N≤104
# 1≤X≤109
#
# SAMPLE INPUT
# 1
# 5 10
# 8 5 4 3 2
#
# SAMPLE OUTPUT
# 3

for _ in range(int(input())):
N, capacity = map(int, input().split())
capacities = [int(bottle) for bottle in input().split()]
capacities.sort()
check, bottles = 0, 0
for i in range(len(capacities)):
if check > capacity:
bottles -= 1
break
bottles += 1
check += capacities[i]

print(bottles)
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Given a string S, and two numbers N, M - arrange the characters of string in between the indexes N
# and M (both inclusive) in descending order. (Indexing starts from 0).
#
# Input Format:
# First line contains T - number of test cases.
# Next T lines contains a string(S) and two numbers(N, M) separated by spaces.
#
# Output Format:
# Print the modified string for each test case in new line.
#
# Constraints:
#
# 1≤T≤1000
# 1≤|S|≤10000 // |S| denotes the length of string.
# 0≤N≤M<|S|
# S∈[a,z]
#
# SAMPLE INPUT
# 3
# hlleo 1 3
# ooneefspd 0 8
# effort 1 4
#
# SAMPLE OUTPUT
# hlleo
# spoonfeed
# erofft

for i in range(int(input())):
user_input = input()
user_input = user_input.split()
to_char = int(user_input[2])
from_char = int(user_input[1])
string = user_input[0][from_char:to_char + 1]
replace = ''.join(sorted(string)[::-1])
print(user_input[0][:from_char] + replace + user_input[0][to_char + 1:len(user_input[0])])
Loading