You are on page 1of 6

DOMAIN: SEARCHING AND SORTING

Experiment – 1.4

Student Name:ROHIT MOHAN UID: 20BCS7358


Branch: BE-CSE Section/Grou604-B
Semester: 5TH Subject Code: 20CSP-314
Subject Name: Competitive Coding

AIM/OVERVIEW OF THE PRACTICAL:


Fraudulent Activity Notifications

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.

STEPS FOR EXPERIMENT/PRACTICAL/CODE:


int get(vector<int>&am, int t, bool mid){
int acum=0; for(int
i=0;i<=200;i++){acum+=a
m[i]; if(acum>=t){ if(!
mid)retur n 2*i; if(acum>t)
return
2*i; for(int j=i+1;j<=200;j++) if(am[j]) return
i+j; }
}
assert(0);
}

int activityNotifications(vector<int> v, int d) {


intans=0,n=v.size();
vector<int>am(201);
for(int
i=0;i<n;i++){ if(i>=d)
{ intm=get(am,(d+1)/2,d%2==0);
ans += m<=v[i];
am[v[i-d]]--;
}
am[v[i]]++;
} return
ans;

OBSERVATIONS/DISCUSSIONS/ COMPLEXITY ANALYSIS:


Time complexity will be O(n^2).
Space complexity will be O(n).

RESULT/OUTPUT/WRITING SUMMARY:
LEARNING OUTCOMES (WHAT I HAVE LEARNT):
1. We have learned to use vector.

2. We have learned the concept of sorting.

3. We have learnt to find time complexity of array.

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 :

• Sort the original array.


• Iterate from the first element and calculate the number to find using the formula
(Element in array)−(Number to find)=target value
• Since the array is sorted, use binary search to find the element in the sorted array.
• If we find the element, that means we can form a pair, and increment the result count.
• Repeat steps 2-4 for every element of the array.
• Return the result.
STEPS FOR EXPERIMENT/PRACTICAL/CODE:

bool binarySearch(vector<int>arr, int numberToFind) {

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) {

sort (arr.begin(), arr.end());

int result = 0;
for (int i :arr) {
int numberToSearch = i - k;
if (binarySearch(arr, numberToSearch)) {result+
+;
}
}

return result;
}

OBSERVATIONS/DISCUSSIONS/ COMPLEXITY ANALYSIS:


Time Complexity: O(n * log n) [Since we are sorting the array]
Space Complexity: O(1)

RESULT/OUTPUT/WRITING SUMMARY:
LEARNING OUTCOMES (WHAT I HAVE LEARNT):
1. We have learned to use vector.

2. We have learned the concept of sorting.

3. We have learnt to find time complexity of array.

You might also like