Skip to content

Commit 6dbdad8

Browse files
committed
contest-solutions
1 parent 9ddbfc1 commit 6dbdad8

File tree

218 files changed

+5647
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

218 files changed

+5647
-0
lines changed

.gitignore

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
*.exe
2+
*.bin
3+
.cph/
4+
*personel-notes.md
5+
*test.c
6+
*test.py
7+
*test.cpp
8+
*test1.c
9+
*test1.c
10+
*test1.py
11+
*test2.py
12+
*test1.cpp
13+
*test2.cpp
14+
vignan-contest-solutions-draft/

ECP-week-3/01multiply_with_shift.py

Whitespace-only changes.

ECP-week-3/02_no_consecutive_bits.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def max_value_no_consecutive_bits(n):
2+
# Convert n to binary string
3+
binary_str = bin(n)[2:]
4+
5+
# Convert binary string to list of integers
6+
bits = list(map(int, binary_str))
7+
8+
# Iterate through bits to make sure no three consecutive bits are set
9+
for i in range(2, len(bits)):
10+
if bits[i] == 1 and bits[i-1] == 1 and bits[i-2] == 1:
11+
bits[i] = 0 # Change the third consecutive bit to 0
12+
13+
# Convert modified list of bits back to integer
14+
new_value = int(''.join(map(str, bits)), 2)
15+
16+
return new_value
17+
18+
# Test cases
19+
test_cases = int(input("Enter the number of test cases: "))
20+
for _ in range(test_cases):
21+
n = int(input())
22+
print(max_value_no_consecutive_bits(n))

ECP-week-3/03_fruit_pile_combinations.py

Whitespace-only changes.

ECP-week-3/04_circultrix.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
for _ in range(int(input())):
2+
s = input()
3+
a, b, c, d = int(s[0]), int(s[1]), int(s[2]), int(s[3])
4+
and1 = a & b
5+
not1 = ~c & 1
6+
or1 = and1 | not1
7+
and2 = or1 & d
8+
not2 = ~and2 & 1
9+
print(bool(not2))
10+

ECP-week-3/05_matching_bits.py

Whitespace-only changes.
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def can_distribute_equally(n, m):
2+
total_chocolates = n + m
3+
# If the total number of chocolates is odd, it's impossible to distribute equally
4+
if total_chocolates % 2 != 0:
5+
return False
6+
# If both have the same number of chocolates, return True
7+
if n == m:
8+
return True
9+
# Otherwise, it's only possible if the total number of chocolates is divisible by 2
10+
return total_chocolates % 2 == 0
11+
12+
# Read the number of test cases
13+
T = int(input("Enter the number of test cases: "))
14+
results = []
15+
16+
# Iterate over each test case
17+
for _ in range(T):
18+
n, m = map(int, input().split())
19+
results.append(can_distribute_equally(n, m))
20+
21+
# Print the results
22+
for result in results:
23+
print("True" if result else "False")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
def find_duplicate_and_replace(N, arr):
2+
frequency = {}
3+
for num in arr:
4+
if num in frequency:
5+
duplicate = num
6+
else:
7+
frequency[num] = True
8+
9+
# Find a replacement roll number
10+
replacement = 1
11+
while replacement in frequency:
12+
replacement += 1
13+
14+
return duplicate, replacement
15+
16+
# Input
17+
N = int(input())
18+
arr = list(map(int, input().split()))
19+
20+
# Find duplicate and replacement
21+
duplicate, replacement = find_duplicate_and_replace(N, arr)
22+
23+
# Output
24+
print(duplicate, replacement)

ECP-week-3/08_a_or_b.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def min_flips_to_or(a, b, c):
2+
flips = 0
3+
for i in range(31, -1, -1): # Considering 32-bit integers
4+
bit_a = (a >> i) & 1
5+
bit_b = (b >> i) & 1
6+
bit_c = (c >> i) & 1
7+
8+
if (bit_a | bit_b) != bit_c:
9+
if bit_c == 1: # If bit_c is 1, we need to flip a or b to make it 1
10+
if bit_a == 0 and bit_b == 0: # If both a and b are 0, flip any one
11+
flips += 1
12+
elif bit_a == 1 and bit_b == 1: # If both a and b are 1, flip any one
13+
flips += 1
14+
else: # If bit_c is 0, we need to ensure both a and b are 0
15+
flips += (bit_a + bit_b)
16+
17+
return flips
18+
19+
# Input
20+
t = int(input("Enter the number of test cases: "))
21+
for _ in range(t):
22+
a, b, c = map(int, input().split())
23+
print(min_flips_to_or(a, b, c))

ECP-week-3/09_shambala_nagram.py

Whitespace-only changes.

ECP-week-3/10_numeria_a_magical_land.py

Whitespace-only changes.

ECP-week-3/11_ben_10.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def min_operations(n):
2+
if n == 1:
3+
return 0
4+
elif n % 2 == 0:
5+
return 1 + min_operations(n // 2)
6+
else:
7+
return 1 + min(min_operations(n + 1), min_operations(n - 1))
8+
9+
# Input
10+
t = int(input("Enter number of test cases: "))
11+
test_cases = []
12+
for _ in range(t):
13+
test_cases.append(int(input()))
14+
15+
# Output
16+
for n in test_cases:
17+
print(min_operations(n))
18+
19+
20+
# gives tle if removed recursion then 100

ECP-week-3/12_help_eswar.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import math
2+
3+
def min_operations(N):
4+
count = 0
5+
while N > 0:
6+
max_power_of_2 = int(math.log2(N))
7+
N -= 2**max_power_of_2
8+
count += 1
9+
return count
10+
11+
# Input
12+
T = int(input("Enter number of test cases: "))
13+
test_cases = []
14+
for _ in range(T):
15+
test_cases.append(int(input()))
16+
17+
# Output
18+
for N in test_cases:
19+
print(min_operations(N))

ECP-week-3/13_neel_cinematic_universe.py

Whitespace-only changes.

ECP-week-3/14_dj_ravi.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def hamming_distance(num1, num2):
2+
# XOR the two numbers to get the bits that differ
3+
xor_result = num1 ^ num2
4+
5+
# Count the number of set bits in the XOR result
6+
# This is the Hamming distance
7+
hamming_dist = 0
8+
while xor_result:
9+
hamming_dist += xor_result & 1
10+
xor_result >>= 1
11+
12+
return hamming_dist
13+
14+
# Input
15+
t = int(input("Enter the number of test cases: "))
16+
for _ in range(t):
17+
num1, num2 = map(int, input().split())
18+
19+
# Output
20+
print(hamming_distance(num1, num2))

ECP-week-3/15_inversion.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def binary_to_decimal(binary_string):
2+
decimal_value = int(binary_string, 2)
3+
return decimal_value
4+
5+
def flip_even_indices(binary_input):
6+
res = ''
7+
for i in range(len(binary_input)):
8+
if i % 2 != 0:
9+
if binary_input[i] == '0':
10+
res += '1'
11+
else:
12+
res += '0'
13+
else:
14+
res += binary_input[i]
15+
return res
16+
17+
18+
n = int(input())
19+
binary_input = bin(n)[2:]
20+
21+
flipped_binary_string = flip_even_indices(binary_input)
22+
decimal_value = binary_to_decimal(flipped_binary_string)
23+
24+
print("Input Binary String:", binary_input)
25+
print("Flipped Binary String:", flipped_binary_string)
26+
print("Decimal Value:", decimal_value)

ECP-week-3/16_decode_secret_password.py

Whitespace-only changes.

ECP-week-3/17_maximum_chocolates.py

Whitespace-only changes.

ECP-week-3/18_the_gameee.py

Whitespace-only changes.

ECP-week-3/19_binraryburgirrrr.py

Whitespace-only changes.

ECP-week-3/20_the_supreme_sorcerer.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
for _ in range(int(input())):
2+
n = int(input())
3+
l = list(map(int, input().split()))
4+
res = [1]*n
5+
for a in range(1, n):
6+
for b in range(a):
7+
if res[a] < res[b] + 1 and l[a] > l[b]:
8+
res[a] = res[b] + 1
9+
print(max(res))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## Problem Statement: Cricket Analyst of the year
2+
3+
As a cricket analyst for a prestigious team preparing for the upcoming IPL season, you aim to impress team owners by presenting the analysis of a player's performance. You possess a series of scores for the player, and you seek to identify the longest strictly increasing sequence of these scores to showcase the player's consistency and potential. The sequence need not comprise consecutive match scores but must exhibit a strictly ascending trend. Your task is to determine the length of this longest increasing sequence for each test case.
4+
5+
### Input
6+
7+
- The first line contains an integer, T, denoting the number of test cases.
8+
- For each test case:
9+
- The first line contains an integer, N, representing the number of scores for the player.
10+
- The second line contains N space-separated integers representing the scores.
11+
12+
### Output
13+
14+
For each test case, output the length of the longest strictly increasing sequence of scores.
15+
16+
### Constraints
17+
18+
- 1 < T < 10
19+
- 1 < N < 10^3
20+
- 1 < scores < 300
21+
22+
### Example
23+
24+
Input:
25+
```
26+
1
27+
3
28+
2 1 5
29+
```
30+
31+
Output:
32+
```
33+
2
34+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
for _ in range(int(input())):
2+
n, k = map(int, input().split())
3+
s = list(input().strip())
4+
i = 0
5+
while k > 0 and i < n:
6+
if s[i] != 'a':
7+
s[i] = chr((ord(s[i]) - 96) % 26 + 97)
8+
k -= 1
9+
else:
10+
i += 1
11+
print(''.join(s))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Get The String
2+
3+
**Problem Statement:**
4+
5+
Given a string, Str, you can replace a character with its next character in the alphabet. You need to find a string such that it will become the first in dictionary order after performing K such operations.
6+
7+
**Input format:**
8+
9+
- The first line contains an integer, T, denoting the number of test cases.
10+
- For each test case:
11+
- The first line contains two space-separated integers, N and K, denoting the length of string Str and the number of operations to be performed, respectively.
12+
- The second line contains string Str consisting of lowercase letters.
13+
14+
**Output Format:**
15+
16+
Print a string such that it will become the first in dictionary order after performing K such operations.
17+
18+
**Constraints:**
19+
20+
- 1 ≤ T ≤ 100
21+
- 1 ≤ N ≤ 10^5
22+
- 0 ≤ K ≤ 10^5
23+
24+
**Sample Input:**
25+
```
26+
2
27+
1 3
28+
x
29+
3 3
30+
zya
31+
```
32+
33+
**Sample Output:**
34+
```
35+
a
36+
aaa
37+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## Problem Statement: Maximum XOR of Two Numbers
2+
3+
In a world of magical numbers, a pair of numbers, `x` and `y`, was considered special if `|x - y|` was less than or equal to the smaller number. Your task is to find two numbers from this set that create the most powerful pair, where their XOR is the highest among all pairs meeting this rule.
4+
5+
Return the maximum XOR value out of all possible strong pairs in the array `nums`.
6+
7+
Note that you can pick the same integer twice to form a pair.
8+
9+
### Input Format
10+
11+
- The first line contains an integer `T`, the number of test cases.
12+
- For each test case:
13+
- The first line contains an integer `N`, the number of elements in the array.
14+
- The second line contains `N` space-separated integers, the elements of the array.
15+
16+
### Output Format
17+
18+
For each test case, print the maximum XOR value out of all possible strong pairs in the array `nums`.
19+
20+
### Constraints
21+
22+
- 1 <= nums.length <= 5 * 10^4
23+
- 1 <= nums[i] <= 2^20 - 1
24+
25+
### Sample Input
26+
27+
```terminal
28+
3
29+
5
30+
9 5 10 10 5
31+
4
32+
4 9 9 8
33+
5
34+
4 4 10 3 10
35+
```
36+
37+
### Sample Output
38+
39+
```terminal
40+
15
41+
12
42+
7
43+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
for _ in range(int(input())):
2+
n = int(input())
3+
l = list(map(int, input().split()))
4+
res = 0
5+
for a in range(n):
6+
for b in range(a, n):
7+
if min(l[a], l[b]) >= abs(l[a] - l[b]):
8+
res = max(l[a] ^ l[b], res)
9+
print(res)

0 commit comments

Comments
 (0)