Professional Documents
Culture Documents
Experiment – 1.4
TASK TO BE DONE:
Given the number of trailing days d and a client's total daily expenditures for a period of n days, determine the
number of times the client will receive a notification over all n days.
Example
expenditure = [10,20,30,40,50]
d=3;
On the first three days, they just collect spending data. At day 4, trailing expenditures are [10,20,30] . The
median is 20 and the day's expenditure is 40. Because 40>=2*20 , there will be a notice. The next day, trailing
expenditures are [20,30,40] and the expenditures are 50 . This is less than 2*30 so no notice will be sent. Over
the period, there was one notice sent.
ALGORITHM/FLOWCHART :
• Take an empty vector named as am
• Insert element into am vector from the expenditure vector as per the value of d.
• Sort the vector am
• Find median of the element of am
• Check the required condition
• Pop the first element of the am vector and push next element from the expenditure vector at a perfect
position in am vector to maintain it in ascending order.
• Again find median and repeat the process until we get desired outcome.
RESULT/OUTPUT/WRITING SUMMARY:
LEARNING OUTCOMES (WHAT I HAVE LEARNT):
1. We have learned to use vector.
PROBLEM -2
AIM/OVERVIEW OF THE PRACTICAL:
Find pairs of array element that have a difference equal to the target value.
TASK TO BE DONE:
Given an array of integers and a target value, determine the number of pairs of array elements that have a
difference equal to the target value. Example k=1
Arr=[1,2,3,4]
There are three values that differ by k=1 : 2-1 =1 ,3-2=1 , and 4-3=1 .
Return 3 .
ALGORITHM/FLOWCHART :
int left = 0;
int right = arr.size() - 1;
while (left <= right) { intmid =
left + (right - left) / 2; if
(arr[mid] == numberToFind)
return true; if (arr[mid]
<numberToFind)
left = mid + 1;
else
right = mid - 1;
} return false; }int pairs(int
k, vector<int>arr) {
int result = 0;
for (int i :arr) {
int numberToSearch = i - k;
if (binarySearch(arr, numberToSearch)) {result+
+;
}
}
return result;
}
RESULT/OUTPUT/WRITING SUMMARY:
LEARNING OUTCOMES (WHAT I HAVE LEARNT):
1. We have learned to use vector.