Resources for learning Python, code editors, expected knowledge, how to practice, and tutorial details
Introduction
Welcome to your Python🐍 competitive programming journey! In this first tutorial, you'll explore the fundamentals of Python programming through various resources, set up a code editor, learn how to use these tutorials effectively, and learn how to practice competitive programming problems to continuously improve.
Tutorials
First, let's discuss how to utilize the tutorials. It is recommended to follow the sections and subsections in chronological order. However, feel free to skip a tutorial and come back to it at a later time.
Each section will begin by teaching the content with text, visuals, and examples. Afterward, you can test your understanding with the mini-challenges, apply your concepts using the practice problems, and vote on the poll. The practice problems (usually easiest to hardest, and ranging from easy to catastrophic) come from a variety of sources (USACO, Codeforces, LeetCode, and more) and have helpful hints if you need help. If you're stuck, viewing the solution is valuable - learn from the experience, and reflect on what you could've done differently. Be sure to implement it after getting ideas from the editorial! After solving a problem, reviewing other solutions can help you discover more efficient or elegant solutions. Understanding other methods will ultimately improve your intuition and implementation skills. Additional resources are also given in each section if applicable.
If you are a teacher/club leader using these tutorials, feel free to use any materials here as a learning resource!
Guide to Competitive Programming
Competitive programming is all about applying algorithms and implementing them into code. First, you need to come up with a solution using problem-solving, critical thinking, mathematics, and logic. To succeed, you must analyze problems effectively and view them from different perspectives. An algorithm for solving a problem needs to be accurate and efficient (within the time and memory constraints of the problem). Understanding the theory behind algorithms and how to apply them effectively to problems is a crucial skill, as well as making observations and insights about the properties of the problem. Then, you need to implement your idea in code.
In competitive programming, your solution is graded by testing your code implementation against a set of test cases, checking the answer returned by your program for each test case. Your program must return the correct output and satisfy the time and memory limits. You'll need to adopt a good coding style that makes your code concise and clear, and you need to be good at implementation skills because you are running against the clock, so most of your time should be spent on coming up with the algorithm. A competitive math background can be beneficial for problems that involve mathematical insights. The most popular languages used in programming contests are C++, Java, and Python.
Mastering competitive programming requires dedication, and you'll grow by spending time practicing problems and consistently participating in various programming contests like CodeEvolve, USACO, Codeforces, LeetCode, CodeChef, and others.
Here are the reasons why you should start competitive programming:
-
Enhances your thinking and intuition ability
-
Gives you a sense of accomplishment after solving a problem
-
Relevant in future technical interviews for software-related jobs and companies
-
Connects you to a community of people who share your passion for programming
Getting a Code Editor and Learning to Code
Now let's delve into installing and running your code on a code editor! Here are some code editors that can be used to run Python code locally:
-
Visual Studio Code (VS Code) - A user-friendly, powerful editor that I highly recommend
You can also use an online editor:
I personally use the VS Code editor.
Now it's time to learn the basics of Python and its essential data structures. Python is a great option for competitive programming because of its simple, readable, and straightforward syntax, which makes implementing code and debugging easier. I recommend the following resources for learning the building blocks:
-
W3Schools (complete the Python Tutorial, File Handling, Python Reference, Python How To, and the Python Examples sections; skip Classes/Objects, Inheritance, Polymorphism, Dates, JSON, RegEx, PIP, and String Formatting)
Make sure that you're comfortable with the following topics:
-
Variables and data types
-
Input/output (IO)
-
Loops/iteration
-
Conditionals (if/elif/else)
-
Logical and mathematical operators
-
Functions and basic recursion
-
Arrays (1D, 2D, and 3D lists, built-in sorting)
-
Sets, Dictionaries, and Tuples
-
Strings
While learning, try to build your own programs and tinker around to actively engage and apply the topics. An important note is that whenever you want to know how to do something or know the built-in method for something, always Google it so that you can learn from it.
After becoming comfortable with Python, it's time to start solving problems. You'll increase your problem-solving and algorithmic programming skills by applying your knowledge. Work on 800-900 rated CodeChef or Codeforces problems, easy level Leetcode, and easy-medium level CodeEvolve. Start with easy problems and then slowly increase the difficulty. Don't hesitate to check out other solutions to expand your skillset, even if you solved it!
How to Submit a Solution
To submit your Python solution for platforms like USACO and Codeforces:
-
Ensure that your solution passes the sample test case(s) and run it on additional tests if needed
-
Log in to your account
-
Select Python 3 (or PyPy 3 for Codeforces)
-
Click the Submit button
-
Analyze how your solution performs with test cases. For USACO, a green box means your program passed that test case (and the time your program took to run for that test case in milliseconds is shown), a red X means wrong answer, a red T means time limit exceeded, a red ! means runtime error or memory limit exceeded, an E means empty output file, and M means missing output file. For Codeforces, it will return a green Accepted, and otherwise, it will return the error and/or which test case your program fails (you can view this test case if it's a practice problem and not during a contest)
Examples:
Conclusion
In conclusion, you should focus on getting comfortable with implementing code by practicing basic applied problems regularly and consistently. Focus on logical thinking and problem-solving, not just coding. If you're stuck, try to draw multiple test cases and use a pencil and paper to take note of observations. Lastly, improve your implementation skills by aiming to write concise, readable, and efficient code. When you're able to consistently solve simple algorithmic problems, like 800-900 Codechef/Codeforces, you're ready to advance to the next section!
Problem
Tess has an array a of length N (1 ≤ N ≤ 2 x 10⁵) satisfying the following property: a₂ < a₃ < ... < aₙ < a₁.
However, Tess prefers a mirrored arrangement of the array satisfying: aₙ₋₁ < aₙ₋₂ < ... < a₁ < aₙ. In other words, the current array has the first element as the maximum element and a[2...N] sorted in increasing order, while she desires the last element as the maximum element and a[1...N-1] sorted in decreasing order.
To achieve this, Tess can choose any two indices i and j (1 ≤ i < j ≤ N) and swap them. Help her determine the minimum number of swaps required to transform the given array into the desired form.
Input Format
The first line contains T (1 ≤ T ≤ 10⁴), the number of independent test cases.
The first line of each test case contains an integer N (1 ≤ N ≤ 2 × 10⁵), the array a's length. The next line contains N integers a₁, a₂, … aₙ (-10⁹ ≤ aᵢ ≤ 10⁹). It is guaranteed that a₂ < a₃ < ... < aₙ < a₁.
The sum of N over all test cases does not exceed 10⁶.
Output Format
For each test case, output a single integer representing the minimum number of swaps Tess needs to make to transform the array.
Sample Input
4
1
2
4
4 1 2 3
5
6 1 2 5 5
7
10 1 5 6 6 7 8
Sample Output
0
2
2
3
💡 Mini-Challenge Solution 💡
The desired array is just the reverse of the original array. For example, if we have [4, 1, 2, 3], then we desire the opposite [3, 2, 1, 4]. To do this, the simple strategy is just to swap the first and last elements, the second and second to last elements, and so on. In fact, this strategy will turn any array into its reverse, not just this particular arrangement.
If N is even, then we do a total of N / 2 swaps. If N is odd, then we do ⌊N / 2⌋ swaps (the notation ⌊⌋ is the floor operation, which rounds the integer down), because we ignore the middle element. Collectively, we do ⌊N / 2⌋ operations, so that is the output. To code this in Python, we can either do N // 2 (integer division) or import the Python math module and do math.floor(N / 2).
Here is the code implementation:
For the full problem (with the original problem and all evolves), see Mirrored Arrangements.

⚔️ Problems ⚔️
-
Easy Problem - Codeforces (Easy)
-
Normal Problem - Codeforces (Easy)
-
Hard Problem - Codeforces (Easy)
-
Was there an Array? - Codeforces (Easy)
-
Brogramming Contest - Codeforces (Easy)
-
Minimal Coprime - Codeforces (Easy)
-
Missing Number - CSES (Easy)
-
Repetitions - CSES (Easy)
-
Increasing Array - CSES (Easy)
-
Permutations - CSES (Easy)
-
Bit Strings - CSES (Medium)
-
MEX Destruction - Codeforces (Medium)
📊 Poll 📊
Vote on this interactive poll to share your opinion on different aspects of competitive programming such as favorite algorithm, data structure, or most challenging problem with real-time results!
