What could go wrong Compilation

What could go wrong Compilation

Hi, folks!
Even after a lot of practice, we still make mucho Silly mistakes. By mistakes, I mean, we ourselves realise them - we shouldn’t have done that. No probs, I’ve got you covered. Here are a few places where we often make mistakes and sometimes blame other elements.

Alright! let’s start.

Newline: You forget - you lose

The output format is something one should really care about. Print on a new line, Print two space-separated Integers and so on. Most of the times, people (at least me) get WA for not printing newline character for each test case. My two codes, WA and AC, are exactly the same, except for the statement to print newline in the latter. Imagine getting 10 minutes of Penalty for this.

Declare int instead of long

This is pretty much a common mistake everyone does. Times when we are supposed to calculate the Cumulative Sum of Sequence or work with bits, we often forget to use long or long long. One might want to write Comments in their template to use long long xD. Common mistakes most of us did and continue to do:

  • 1 << i; instead of 1LL << i;
  • int sum = 0; instead of long long sum = 0;
    (You might want to add others)

Annoying Runtime error

Every time you get a Runtime error, the reason could be one (or many) of these:

  • No base case in Recursion - Segmentation Fault (Core dumped) :crazy_face:
  • Declare an array of {10}^9 elements - Segmentation Fault
  • Access invalid memory location - Index out of bound (Java), IndexError (Python) and probably Segmentation Fault (C and CPP).
  • Run without providing Custom Input - NZEC.

Modular Arithmetic to Avoid Overflow

Since the answer could be very large, print it modulo 10^9 + 7

Even after noting this, some of us forget it while coding. Some of us are not aware of the steps in calculating it. Instead of me giving tips on this, I would like to attach an existing topic by @galencolin, easy to understand.

Compilation Error (bruh! seriously?)

Who among us confidently submits codes without testing?
For this, I don’t have any tips because readers already know what to do.

Overlook Constraints

The sum of N over all test cases doesn’t exceed 10^6

At least some problems have very small constraints - A Naive solution for problems like this or a slight optimal solution for problems like this will easily win.

Ignore Hard Questions?

Some of us research on some or other topics while we learn. Imagine the hardest problem in a contest is well known to you Like I knew the concept for this problem. It was the hardest problem in October Lunchtime - Division 2 (based on the number of successful submissions). If I had ignored that problem, I would have secured a worse rank. One should read all the problems, at least after the contest is over.

Pre Calculation - keeps TLE away

Who would expect TLE for not storing inverses of 10^6 Integers?
This Problem made me understand the importance of Optimisation. Just because I didn’t Pre-Calculate Inverses of about 10^6 Integers, I faced TLE. Note that One can make use of Memory to avoid TLE :thinking:.

Have any other problems? You can share them here.

4 Likes

Thanks a lot bro, that’s very much helpful. :v:

But still, I forgot to mention this

Fast IO - Avoid Unnecessary TLE

C Code

I was kidding. scanf() and prinf() are more than enough.

CPP Code
#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false); 
    cin.tie(NULL);
    // Include these two lines
    return 0;
}
Java Code
// Working program with FastReader
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {
	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader()
		{
			br = new BufferedReader(
				new InputStreamReader(System.in));
		}

		String next()
		{
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				}
				catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() { return Integer.parseInt(next()); }

		long nextLong() { return Long.parseLong(next()); }

		double nextDouble()
		{
			return Double.parseDouble(next());
		}

		String nextLine()
		{
			String str = "";
			try {
				str = br.readLine();
			}
			catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}

	public static void main(String[] args)
	{
		FastReader s = new FastReader();
		int n = s.nextInt();
		int k = s.nextInt();
		int count = 0;
		while (n-- > 0) {
			int x = s.nextInt();
			if (x % k == 0)
				count++;
		}
		System.out.println(count);
	}
}

Python Code
from sys import stdin, stdout, stderr
read = stdin.readline
def main():
    n = int(read())
    print(n)
main()

Bibliography
Geeksforgeeks

2 Likes