Posts

Solve a financial problem

Image
  Description Given a list of prices of a single stock for N number of days, find stock span for each day. Stock span is defined as a number of consecutive days prior to the current day when the price of a stock was less than or equal to the price at current day. For the first day, span is always 1. In the example we can see that for day 2 at 60, there is no day before it where the price was less than 60. Hence span is 1 again. For day 3, the price at day 2 (60) is less than 70, hence span is 2. Similarly, for day 4 and day 5. Remember days should be consecutive, that’s why the span for day 4 is 1 – even though there was a day 2 where the price was less than 65. Input Input Format First line contains an integer T specifying the number of test cases. First line of each test case contains the value of N Second line of each test case contains N space separated integers which are the stock price for N days Constraints N <= 1000000 Output Output Format Print the stock span for each d...

Understanding the role of Operating System

Image
How does a computer work? Operating System is the middle man between computer's hardware and the software applications that you run. Whenever users want to perform any actions like Listen Music, Creating Documents, Video calls, etc - they use applications like VLC player to watch videos, Excel for documents, Skype for video calling, etc. Operating system allocates the hardware resources like Ram, HDD  for that application. Operating System provides an interactive GUI through which user can perform their task.   What is a computer program? If someone knows Japanese, then he/she can only communicate with another person only if they also know Japanese. For communication, we need a common language through which I can put my request and others will interpret that. If a german person talking to a French person then it is not possible to communicate unless they have something known as a translator. Even the people who don't have any ability to speak, use sign language for commun...

Multer file upload and delete - Nodejs, Mongodb, Express

Multer is a node.js middleware  which is primarily used for uploading files. Dependencies     "express" : "^4.17.1" ,     "mongoose" : "^6.0.9" ,     "multer" : "^1.4.3 Multer doc :  https://www.npmjs.com/package/multer Middleware required : Path and Multer Folder to upload file and name of a file. const storage = multer . diskStorage ({     destination : function ( req , file , cb ) {         cb ( null , path . join ( __dirname , '../uploads' )) //upload file in uploads folder       },       filename : function ( req , file , cb ){         const uniquePrefix = Date . now () + '-' + Math . round ( Math . random () * 1E9 )         cb ( null , uniquePrefix + '-' + file . originalname )   //uploaded file name     } }) Filter file types  const fileFilter = ( req , file , cb ) => {     // The function sho...

Nodemailer setup on NodeJS Express MongoDB

"dependencies" : {     "dotenv" : "^10.0.0" ,     "express" : "^4.17.1" ,     "mongoose" : "^6.0.11" ,     "nodemailer" : "^6.7.0" SMTP Transport config/mail.js mailtrap.io is free email checker for developers. const nodemailer = require ( 'nodemailer' ) require ( 'dotenv' ). config () module . exports = nodemailer . createTransport ({     host : process . env . CURRENT_ENVIRONMENT == "development" ? "smtp.mailtrap.io" : "" ,     port : 587 ,     secure : false , // upgrade later with STARTTLS     auth : {       user : process . env . SMTP_USERNAME ,   //from .env       pass : process . env . SMTP_PASSWORD     },   }) Message configuration utils/sendmail.js const transporter = require ( '../config/mail' ) module . exports = (({ from , to , subject , text , html }) => {       var message = {         from ...

Maximise top

Description You are given a stack of N integers. In one operation, you can either pop an element from the stack or push any popped element into the stack. You need to maximize the top element of the stack after performing exactly K operations. If the stack becomes empty after performing K operations and there is no other way for the stack to be non-empty, print -1. Input Input Fomat The first line of input consists of two space-separated integers N and K. The second line of input consists N space-separated integers denoting the elements of the stack. The first element represents the top of the stack and the last element represents the bottom of the stack. Constraints N < 2000000 K < 10^9 Numbers of stack <10^18 (Note that numbers are very large) Output Output Format Print the maximum possible top element of the stack after performing exactly K operations. Sample Input 1  6 4 1 2 4 3 3 5 Sample Output 1 4

Can you find the sum

  Description Teena is very good at competitive programming, she solved enough problems on arrays and her friend wants to test her knowledge in arrays, so gave her following task : The problem is given an array A having N integers, for each element i (1 <= i <= N), find x+y  where x is the largest number less than i such that A[x]>A[i] and y is the smallest number greater than i such that A[y]>A[i]. If there is no x < i such that A[x]>A[i], then take x=−1. Similarly, if there is no y>i such that A[y]>A[i], then take y=−1. Input Input Format First line consists of a single integer denoting N. Second line consists of N space separated integers denoting the array A. Constraints 1 <= N <= 10^6 1 <= A[i] <= 10^9 Output Print N space separated integers, denoting x+y for each i Sample Input 1  5 5 4 1 3 2 Sample Output 1 -2 0 6 1 3 Hint Sample 1 Explanation Values of x for each i: -1,1,2,2,4 Values of y for each i: -1,-1,4,-1,-1 Answer :