Coding tips! (Helpful for contests, interviews and OAs) How to code...

@i_pranavmehta
Pranav Mehta@i_pranavmehta
34 views Feb 15, 2024 ~1 min read
Advertisement
1
Coding tips!
(Helpful for contests, interviews and OAs)

How to code faster (part 1)

Use these STL🧵
2
accumulate(): utilise this function rather than doing summation using a loop, it can also be modified to perform other operations like multiplication, xor etc by providing the relevant function as a parameter).
Media image
3
iota(): several times the problem requires us to have an array of the type arr[i]=i, mostly people populate the array elements using a loop, however iota can do this in a single function. iota() takes 3 args i.e. begin, end iters & starting value for e.g. 0 in the above example.
4
partial_sum(): how do you calculate prefix_sum? Using loops? Well just use partial_sum! It would take 3 args i.e. beg and end of the i/p container and the iterator for the o/p. Time saver right?
5
Some more important stl, wherein the usecase is self explanatory: min_element(), max_element(), count(), find(), lower_bound(), upper_bound(), next_permutation(), unique().
6
Knowing these stl function isnt hard, but it needs practice to get used to! These are absolute time savers and helps you code clean! I have personally used these in contests, OAs and interviews!

Lmk if there are any doubts! Hope this post helps!🙌
Actions
What You Can Do
  • Export as PDF or Markdown
  • Batch Export to Notion
  • Bookmark & Highlight
  • Screenshot Tweet
Create Free Account

Includes 7-day Premium trial

Advertisement