Kickstart Round C 2020 Countdown Problem - finding number of contiguous subarray - python

Edit - The output didn't have a space before the colon. But after changing that, instead of wrong answer, it's saying runtime error. Even the sample input tests ran successfully. What could possible be wrong?
Problem
Avery has an array of N positive integers. The i-th integer of the array is Ai.
A contiguous subarray is an m-countdown if it is of length m and contains the integers m, m-1, m-2, ..., 2, 1 in that order. For example, [3, 2, 1] is a 3-countdown.
Can you help Avery count the number of K-countdowns in her array?
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a line containing the integers N and K. The second line contains N integers. The i-th integer is Ai.
Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the number of K-countdowns in her array.
Limits
Time limit: 20 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
2 ≤ K ≤ N.
1 ≤ Ai ≤ 2 × 105, for all i.
Test set 1
2 ≤ N ≤ 1000.
Test set 2
2 ≤ N ≤ 2 × 105 for at most 10 test cases.
For the remaining cases, 2 ≤ N ≤ 1000.
Sample
Input
3
12 3
1 2 3 7 9 3 2 1 8 3 2 1
4 2
101 100 99 98
9 6
100 7 6 5 4 3 2 1 100
Output
Case #1: 2
Case #2: 0
Case #3: 1
T = int(input())
res = []
for i in range(T):
N, K = map(int, input().split(' '))
ai = list(map(int, input().split(' ')))
y=0
count=0
for m in range(len(ai)):
count=0
if(ai[m]==K):
if(len(ai)>=m+K-1):
for j in reversed(range(1, K)):
if(ai[m+K-j]==j):
count=count+1
if(count==K-1):
y=y+1
else:
break
else:
continue
res.append("Case #"+str(i+1)+" : "+str(y))
for g in range(len(res)):
print(res[g])

You can use a two-pointer approach like take two variables i and j and initialize both with 0
Keeping i fixed increment j if the required condition is met i.e.
while(a[k+1]-a[k]==1):
j++
k++
wherever condition fails, calculate j-i. If at all j-i==m then increment your counter say cnt and then put i=j and repeat until the end of array

Related

Google Kickstart 2022 Problem Record Breaker Wrong Answer

Sorry for the formatting. New at posting questions
I was practicing this problem asked in the last round of Google Kick Start 2020. The problem is called Record Breaker and is as follows:
Isyana is given the number of visitors at her local theme park on N consecutive days. The number of visitors on the i-th day is Vi. A day is record breaking if it satisfies both of the following conditions: The number of visitors on the day is strictly larger than the number of visitors on each of the previous days. Either it is the last day, or the number of visitors on the day is strictly larger than the number of visitors on the following day. Note that the very first day could be a record breaking day!
Please help Isyana find out the number of record breaking days.
Input: The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a line containing the integer N. The second line contains N integers. The i-th integer is Vi.
Output: For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the number of record breaking days.
Limits Time limit: 20 seconds per test set. Memory limit: 1GB. 1 ≤ T ≤ 100. 0 ≤ Vi ≤ 2 × 105.
Test set 1
1 ≤ N ≤ 1000.
Test set 2
1 ≤ N ≤ 2 × 105 for at most 10 test cases. For the remaining cases, 1 ≤ N ≤ 1000.
Sample
Input
4
8
1 2 0 7 2 0 2 0
6
4 8 15 16 23 42
9
3 1 4 1 5 9 2 6 5
6
9 9 9 9 9 9
Output
Case #1: 2
Case #2: 1
Case #3: 3
Case #4: 0
In Sample Case #1, the bold and underlined numbers in the following represent the record breaking days: 1 2 0 7 2 0 2 0.
In Sample Case #2, only the last day is a record breaking day.
In Sample Case #3, the first, the third, and the sixth days are record breaking days.
In Sample Case #4, there is no record breaking day.
This is the solution I created. It gives a Wrong Answer in the first Test Case but I can't think of any specific case that I have missed.
def sol(testcase):
days = int(input())
record_breaking = 0
greatest = 0
visitors = [0] + list(map(int, input().split())) + [0]
for x in range(days+1):
if x == 0:
continue
if visitors[x] > greatest:
greatest = visitors[x]
if (visitors[x] > visitors[x-1]) and (visitors[x] > visitors[x+1]):
record_breaking += 1
print(f"Case #{testcase}: {record_breaking}")
testcases = int(input())
for x in range(testcases):
sol(x+1)
// Here Is Your Answer Cleared All Test Cases
T = int(input())
for i in range(T):
N = int(input())
Vs = [int(v) for v in input().split(" ")]
max_number = -1
record_breaks = 0
for j in range(N):
first_condition = Vs[j] > max_number
if j+1 < N:
second_condition = Vs[j] > Vs[j+1]
else:
second_condition = True
if first_condition and second_condition:
record_breaks +=1
if first_condition:
max_number = Vs[j]
print("Case #{}: {}".format(str(i+1), str(record_breaks)))

How to take Different multi line inputs?

The other day, for the first time I participated in a coding contest and I faced an issue with taking inputs. There were 4 sample cases and each sample case had n test cases. So each sample case was in the format:
First line denotes number of test cases
Second line denotes number of elements in array A
Third line denotes elements of array A
Fourth line denotes elements of array B
To make more sense, the input was (say),
3 // Number of test cases
5 // Size of array A
2 4 3 7 9 // array A
14 21 11 // array B
3 // Size of array A
6 7 2 // array A
9 8 // array B
4 // Size of array A
5 2 6 7 // array A
12 17 // array B
The solution that I tried was
t = int(input()) # Number of sample cases
for i in t:
n = int(input()) # Size of array A
arr_A = [int(x) for x in input().split()]
arr_B = [int(x) for x in input().split()]
# Statements...
When I was running the code I was getting EOF error or some error. To test what was causing the error I reduced the aforementioned sample case to
1 // Number of test cases
5 // Size of array A
2 4 3 7 9 // array A
14 21 11 // array B
And with this input the code was running fine.
I will truly appreciate if you tell me possible ways of taking such inputs?
In your code, for i in t should be modified to for i in range(t) since we need to iterate up to a certain number.
This is the approach I usually use when it comes to taking inputs.
t = int(input())
while t > 0:
n = int(input())
arrA = list(map(int, input().split()))
arrB = list(map(int, input().split()))
t-=1

How to use a loop in python to return elements of an array into a given set?

For each index of a sorted array i = 1,...,N in this new array I want to find all indexes j such that xj is in the set (xi-5, xi+5).
x= np.array([1,2,2,3,4,6,7,9])
n=len(x)
for i in range(n):
for j in range(n):
if x[i] - 5 < x[j] and x[i] + 5 > x[j]:
print(j)
So for this array example I gave, when i=0, x=1. So it should return [1,2,3,4] because they are all in (x_1-5, x_1+5). Then when i=1, x=2, and should return [0,2,3,4,5] and so on. I should get N arrays.
But, the output gives me
0 1 2 3 4 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 6 0 1 2 3 4 5 6 1 2 3 4 5 6 7 3 4 5 6 7 5 6 7
I don't know how I can implement this and I think the problem is for j in x since it is the same as the first loop. In my eyes, it says for every i, if any xj is in (xi-5,xi+5) then return the jth element. But, I want to return N sets.
Writing your indexes that way is probably what is confusing you.
If you want to access an index that is n-2 then you have to index it that way.
What you are doing is accessing the nth index of your list and then subtracting or adding 2 from the value that you accessed.
x[i]
returns whatever value is at position i in the list.
x[i-2]
will return whatever value is at position i-2.
Also you will need to account for the fact that subtracting any number from your index variable will cause your resulting index to be out of bounds if the resulting index is greater than the length of the list - 1 or less than 0.
If you are looking for duplicates the best way is to sort first then compare neighbors.
for i in x:
if x[i] == x[i+1]:
print i;
Subsetting is done like this: x[i-2:i+2]
So to divide into n sets of 2 elements you would:
n=2
for i in x:
if i+n < len(x):
return x[i:i+n];
To divide into n sets of equal number of elements you would:
n = // Some arbitrary number.
j = int(len(x) / n);
for i in x:
if i+j < len(x):
return x[i:i+j];

Google Kickstart Round E 2020 Longest Arithmetic Runtime Error

I tried solving this challenge mentioned below but I got a Run Time error. I used Python
Problem
An arithmetic array is an array that contains at least two integers and the differences between consecutive integers are equal. For example, [9, 10], [3, 3, 3], and [9, 7, 5, 3] are arithmetic arrays, while [1, 3, 3, 7], [2, 1, 2], and [1, 2, 4] are not arithmetic arrays.
Sarasvati has an array of N non-negative integers. The i-th integer of the array is Ai. She wants to choose a contiguous arithmetic subarray from her array that has the maximum length. Please help her to determine the length of the longest contiguous arithmetic subarray.
Input:
The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a line containing the integer N. The second line contains N integers. The i-th integer is Ai.
Output:
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the length of the longest contiguous arithmetic subarray.
Limits
Time limit: 20 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
0 ≤ Ai ≤ 109.
Test Set 1
2 ≤ N ≤ 2000.
Test Set 2
2 ≤ N ≤ 2 × 105 for at most 10 test cases.
For the remaining cases, 2 ≤ N ≤ 2000.
Sample Input
4
7
10 7 4 6 8 10 11
4
9 7 5 3
9
5 5 4 5 5 5 4 5 6
10
5 4 3 2 1 2 3 4 5 6
Output
Case #1: 4
Case #2: 4
Case #3: 3
Case #4: 6
Here's my python3 solution which gives run time error
t = int(input())
for t_case in range(t):
n = int(input())
arr = list(map(int, input().split()))
x = []
for i in range(n - 1) :
x.append((arr[i] - arr[i + 1]))
ans, temp = 1, 1
j = len(x)
for i in range(1,j):
if x[i] == x[i - 1]:
temp = temp + 1
else:
ans = max(ans, temp)
temp = 1
ans = max(ans, temp)
print(f"Case #{t_case+1}: {ans+1}")
Could anyone please help me out.
As of now Kickstart is using Python 3.5 which does not support f-strings (they were added in py3.6). Try to replace them with str.format.
t=int(input())
for test in range(t):
n=int(input())
arr = list(map(int, input().split()))
x = []
for i in range(n - 1) :
x.append((arr[i] - arr[i + 1]))
ans, temp = 1, 1
j = len(x)
for i in range(1,j):
if x[i] == x[i - 1]:
temp = temp + 1
else:
ans = max(ans, temp)
temp = 1
ans = max(ans, temp)
print('Case #{0}: {1}'.format(test+1,ans+1))

How do you triangulate a number in python?

I have to do these for school and I don't know how to.
Write a function print_triangular_numbers(n) that prints out the first n triangular numbers (n is an input). A call to print_triangular_numbers(5) would produce the following output:
n result
1 1
2 3
3 6
4 10
5 15
A triangular number can be expressed as
n(n+1)/2
Thus, you need to build a simple loop, starting at 1 and going through your passed parameter:
def print_triangular_numbers(n):
for x in range(1,n+1):
print x, x * (x + 1) / 2
The for loop starts at 1 and goes through n+1 because range is not inclusive of the end point.
This outputs:
1 1
2 3
3 6
4 10
5 15

Categories