At the very beginning to competitive programming, barely anyone knows the coding style to be followed. Below is an example to help understand that style.

Let us consider below problem statement as an example.

**Problem Statement:**

**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 inputted 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

**Example Input and Output for Your Program:**

Input:2 4 1 2 3 4 3 5 10 90 20 30 40 40Output: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.

## C

// A Sample C program for beginners with Competitive Programming #include<stdio.h> // This function returns index of element x in arr[] int search(int arr[], int n, int x) { int i; for (i = 0; i < n; i++) { // Return the index of the element if the element // is found if (arr[i] == x) return i; } //return -1 if the element is not found return -1; } int main() { // Note that size of arr[] is considered 100 according to // the constraints mentioned in problem statement. int arr[100], x, t, n, i; // Input the number of test cases you want to run scanf("%d", &t); // One by one run for all input test cases while (t--) { // Input the size of the array scanf("%d", &n); // Input the array for (i=0; i<n; i++) scanf("%d",&arr[i]); // Input the element to be searched scanf("%d", &x); // Compute and print result printf("%d\n", search(arr, n, x)); } return 0; }

## C++

// A Sample C++ program for beginners with Competitive Programming #include<iostream> using namespace std; // This function returns index of element x in arr[] int search(int arr[], int n, int x) { for (int i = 0; i < n; i++) { // Return the index of the element if the element // is found if (arr[i] == x) return i; } // return -1 if the element is not found return -1; } int main() { // Note that size of arr[] is considered 100 according to // the constraints mentioned in problem statement. int arr[100], x, t, n; // Input the number of test cases you want to run cin >> t; // One by one run for all input test cases while (t--) { // Input the size of the array cin >> n; // Input the array for (int i=0; i<n; i++) cin >> arr[i]; // Input the element to be searched cin >> x; // Compute and print result cout << search(arr, n, x) << endl; } return 0; }

## Python

# A Sample Python program for beginners with Competitive Programming # Returns index of x in arr if it is present, # else returns -1 def search(arr, x): n = len(arr) for j in range(0,n): if (x == arr[j]): return j return -1 # Input number of test cases t = int(raw_input()) # One by one run for all input test cases for i in range(0,t): # Input the size of the array n = int(raw_input()) # Input the array arr = map(int, raw_input().split()) # Input the element to be searched x = int(raw_input()) print(search(arr, x)) # The element can also be searched by index method # But you need to handle the exception when element is not found # Uncomment the below line to get that working. # arr.index(x)

## Java

// A Sample Java program for beginners with Competitive Programming import java.util.*; import java.lang.*; import java.io.*; class LinearSearch { // This function returns index of element x in arr[] static int search(int arr[], int n, int x) { for (int i = 0; i < n; i++) { // Return the index of the element if the element // is found if (arr[i] == x) return i; } // return -1 if the element is not found return -1; } public static void main (String[] args) { // Note that size of arr[] is considered 100 according to // the constraints mentioned in problem statement. int[] arr = new int[100]; // Input the number of test cases you want to run Scanner sc = new Scanner(System.in); int t = sc.nextInt(); // One by one run for all input test cases while (t > 0) { // Input the size of the array int n = sc.nextInt(); // Input the array for (int i = 0; i < n; i++) arr[i] = sc.nextInt(); // Input the element to be searched int x = sc.nextInt(); // Compute and print result System.out.println(search(arr, n, x)); t--; } } }

**Common mistakes by beginners**

- Program should not print any extra character. Writing a statement like
`printf("Enter value of n")`

would cause rejection on any platform. - Input and output format specifications must be read carefully. For example, most of the problems expect a new line after every output. So if we don’t write printf(“\n”) or equivalent statement in a loop that runs for all test cases, the program would be rejected.

**How to Begin Practice?**

You can begin with above problem itself. Try submitting one of the above solutions here.

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.

**How to Begin Study?**

Top 10 Algorithms and Data Structures for Competitive Programming.

Visit here to decide which category suits you more.

See this for more FAQs for beginners.

How to prepare for ACM – ICPC?

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above