Competitve Programming
Competitve Programming
Ans [Link]
n = 11: There are two 5s and three 2s in prime factors of 11! (2 8 * 34 * 52 * 7).
So count of trailing 0s is 2.
We can easily observe that the number of 2s in prime factors is always more than or
equal to the number of 5s. So if we count 5s in prime factors, we are done. How to
count total number of 5s in prime factors of n!? A simple way is to calculate
floor(n/5). For example, 7! has one 5, 10! has two 5s. It is done yet, there is one
more thing to consider. Numbers like 25, 125, etc have more than one 5. For example
if we consider 28!, we get one extra 5 and number of 0s become 6. Handling this is
simple, first divide n by 5 and remove all single 5s, then divide by 25 to remove
extra 5s and so on. Following is the summarized formula for counting trailing 0s.
#include <iostream>
using namespace std;
int findTrailingZeros(int n)
{
// Initialize result
int count = 0;
return count;
}
int main()
{
int n = 100;
cout << "Count of trailing 0s in " << 100
<< "! is " << findTrailingZeros(n);
return 0;
}
--------------------------------------------------
[Link]
questions-du1089cq6
int F(int x)
{
//We can always place the first cow in the leftmost stall
int cowsplaced=1,lastpos=pos[0];
for(int i=1;i<N;i++)
{
if(pos[i]-lastpos>=x)
{
//We are at least x away from last placed cow
//So we can place one here
if(++cowsplaced==C)return 1;
lastpos=pos[i];
}
}
return 0;
}
sort(pos,pos+N);
//Invariant : F(start) is always 1, F(end) is always 0
int start=0,end=pos[N-1]-pos[0]+1,mid;
while(end-start>1)
{
mid=(end+start)>>1;
(F(mid)?start:end)=mid;
}
return start;
-------------------------------------------------------------
#include <bits/stdc++.h>
----------------------------------------------------------
[Link]
// Initialize result
int ans = 0;
// Return result
return ans;
}
----------------------------------------------------------
int main()
{
int t,n;
ll s,tmp;
vl::iterator x;
ll left,sum;
bool found;
int pos;
cin>>t;
while(t--)
{
cin>>n>>s;
found=false;
unordered_map<int,vl> store;
ll arr[n][2];
fo(i,n) cin>>arr[i][0];
fo(i,n)
{
cin>>arr[i][1];
if(arr[i][1]==0) store[0].push_back(arr[i][0]);
else store[1].push_back(arr[i][0]);
}
sort(store[0].begin(),store[0].end());
sort(store[1].begin(),store[1].end());
for(int ii=0;ii<n;++ii)
{
if(arr[ii][1]==1)
{
x=lower_bound(store[0].begin(),store[0].end(),arr[ii][0]);
if(x==store[0].end())
{
continue;
}
sum=*x+arr[ii][0]+s;
if(sum<=100)
{
found=true;
break;
}
}
else
{
x=lower_bound(store[1].begin(),store[1].end(),arr[ii][0]);
if(x==store[1].end()) continue;
sum=*x+arr[ii][0]+s;
if(sum<=100)
{
found=true;
break;
}
}
}
if(found) cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
return 0;
-----------------------------------------------------------------------
Nearest One in matrix
#include<iostream>
#include<stdio.h>
#include<queue>
#include<utility>
using namespace std;
int isSafe(int x,int y,int n,int m)
{
if(x<0 || x>=m || y<0 || y>=n) return 0;
return 1;
}
int main()
{
int n,m;
queue<pair<int,int>> que;
cin>>n>>m;
int dist[n][m];
int mtx[n][m];
int row[]={0,1,0,-1};
int col[]={1,0,-1,0};
for(int i=0;i<n;++i)
{
for(int j=0;j<m;++j)
{
cin>>mtx[i][j];
if(mtx[i][j]==1)
{
[Link](make_pair(i,j));
dist[i][j]=0;
}
else dist[i][j]=-1;
}
}
pair<int,int> p;
int x,y;
while(![Link]())
{
p=[Link]();
[Link]();
cout<<"popped:"<<[Link]<<" "<<[Link]<<endl;
for(int i=0;i<4;++i)
{
x=row[i];
y=col[i];
if(!isSafe([Link]+x,[Link]+y,n,m)) continue;
if(dist[[Link]+x][[Link]+y]==-1)
{
dist[[Link]+x][[Link]+y]=dist[[Link]][[Link]]+1;
[Link](make_pair([Link]+x,[Link]+y));
}
else
{
if(dist[[Link]+x][[Link]+y]>dist[[Link]][[Link]]+1)
dist[[Link]+x][[Link]+y]=dist[[Link]][[Link]]+1;
//printf("dist[%d][%d]:%d\n",[Link]+x,[Link]+y,dist[[Link]+x]
[[Link]+y]);
}
cout<<dist[0][0]<<endl;
return 0;
}
---------------------------------------------------------
--------------------------------------------------------------------
K-Partitions of the array such that the sum is equal
[Link]
#include <iostream>
#include <numeric>
return r;
}
if (!res)
{
std::cout << "k-Partition of set S is not Possible";
return;
}
// number of items in S
int n = sizeof(S) / sizeof(S[0]);
int k = 5;
partition(S, n, k);
return 0;
}
--------------------------------------------------------------------
#include <bits/stdc++.h>
#define N 4
using namespace std;
// anti-clockwise direction by 90
void rotateMatrix(int mat[][N])
{
// Consider all squares one by one
for (int i = 0; i < N / 2; i++) {
// Consider elements in group
// of 4 in current square
for (int j = i; j < N - i - 1; j++) {
// Store current cell in
// temp variable
int temp = mat[i][j];
-----------------------------------------------------------
--------------------------------------------------------------------
------------------------------------------------------------
1st approach
2nd approach
--------------------------------------------------------------------
Find a pair with given sum
[Link]
-----------------------------------------------------------------
[Link]
-----------------------------------------------------------------
DP Problems
Heap/Priority Queue
-----------------------------------------------------------------
1) Build a Min Heap MH of the first k elements (arr[0] to arr[k-1]) of the given
array. O(k)
2) For each element, after the kth element (arr[k] to arr[n-1]), compare it with
root of MH.
��a) If the element is greater than the root then make it root and call heapify for
MH
��b) Else ignore it.
// The step 2 is O((n-k)*logk)
3) Finally, MH has k largest elements and root of the MH is the kth largest
element.
Time Complexity: O(k + (n-k)Logk) without sorted output. If sorted output is needed
then O(k + (n-k)Logk + kLogk)
All of the above methods can also be used to find the kth largest (or smallest)
element.
---------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
map<long,long> memo;
long long nSteps(long long n,bool flag)
{
if(n==0) return 1;
long key=n;
long a=0,b=0,c=0,total=0;
if([Link](key))
{
return memo[key];
}
if(n-1>=0)a = nSteps(n-1,flag);
if(n-2>=0)b = nSteps(n-2,flag);
if(!flag && n-3>=0)c = nSteps(n-3,flag);
total = a+b+c;
memo[key] = total;
return total;
}
int main()
{
long long n;
cin>>n;
cout<<nSteps(n,false);
return 0;
}
---------------------------------------------------
isSubsetSum Problem
Find the element at index if the sorting is performed without sorting the array.
Permutation of array
AVL Tree
1 D Array
2 D Array
Activity Selection Problem
� Banker Algorithm
� Binary tree/binary search tree
� Bit Manipulation
� Character Matching
� Graph
Graph Traversal Algorithm
� Hash Map
� Math/Logic
� Optimization Algorithm
� Palindrome
� Pattern Searching
� Priority Queue�
Recursion
� Searching
� Sliding Window
� Sorting
String Manipulation
� String Matching
� Tree Traversal Algorithm
Questions Done :-
Median of two sorted arrays
Check BST
DFS
BFS
Pattern matching using KMP
Wildcard Pattern matching
Paper Cut into minimum number of squares
Longest Pallindrome
Print All Partition of k subset sum
Partition Problem
Partition of set into two subset of equal sum
/*
*/
-----------------------
Permutation of a string
Coin change problem
Breadth First Search
Depth First Search
Binary Weighted Graph
Dijkstra's Algorithm
Floyd Warshall // not done
Bellman Ford
Infix to Prefix
Evaluation of Prefix
Infix To Postfix
Evaluation of postfix
quickSort(with and without recursion)
merge sort (with and without recursion)
linear sort
bubble sort
selection sort
heap sort
radix sort
shell sort // not done
insertion sort
Huffman Coding
Prim's Algo
--------------------------------------
Kruskal Algo
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
const int MAX = 1e4 + 5;
int id[MAX], nodes, edges;
pair <long long, pair<int, int> > p[MAX];
void initialize()
{
for(int i = 0;i < MAX;++i)
id[i] = i;
}
int root(int x)
{
while(id[x] != x)
{
id[x] = id[id[x]];
x = id[x];
}
return x;
}
int main()
{
int x, y;
long long weight, cost, minimumCost;
initialize();
cin >> nodes >> edges;
for(int i = 0;i < edges;++i)
{
cin >> x >> y >> weight;
p[i] = make_pair(weight, make_pair(x, y));
}
// Sort the edges in the ascending order
sort(p, p + edges);
minimumCost = kruskal(p);
cout << minimumCost << endl;
return 0;
}
-------------------------------------------------------------------
8 Queen's Problem
Rat-maze problem
Hamiltonian Cycle
Knapsack Algorithm & 0/1 Knapsack Problem
Job Sequencing Algorithm
Travelling salesman problem
Longest common subsequence
Multistage Graph
HopCroft-Karp for Maximum pattern matching
Jarvi's Algorithm or wrapping
Subset sum
Knight Tour's Problem
Questions based on Pallindrome
Rabin-Karp Algorithm
KMP algorithm
Finite Automata
Edit distance
Longest Pallindromic subsequence