Skip to content

Commit bc7d334

Browse files
committed
Added Codechef easy problems
1 parent 4cc4cbd commit bc7d334

File tree

3 files changed

+195
-0
lines changed

3 files changed

+195
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# A palindrome is a string that reads same in both directions: forwards and backwards. For example,
2+
# the strings radar and noon are palindromes, whereas the string chef is not a palindrome as being read
3+
# backwards is becomes equal to fehc, which is not equal to chef.
4+
#
5+
# Let's say that the pair of indices (i, j) denotes a palindrome in some string S iff i ≤ j and the
6+
# substring starting at the i-th character and ending at the j-th character of S is a palindrome.
7+
#
8+
# Given an integer N. Your task is to construct a string S such that there are exactly N different
9+
# pairs (i, j) that denotes a palindrome.
10+
#
11+
# Input
12+
# The first line of the input contains an integer T denoting the number of test cases. The description
13+
# of T test cases follows.
14+
#
15+
# The first line of each test case contains a single integer N denoting the sought number of pairs that
16+
# denote palindrome.
17+
#
18+
# Output
19+
# For each test case, output a single line containing a string S, consisting of lowecase Latin letters,
20+
# and having exactly N distinct palindrome-denoting pairs. If there's a few such strings, output any one.
21+
#
22+
# If such string S doesn't exist, output -1 instead of it.
23+
#
24+
# Constraints
25+
# 1 ≤ T ≤ 100
26+
# 1 ≤ N ≤ 104
27+
#
28+
# Example
29+
# Input:
30+
# 3
31+
# 6
32+
# 7
33+
# 2
34+
#
35+
# Output:
36+
# noon
37+
# radar
38+
# ab
39+
#
40+
# Explanation:
41+
# 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).
42+
#
43+
# 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).
44+
#
45+
# Example case 3. In the string "ab", the pairs denoting a palindrome are : (1, 1), (2, 2)
46+
47+
for _ in range(int(input())):
48+
n = int(input())
49+
s = 'abcdefghijklmnopqrstuvwxyz'
50+
if (n <= 26):
51+
print(s[:n])
52+
else:
53+
a = n // 26
54+
b = n % 26
55+
c = a * s
56+
c = c + s[:b]
57+
print (c)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# We all know that the princess is very beautiful but one day jealous from her beauty, a person asked a
2+
# question from princess in order to check her wisdom. Since princess is not good at programming you need
3+
# to help her in solving the problem.
4+
# You are given a string of length N. You have to check among all the the substrings that whether a substring
5+
# exist or not which is palindrome and having length greater than 1. If such a substring exists then print
6+
# YES else print NO.
7+
#
8+
# Input
9+
# The first line contains a single integer T, the number of test cases. Each test case is described by a
10+
# single line containing a string.
11+
#
12+
# Output
13+
# For each test case, output a single line containing the YES or NO.
14+
#
15+
# Constraints
16+
# 1 ≤ T ≤ 10
17+
# 1 ≤ N ≤ 100000
18+
#
19+
# Example
20+
# Input:
21+
# 2
22+
# ab
23+
# babba
24+
#
25+
# Output:
26+
# NO
27+
# YES
28+
# Explanation
29+
# Example case 1.The only substring whose length is greater than 1 is ab, and its not a palindrome.
30+
#
31+
# Example case 2.abba is a substring of the string and its a palindrome thus YES.
32+
33+
def manacher(string):
34+
35+
string_with_bounds = '#'.join('^{}$'.format(string))
36+
length = len(string_with_bounds)
37+
P = [0] * length
38+
center = right = 0
39+
40+
for i in range(1, length - 1):
41+
P[i] = (right > i) and min(right - i, P[2 * center - i])
42+
43+
# Attempt to expand palindrome centered at i
44+
while string_with_bounds[i + 1 + P[i]] == string_with_bounds[i - 1 - P[i]]:
45+
P[i] += 1
46+
47+
# If palindrome centered at i expand past R,
48+
# adjust center based on expanded palindrome.
49+
if i + P[i] > right:
50+
center, right = i, i + P[i]
51+
52+
# Find the maximum element in P and return the string
53+
maxLen, centerIndex = max((n, i) for i, n in enumerate(P))
54+
return string[(centerIndex - maxLen)//2: (centerIndex + maxLen)//2]
55+
56+
for _ in range(int(input())):
57+
string = input()
58+
result = manacher(string)
59+
if len(result) > 1:
60+
print('YES')
61+
else:
62+
print('NO')
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Anmol always comes to class when the class is about to end. Frustrated by this behaviour of Anmol, his
2+
# teacher has given him a special question as his homework. We all know that Anmol is very weak at computer
3+
# science thus he came to you for help. Help Anmol in order to solve the problem.
4+
# You are given an array A of length N(1 indexed). You have to process Q queries of two different types:
5+
# 1 x — print func(x)
6+
# 2 x y— change the value of A[x] to y.
7+
# func(x) is defined as ::
8+
#
9+
# func(x)
10+
# {
11+
# sum = 0 ;
12+
# for(i=x;i<=N;i+=x)
13+
# sum = (sum + A[i]*A[i]) ;
14+
# return sum ;
15+
# }
16+
#
17+
# For each query of type 1 print the value of func(x) in a new line.
18+
# Input
19+
# The first line contains a single integer T, the number of test cases.
20+
# Each test case is described as follows :
21+
# The first line contains two numbers N and Q.
22+
# In the next line N space separated numbers denoting the values of the array A.
23+
# Each of the following Q lines contains a query of one of the above mentioned two types.
24+
# Note :: Since the test files are large use scanf/printf for I/O.
25+
#
26+
# Output
27+
# For each test case, For each query of type 1 print the required answer.
28+
#
29+
#
30+
# Since the answer can be very large, output it modulo 1000000007
31+
# Constraints
32+
# 1 ≤ T ≤ 10
33+
# 1 ≤ N ≤ 100000
34+
# 1 ≤ Q ≤ 100000
35+
# 1 ≤ A[i] ≤ 1e9
36+
# 1 ≤ x ≤ N
37+
# 1 ≤ y ≤ 1e9
38+
#
39+
# Subtasks
40+
#
41+
# Subtask #1 (20 points), Time limit : 1 sec
42+
# 1 ≤ T<=10, N<=100
43+
#
44+
#
45+
# Subtask #2 (80 points), Time limit : 1 sec
46+
# 1 ≤ T<=10, N<=100000
47+
#
48+
#
49+
# Example
50+
# Input:
51+
# 1
52+
# 5 3
53+
# 1 2 3 4 5
54+
# 1 1
55+
# 2 2 1
56+
# 1 2
57+
# Output:
58+
# 55
59+
# 17
60+
61+
def func(x):
62+
sum = 0
63+
for i in range(x, int(n) + 1, x):
64+
sum = sum + array[i] * array[i]
65+
return sum
66+
67+
for _ in range(int(input())):
68+
n, q = input().split()
69+
array = [int(i) for i in input().split()]
70+
array.insert(0, 0)
71+
for _ in range(int(q)):
72+
inputs = [int(i) for i in input().split()]
73+
if len(inputs) == 2:
74+
print(func(inputs[1]))
75+
else:
76+
array[inputs[1]] = inputs[2]

0 commit comments

Comments
 (0)