├── Advanced Recursion ├── Merge Sort Code.cpp ├── Print Keypad combination code.cpp ├── Print Keypad combination code2.cpp ├── Quick sort code.cpp ├── Remove duplicates Recursively.cpp ├── Replace Characters recursively.cpp └── Return keypad code.py ├── Assignment Backtracking and searching and sorting applications ├── Collecting the balls.cpp ├── Collecting the balls.exe ├── Find power of a number.cpp ├── Sudoku solver.cpp └── sorting the skills.py ├── Backtracking ├── Crossword Problem.cpp ├── Nqueen.cpp └── Rat in a maze problem.cpp ├── Basics of recursion ├── check numbers.py ├── check numbers.txt ├── first index of number.py ├── first index of number.txt ├── number of digits.py ├── number of digits.txt ├── power.py ├── power.txt ├── print numbers.py ├── print numbers.txt ├── sum of array.py └── sum of array.txt ├── Bit manipulation ├── Clear all bits from msb.cpp ├── Set ith bit.cpp ├── Turn off first set bit.cpp ├── find 1st set bit.cpp └── unset ith bit.cpp ├── Greedy problems └── Minimum absolute difference in array.cpp ├── Language Tools ├── Diffrent Names.cpp ├── Diffrent Names.txt ├── Extract unique characters.py ├── Extract unique characters.txt ├── Love for Characters.py ├── Love for Characters.txt ├── Tell the position.cpp ├── Tell the position.txt ├── Warm reception.cpp └── Warm reception.txt ├── Language tools and time complexity assignment ├── Duplicate the array.cpp ├── Duplicate the array.txt ├── Find the Unique element.cpp ├── Find the Unique element.txt ├── Longest consecutive subsequence.cpp ├── Longest consecutive subsequence.txt ├── Pair sum to zero.cpp ├── Pair sum to zero.txt ├── Rotate array.cpp ├── Rotate array.txt ├── Sum me up.cpp ├── Sum me up.txt ├── Triplet sum.cpp ├── Triplet sum.txt └── triplet sum 2.cpp ├── Prerequisites ├── Even and Odd indices.cpp ├── Even and odd indices.txt ├── Oscillating prices of chakri.py ├── Oscillating prices of chakri.txt ├── PRE3.py ├── PRE4.py ├── PRE4.txt ├── PREQ3.txt ├── Target marbles.txt └── target marbles.cpp ├── README.md ├── Searching and sorting applications ├── Aggressive cows problem.cpp ├── Aggressive cows problem.txt ├── Distribute Candies.cpp ├── Inversion count.txt ├── Momos Market.cpp ├── Murder.cpp ├── Murder.txt ├── Taj mahal entry.py └── inversion count.cpp ├── _config.yml ├── miserman.cpp ├── miserman.exe └── segment tree └── maximum sum in subarray.cpp /Advanced Recursion/Merge Sort Code.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Merge Sort Code 3 | Send Feedback 4 | Sort an array A using Merge Sort. 5 | Change in the input array itself. So no need to return or print anything. 6 | Input format : 7 | Line 1 : Integer n i.e. Array size 8 | Line 2 : Array elements (separated by space) 9 | Output format : 10 | Array elements in increasing order (separated by space) 11 | Constraints : 12 | 1 <= n <= 1000 13 | Sample Input: 14 | 6 15 | 2 6 8 5 4 3 16 | Sample Output: 17 | 2 3 4 5 6 8 18 | */ 19 | 20 | void merge(int input[], int start, int mid, int end){ 21 | int n1 = mid - start + 1; 22 | int n2 = end-mid; 23 | int L[n1]; 24 | int R[n2]; 25 | 26 | int i = 0; 27 | int j = 0; 28 | int k = start; 29 | for(i = 0; i < n1; i++){ 30 | L[i] = input[k]; 31 | k++; 32 | } 33 | for(j = 0; j < n2; j++){ 34 | R[j] = input[k]; 35 | k++; 36 | } 37 | i = 0; 38 | j = 0; 39 | k = start; 40 | 41 | while(i < n1 && j < n2){ 42 | if(L[i]= last){ 67 | return; 68 | } 69 | int mid = (first+last)/2; 70 | _mergeSort(input, first, mid); 71 | _mergeSort(input, mid + 1, last); 72 | merge(input, first, mid, last); 73 | } 74 | 75 | void mergeSort(int input[], int size){ 76 | _mergeSort(input,0,size-1); 77 | return; 78 | } 79 | 80 | -------------------------------------------------------------------------------- /Advanced Recursion/Print Keypad combination code.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Print Keypad Combinations Code 3 | Send Feedback 4 | Given an integer n, using phone keypad find out and print all the possible strings that can be made using digits of input n. 5 | Note : The order of strings are not important. Just print different strings in new lines. 6 | Input Format : 7 | Integer n 8 | Output Format : 9 | All possible strings in different lines 10 | Constraints : 11 | 1 <= n <= 10^6 12 | Sample Input: 13 | 23 14 | Sample Output: 15 | ad 16 | ae 17 | af 18 | bd 19 | be 20 | bf 21 | cd 22 | ce 23 | cf 24 | */ 25 | 26 | #include 27 | #include 28 | using namespace std; 29 | string dir[]={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; 30 | 31 | void print(int num,string output) 32 | { 33 | if(num==0) 34 | { 35 | cout< 27 | #include 28 | using namespace std; 29 | 30 | 31 | string getString(int n){ 32 | if(n==1){ 33 | return ""; 34 | } 35 | if(n==2){ 36 | return "abc"; 37 | } 38 | if(n==3){ 39 | return "def"; 40 | } 41 | if(n==4){ 42 | return "ghi"; 43 | } 44 | if(n==5){ 45 | return "jkl"; 46 | } 47 | if(n==6){ 48 | return "mno"; 49 | } 50 | if(n==7){ 51 | return "pqrs"; 52 | } 53 | if(n==8){ 54 | return "tuv"; 55 | } 56 | if(n==9){ 57 | return "wxyz"; 58 | } 59 | } 60 | 61 | void printkeypadUtil(int num,string output){ 62 | if(num==0){ 63 | cout< 22 | using namespace std; 23 | 24 | 25 | int function(int x,int n){ 26 | if(n<=1){ 27 | return x; 28 | } 29 | else { 30 | return x*function(x,n-1); 31 | } 32 | } 33 | 34 | int main() { 35 | // Write your code here 36 | int x,n; 37 | cin>>x>>n; 38 | int result = function(x,n); 39 | cout<=(idx-1): 30 | pass 31 | else: 32 | flag = False 33 | break 34 | if flag: 35 | print("Yes") 36 | else: 37 | print("No") 38 | -------------------------------------------------------------------------------- /Backtracking/Crossword Problem.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Crossword Problem 4 | Send Feedback 5 | CodingNinjas has provided a crossword of 10*10 grid. The grid contains '+' or '-' as its cell values. Now, you are also provided with word list which needs to placed accurately in the grid. Cells marked with '-' are to be filled with word list. 6 | For example: The following is an example for the input crossword grid and the word list. 7 | +-++++++++ 8 | +-++-+++++ 9 | +-------++ 10 | +-++-+++++ 11 | +-++-+++++ 12 | +-++-+++++ 13 | ++++-+++++ 14 | ++++-+++++ 15 | ++++++++++ 16 | ---------- 17 | CALIFORNIA;NIGERIA;CANADA;TELAVIV 18 | Output for the given input should be: 19 | +C++++++++ 20 | +A++T+++++ 21 | +NIGERIA++ 22 | +A++L+++++ 23 | +D++A+++++ 24 | +A++V+++++ 25 | ++++I+++++ 26 | ++++V+++++ 27 | ++++++++++ 28 | CALIFORNIA 29 | Note: We have provided such test cases that there is only one solution for the given input. 30 | Input format: 31 | The first 10 lines of input contains crossword. Each of 10 lines have a character array of size 10. Input characters are either '+' or '-'. 32 | Next line of input contains the word list, in which each word is separated by ';'. 33 | Output format: 34 | Print the crossword grid, after placing the words of word list in '-' cells. 35 | Sample Test Cases: 36 | Sample Input 1: 37 | +-++++++++ 38 | +-++-+++++ 39 | +-------++ 40 | +-++-+++++ 41 | +-++-+++++ 42 | +-++-+++++ 43 | ++++-+++++ 44 | ++++-+++++ 45 | ++++++++++ 46 | ---------- 47 | CALIFORNIA;NIGERIA;CANADA;TELAVIV 48 | Sample Output 1: 49 | +C++++++++ 50 | +A++T+++++ 51 | +NIGERIA++ 52 | +A++L+++++ 53 | +D++A+++++ 54 | +A++V+++++ 55 | ++++I+++++ 56 | ++++V+++++ 57 | ++++++++++ 58 | CALIFORNIA 59 | 60 | */ 61 | 62 | #include 63 | #include 64 | 65 | using namespace std; 66 | pair IsVertical(char input[10][10], string name, int n){ 67 | 68 | pair ans; 69 | ans.first=false; 70 | int j=0; 71 | 72 | int counter=0; 73 | 74 | for(int i=0; i<10; i++){ 75 | 76 | if(input[i][n]=='-' || input[i][n]== name[j]){ 77 | counter++; 78 | j++; 79 | 80 | if(counter==name.length()){ 81 | ans.first=true; 82 | ans.second=i-counter+1; 83 | break; 84 | } 85 | 86 | } 87 | 88 | else{ 89 | counter=0; 90 | j=0; 91 | } 92 | } 93 | 94 | return ans; 95 | } 96 | 97 | 98 | pair IsHorizontal(char input[10][10],string name, int n){ 99 | 100 | pair ans; 101 | 102 | ans.first=false; 103 | 104 | int j=0; 105 | 106 | int counter=0; 107 | 108 | for(int i=0; i<10; i++){ 109 | if(input[n][i]=='-' || input[n][i]== name[j] ){ 110 | counter++; 111 | j++; 112 | 113 | if(counter==name.length()){ 114 | ans.first=true; 115 | ans.second=i-counter+1; 116 | break; 117 | } 118 | } 119 | 120 | else{ 121 | counter=0; 122 | j=0; 123 | 124 | } } 125 | return ans; 126 | 127 | } 128 | 129 | 130 | 131 | void setVertical(char input[10][10], string name, int n, bool helper[10], int start){ 132 | 133 | for(int i=0; i check; 191 | 192 | for(int i=0; i<10; i++){ 193 | check=IsVertical(input,names[index],i); 194 | 195 | if(check.first==true){ 196 | 197 | setVertical(input,names[index],i,helper,check.second); 198 | if(crossward(input,names,index+1,number)==true){ 199 | return true; 200 | } else{ 201 | resetVertical(input,helper,i); 202 | } 203 | 204 | } 205 | 206 | } 207 | 208 | 209 | for(int i=0; i<10; i++){ 210 | check=IsHorizontal(input,names[index],i); 211 | 212 | if(check.first==true){ 213 | 214 | setHorizontal(input,names[index],i,helper,check.second); 215 | 216 | if(crossward(input,names,index+1,number)==true){ 217 | return true; 218 | } else{ 219 | resetHorizontal(input,helper,i); 220 | } 221 | 222 | } 223 | 224 | } 225 | 226 | 227 | return false; 228 | } 229 | 230 | 231 | 232 | 233 | 234 | 235 | int main() { 236 | 237 | // Write your code here 238 | 239 | char input[10][10]; 240 | 241 | for(int i=0; i<10; i++){ 242 | for(int j=0; j<10; j++){ 243 | cin>>input[i][j]; 244 | } 245 | } 246 | 247 | string names[100]; 248 | 249 | string input_line; 250 | 251 | cin>>input_line; 252 | 253 | int k=0; 254 | 255 | for(int i=0; input_line[i]!='\0'; i++){ 256 | if(input_line[i]==';'){ 257 | k++; 258 | i++; 259 | } 260 | names[k]=names[k]+input_line[i]; 261 | } 262 | 263 | 264 | bool help= crossward(input,names,0,k+1); 265 | 266 | 267 | for(int i=0; i<10; i++){ 268 | for(int j=0; j<10; j++){ 269 | cout<> 00000100 31 | 00000011 32 | 33 | 34 | ~ 11111011 35 | 00000111 36 | 37 | */ 38 | int m = (1< 24 | using namespace std; 25 | int minAbsoluteDiff(int arr[], int n) { 26 | /* Don't write main(). 27 | * Don't read input, it is passed as function argument. 28 | * Return output and don't print it. 29 | * Taking input and printing output is handled automatically. 30 | */ 31 | sort(arr,arr+n); 32 | int min = arr[n-1]-arr[0]; 33 | for(int i=1;i(arr[i]-arr[i-1])){ 35 | min = arr[i]-arr[i-1]; 36 | } 37 | } 38 | return min; 39 | 40 | } 41 | 42 | -------------------------------------------------------------------------------- /Language Tools/Diffrent Names.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | string s; 6 | getline(cin,s); 7 | long start = 0; 8 | map m; 9 | for(int i = 0;i :: iterator it; 20 | for(it = m.begin();it!=m.end();it++){ 21 | cout<<(*it).first<<" a "<1){ 26 | cout<<(*it).first<<" "<<(*it).second< 2 | using namespace std; 3 | class sort_map 4 | { 5 | public: 6 | string key; 7 | int val; 8 | }sm; 9 | 10 | bool Sort_by(const sort_map& a ,const sort_map& b) 11 | { 12 | return a.val > b.val; 13 | } 14 | int main() 15 | { 16 | long t; 17 | map d; 18 | cin>>t; 19 | for(long i = 0;i>temp; 22 | int s1,s2,s3; 23 | cin>>s1>>s2>>s3; 24 | int s = s1+s2+s3; 25 | d[temp] = s; 26 | } 27 | vector< sort_map > v; 28 | vector< sort_map >::iterator itv; 29 | map :: iterator it; 30 | for (it = d.begin(); it != d.end(); ++it) 31 | { 32 | sm.key = (*it).first; sm.val = (*it).second; 33 | v.push_back(sm); 34 | } 35 | // for (itv = v.begin(); itv != v.end(); ++itv) 36 | // { 37 | // // cout << (*itv).key << " : " << (*itv).val << endl; 38 | // } 39 | // 40 | sort(v.begin(),v.end(),Sort_by); 41 | 42 | // cout << "sorted" << endl; 43 | long f = 0; 44 | for (itv = v.begin(); itv != v.end(); ++itv) 45 | { 46 | // cout << (*itv).key << " : " << (*itv).val << endl; 47 | cout << f+1<<" "<<(*itv).key<< endl; 48 | f++; 49 | } 50 | 51 | return 0; 52 | } 53 | 54 | -------------------------------------------------------------------------------- /Language Tools/Tell the position.txt: -------------------------------------------------------------------------------- 1 | Tell the positions 2 | Send Feedback 3 | In a class there are ‘n’ number of students. They have three different subjects: Data Structures, Algorithm Design & Analysis and Operating Systems. Marks for each subject of all the students are provided to you. You have to tell the position of each student in the class. Print the names of each student according to their position in class. Tie is broken on the basis of their roll numbers. Between two students having same marks, the one with less roll number will have higher rank. The input is provided in order of roll number. 4 | Input Format: 5 | First line will have a single integer ‘n’, denoting the number of students in the class. 6 | Next ‘n’ lines each will have one string denoting the name of student and three space separated integers m1, m2, m3 denoting the marks in three subjects. 7 | Output Format: 8 | Print ‘n’ lines having two values: First, the position of student in the class and second his name. 9 | Constraints: 10 | 1 <= n <= 10^5 11 | 0 <= m1, m2, m3 <= 100 12 | Sample Input: 13 | 3 14 | Mohit 94 85 97 15 | Shubham 93 91 94 16 | Rishabh 95 81 99 17 | Sample Output: 18 | 1 Shubham 19 | 2 Mohit 20 | 3 Rishabh -------------------------------------------------------------------------------- /Language Tools/Warm reception.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct interval{ 5 | long st; 6 | long et; 7 | }; 8 | 9 | int main() { 10 | long t; 11 | cin>>t; 12 | long counter = 0; 13 | interval ar[t]; 14 | for(long i = 0;i>ar[i].st; 16 | } 17 | for(long i = 0;i>ar[i].et; 19 | } 20 | long max = 0; 21 | for(long i = 0;i<2400;i++){ 22 | for(long j = 0;j1){ 17 | return i; 18 | } 19 | } 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Language tools and time complexity assignment/Duplicate the array.txt: -------------------------------------------------------------------------------- 1 | Duplicate in array 2 | 3 | Given an array of integers of size n which contains numbers from 0 to n - 2. Each number is present at least once. That is, if n = 5, numbers from 0 to 3 is present in the given array at least once and one number is present twice. You need to find and return that duplicate number present in the array. 4 | Assume, duplicate number is always present in the array. 5 | Input format : 6 | Line 1 : Size of input array 7 | Line 2 : Array elements (separated by space) 8 | Output Format : 9 | Duplicate element 10 | Constraints : 11 | 1 <= n <= 10^6 12 | Sample Input: 13 | 9 14 | 0 7 2 5 4 7 1 3 6 15 | Sample Output: 16 | 7 17 | -------------------------------------------------------------------------------- /Language tools and time complexity assignment/Find the Unique element.cpp: -------------------------------------------------------------------------------- 1 | // arr - input array 2 | // size - size of array 3 | #include 4 | using namespace std; 5 | 6 | int FindUnique(int arr[], int size){ 7 | /* Don't write main(). 8 | * Don't read input, it is passed as function argument. 9 | * Return output and don't print it. 10 | * Taking input and printing output is handled automatically. 11 | */ 12 | map m; 13 | for(int i = 0;i :: iterator it; 20 | for(it = m.begin();it!=m.end();it++){ 21 | if((*it).second==1){ 22 | return (*it).first; 23 | } 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Language tools and time complexity assignment/Find the Unique element.txt: -------------------------------------------------------------------------------- 1 | Find the Unique Element 2 | Send Feedback 3 | Given an integer array of size 2N + 1. In this given array, N numbers are present twice and one number is present only once in the array. 4 | You need to find and return that number which is unique in the array. 5 | Note : Given array will always contain odd number of elements. 6 | Input format : 7 | Line 1 : Array size i.e. 2N+1 8 | Line 2 : Array elements (separated by space) 9 | Output Format : 10 | Unique element present in the array 11 | Constraints : 12 | 1 <= N <= 10^6 13 | Sample Input : 14 | 7 15 | 2 3 1 6 3 6 2 16 | Sample Output : 17 | 1 -------------------------------------------------------------------------------- /Language tools and time complexity assignment/Longest consecutive subsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | vector longestSubsequence(int *arr, int n){ 6 | 7 | unordered_map m; 8 | for(int i = 0;imax){ 39 | max = count; 40 | newstart = start; 41 | } 42 | } 43 | } 44 | 45 | 46 | vector vc; 47 | for(int i = newstart;i<=max+newstart;i++){ 48 | //cout< 2 | using namespace std; 3 | 4 | void PairSum(int *input, int n){ 5 | /* Don't write main(). 6 | * the input array is already passed as function argument. 7 | * Don't need to return anything. Print the desired pairs in the function itself. 8 | */ 9 | map um; 10 | for(int i = 0;i :: iterator it; 15 | for(it = um.begin();it!=um.end();it++){ 16 | if((*it).first>0) 17 | if((*it).second>0){ 18 | if(um[0-(*it).first]){ 19 | for(int i = 0;i<((*it).second * um[0-(*it).first]);i++){ 20 | cout<<0-(*it).first<<" "<<(*it).first< 2 | using namespace std; 3 | typedef unsigned long long ull; 4 | int main() { 5 | 6 | ull t; 7 | cin>>t; 8 | while(t--){ 9 | ull val; 10 | cin>>val; 11 | ull s = 0; 12 | while(val!=0){ 13 | s+=val%10; 14 | val/=10; 15 | } 16 | cout<arr[j]){ 15 | int temp = arr[i]; 16 | arr[i] = arr[j]; 17 | arr[j] = temp; 18 | } 19 | } 20 | } 21 | 22 | for(int i = 0;i 2 | void FindTriplet(int *input,int size, int x) { 3 | sort(input,input+size); 4 | for(int i=0;i val) 9 | end--; 10 | else 11 | if(input[st]+input[end]=st;ptr--) { 22 | if(input[ptr] == input[end]) 23 | count2++; 24 | else 25 | break; 26 | } 27 | int combinations = count1*count2; 28 | if(input[st] == input[end]) 29 | combinations = ((end-st+1)*(end-st))/2; 30 | for(int k=0;k 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | long int even=0,odd=0; 7 | long int t; 8 | cin>>t; 9 | int *ar; 10 | ar = new int[t]; 11 | for(int i = 0;i>ar[i]; 13 | if(i%2==0){ 14 | if(ar[i]%2==0){ 15 | even += ar[i]; 16 | } 17 | } 18 | else{ 19 | if(ar[i]%2!=0){ 20 | odd += ar[i]; 21 | } 22 | } 23 | } 24 | cout< 2 | using namespace std; 3 | 4 | void printing(int ar[],int i, int j){ 5 | cout<<"true"<>n>>s; 15 | int ar[n]; 16 | for(int i = 0;i>ar[i]; 18 | } 19 | bool flag = false; 20 | for(int i = 0; is){ 30 | break; 31 | } 32 | } 33 | if(flag){ 34 | break; 35 | } 36 | } 37 | if (!flag){ 38 | cout<<"false"; 39 | } 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## The repo consist of some of the very popular questions of SPOJ, codezen and HackerEarth. 2 | 3 | The questions are well categorised and picked in the sequence of a popular course of coding ninjas (https://codingninjas.in/) 4 | 5 | I solved the questions accordingly and the solutions are capable of passing all the testcases that i think of. 6 | 7 | This repo is for those who wants to start their coding days in a sequential order by grasping all the concepts orderly. However it is mandatory for them to have a good knowledge of c++ or Python syntax. 8 | 9 | The order to follow the modules are listed: 10 | 1. Prerequisites 11 | 2. Basics of recursion 12 | 3. Time and space complexity 13 | 4. Language tools 14 | 5. Language tools and time complexity analysis 15 | 6. Searching and sorting applications 16 | 7. 17 | -------------------------------------------------------------------------------- /Searching and sorting applications/Aggressive cows problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include 4 | int main() 5 | { 6 | long long int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | long long int n,c; 11 | cin>>n>>c; 12 | long long int arr[n+5]; 13 | for(long long int i=0;i>arr[i]; 16 | } 17 | sort(arr,arr+n); 18 | long long int hi=arr[n-1]; 19 | long long int lo=arr[0]; 20 | long long int mid; 21 | int ans=-1; 22 | while(lo=mid) 31 | { 32 | taken++; 33 | if(taken==c) break; 34 | last_pos=i; 35 | } 36 | } 37 | // cout<<"taken "<ans) 49 | { 50 | ans=mid; 51 | } 52 | } 53 | cout< 24 | using namespace std; 25 | 26 | typedef long long ll; 27 | 28 | bool func(ll a[],ll val,ll k,ll n) 29 | { 30 | if(k==1) 31 | return true; 32 | ll sum=0; 33 | for(ll i=n-1;i>=0;i--) 34 | { 35 | sum +=(a[i]/val); 36 | } 37 | if(sum >= k) 38 | return true; 39 | else 40 | return false; 41 | } 42 | void binary_search(ll a[],ll n,ll k) 43 | { 44 | ll lo =1; 45 | ll high =a[n-1]+1; 46 | ll mid=0; 47 | while(high > lo) 48 | { 49 | mid = (lo+high)/2; 50 | if(func(a,mid,k,n)) 51 | lo = mid+1; 52 | else 53 | high = mid; 54 | } 55 | cout<>t; 61 | while(t--){ 62 | ll n,k; 63 | cin>>n>>k; 64 | ll ar[500010]; 65 | 66 | for(ll i = 0;i>ar[i]; 68 | } 69 | sort(ar,ar+n); 70 | binary_search(ar,n,k); 71 | } 72 | return 0; 73 | 74 | } 75 | -------------------------------------------------------------------------------- /Searching and sorting applications/Inversion count.txt: -------------------------------------------------------------------------------- 1 | Inversion Count 2 | 3 | Let A[0 ... n-1] be an array of n distinct positive integers. If i < j and A[i] > A[j] then the pair (i, j) is called an inversion of A (where i and j are indexes of A). Given an integer array A, your task is to find the number of inversions in A. 4 | Input format : 5 | Line 1 : n, array size 6 | Line 2 : Array elements (separated by space). 7 | Output format : 8 | Count of inversions 9 | Constraints : 10 | 1 <= n <= 10^5 11 | 1 <= A[i] <= 10^9 12 | Sample Input 1 : 13 | 3 14 | 3 2 1 15 | Sample Output 1 : 16 | 3 17 | Sample Input 2 : 18 | 5 19 | 2 5 1 3 4 20 | Sample Output 1 : 21 | 4 -------------------------------------------------------------------------------- /Searching and sorting applications/Momos Market.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adityaraj1711/Eminance-coding-ninjas/8b7393fe8db10dd047d8841b15f332899e52daf3/Searching and sorting applications/Momos Market.cpp -------------------------------------------------------------------------------- /Searching and sorting applications/Murder.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define ll long long 5 | 6 | using namespace std; 7 | 8 | ll count(int a[],int l,int mid,int r){ 9 | int c[r-l+1]; 10 | int ci=0,i = l,j=mid+1; 11 | ll sum =0; 12 | while(i <= mid && j<= r){ 13 | if(a[i] < a[j]){ 14 | sum += (ll)((ll)(r-j+1)*(ll)a[i]); 15 | c[ci++] = a[i]; 16 | i++; 17 | } 18 | else{ 19 | c[ci++] = a[j]; 20 | j++; 21 | } 22 | } 23 | while(i<=mid){ 24 | c[ci++] = a[i++]; 25 | } 26 | while(j <= r){ 27 | c[ci++] = a[j++]; 28 | } 29 | for(int i=0;i>T; 50 | while(T--){ 51 | int n; 52 | cin>>n; 53 | int a[n]; 54 | for(int i=0;i>a[i]; 56 | } 57 | cout< 2 | using namespace std; 3 | typedef long long ll; 4 | 5 | 6 | ll merge(int arr[], int temp[], int left, int mid, int right) 7 | { 8 | int i, j, k; 9 | ll inv_count = 0; 10 | 11 | i = left; 12 | j = mid; 13 | k = left; 14 | while ((i <= mid - 1) && (j <= right)) { 15 | if (arr[i] <= arr[j]) { 16 | temp[k++] = arr[i++]; 17 | } 18 | else { 19 | temp[k++] = arr[j++]; 20 | inv_count = inv_count + (mid - i); 21 | } 22 | } 23 | while (i <= mid - 1) 24 | temp[k++] = arr[i++]; 25 | 26 | while (j <= right) 27 | temp[k++] = arr[j++]; 28 | for (i = left; i <= right; i++) 29 | arr[i] = temp[i]; 30 | 31 | return inv_count; 32 | } 33 | ll zmergeSort(int arr[], int temp[], int left, int right) 34 | { 35 | int mid; 36 | ll inv_count = 0; 37 | if (right > left) { 38 | mid = (right + left) / 2; 39 | inv_count = zmergeSort(arr, temp, left, mid); 40 | inv_count += zmergeSort(arr, temp, mid + 1, right); 41 | inv_count += merge(arr, temp, left, mid + 1, right); 42 | } 43 | return inv_count; 44 | } 45 | 46 | 47 | ll mergeSort(int arr[], int array_size) 48 | { 49 | int* temp = (int*)malloc(sizeof(int) * array_size); 50 | return zmergeSort(arr, temp, 0, array_size - 1); 51 | } 52 | 53 | long long solve(int A[], int n) 54 | { 55 | return mergeSort(A,n); 56 | } 57 | 58 | /* 59 | int main() { 60 | ll t; 61 | cin>>t; 62 | ll arr[t]; 63 | for(ll i = 0;i>arr[i]; 65 | } 66 | ll inv_count = 0; 67 | inv_count = mergeSort(arr,t); 68 | cout< 2 | #include 3 | using namespace std; 4 | int dp[101][100]; 5 | int main() { 6 | int N, M; 7 | cin >> N >> M; 8 | memset(dp[0], 0, sizeof(dp[0])); 9 | for (int i = 0; i < N; i++) { 10 | for (int j = 0; j < M; j++) { 11 | int val; cin >> val; 12 | dp[i+1][j] = val + dp[i][j]; 13 | if (j > 0) dp[i+1][j] = min(dp[i+1][j], val + dp[i][j-1]); 14 | if (j < M-1) dp[i+1][j] = min(dp[i+1][j], val + dp[i][j+1]); 15 | } 16 | } 17 | int best = 1e9; 18 | for (int i = 0; i < M; i++) { 19 | best = min(best, dp[N][i]); 20 | } 21 | for (int i = 0; i < N; i++) { 22 | for (int j = 0; j < M+1; j++) { 23 | cout< 24 | using namespace std; 25 | 26 | struct node { 27 | long long int sum, prefixsum, suffixsum, maxsum; 28 | }; 29 | 30 | node tree[1000100]; 31 | long long int arr[5000100]; 32 | 33 | void build(int index, int start, int end) 34 | { 35 | 36 | if (start == end) { 37 | 38 | tree[index].sum = arr[start]; 39 | tree[index].prefixsum = arr[start]; 40 | tree[index].suffixsum = arr[start]; 41 | tree[index].maxsum = arr[start]; 42 | } 43 | else if(start>end) return ; 44 | else { 45 | int mid = (start + end) / 2; 46 | build(2 * index, start, mid); 47 | build(2 * index + 1, mid + 1, end); 48 | 49 | tree[index].prefixsum = max(tree[2 * index].prefixsum, tree[2 * index].sum + tree[2 * index + 1].prefixsum); 50 | tree[index].suffixsum = max(tree[2 * index + 1].suffixsum, tree[2 * index + 1].sum + tree[2 * index].suffixsum); 51 | tree[index].sum = tree[2 * index].sum + tree[2 * index + 1].sum; 52 | tree[index].maxsum = max(tree[2*index].maxsum, max(tree[2 * index + 1].maxsum, tree[2 * index].suffixsum + tree[2 * index + 1].prefixsum)); 53 | 54 | // tree[index].maxsum = max(tree[index].prefixsum, 55 | // max(tree[index].suffixsum, 56 | // max(tree[2 * index + 1].maxsum, 57 | // max(tree[2 * index + 2].maxsum, 58 | // tree[2 * index + 1].suffixsum + tree[2 * index + 1].suffixsum 59 | // + tree[2 * index + 2].prefixsum)))); 60 | } 61 | } 62 | 63 | node query(int index, int start, int end, int l, int r) 64 | { 65 | node result; 66 | result.sum = result.prefixsum = INT_MIN; 67 | result.suffixsum = result.maxsum = INT_MIN; 68 | if (start > end || start > r || end < l){ 69 | return result; 70 | } 71 | 72 | if (l <= start && end <= r) 73 | return tree[index]; 74 | 75 | int mid = (start + end) / 2; 76 | // if (l > mid) 77 | // return query(2 * index + 2, mid + 1, end, l, r); 78 | // if (r <= mid) 79 | // return query(2 * index + 1, start, mid, l, r); 80 | 81 | node left = query(2 * index, start, mid, l, r); 82 | node right = query(2 * index + 1, mid + 1, end, l, r); 83 | 84 | result.prefixsum = max(left.prefixsum, left.sum + right.prefixsum); 85 | result.suffixsum = max(right.suffixsum, right.sum + left.suffixsum); 86 | result.sum = left.sum + right.sum; 87 | result.maxsum=max(left.maxsum,max(right.maxsum,left.suffixsum+right.prefixsum)); 88 | 89 | // result.maxsum = max(result.prefixsum, 90 | // max(result.suffixsum, 91 | // max(left.maxsum, 92 | // max(right.maxsum, 93 | // left.suffixsum + right.prefixsum)))); 94 | return result; 95 | } 96 | 97 | int main() 98 | { 99 | long long int n, m, a, b; 100 | scanf("%lld", &n); 101 | // cin>>n; 102 | for (int i = 0; i < n; ++i) 103 | scanf("%lld", &arr[i]); 104 | 105 | build(1, 0, n - 1); 106 | scanf("%d", &m); 107 | 108 | for (int i = 0; i < m; ++i) { 109 | scanf("%lld%lld", &a, &b); 110 | // cin>>a>>b; 111 | printf("%lld\n", query(1, 0, n - 1, a - 1, b - 1).maxsum); 112 | 113 | } 114 | return 0; 115 | } 116 | 117 | --------------------------------------------------------------------------------