***PBT CODING ONLY ***
Problem 1:
Problem Statement
The stock span problem is a financial problem where we have a series of n daily price quotes for a
stock and we need to calculate the span of stock’s price for all n days. The span Si of the stock’s
price on a given day i is defined as the maximum number of consecutive days just before the given
day, for which the price of the stock on the current day is less than or equal to its price on the
given day.
Input Format
First line contains n-the number of days. Next line contains n integers-the stock price of each day.
Constraints
1 ≤ N ≤ 10^5 1 ≤ Stock Prices ≤ 10^5
Output Format
Print an array of length N denoting the span for the i-th day.
C++ Program:
#include <bits/stdc++.h>
using namespace std;
void calculateSpan(int price[], int n, int S[])
{
S[0] = 1;
for (int i = 1; i < n; i++)
{
S[i] = 1;
for (int j = i - 1; (j >= 0) &&
(price[i] >= price[j]); j--)
S[i]++;
}
}
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
int main()
{
int n;cin>>n;int price[n];
for(int i=0;i<n;i++)
{
cin>>price[i];
}
int S[n];
calculateSpan(price, n, S);
printArray(S, n);
return 0;
}
Problem 2:
Problem Statement
Given a square chessboard, the initial position of Knight and position of a target. Find out the
minimum steps a Knight will take to reach the target position.
Note: The initial and the target position co-ordinates of Knight have been given accoring to 1-base
indexing.
Input Format
First line of input contains N-the size of NxN chessboard. Next line of input contains two space
separated integer the x coordinate and y coordinate respectively of knight's position at chessboard
Next line of input contains two space separated integer the x coordinate and y coordinate
respectively of target's position at chessboard.
Constraints
1 <= N <= 1000
1 <= Knight_pos(X, Y), Targer_pos(X, Y) <= N
Output Format
Print the minimum number of steps required by the knight to reach from its current position to the
given target position.
Post a Comment