[go: up one dir, main page]

0% found this document useful (0 votes)
71 views7 pages

Experiment 4

The document summarizes 4 coding challenges completed by the student. It includes the student name and details, links to coding challenges on HackerRank, code solutions in C++, and expected output for each challenge. The challenges addressed fraudulent activity notifications, missing numbers, minimum loss, and pairs.

Uploaded by

Aniket Sugara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views7 pages

Experiment 4

The document summarizes 4 coding challenges completed by the student. It includes the student name and details, links to coding challenges on HackerRank, code solutions in C++, and expected output for each challenge. The challenges addressed fraudulent activity notifications, missing numbers, minimum loss, and pairs.

Uploaded by

Aniket Sugara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Experiment – 4

Student Name: Aniket Sugara UID: 20BCS7284


Branch: BE-CSE Section/Group: 619-A
Semester: 5 Date of Performance: 29/08/22
Subject Name: Competitive Coding – I Subject Code: 20CSP-312

Q1. Fraudulent Activity Notifications


Link: https://www.hackerrank.com/challenges/fraudulent-activity-
notifications/problem?isFullScreen=true

• Code:
#include <bits/stdc++.h>

using namespace std;

long long median(long long count[], long long d){


long long countfreq[201];
for(long long i = 0; i < 201; i++){
countfreq[i] = count[i];
}
for(long long i = 1; i < 201; i++){
countfreq[i]+=countfreq[i-1];
}
if(d % 2 == 0){
long long a, b;
for(long long i = 0; i < 201; i++){
if(countfreq[i] >= (d/2)+1){
a = i;
break;
}
}
for(long long i = 0; i < 201; i++){
if(countfreq[i] >= (d/2)){
b = i;
break;
}
}
return a+b;
}
else{ //else median is = a*2;
for(long long i = 0; i < 201; i++){
if(countfreq[i] >= (d/2)+1){
return i*2;
}
}
}
return -1;
}

int main(){
long long n, d, b, ans = 0, count[201];
cin >> n >> d;
vector<long long> e;
for(long long i = 0; i < n; i++){
long long b;
cin >> b;
e.push_back(b);
}
for(long long i = 0; i < 201; i++){
count[i] = 0;
}
for(long long i = 0; i < d; i++){
count[e[i]]++;
}
for(long long i = d; i < n; i++){
ans+=(e[i]>=median(count,d));

count[e[i-d]]--;
count[e[i]]++;
}
cout << ans;
return 0;
}
• Output:
Q2. Missing Numbers
Link: https://www.hackerrank.com/challenges/missing-numbers/problem?isFullScreen=true

• Code:
#include <bits/stdc++.h>
using namespace std;

int main() {

long long n,m,temp;


cin>>n;
vector<int> a;
for(long long i=0;i<n;i++) {
cin >> temp;
a.push_back(temp);
}
cin>>m;
vector<int> b;
for(long long i=0;i<m;i++){
cin >> temp;
b.push_back(temp);
}
sort(a.begin(),a.end());
sort(b.begin(),b.end());
long long i=0,j=0;
while(i<n && j<m){
if(a[i]==b[j]) {
i++;j++;
b[j-1]=0;
}
else if(a[i]>b[j])j++;
else i++;
}
set<int> st;
for(i=0;i<m;i++) {
if(b[i]!=0) st.insert(b[i]);
}
for(set<int>::iterator it = st.begin();it!=st.end();it++){
cout<<*it<<" ";
}
cout<<endl;
return 0;
}

• Output:
Q3. Minimum Loss
Link: https://www.hackerrank.com/challenges/minimum-loss/problem?isFullScreen=true

• Code:
int minimumLoss(vector<long> price) {
unordered_map<long,int>m;
long minn=INT64_MAX;
long num;
for(int i=0;i<price.size();i++){
m[price[i]]=i;
}
sort(price.begin(),price.end(),greater<long>());
for(int i=0;i<price.size()-1;i++){
if(m[price[i]]<m[price[i+1]]){
num=price[i]-price[i+1];
if(num<minn)minn=num;
}
}
return (minn);
}

• Output:
Q4. Pairs
Link: https://www.hackerrank.com/challenges/pairs/problem?isFullScreen=true

• Code:
int pairs(int k, vector<int> arr) {
int total = 0;
sort(arr.begin(), arr.end());
for(int i = 0; i < arr.size(); i++) {
if(binary_search(arr.begin(), arr.end(), (k+arr[i])))
total++;
}
return total;
}

• Output:

You might also like