Thank you. I got it now
I think you need to include the case where k is 0
observe this pattern:
1-> 4 6 8 1 7 10 ; max = 10
2-> 6 4 2 9 3 0 ; max = 9
3-> 3 5 7 0 6 9 ; max = 9
4-> 6 4 2 9 3 0 ; max = 9
5-> 3 5 7 0 6 9 ; max = 9
6-> 6 4 2 9 3 0 ; max = 9
…
so from step 2 each step repeats after 2 cycles
I belong to the category who specify the data type if it may not be clear to the user. That is why I mentioned that it is a subjective topic.
your algorithm complexity is O(N*K)
as
1 <= N <= 10^5
0 <= K <= 10^9
so in worst case N*K can be 10^14
since you are looping from 0 to 10^14(beyond 10^9) you are getting TLE.
I mean actual test cases may not contain upto 10^14 but if it contains beyond 10^9 you may get TLE
Thanks. Made the same mistake.
Why am I getting TLE for O(n) solution?
N , K = [int(s) for s in input().split()]
arr = [int(s) for s in input().split()]
if K ==0:
s = “”
for i in arr:
s = s + str(i)+" "
print(s)
else:
brr = [max(arr)-i for i in arr]
if K>1:
crr = [max(brr)-i for i in brr]
if (K%2 == 1):
s = ""
for i in brr:
s = s + str(i) +" "
print(s)
else:
s = ""
for i in crr:
s = s + str(i)+" "
print(s)
Someone please tell me whats wrong with my code,its running well and giving me the correct output in my compiler but in codechef IDE its doesn’t,
here’s my code:-
#include <bits/stdc++.h>
#define mi 1000000000
#define int unsigned long long int
#define test int t;cin>>t;while(t–)
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using namespace std;
signed main() {
io;
int n,k;
cin>>n>>k;
int x = 0 , y = 0;
int arr[mi];
int temp = INT_MIN;
for(int i = 0;i < n;i++)
{
cin>>arr[i];
if(arr[i] > temp)
{
temp = arr[i];
}
}
int ct = INT_MIN;
while(k–)
{
for(int i = 0;i < n;i++)
{
arr[i] = temp - arr[i];
if(arr[i] > ct)
{
ct = arr[i];
}
}
temp = ct;
}
for(int i = 0 ;i < n;i++)
{
cout<<arr[i]<<" ";
}
// your code goes here
return 0;
}
INPUT:-
4 1
5 -1 7 0
OUTPUT:-
18446744073709551610 0 18446744073709551608 18446744073709551615
I got it!!!
Thanks People.
Glad it helped
[CodeChef: Practical coding for everyone](http://My Submission) can someone please tell which corner case I am missing?
My code is showing SIGSEVG error, though everything looks fine to me.Preformatted text
Can someone pls tell why?
Thanks in advanced
#include <iostream>
using namespace std;
int max(long long int arr[],long long int n){
long long int m=arr[0];
for(long int i=1;i<n;i++){
m=max(arr[i],m);
}return m;
}
int main()
{
long int n;
long long int k;
cin>>n;
cin>>k;
long long int arr[n]={};
long long int li[n]={};
for(long int i=0;i<n;i++){
cin>>arr[i];
li[i]=arr[i];
}
if(k==0){
}else{
for(int r=1;r<=k;r++){
long long int m=max(arr,n);
for(long int i=0;i<n;i++){
li[i]=m-li[i];
}}}
for(long int i=0;i<n;i++){
cout<<li[i]<<" ";
}
return 0;
}