/*
this is quick fix for your solution since we don't need an array to store the value
and then perform a scan after to find the minimum it's unecessery loops and waste of time
to store the values in an arrays.
but this solution still gives TLE
*/
#include<bits/stdc++.h>
using namespace std;
const int INF = 1e5 + 5 ;
void solve( ) {
int a,b;
cin >> a >> b;
vector< int > arr( a) ;
for ( int & x : arr) {
scanf ( "%d" , & x) ;
}
int ans = INF;
for ( int i= 0 ; i < a; i++ ) {
for ( int j = i+ 1 ; j <= i + b && j < a; j++ ) {
if ( arr[ i] == arr[ j] ) {
ans = min( ans, arr[ i] ) ;
}
}
}
printf ( "%d\n " , ans ! = INF ? ans : - 1 ) ;
}
int main( ) {
solve( ) ;
return 0 ;
}
/*
Another improvement we can do is to remove some loops if we have computed the answer
*/
#include<bits/stdc++.h>
using namespace std;
const int INF = 1e5 + 5 ;
void solve( ) {
int a,b;
cin >> a >> b;
vector< int > arr( a) ;
for ( int & x : arr) {
scanf ( "%d" , & x) ;
}
set< int > S;
int ans = INF;
for ( int i= 0 ; i < a; i++ ) {
if ( ! S.count ( arr[ i] ) ) {
for ( int j = i+ 1 ; j <= i + b && j < a; j++ ) {
if ( arr[ i] == arr[ j] ) {
ans = min( ans, arr[ i] ) ;
S.insert ( arr[ i] ) ;
break ;
}
}
}
}
printf ( "%d\n " , ans ! = INF ? ans : - 1 ) ;
}
int main( ) {
solve( ) ;
return 0 ;
}
// still this gives error so try to come up with a solution that works in O(n) or O(nlog(n))
Ci8qCnRoaXMgaXMgcXVpY2sgZml4IGZvciB5b3VyIHNvbHV0aW9uIHNpbmNlIHdlIGRvbid0IG5lZWQgYW4gYXJyYXkgdG8gc3RvcmUgdGhlIHZhbHVlCmFuZCB0aGVuIHBlcmZvcm0gYSBzY2FuIGFmdGVyIHRvIGZpbmQgdGhlIG1pbmltdW0gaXQncyB1bmVjZXNzZXJ5IGxvb3BzIGFuZCB3YXN0ZSBvZiB0aW1lCnRvIHN0b3JlIHRoZSB2YWx1ZXMgaW4gYW4gYXJyYXlzLgpidXQgdGhpcy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7IGogPD0gaSArIGIgJiYgaiA8IGE7IGorKyl7CgkJCQlpZihhcnJbaV09PWFycltqXSkgewoJCQkJCWFucyA9IG1pbihhbnMsIGFycltpXSk7CgkJCQkJUy5pbnNlcnQoYXJyW2ldKTsKCQkJCQlicmVhazsKCQkJCX0KCQkJfQoJCX0KCX0KCXByaW50ZigiJWRcbiIsIGFucyAhPSBJTkYgPyBhbnMgOiAtMSk7Cn0KCmludCBtYWluKCl7Cglzb2x2ZSgpOwoJcmV0dXJuIDA7Cn0KCi8vIHN0aWxsIHRoaXMgZ2l2ZXMgZXJyb3Igc28gdHJ5IHRvIGNvbWUgdXAgd2l0aCBhIHNvbHV0aW9uIHRoYXQgd29ya3MgaW4gTyhuKSBvciBPKG5sb2cobikpCgoK
compilation info
prog.cpp:47:11: error: redefinition of ‘const int INF’
const int INF = 1e5 + 5;
^~~
prog.cpp:13:11: note: ‘const int INF’ previously defined here
const int INF = 1e5 + 5;
^~~
prog.cpp:49:6: error: redefinition of ‘void solve()’
void solve(){
^~~~~
prog.cpp:15:6: note: ‘void solve()’ previously defined here
void solve(){
^~~~~
prog.cpp:72:5: error: redefinition of ‘int main()’
int main(){
^~~~
prog.cpp:34:5: note: ‘int main()’ previously defined here
int main(){
^~~~
prog.cpp: In function ‘void solve()’:
prog.cpp:20:8: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &x);
~~~~~^~~~~~~~~~
prog.cpp: In function ‘void solve()’:
prog.cpp:54:8: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &x);
~~~~~^~~~~~~~~~
stdout