From 66e4183a9fc3437f20d20ddf83f2aba495ea3f31 Mon Sep 17 00:00:00 2001 From: sudiptarathi2020 Date: Fri, 17 Jan 2025 01:02:48 +0600 Subject: [PATCH] Added English tutorial for 1093 #512 --- 1093/en.md | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 1093/en.md diff --git a/1093/en.md b/1093/en.md new file mode 100644 index 00000000..c79aa073 --- /dev/null +++ b/1093/en.md @@ -0,0 +1,88 @@ + +## [LightOj-1093](https://lightoj.com/problem/ghajini) +## Problem Summary +Amir is playing a game called "Find Max Difference". But he can only remember numbers for a short period of time, that is `d` milliseconds. +The game involves: + +- A sequence of integers appearing one at a time, for 1 millisecond each. +- The goal is to calculate the maximum difference between any two integers that Amir can remember(i.e., within the last `d` milliseconds). +- Input Constrains + * T <= 5 (Number of test case) + * 2 <= n <=10^5 (Total number of integers the screen shows) + * 1 <= d <= n (Amir memory duration) + * Sequence values: 0 <= a[i] <= 10^8 + + + + + +## Solution + +- We will iterate the array. In each index we compare the minimum and maximum numbers from the last `d` integers. Amir remembers to maximize the difference and stores the max difference in a variable. +- A naive approach of iterating through the last `d` numbers for every index would be too slow((O(n * d)). +- We can efficiently find the minimum and maximum of the last `d` numbers using sparse table or segment tree. Then the complexity would be O(logn). + +### C++ code using sparse table + +```c++ +#include +using namespace std; +const int maxN = 1e5+1; +const int maxLog = 20; +int stMax[maxN][maxLog]; +int stMin[maxN][maxLog]; +int lg[maxN]; +void init(int arr[],int n){ + for(int i=0;i>tcs; + for(int tc=1;tc<=tcs;tc++){ + int n,d; + cin>>n>>d; + int arr[n]; + for(int i=0;i>arr[i]; + } + init(arr,n); + int ans = 0; + for(int i=0;i