Very-very basic doubt

Both codes are exactly same

This code gives WA : code
This code gives AC : code

Can anyone please tell me why it is happening so ?

Somebody please help :pray:
@ssrivastava990 @galencolin @vijju123 @taran_1407 @everule1 @shivansh100

Both are same , why one is giving WA and another AC :thinking: @ssjgz please check.

This. How.

@ssrivastava990 @sanjyathri @ssjgz @galencolin
the program should give TLE for the AC solution since,the constraints for N in this problem is 10^5 and your AC code uses O(N*N) approach this should fail for the case
N=10^5 and Arr[]=(6,6,6,6,6…)

  1. My AC: (O(n))Code: takes time of 0.78 whereas the above AC code(O(N*N)) : :takes 0.59
    PS: correct me if I am wrong

Come on, everyone - you’ve seen me do this dozens of times :slight_smile:

Compiling sanjyathri-FIZZA.cpp
+ g++ -std=c++14 sanjyathri-FIZZA.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
+ set +x
[~/devel/hackerrank/otherpeoples]>echo "3                  
6 3 6 4 5 4 3 6
5 5 4 5 2 1 3 4 2
1 2 3 4 5 6" | ./a.out
sanjyathri-FIZZA.cpp:23:30: runtime error: index 8 out of bounds for type 'long long int [*]'
sanjyathri-FIZZA.cpp:21:25: runtime error: index 8 out of bounds for type 'long long int [*]'

Undefined Behaviour.


But how this code gives AC then : code instead it should also give “Array index out of bounds”

What is this sorcery and how can I learn it

1 Like

Languages like C and C++ are not very strict in boundary checking for arrays,
When you use a[j+1] and a[j+2] and j==n you are going out of bounds.
In first case it is using same locations when you reallocate a[n]. In this case previous input and new input may overlap and gives wa.
In second case when we reallocate a[n] it is using some other locations containing complete garbage. Although logic is wrong but result is correct hence accepted.

Example where first program goes wrong.
1 2 3 2 4 5
5 5 5 5

1 Like