I have tried using mostly all of the solutions present online including Stack Overflow, I have tried using <stdint.h>
, <cstdint>
<limits.h>
<climits>
nothing works! I am using gcc version: gcc version 6.3.0 (MinGW.org GCC-6.3.0-1)
if I am using INT32_MAX
the program works but I am not getting the desired output. What should I do?
the program below is Kadane's Algorithm
which gives the max possible sum of contiguous subarray as the answer.
- when using
INT32_MIN
I get the answer:132
(the output changes on every run idk how and why) Expected Ans:6
.
explanation: max possible sum of contiguous subarray : [4,-1,2,1]
= 6
.
- When Using
<climits>
I get the answer:54686
(output changes on every run idk how and why) and not6
I am preparing for competitive coding interviews what should I do if I face similar issues in my exams?
#include<iostream>
#include<climits>
#include<cstdint>
#include<stdint.h>
#include<limits>
using namespace std;
int main()
{
int a[] = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
int n = sizeof(a) / sizeof(a[0]);
int local_max = 0;
int global_max = INT_MIN;
for(int i=0; i<n; i++)
{
local_max = max(a[i], a[i] + local_max);
if(local_max > global_max)
{
global_max = local_max;
}
}
cout<<global_max<<endl;
return 0;
}
Aucun commentaire:
Enregistrer un commentaire