Can someone please tell me what’s wrong with my Code
import java.util.;
import java.lang. ;
import java.io .*;
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
long tcases = Long.parseLong(br.readLine());
while(tcases–>0)
{
String str = br.readLine();
String str1[] = str.trim().split(" “);
long N = Long.parseLong(str1[0]);
long K = Long.parseLong(str1[1]);
String s = br.readLine();
String s1[] = s.trim().split(” ");
long arr[] = new long[(int)N];
for(long i=0;i<N;i++)
arr[(int)i] = Long.parseLong(s1[(int)i]);
long max = Integer.MIN_VALUE;
for(long i=0;i<N;i++)
{
if(arr[(int)i]>max)
max=arr[(int)i];
}
int ctr =0;
for(long i=K-1;i<N;i++)
if(arr[(int)i]==max)
ctr+=(N-i);
System.out.println(ctr);
}
}
}
#include <iostream>
typedef long long ll;
using namespace std;
int main() {
ll t;
cin>>t;
while(t)
{
ll n,k;
cin>>n>>k;
ll arr[n];
for(ll j=1;j<=n;j++)
{
cin>>arr[j];
}
ll mx=arr[0];
for(ll j=1;j<=n;j++)
{
if(mx<arr[j])
{
mx=arr[j];
}
}
cout<<mx<<endl;
for(ll j=k;j<=n;j++)
{
if(mx==arr[j])
{
cout<<n-j+1<<endl;
break;
}
}
t--;
}
return 0;
}
bro can you please help me why this code is giving wrong answer
ssjgz
August 29, 2021, 7:41pm
23
Out of bounds access on sample test input:
[simon@simon-laptop][20:40:11]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh
Compiling akshansh_1-KMAX2.cpp
+ g++ -std=c++14 akshansh_1-KMAX2.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv -fno-sanitize-recover
+ set +x
Successful
[simon@simon-laptop][20:40:55]
[~/devel/hackerrank/otherpeoples]>echo "1
5 3
1 2 3 4 5
" | ./a.out
akshansh_1-KMAX2.cpp:20:24: runtime error: index 5 out of bounds for type 'long long int [*]'
1 Like
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
ll t,n,k,z,l;
cin>>t;
while(t–)
{
cin>>n>>k;
ll a[n];
ll max1=INT_MIN;
for(ll i=0;i<n;i++)
{
cin>>a[i];
}
for(ll i=0;i<n;i++)
{
if(a[i]>max1)
{
max1=a[i];
l=i;
}
}
ll z=l-(k-1);
if(z<0)
cout<<“0”<<endl;
else
{
int m=n-(l+1);
cout<<m+1<<endl;
}
}
return 0;
}
please help me to find out error in my code it is giving wrong anwer but according to me it seems to be right for everytest case.
@nitishsharma12 You are finding the maximum element and also storing its index. But there can be a situation where the maximum element is present more than once in the array. Consider Array [5,5,5,5,5] or [3,4 5,6 2,1,6,3,1,3].
1 Like
ohh i understand where is my error thanku so much.
why my code not working?
#include<bits/stdc++.h>
using namespace std;
int main() {
long long t;
cin>>t;
long long sum=-1e9;
while(t–)
{
long long int n,k;
cin>>n>>k;
vector<long long>v(n);
for(int i=1;i<=n;i++){
cin>>v[i];
sum=max(sum,v[i]);
}
long long ans=0;
for(int i=k;i<=n;i++)
{
if(sum==v[i])
{
ans+=n+1-i;
}
}
cout<<ans<<endl;
}
return 0;
}
you realize there can be more than 1 value that is equal to max_element right
1 2 5 5 5 5
what do u think the answer is to this test case for k = 3? its 10 i hope you can figure out now
if array size is 5 and k = 3 and arr[]: 5 5 5 5 5
Correct o/p is 6 not 3
your code is fine. simply return long long instead of int.
s1k7
April 12, 2022, 3:32pm
34
A Note to Editorial Solution -
@janmansh You should provide most efficient solution. Why you are using space O(N) if you can do it O(1)?
Check this out - time complexity O(N) Single pass. Space complexity O(1).
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t–){
int n,k;
cin>>n>>k;
int a,max;
cin>>a;
max = a;
long long int count = 0;
if(k == 1) count = n;
for(int i=1;i<n;i++){
cin>>a;
if(a > max){
max = a;
//initialise count..
if(i >= k-1) {
count = n-i;
}
} else if(a == max && i >= k-1) {
count += n-i;
}
}
cout<<count<<endl;
}
return 0;
}