Skip to content

effecient and easy to understand code #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
139 changes: 139 additions & 0 deletions Adhoc Problems/winning-Strategy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
The college team, along with their coach, is going to the sports fest to play a football match. There are n players in the team, numbered from 1 to n.
Someone gives a paper to the coach. The paper elaborates on the positions and strategies of the opponent team. Based on it, the coach creates a winning strategy. In that strategy, he decides and gives a particular position to every player.
After this, the coach starts swapping two players at a time to make them stand according to new positions decided on paper.
He swaps players by applying following rules:
1. Any player can swap with the player standing next to him.
2. One player can swap with at most two other players.
Given that initially all the players are standing linearly, numbered from 1 to n, you have to tell whether it is possible for the coach to create new positions by swapping within the constraints defined in the task.
Input Format
The first line of input will contain an integer, that denotes the value of the number of test cases. Let us denote the number of test cases by the symbol T.
Each of the following T test cases consists of two lines. The first line of each test case contains an integer n, that denotes the number of players in the team. The following line contains n space separated integers, denoting the specific position of players in winning strategy.
Output Format
For each test case, if it is possible to create winning strategy positions, then print "YES" (without quotes) and in the next line, print the minimum numbers of swaps required to form the winning strategy order, otherwise print "NO"(without quotes) in a new line.
Constraints
1 <= T <= 50
1 =< N <= 10^5
1 <= A[i] <= n
Time Limit: 1 second
Sample Input 1:
1
5
2 1 5 3 4
Sample Output 1:
YES
3
Explanation
In this case, we can achieve winning strategy positions in 3 swaps. Initial state of positions: 1 2 3 4 5
Three moves required to form winning strategy positions:
1 2 3 4 5 -> 1 2 3 5 4 -> 1 2 5 3 4 -> 2 1 5 3 4
Sample Input 2:
1
5
2 5 1 3 4
Sample Output 2:
NO
Explanation:
In the second case, there is no way to form the specific winning strategy positions by swapping within the constraints mentioned in the task.
*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
ll t;
cin>>t;
while(t--){
ll n;
cin>>n;
ll arr[n]={0};
ll flag=0;
for(int i=0;i<n;i++){
cin>>arr[i];
if(abs(arr[i]-(i+1))>2)
flag=1;
}
if(flag==1){
cout<<"NO"<<endl;
continue;
}
bool gh=0;
ll count=0;

for(int i=0;i<n-1;i++){
if(arr[i]>arr[i+1]){
swap(arr[i],arr[i+1]);
count++;
}
}


for(int i=0;i<n-1;i++){
if(arr[i]>arr[i+1])
{
gh=1;
break;
}
}
if(gh==1){
cout<<"NO"<<endl;
}
else{
cout<<"YES"<<endl<<count<<endl;
}

}
return 0;

}
//Code
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main() {

// Write your code here
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){

cin>>a[i];

}
int count =0;
for(int i=0;i<(n-1);i++){

if(a[i+1]<a[i]){

swap(a[i+1],a[i]);
count++;

}



}
ll gh =1;
for(int i=0;i<(n-1);i++)
{

if(a[i+1]<a[i]){

gh=0;
break;
}


}

if(gh==0)
{
cout<<"NO";

}
else{
cout<<"YES"<<endl;
cout<<count;

}
}
66 changes: 66 additions & 0 deletions DP and BitMasking/StringMaker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*According to Ancient Ninjas , string making is an art form . There are various methods of string making , one of them uses previously generated strings to make the new one . Suppose you have two strings A and B , to generate a new string C , you can pick a subsequence of characters from A and a subsequence of characters from B and then merge these two subsequences to form the new string.
Though while merging the two subsequences you can not change the relative order of individual subsequences. What this means is - suppose there two characters Ai and Aj in the subsequence chosen from A , where i < j , then after merging if i acquires position k and j acquires p then k<p should be true and the same apply for subsequence from C.
Given string A , B , C can you count the number of ways to form string C from the two strings A and B by the method described above. Two ways are different if any of the chosen subsequence is different .
As the answer could be large so return it after modulo 10^9+7 .
Input Format :
First line will contain T(number of test cases), each test case consists of three lines.
Line 1 : String A
Line 2 : String B
Line 3 : String C
Output Format :
The number of ways to form string C for each test case in new line.
Constraints :
1 <= T <= 500
1 <= |A| , |B|, |C| <=50
Sample Input :
1
abc
abc
abc
Sample Output :
8
*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int mod=1e9+7;
ll solver(string &a,string &b,string &c,ll x,ll y,ll z,ll dp[][51][51]){
if(z==0){
return 1;
}
if(x<=0 && y<=0){
return 0;
}
if(dp[x][y][z]!=-1){
return dp[x][y][z];
}
int ways=0;
for(int i=x-1;i>=0;i--){
if(a[i]==c[z-1])
ways=(ways+solver(a,b,c,i,y,z-1,dp))%mod;
}
for(int j=y-1;j>=0;j--){
if(b[j]==c[z-1])
ways=(ways+solver(a,b,c,x,j,z-1,dp))%mod;
}
dp[x][y][z]=ways;
return dp[x][y][z];
}
ll solve(string a,string b,string c){
ll lena=a.length();
ll lenb=b.length();
ll lenc=c.length();
ll dp[51][51][51];
memset(dp,-1,sizeof(dp));
return solver(a,b,c,lena,lenb,lenc,dp);
}
int main(){
int t;
cin>>t;
while(t--){
string a,b,c;
cin>>a>>b>>c;
cout<<solve(a,b,c)<<endl;
}
return 0;
}
81 changes: 81 additions & 0 deletions Dynamic Programming - 2/MehtaAndBankRObbery.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
One fine day, when everything was going good, Mehta was fired from his job and had to leave all the work. So, he decided to become a member of gangster squad and start his new career of robbing. Being a novice, mehta was asked to perform a robbery task in which he was given a bag having a capacity W units.
So, when he reached the house to be robbed, there lay N items each having particular weight and particular profit associated with it. But, theres a twist associated,
He has first 10 primes with him, which he can use atmost once, if he picks any item x, then he can multiply his profit[x] with any of the first 10 primes and then put that item into his bag.
Each prime can only be used with one particular item and one item can only have atmost one prime multiplied with its profit. Its not necessary to pick all the items.
If he doesnt want to use a prime with any particular item, he can simply add the profit as it is, more specifically, 1*profit[x] for xth item will get added to its total profit, and that he can do with as many items as he wants.
He cannot fill his bag more than weight W units. Each item should be picked with its whole weight, i.e it cannot be broken into several other items of lesser weight.
So, now to impress his squad, he wishes to maximize the total profit he can achieve by robbing this wealthy house.
Input Format :
The first line of input will contain T(number of test cases), each test will follow as.
First Line will contain two integers N and W (number of items and maximum weight respectively).
Second-line will contain N space-separated integers denoting the profit associated with the Ith item.
The third line will contain N space-separated integers denoting the weight of the Ith item.
Constraints:
1 <= T <= 20
1 <= N, W <= 500
1 <= profit[i] <= 10^4
1 <= weight[i] <= 10^4
Output the maximum profit obtainable for each test case in a new line.
Sample Input:
1
7 37
33 5 14 14 16 25 15
5 19 30 4 15 31 25
Sample output:
1591
*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
int t;
cin>>t;
while(t--){
ll n,wi;
cin>>n>>wi;
pair<ll,ll> *arr=new pair<ll,ll>[n];
for(int i=0;i<n;i++){
cin>>arr[i].first;
}
for(int i=0;i<n;i++){
cin>>arr[i].second;
}
sort(arr,arr+n);
ll prime[11]={0,2,3,5,7,11,13,17,19,23,29};
ll ***dp=new ll**[2];
for(int i=0;i<2;i++){
dp[i]=new ll *[n+1];
for(int j=0;j<=n;j++){
dp[i][j]=new ll[wi+1];
for(int k=0;k<=wi;k++){
dp[i][j][k]=0;
}
}
}
//base case
for(int i=1;i<=n;i++){
for(int j=1;j<=wi;j++){
dp[0][i][j]=dp[0][i-1][j];
if(j>=arr[i-1].second){
dp[0][i][j]=max(dp[0][i][j],dp[0][i-1][j-arr[i-1].second]+arr[i-1].first);
}
}
}
//follow-up
int curr=0;
for(int p=1;p<=10;p++){
curr=p%2;
for(int i=1;i<=n;i++){
for(int j=1;j<=wi;j++){
dp[curr][i][j]=dp[curr][i-1][j];
if(j>=arr[i-1].second)
dp[curr][i][j]=max(dp[curr][i][j],max(dp[curr][i-1][j-arr[i-1].second]+arr[i-1].first,dp[curr^1][i-1][j-arr[i-1].second]+arr[i-1].first*prime[p]));
}
}
}
cout<<dp[0][n][wi]<<endl;
}

return 0;
}