Time Limit exceeded error
Time Limit Exceeded error is caused when a code takes too long to execute and execution time exceeds the given time in any coding contest. TLE comes because the online judge has some restrictions that the code for the given problem must be executed within the given time limit.
Example:
Given two arrays, arr1 and arr2 of equal length N, the task is to find if the given arrays are equal or not. Two arrays are said to be equal if: both of them contain the same set of elements, arrangements (or permutations) of elements might/might not be the same. If there are repetitions, then counts of repeated elements must also be the same for two arrays to be equal.
Expected Time Complexity: O(N)
One possible approach can be to Sort both arrays, then linearly compare the elements of both arrays. If all are equal then return true, else return false. The time complexity for this approach will be O(N*log(N)). But the expected time complexity is O(N), so this code will give Time Limit Exceeded error. In online judges, we need to write code that executes within a given time limit. Hence, we need to optimize the approach.
Another possible approach can be to store the count of all elements of arr1[] in a hash table. Then traverse arr2[] and check if the count of every element in arr2[] matches with the count of elements of arr1[]. The time complexity for this approach will be O(N). Hence code for this approach will not give a time limit error and will get submitted successfully.
Below is the code for the above approach:
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Returns true if arr1[0..N-1] and arr2[0..M-1]
// contain same elements.
bool areEqual(int arr1[], int arr2[], int N, int M)
{
// If lengths of arrays are not equal
if (N != M)
return false;
// Store arr1[] elements and their counts in
// hash map
unordered_map<int, int> mp;
for (int i = 0; i < N; i++)
mp[arr1[i]]++;
// Traverse arr2[] elements and check if all
// elements of arr2[] are present same number
// of times or not.
for (int i = 0; i < N; i++) {
// If there is an element in arr2[], but
// not in arr1[]
if (mp.find(arr2[i]) == mp.end())
return false;
// If an element of arr2[] appears more
// times than it appears in arr1[]
if (mp[arr2[i]] == 0)
return false;
// decrease the count of arr2 elements in the
// unordered map
mp[arr2[i]]--;
}
return true;
}
// Driver's Code
int main()
{
int arr1[] = { 3, 5, 2, 5, 2 };
int arr2[] = { 2, 3, 5, 5, 2 };
int N = sizeof(arr1) / sizeof(int);
int M = sizeof(arr2) / sizeof(int);
// Function call
if (areEqual(arr1, arr2, N, M))
cout << "Yes";
else
cout << "No";
return 0;
}
import java.util.HashMap;
public class Main {
// Returns true if arr1[0..N-1] and arr2[0..M-1]
// contain the same elements.
static boolean areEqual(int arr1[], int arr2[], int N, int M) {
// If lengths of arrays are not equal
if (N != M)
return false;
// Store arr1[] elements and their counts in a HashMap
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < N; i++)
map.put(arr1[i], map.getOrDefault(arr1[i], 0) + 1);
// Traverse arr2[] elements and check if all
// elements of arr2[] are present the same number
// of times or not.
for (int i = 0; i < N; i++) {
// If there is an element in arr2[], but
// not in arr1[]
if (!map.containsKey(arr2[i]))
return false;
// If an element of arr2[] appears more
// times than it appears in arr1[]
if (map.get(arr2[i]) == 0)
return false;
// Decrease the count of arr2 elements in the HashMap
map.put(arr2[i], map.get(arr2[i]) - 1);
}
return true;
}
// Driver's Code
public static void main(String[] args) {
int arr1[] = { 3, 5, 2, 5, 2 };
int arr2[] = { 2, 3, 5, 5, 2 };
int N = arr1.length;
int M = arr2.length;
// Function call
if (areEqual(arr1, arr2, N, M))
System.out.println("Yes");
else
System.out.println("No");
}
}
# Function to check if arr1 and arr2 contain the same elements
def areEqual(arr1, arr2):
# If lengths of arrays are not equal
if len(arr1) != len(arr2):
return False
# Dictionary to store arr1 elements and their counts
mp = {}
for num in arr1:
mp[num] = mp.get(num, 0) + 1
# Traverse arr2 and check if all elements are present in arr1
# with the same counts
for num in arr2:
# If an element in arr2 is not in arr1
if num not in mp:
return False
# If an element appears more times in arr2 than in arr1
if mp[num] == 0:
return False
# Decrease the count of arr2 elements in the dictionary
mp[num] -= 1
return True
# Driver code
if __name__ == "__main__":
arr1 = [3, 5, 2, 5, 2]
arr2 = [2, 3, 5, 5, 2]
# Function call
if areEqual(arr1, arr2):
print("Yes")
else:
print("No")
using System;
using System.Collections.Generic;
public class Program
{
// Function to check if arr1 and arr2 contain the same elements
static bool AreEqual(int[] arr1, int[] arr2)
{
// If lengths of arrays are not equal
if (arr1.Length != arr2.Length)
return false;
// Dictionary to store arr1 elements and their counts
Dictionary<int, int> mp = new Dictionary<int, int>();
foreach (int num in arr1)
{
if (mp.ContainsKey(num))
mp[num]++;
else
mp[num] = 1;
}
// Traverse arr2 and check if all elements are present in arr1
// with the same counts
foreach (int num in arr2)
{
// If an element in arr2 is not in arr1
if (!mp.ContainsKey(num))
return false;
// If an element appears more times in arr2 than in arr1
if (mp[num] == 0)
return false;
// Decrease the count of arr2 elements in the dictionary
mp[num]--;
}
return true;
}
// Entry point of the program
public static void Main(string[] args)
{
int[] arr1 = { 3, 5, 2, 5, 2 };
int[] arr2 = { 2, 3, 5, 5, 2 };
// Function call
if (AreEqual(arr1, arr2))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
function areEqual(arr1, arr2) {
// If lengths of arrays are not equal
if (arr1.length !== arr2.length)
return false;
// Store arr1 elements and their counts in a Map
let map = new Map();
for (let i = 0; i < arr1.length; i++)
map.set(arr1[i], (map.get(arr1[i]) || 0) + 1);
// Traverse arr2 elements and check if all
// elements of arr2 are present the same number
// of times or not.
for (let i = 0; i < arr2.length; i++) {
// If there is an element in arr2, but
// not in arr1
if (!map.has(arr2[i]))
return false;
// If an element of arr2 appears more
// times than it appears in arr1
if (map.get(arr2[i]) === 0)
return false;
// Decrease the count of arr2 elements in the Map
map.set(arr2[i], map.get(arr2[i]) - 1);
}
return true;
}
// Driver's Code
let arr1 = [3, 5, 2, 5, 2];
let arr2 = [2, 3, 5, 5, 2];
// Function call
if (areEqual(arr1, arr2))
console.log("Yes");
else
console.log("No");
Output
Yes
Types of Issues and Errors in Programming/Coding
“Where there is code, there will be errors”
If you have ever been into programming/coding, you must have definitely come across some errors. It is very important for every programmer to be aware of such errors that occur while coding.
Table of Content
- 1. Syntax errors:
- 2. Logical errors:
- 3. Runtime errors:
- 4. Time Limit exceeded error:
In this post, we have curated the most common types of programming errors and how you can avoid them.