# Breaking the Records

# Breaking the Records

lahouari + 4 comments My java submission:

static int[] getRecord(int[] s) { int highest, lowest; highest = lowest = s[0]; int[] result = new int[2]; for (int s_i = 1; s_i < s.length; s_i++) { if (s[s_i] > highest) { highest = s[s_i]; ++result[0]; } else if (s[s_i] < lowest) { lowest = s[s_i]; ++result[1]; } } return result; }

JulioLeme + 1 comment Show, my solution is very like with yours, nice to see the similarities, see it:

`static int[] getRecord(int[] s){ int[] result = new int[2]; int countMax=0, countMin=0 ,max = s[0], min = s[0]; for(int i=1; i< s.length; i++){ if(s[i] > max){ max = s[i]; countMax++; } if(s[i] < min){ min = s[i]; countMin++; } } result[0] = countMax; result[1] = countMin; return result; }`

work30 + 1 comment "else if" is slightly better than if, if because both cannot be true Also pre-increment slightly better than post-increment I think?

HTH

enricogiurin + 0 comments or you can add a

*continue*after the first*if*

alphazygma + 0 comments Heh heh heh, my solution is very similar to yours and Julio's

static int[] getRecord(int[] s){ if (s.length == 0) { return new int[2]; } int lowest = s[0]; int highest = s[0]; int lowestCount = 0; int highestCount = 0; for (int score : s) { if (score < lowest) { lowest = score; lowestCount++; } else if (score > highest) { highest = score; highestCount++; } } return new int[]{highestCount, lowestCount}; }

hiljusti + 2 comments Mine (in JS) was very similar:

function getRecord(games) { let most = games[0]; let least = games[0]; let dMost = 0; let dLeast = 0; for (const score of games) { if (most < score) { most = score; dMost++; } else if (score < least) { least = score; dLeast++; } } return [dMost, dLeast]; }

armadillo_ko167 + 0 comments it's min (in js)

function breakingRecords(score) { const scoreLength = score.length; let mostScore = score[0]; let worstScore = score[0]; let mostScoreIncreased = 0; let worstScoreDecreased = 0; for(let i = 0; i < scoreLength; i++){ const curScore = score[i]; if(mostScore < curScore){ mostScore = curScore; mostScoreIncreased++; } if(worstScore > curScore){ worstScore = curScore; worstScoreDecreased++; } } return [mostScoreIncreased, worstScoreDecreased]; }

anishd19 + 0 comments [deleted]

cu_16bcs2416 + 1 comment you could have used break in first if stmt as is would reduce time complexity!

enricogiurin + 0 comments You mean a

*continue*, not a*break*.

dainaburnwood + 1 comment The code below works in all other test cases but

**show runtime error in test case#7**,i don't know why.**Please guide me.**int* breakingRecords(int score_count, int* score,int* result_count) { int max, min, up = 0, down = 0, itr; static int result[2]; max = min = score[0]; for(itr = 1; itr < score_count; itr++) { if(score[itr] > max) { max = score[itr]; up++; } else if(score[itr] < min) { min = score[itr]; down++; } } result[0] = up; result[1] = down; *result_count = 2; return result; }

benjamin_zhao_11 + 0 comments disable line in the for loop of main: *if (scoreitemendptr == scoreitemstr || scoreitemendptr != '\0') { exit(EXITFAILURE); }

I found that the last int of case 7 in string format is "99811Q", which means the last char is not '\0'. (note: 99811 in int, which is correct.) So the statement check above exit with failure error.

madhudskumar + 6 comments def getRecord(scores): ls = hs = s[0]; lc = hc = 0; for score in scores: if(score > hs): hs = score; hc += 1; if(score < ls): ls = score; lc += 1; return [hc, lc]

This worked

esyray15 + 0 comments You could have just wrote the

`return`

statment as`return hc, lc`

david_mathis_ii + 0 comments why iterate through s[0] if you already assigned it? you could shave this like:

fore score in scores[1:]:

asd_shivam98 + 0 comments # include

using namespace std; int main() { int n,s[n]; cin >> n; cin >> s[0]; int c=0,d=0; int a=s[0]; int b=s[0]; for(int i=1;i> s[i]; if(s[i]>a) { a=s[i]; c++; } else if(s[i] cout << c << " " << d; return 0; }

maxim_marquez + 1 comment My code is almost similar to yours. But there are apparently problems with mine. Could you please point them out?

n = int(raw_input().strip()) score = list(map(int, raw_input().strip().split(' '))) hb, lb = 0, -1 a = score[0] for b in score: if (b>a): a = b hb +=1 for c in score: if (c<=a): a = c lb +=1 print hb, print lb

lrkoehler77 + 0 comments Your not returning correctly. Do it like:

**return hb,lb**

chinmay43 + 2 comments My logic is almost same, But this code is giving runtime error in test case 10. what can be the issue ?

def breakingRecords(score): min_1 = score[0] max_1 = score[0] min_brk = 0 max_brk = 0 if len(score) < 2: return 0 for i in range(len(score)): if (score[i] < min_1): min_brk += 1 min_1 = score[i] elif(score[i] > max_1): max_brk += 1 max_1 = score[i] else: max_brk = max_brk min_brk = min_brk i += 1 return(max_brk, min_brk)

tylercjulian + 1 comment I just tested your code. The issue is with this line:

if len(score) < 2: return 0

Remove that line and your code passes the tests.

Also, for efficiency, there is no need for the

`i += 1`

, since the for loop automatically increments i. You could also iterate directly through the scores instead of having to rely on using a range for index numbers. The parent example uses this method.chinmay43 + 1 comment Thanks a lot :-)

tylercjulian + 0 comments You are welcome :)

sshayann7 + 0 comments [deleted]

mumbaikar + 0 comments We can do directly in main function -

`public static void main(String[] args) { int min_record = 0; int max_record = 0; int worst = 0; int best = 0; int curr_record = 0; Scanner in = new Scanner(System.in); int n = in.nextInt(); for(int s_i=0; s_i < n; s_i++){ curr_record = in.nextInt(); if(s_i == 0) { min_record = curr_record; max_record = curr_record; } if(s_i > 0) { if(curr_record < min_record) { min_record = curr_record; worst++; } else if (curr_record > max_record) { max_record = curr_record; best++; } } } System.out.println(best + " " + worst); System.out.println(""); in.close(); }`

sgawlik + 5 comments I haven't found a C solution that actually uses the code as it is given. I wanted something that doesn't change any of the code but still returns the result in the format the author intended. If you follow my comments you can see how to pass values between functions in C. I hope this helps people who were also unsatisfied with solutions that were written entirely in the main function or those who've wondered what to do about result_size in the C starter code of so many of these early exercises.

#define HOWMANYRESULTS 2 int* getRecord(int s_size, int* s, int *result_size) { // set best and worst to first game int worst = s[0]; int best = s[0]; // let main know how many results you'll print *result_size = HOWMANYRESULTS; // allocate heap space for the results and initialize to 0 int *result = malloc(sizeof(int) * *result_size); result[0] = 0; result[1] = 0; // start loop from the second game for (int i = 1; i < s_size; i++) { if (s[i] > best) { // increase counter for best and reset best result[0]++; best = s[i]; } if (s[i] < worst) { // increase counter for worst and reset worst result[1]++; worst = s[i]; } } // return the address of the results on heap to main return result; }

I'm guessing that other people have also struggled with the common format for all C starter code so I have added some comments to the part of main that I found tricky:

// how many things will be printed int result_size; // call helper function, expect implementer to return // an int pointer (to the results) and set result_size int* result = getRecord(n, s, &result_size); for(int i = 0; i < result_size; i++) { // when i = 0 this evaluates to 0 which is same as // false and we don't print a space if (i) { printf(" "); } // on every round through the loop print another result printf("%d", result[i]); }

I know that allocating space on the heap is not as efficient/fast in the real world as just doing it all in main or printing in getRecord, my focus was on using the starter code and practicing with passing values between functions in C. Solution is O(n) runtime and O(1) space.

lee_rw_cs + 0 comments I like your code comment, I always forget to write documentation, sigh

rajputnisarg17 + 0 comments thanks

prusovigor + 0 comments You can also declare result as a static variable instead of using malloc, this will intialize it with zeros and it will still be accessible after return from function.

AzizStark + 0 comments int main(){ long int arra[2000],n,i,j,haa=0,great=0; scanf("%ld",&n); for(i=0;i<n;i++){ scanf("%ld", &arra[i]); } //========================================= great=arra[0]; for(i=0;i<n;i++){ for(j=1;j<n+1;j++){ if(great<arra[j]) { great=arra[j]; haa=haa+1; } } } printf("%ld", haa); //========================================= haa=0; great=arra[0]; for(i=0;i<n;i++){ for(j=0;j<n;j++){ if(great>arra[j]) { great=arra[j]; haa=haa+1; } } } printf("% ld", haa); return 0; }

Joe_08 + 0 comments Hello sgawlik! Could please explain how *result_size gets the value HOWMANYRESULTS? and also what is the purpose of doing so?....Thank you for your clear solution using C.

ser_zhile + 0 comments Hi guys, here is mine JS solution:

let hiBreak = 0; let lowBreak = 0; score.reduce((agg, cur)=> { if(cur > agg[0]) { ++hiBreak; agg[0] = cur; } else if(cur < agg[1]){ ++lowBreak; agg[1] = cur; } return agg; },[score[0], score[0]]) return [hiBreak, lowBreak];

hellomici + 0 comments simple Python2

def breakingRecords(lst): highest = len(set([max(lst[:(num+1)]) for num, i in enumerate(lst) if lst.index(max(lst[:(num+1)])) != 0])) lowest = len(set([min(lst[:(num+1)]) for num, i in enumerate(lst) if lst.index(min(lst[:(num+1)])) != 0])) return highest, lowest

dcngenmail + 2 comments My Python 3 submission:

def breakingRecords(scores): rlow = scores[0] rhigh = scores[0] lowBreaks = 0 highBreaks = 0 for x in scores: if x < rlow: rlow = x lowBreaks += 1 elif x > rhigh: rhigh = x highBreaks += 1 return str(highBreaks)+str(lowBreaks)

It worked for all except the Testcases 5-8. What might be wrong?

network_coder + 0 comments for me same logic , but i skipped the first elemet in result as its already assigned to rlow and rhigh .

`for i in range(1,len(scores)):`

nishadganesh254 + 0 comments def breakingRecords(scores): max = min = scores[0] result = [0,0] for i in range(len(scores)): if scores[i] > max: max = scores[i] result[0] += 1 elif scores[i] < min: min = scores[i] result[1] += 1 return result

Lunaabel9 + 0 comments Here's my C++ version:

vector<int> breakingRecords(vector<int> score) { int min=score[0]; int max=score[0]; vector<int> myreturn = {0,0}; for(int i=1;i<score.size();i++){ if(score[i]>max){ myreturn[0]+=1; max = score[i]; } else if(score[i]<min){ myreturn[1]+=1; min = score[i]; } } return myreturn; }

Sort 412 Discussions, By:

Please Login in order to post a comment