Longest run | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Longest run

11 2 2 5 4 3 3 8 7 11 15 Find the longest run. If you make the wildcards a 6 and 9, you can make the run (2,3,4,5,[6],7,8,[9]), which has length 8. you cannot make a longer run. My idea is sort the list in ascending order and then put the wild cards in between the cards. But I stucked on how to put them between the cards. Statement You have N cards in your hand. N−K cards have integers on them. The remaining K cards are wild cards that can each represent a card of any integer you choose. A run of length m is a sequence of m cards with the integers (a,a+1,a+2,…,a+m−1) for some integer a. For example, the sequences (7,8,9) , (1,2,3,4,5), and (6) are runs, but (2,3,5,6), (1,6), and (4,3,2,1) are not runs. What is the longest run you can make with these cards? You may rearrange the cards in your hand however you like to form this run. The input format and output format is in the chat section

4th Nov 2023, 6:23 AM
Zoey
Zoey - avatar
108 Answers
6th Nov 2023, 10:08 AM
Oma Falk
Oma Falk - avatar
+ 3
https://www.sololearn.com/post/1748052/?ref=app
5th Nov 2023, 10:50 AM
Oma Falk
Oma Falk - avatar
+ 1
Would you mind to show your code?
4th Nov 2023, 8:40 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Zoey I fixed my code and now it produces the expected results for Case 1 & 2. https://code.sololearn.com/cXp7vLn3WbIq/?ref=app
6th Nov 2023, 2:02 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
I added a conditional statement to check in case a wildcard is not available. Also I fixed a bug which it was separating the runs wrongly at the end. It should be able to find all the longest run, and I'm done with this challenge (for now). https://code.sololearn.com/clO81qT2Cr27/?ref=app
7th Nov 2023, 9:41 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
It's clear now that the longest run on test case 3 is 1516. I made a total check on the hole index range, and that worked. https://code.sololearn.com/cBDeTxhog5n3/?ref=app
8th Nov 2023, 7:06 PM
Jan
Jan - avatar
0
a, b = map(int, input().split()) integers = list(map(int, input().split())) integers.sort() print(integers) Wong Hei Ming This is the code i have
4th Nov 2023, 9:24 AM
Zoey
Zoey - avatar
0
Zoey What is the input for the first line? I tried '6 2', it return a=6 and b=2. PS. I assume the description is '6 2', not '5 2' because 5 already in the second line.
4th Nov 2023, 9:31 AM
Wong Hei Ming
Wong Hei Ming - avatar
0
Wong Hei Ming 5 is the number of cards and 2 is the number of wild cards. The second line is 5 cards with different numbers.
4th Nov 2023, 9:58 AM
Zoey
Zoey - avatar
0
Zoey What is the purpose of variable a and b? Is this a code challenge or a exercise? What are the input the program will receive? I can't see how the program receive the cards 3 1 4 5 9. It looks like someone will supply the cards base on variable a (number of cards). And someone (maybe the user?) can decide the wild cards, and that number of cards is base on variable b.
4th Nov 2023, 10:18 AM
Wong Hei Ming
Wong Hei Ming - avatar
0
Wong Hei Ming this is a code challenge. The website will provide the input. This is just one of the inputs.
4th Nov 2023, 10:20 AM
Zoey
Zoey - avatar
0
I'm sure, but I think that task is to count the longest sequence of numbers, and then you can use a wildcard to increase the sequence if it matches.
4th Nov 2023, 10:43 AM
Jan
Jan - avatar
0
Zoey I think this challenge is not in the community code coach, am I right? It is quite challenging. If I understand correctly, the website provide '5 2' as the first input. Then you use the first number to receive the cards, and the second number is in your disposal, allowing you to add N number of cards. So let me rephrase your question: How to determine where the wild cards to be inserted to produce the longest run? The website provide '5 2' at the beginning, telling you there are 5 cards and 2 wild cards you can insert. With the '5' you get '1 3 4 5 9'. So there would be lots of combination. 1 (2) 3 4 5 (6) 9 1 3 4 5 (6) (7) 9 1 3 4 5 (7) (8) 9 1 3 4 5 9 (10) (11) I don't have a slight idea yet, but itretools maybe is the library you need.
4th Nov 2023, 10:47 AM
Wong Hei Ming
Wong Hei Ming - avatar
0
Wong Hei Ming Yeah, you are right. I am stuck on how to insert the wild card in between the cards. Btw, thanks
4th Nov 2023, 10:56 AM
Zoey
Zoey - avatar
0
Quantum Do you know how to solve it?
4th Nov 2023, 10:57 AM
Zoey
Zoey - avatar
0
Zoey Where did you get that task from, because I want to see the hole description before I suggest a solution?
4th Nov 2023, 11:20 AM
Jan
Jan - avatar
0
Quantum Statement You have N cards in your hand. N−K cards have integers on them. The remaining K cards are wild cards that can each represent a card of any integer you choose. A run of length m is a sequence of m cards with the integers (a,a+1,a+2,…,a+m−1) for some integer a. For example, the sequences (7,8,9) , (1,2,3,4,5), and (6) are runs, but (2,3,5,6), (1,6), and (4,3,2,1) are not runs. What is the longest run you can make with these cards? You may rearrange the cards in your hand however you like to form this run. Input Format The first line consists of an integer N (1≤N≤10^5), followed by another integer K(0≤K≤N). The second line contains N−K integers, representing the cards in your hand that aren’t wild cards: C1,C2,…,CN−K(1≤Ci≤10^9). Output Format Output a single integer, the length of the longest run you can form from the cards you have.
4th Nov 2023, 11:46 AM
Zoey
Zoey - avatar
0
Sure, I can help you with that. You can write a Python program to find the longest run given the input list of cards and the number of wild cards. Here's a sample program: ```python def find_longest_run(cards, wildcards): cards.sort() # Sort the cards in ascending order max_run = 0 current_run = 1 for i in range(1, len(cards)): diff = cards[i] - cards[i-1] - 1 if diff <= wildcards: current_run += diff + 1 wildcards -= diff else: current_run += wildcards wildcards = 0 current_run += 1 max_run = max(max_run, current_run) return max_run # Example usage cards = [3, 1, 4, 5, 9] wildcards = 2 result = find_longest_run(cards, wildcards) print(result) ``` In this program, `find_longest_run` takes a list of cards (`cards`) and the number of wild cards (`wildcards`) as input. It sorts the cards in ascending order, then iterates through the cards to find the longest run. It keeps track o
4th Nov 2023, 12:06 PM
রহস্যময়— পৃথিবী
রহস্যময়— পৃথিবী - avatar
0
Zoey Aha, it seems to be the same as a straight in poker, so you just need to count the longest straight I assume, including using the wildcards.
4th Nov 2023, 12:20 PM
Jan
Jan - avatar