![]() |
VOOZH | about |
At the very beginning of competitive programming, barely anyone knew the coding style to be followed. Below is an example to help you understand how problems are crafted in competitive programming.
Let us consider below problem statement as an example.
Linear Search: Given an integer array and an element x, find if element is present in array or not. If element is present, then print index of its first occurrence. Else print -1.
Input:
First line contains an integer, the number of test cases 'T'. Each test case should be an integer. Size of the array 'n' in the second line. In the third line, input the integer elements of the array in a single line separated by space. Element X should be input in the fourth line, i.e., after entering the elements of array. Repeat the above steps second line onwards for multiple test cases.
Output:
Print the output in a separate line returning the index of the element X. If the element is not present, then print -1.
Constraints:
1 <= T <= 100
1 <= n <= 100
1 <= arr[i] <= 100
Input:
2
4
1 2 3 4
3
5
10 90 20 30 40
40
Output:
2
4
Explanation:
There are 2 test cases (Note 2 at the beginning of input)
Test Case 1: Input: arr[] = {1, 2, 3, 4},
Element to be searched = 3.
Output: 2
Explanation: 3 is present at index 2.
Test Case 2: Input: arr[] = {10, 90, 20, 30, 40},
Element to be searched = 40.
Output: 4
Explanation: 40 is present at index 4.
Common mistakes by beginners
You can begin with above problem itself. Try submitting one of the above solutions here. It is recommended solve problems on Practice for cracking any coding interview.
Now you know how to write your first program in Competitive Programming Environment, you can start with School Practice Problems for Competitive Programming or Basic Practice Problems for Competitive Programming.
Platforms for practicing the Competitive Programming
Must Read
In conclusion, competitive programming starts with understanding how to read and follow problem statements carefully. As shown in the linear search example, it's important to stick to the exact input and output formats without adding extra prompts or text. Small mistakes like missing a newline or printing unnecessary messages can lead to rejection. Beginners should focus on solving simple problems first to build confidence and learn the structure of coding challenges. Platforms like Codechef, Leetcode, and GeeksForGeeks offer a great place to practice and improve. With regular practice, anyone can develop strong problem-solving and coding skills.