Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions Domains/CompetitiveProgramming/Programs/C++/PoliceRecruits.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Problem: Police Recruits
Platform: Codeforces
Problem Code: 427A
Difficulty: Easy
Link: https://codeforces.com/problemset/problem/427/A

Problem Statement:
Given a sequence of events representing crimes and police recruitments:
- Each -1 represents a crime.
- Each positive number represents the number of police officers recruited.
When a crime occurs and there are no available officers, it goes untreated.
Find the total number of untreated crimes.

Example:
Input:
7
-1 -1 1 1 -1 -1 1

Output:
2

Approach:
1. Traverse through the list of events.
2. Maintain a count of active police officers.
3. If a crime occurs and no officer is available, increase the untreated crimes count.
4. Otherwise, assign one available officer to handle the crime.

Time Complexity: O(n)
Space Complexity: O(1)

Contributor: Paila-Sahitya

*/

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

int policeRecruits(int n, vector<int> &arr){
int crimeCount=0;
int activePolice=0;
for(int i=0;i<n;i++){
if(arr[i]==-1 && activePolice==0) crimeCount++;
else if(arr[i]==-1){
activePolice--;
}
else activePolice+=arr[i];
}
return crimeCount;
}

int main(){
int n;
cin>>n;
vector<int> arr(n);
for(int i=0;i<n;i++){
cin>>arr[i];
}
cout<<policeRecruits(n, arr)<<endl;
return 0;
}
60 changes: 60 additions & 0 deletions Domains/CompetitiveProgramming/Programs/C++/Twins.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Problem: Twins
Platform: Codeforces
Problem Code: 160A
Difficulty: Easy
Link: https://codeforces.com/problemset/problem/160/A

Problem Statement:
Two brothers have a collection of coins of different values.
The goal is to find the minimum number of coins one brother must take
so that the total value of his coins becomes strictly greater than the
total value of the remaining coins.

Example:
Input:
4
3 3 4 3
Output:
2

Approach:
1. Sort the coins in descending order.
2. Keep picking the largest coins until the sum of selected coins > sum of remaining coins.
3. Return the number of coins picked.

Time Complexity: O(n log n)
Space Complexity: O(1)

Contributor: Paila-Sahitya
*/

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

int minimumCoins(int n, vector<int>&arr){
sort(arr.begin(), arr.end(), greater<int>());
int total=0;
for(int i=0;i<n;i++){
total+=arr[i];
}
int sum=0;
int count=0;
for(int i=0;i<n;i++){
sum+=arr[i];
count++;
if(sum>total-sum) return count;
}
return count;
}

int main(){
int n;
cin>>n;
vector<int> arr(n);
for(int i=0;i<n;i++){
cin>>arr[i];
}
cout<<minimumCoins(n, arr)<<endl;
return 0;
}
Loading