├── README.md ├── Possiblepermutation.py ├── Binary Duplication.py ├── Paint House Min Price.java ├── Replacearray.CPP ├── HarryPotterHalfBlood.CPP ├── HarrypotterSoccers.CPP ├── StringMatching.C ├── Array └── Gfg │ ├── Maximize sum of consecutive differences in a circular array.CPP │ ├── Sort Elements by frequency.CPP │ ├── Relative_Sorting.CPP │ ├── Search an element in a sorted and rotated array.CPP │ └── Next Greater Element.CPP ├── Bitwise_XOR.py ├── OS ├── Sequential File Allocation.CPP ├── FCFS.CPP └── SJF.CPP ├── Pattern5.C ├── Pattern3.CPP ├── Add 2 values.CPP ├── Minimum_frequency.C ├── Decimal to binary.CPP ├── Compiler └── Left recursion.CPP ├── ReverseVowels.C ├── HighestPossibleProduct.CPP ├── Pattern4.C ├── MinimumNumberFunction.CPP ├── Pattern2.C ├── Stairofnumbers.CPP ├── Frequency_in_strings.C ├── Equal_bases.CPP ├── Forming_Numericals_Pattern.C ├── SmallestPalindrome.C ├── Special_array_sorting.CPP ├── StringRepetitions.CPP ├── Square in a circle.CPP ├── LL_insertatanypos.CPP ├── MaximumElementonStack.CPP ├── Domino_Piles.CPP ├── Reincarnation.CPP ├── SmartFunction.CPP ├── 7 divisible_pairs.CPP ├── LittleShino.CPP ├── Pattern_printing9.C ├── Bug and Secret Santa.C ├── StringConversionzigzag.CPP ├── ParliamentSquare.CPP ├── ConfusedInteger.CPP ├── N_as_sum_of_primes.CPP ├── Let us xor.CPP ├── Char Compression.C ├── Count_the_primes.CPP ├── Lex Characters Unique.CPP ├── Blissfull pairs.CPP ├── Athletic_Akhil.CPP ├── Sorted_team.CPP ├── Array_as_a_hill.CPP ├── Friendshipended.CPP ├── Vowel Repitition.CPP ├── Color the Fence(Love).CPP ├── *rotate the array.CPP ├── BS !.CPP ├── Greatestnumberwithdigits.CPP ├── Consecutiveprimesum.CPP ├── Coders_day.CPP ├── CircularQueue.CPP ├── Eat less.CPP ├── RomantoDecimal.CPP ├── Electronic_Store_Sort.CPP ├── HappySighting.C ├── Stackusingarray.C ├── Canadian to turn off light bulb.CPP ├── Candies to Children.CPP ├── Slime(Microbial Infestation).CPP ├── Ways to decode.C ├── Algorithms ├── Water the grass.CPP ├── Maximizing special nodes.CPP ├── Bag of gold again.CPP └── Ebola curve.CPP ├── Cityofstars.CPP ├── Exclusive OR.CPP ├── Star_Strings.CPP ├── PermutationProblemwithKswaps.CPP ├── Sumofalldigits.CPP ├── Fewa and her photoshoot.CPP ├── LLinsertionatbeginning.CPP ├── Shortest_man.CPP ├── Smaller_team.CPP ├── Diamond_Pattern.CPP ├── Largest Rectangular Area Under Histogram.CPP ├── Electronicstore.CPP ├── Disintegrate LL.CPP ├── PaneerLove.CPP ├── Residueofexecution.CPP ├── SMS_language.C ├── Pstring.CPP ├── Amogh's Homework.CPP ├── Johny_in_a_matrix.CPP ├── LL_searching.C ├── Subway_runner.CPP ├── Subwayrunner.CPP ├── Maximum xor subset.CPP ├── Bracket Sequence.CPP ├── ResetButtons.CPP ├── Farthest String.CPP ├── Tricky_Treat.CPP ├── GCD.CPP ├── Word_index1.CPP ├── Play_with_strings.C ├── OrangePartitioning.C ├── Shopping in clusters.CPP ├── Nastya studies informatics.CPP ├── Bag of Gold again.C ├── Datesorting.CPP ├── Barua Number.CPP ├── Two_sum_value.CPP ├── LoopedList.C ├── One teacher Two classes.CPP ├── Pyramidbynumericals.CPP ├── Into the forest.C ├── Intothewater.CPP ├── Great_Pattern.CPP ├── Tilak has fun with the Lights.CPP ├── Gas Stations.CPP ├── EmployeeSheet.CPP ├── Dylan and array(Arrays without local maximum).CPP ├── Pucca and the card game.CPP ├── PrimeOfPrimes.CPP ├── Jason fights the monster.CPP ├── KefaHatesCoins.C ├── Tisha_and_Orange_Sorting.CPP └── TurningBinaryMatrix.CPP /README.md: -------------------------------------------------------------------------------- 1 | # Competitive_Programming 2 | Examples to showcase problems involving competitive programming 3 | -------------------------------------------------------------------------------- /Possiblepermutation.py: -------------------------------------------------------------------------------- 1 | from itertools import permutations 2 | s = input() 3 | l = list(map("".join,permutations(s))) 4 | c=0 5 | for i in l: 6 | if(l.count(i)>1): 7 | c=1 8 | break; 9 | if(c==1): 10 | print("\n".join(l)) 11 | else: 12 | l.sort() 13 | print("\n".join(l)) 14 | -------------------------------------------------------------------------------- /Binary Duplication.py: -------------------------------------------------------------------------------- 1 | t=int(input()) 2 | l=[] 3 | for i in range(t): 4 | l.append(int(input())) 5 | x=(max(l)//2)-2 6 | j=0 7 | s=['0','1','1','0'] 8 | while (j=l[j]: 11 | print(s[l[j]]) 12 | else: 13 | for i in range(p): 14 | if s[i]=='0': 15 | s.append('1') 16 | else: 17 | s.append('0') 18 | continue 19 | j+=1 20 | -------------------------------------------------------------------------------- /Paint House Min Price.java: -------------------------------------------------------------------------------- 1 | public int minCost(int[][] costs) { 2 | if(costs==null||costs.length==0) 3 | return 0; 4 | 5 | for(int i=1; i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int main() { 10 | int n; 11 | cin>>n; 12 | int a[n]; 13 | for(int i = 0;i>a[i]; 15 | int max = a[n-1]; 16 | a[n-1] = 0; 17 | for(int i=n-2;i>=0;i--) 18 | { 19 | int temp = a[i]; 20 | a[i] = max; 21 | 22 | if (max 2 | using namespace std; 3 | int main() 4 | { 5 | stack s; 6 | int n; 7 | cin>>n; 8 | int a[n]; 9 | for(int i=0;i>a[i]; 12 | } 13 | int t; 14 | int x,top1; 15 | int sum=0; 16 | int k =0; 17 | cin>>t>>x; 18 | int count =0; 19 | while(t--) 20 | { 21 | 22 | string s1; 23 | cin>>s1; 24 | if(s1 == "Harry") 25 | { 26 | s.push(a[k]); 27 | sum+= a[k]; 28 | k++; 29 | 30 | } 31 | if(sum == x) 32 | { 33 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | stack S; 6 | int N; 7 | cin>>N; 8 | int arr[N]; 9 | for(int i=0;i>arr[i]; 12 | } 13 | int p; 14 | int q,TOP; 15 | int sum=0; 16 | int k =0; 17 | cin>>p>>q; 18 | int count =0; 19 | while(p--) 20 | { 21 | 22 | string strr; 23 | cin>>strr; 24 | if(strr == "Harry") 25 | { 26 | S.push(arr[k]); 27 | sum+= arr[k]; 28 | k++; 29 | 30 | } 31 | if(sum == q) 32 | { 33 | cout< 15 | #include 16 | #include 17 | int main() 18 | { char s[10000000]; 19 | scanf("%[^\n]s ",s); 20 | char sec[1000000]; 21 | scanf("%s",sec); 22 | long long int n,m; 23 | n=strlen(s); 24 | m=strlen(sec); 25 | for(long long int i=0;i<=n-m;i++){ 26 | long long int j; 27 | for(j=0;j 14 | using namespace std; 15 | 16 | int maxSum(int arr[], int n) 17 | { 18 | int sum = 0; 19 | 20 | sort(arr, arr + n); 21 | 22 | for (int i = 0; i < n/2; i++) 23 | { 24 | sum -= (2 * arr[i]); 25 | sum += (2 * arr[n - i - 1]); 26 | } 27 | 28 | return sum; 29 | } 30 | int main() 31 | { 32 | int arr[] = { 4, 2, 1, 8 }; 33 | int n = sizeof(arr)/sizeof(arr[0]); 34 | cout << maxSum(arr, n) << endl; 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Bitwise_XOR.py: -------------------------------------------------------------------------------- 1 | Find the XOR of two numbers and print it. 2 | 3 | Hint : Input the numbers as strings. 4 | 5 | Input Format 6 | 7 | First line contains first number X and second line contains second number Y. 8 | The numbers will be given to you in binary form. 9 | 10 | Constraints 11 | 12 | 0 <= X <= 2^1000 13 | 0 <= Y <= 2^1000 14 | 15 | Output Format 16 | 17 | Output one number in binary format, the XOR of two numbers. 18 | 19 | Sample Input 0 20 | 21 | 11011 22 | 10101 23 | Sample Output 0 24 | 25 | 01110 26 | 27 | 28 | a=input() 29 | b=input() 30 | if(len(a)>len(b)): 31 | b='0'*(len(a)-len(b))+b 32 | elif(len(b)>len(a)): 33 | a='0'*(len(b)-len(a))+a 34 | for i in range(len(a)): 35 | if((a[i]=='1' and b[i]=='1')or(a[i]=='0' and b[i]=='0')): 36 | print("0",end="") 37 | else: 38 | print("1",end="") 39 | -------------------------------------------------------------------------------- /OS/Sequential File Allocation.CPP: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct fileTable{ 4 | char name[20]; 5 | int nob,blocks[30]; 6 | }; 7 | int main(){ 8 | struct fileTable ft[30]; 9 | int i,j,n; 10 | char s[20]; 11 | cin>>n; 12 | for(i=0;i>ft[i].name; 15 | cin>>ft[i].nob; 16 | for(j=0;j>ft[i].blocks[j]; 18 | } 19 | cin>>s; 20 | for(i=0;i 30 | #include 31 | #include 32 | #include 33 | 34 | int main() { 35 | 36 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 37 | int n,i,j,k; 38 | scanf("%d",&n); 39 | for(i=1;i<=n;i++) 40 | { 41 | for(j=1;j<=i-1;j++) 42 | printf(" "); 43 | 44 | printf("%d",i); 45 | 46 | printf("\n"); 47 | } 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Pattern3.CPP: -------------------------------------------------------------------------------- 1 | PRINT THE BELOW MENTIONED PATTERN FOR ANY "N" VALUE. WHERE "N" INDICATES NO.OF ROWS. 2 | 3 | Input Format 4 | 5 | A SINGLE INTEGER DENOTING N VALUE 6 | 7 | Constraints 8 | 9 | 1<=N<=100 10 | 11 | Output Format 12 | 13 | PATTERN AS SHOWN IN SAMPLE TEST CASE 14 | 15 | Sample Input 0 16 | 17 | 5 18 | Sample Output 0 19 | 20 | 1 21 | 12 22 | 123 23 | 1234 24 | 12345 25 | 26 | 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | int main() { 34 | 35 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 36 | int n,i,j,k; 37 | scanf("%d",&n); 38 | for(i=1;i<=n;i++) 39 | { 40 | for(j=1;j<=n-i;j++) 41 | printf(" "); 42 | for(k=1;k<=i;k++){ 43 | printf("%d",k); 44 | } 45 | printf("\n"); 46 | } 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Add 2 values.CPP: -------------------------------------------------------------------------------- 1 | Given two values, they can be integer or floating point numbers, add them. 2 | 3 | Input Format 4 | 5 | Two values separated by a space. 6 | 7 | Constraints 8 | 9 | No constraints. 10 | 11 | Output Format 12 | 13 | One value, the result of sum of two input values. 14 | 15 | Sample Input 0 16 | 17 | 5 4 18 | Sample Output 0 19 | 20 | 9 21 | Explanation 0 22 | 23 | 5 + 4 = 9 24 | 25 | 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | using namespace std; 34 | 35 | 36 | int main() { 37 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 38 | double a,b; 39 | cin>>a>>b; 40 | a=a+b; 41 | long int c=ceil(a); 42 | if(c>a){ 43 | cout< 21 | #define MAX_SIZE 100 22 | #define MAX_CHARS 255 23 | int main() 24 | { 25 | char str[MAX_SIZE]; 26 | int freq[MAX_CHARS]; 27 | int i=0,min; 28 | int ascii; 29 | scanf("%s",str); 30 | for(i=0;i 25 | #include 26 | #include 27 | #include 28 | #include 29 | using namespace std; 30 | 31 | 32 | int main() { 33 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 34 | long long int n; 35 | cin>>n; 36 | if(n==0){ 37 | cout<<0; 38 | return 0; 39 | } 40 | string s; 41 | while(n!=0){ 42 | int rem = n%2; 43 | s+=to_string(rem); 44 | n=n/2; 45 | } 46 | for(int i=s.length()-1;i>=0;i--){ 47 | cout<Aa|b 3 | output 4 | A'->aA' 5 | A->bA' 6 | A->e 7 | 8 | 9 | #include 10 | #include 11 | int main(){ 12 | char input[100],*l,*r,*temp,tempprod[20],productions[25][10]; 13 | int i=0,j=0,flag=0; 14 | printf("enter the productions"); 15 | scanf("%s",input); 16 | l=strtok(input,"->"); 17 | r=strtok(NULL,"->"); 18 | temp=strtok(r,"|"); 19 | while(temp){ 20 | if(temp[0]==l[0]){ 21 | flag=1; 22 | sprintf(productions[i++],"%s'->%s%s'\0",l,temp+1,l); 23 | } 24 | else 25 | sprintf(productions[i++],"%s->%s%s'\0",l,temp,l); 26 | temp=strtok(NULL,"|"); 27 | } 28 | sprintf(productions[i++],"%s->%c",l,(char)238); 29 | if(flag==0){ 30 | printf("No Left recursion"); 31 | } 32 | else{ 33 | for(j=0;j 26 | 27 | int main() { 28 | char s[100000],a[100000]; 29 | scanf("%s",s); 30 | int i,k=0,st[100001]; 31 | for(i=0;s[i]!='\0';i++) 32 | { 33 | if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u' || s[i]=='A' || s[i]=='E'|| s[i]=='I' || s[i]=='O' || s[i]=='U') 34 | { 35 | st[k]=i; 36 | a[k]=s[i]; 37 | k++; 38 | } 39 | } 40 | int end=k-1; 41 | for(i=0;i 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | using namespace std; 33 | 34 | int main() { 35 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 36 | int n; 37 | cin>>n; 38 | int a[n]; 39 | for(int i=0;i>a[i]; 41 | } 42 | sort(a,a+n); 43 | int res; 44 | res=a[n-1]*a[n-2]*a[n-3]; 45 | cout< 29 | #include 30 | #include 31 | #include 32 | 33 | int main() { 34 | 35 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 36 | int n,i,j,k,x; 37 | scanf("%d",&n); 38 | x=ceil(n/2.0); 39 | for(i=1;i<=x;i++) 40 | { 41 | for(j=1;j 33 | #include 34 | #include 35 | #include 36 | #include 37 | using namespace std; 38 | 39 | 40 | int main() { 41 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 42 | int n,i; 43 | cin>>n; 44 | for(i=0;i 37 | #include 38 | #include 39 | #include 40 | 41 | int main() { 42 | int n; 43 | scanf("%d",&n); 44 | for(int i=1;i<=n;i++){ 45 | if(i%2==1){ 46 | for(int j=1;j<=i;j++){ 47 | printf("%d",j%2); 48 | } 49 | } 50 | else{ 51 | for(int j=0;j 43 | #include 44 | #include 45 | #include 46 | #include 47 | using namespace std; 48 | 49 | 50 | int main() { 51 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 52 | int n,i,j,k=1; 53 | cin>>n; 54 | for(i=1;i0;j--) 57 | cout<<" "; 58 | for(j=0;j 30 | #include 31 | 32 | int main() 33 | { 34 | char string[100]; 35 | int c = 0, count[26] = {0}, x; 36 | 37 | scanf("%s",string); 38 | 39 | while (string[c] != '\0') { 40 | 41 | 42 | if (string[c] >= 'a' && string[c] <= 'z') { 43 | x = string[c] - 'a'; 44 | count[x]++; 45 | } 46 | 47 | c++; 48 | } 49 | 50 | for (c = 0; c <26; c++) 51 | { if(count[c]!=0) 52 | printf("%c=%d\n",c+'a',count[c]); 53 | } 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /Equal_bases.CPP: -------------------------------------------------------------------------------- 1 | Given a decimal number N, you need to find the number of bases 1 < b <= n such that when the number N is represented in base 'b', it ends in a zero. 2 | 3 | Input Format 4 | 5 | Input contains only one number, N. 6 | 7 | Constraints 8 | 9 | 1 <= N <= 10000 10 | 11 | Output Format 12 | 13 | Output one number, the number of bases b such that when number N is represented in base b, the number contains a trailing zero. 14 | 15 | Sample Input 0 16 | 17 | 12 18 | Sample Output 0 19 | 20 | 5 21 | Explanation 0 22 | 23 | Base : Number : 24 | 2 1100 25 | 3 110 26 | 4 30 27 | 5 22 28 | 6 20 29 | 7 15 30 | 8 14 31 | 9 13 32 | 10 12 33 | 11 11 34 | 12 10 35 | 36 | 12 ends with a zero in 5 different number systems - 2,3,4,6 and 12. Hence the answer is 12. 37 | 38 | 39 | 40 | 41 | #include 42 | int main() 43 | { 44 | int number, i,count=0,t; 45 | 46 | 47 | scanf("%d",&number); 48 | 49 | for(i=1; i <= number; ++i) 50 | { 51 | if (number%i == 0) 52 | { 53 | count++; 54 | } 55 | } 56 | t= count-1; 57 | printf("%d",t); 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /Forming_Numericals_Pattern.C: -------------------------------------------------------------------------------- 1 | Print the following pattern : 2 | 3 | N = 1 4 | 1 5 | 6 | N = 2 7 | 2 2 2 8 | 2 1 2 9 | 2 2 2 10 | 11 | N = 3 12 | 13 | 3 3 3 3 3 14 | 3 2 2 2 3 15 | 3 2 1 2 3 16 | 3 2 2 2 3 17 | 3 3 3 3 3 18 | 19 | and so on. 20 | Input Format 21 | 22 | One number N 23 | 24 | Constraints 25 | 26 | 1 <= N <= 10 27 | 28 | Output Format 29 | 30 | The pattern 31 | 32 | Sample Input 0 33 | 34 | 2 35 | Sample Output 0 36 | 37 | 2 2 2 38 | 2 1 2 39 | 2 2 2 40 | 41 | 42 | 43 | #include 44 | 45 | int main() { 46 | int n,i,j,k; 47 | scanf("%d",&n); 48 | for (i=n;i>=1;i--) { 49 | for (j=n;j>=i;j--) 50 | printf("%d ",j); 51 | for (j=1;j<(i*2)-1;j++) 52 | printf("%d ",i); 53 | for (j=i+1;j<=n;j++) 54 | printf("%d ",j); 55 | printf("\n"); 56 | } 57 | for (i=2;i<=n;i++) { 58 | for (j=n;j>=i;j--) 59 | printf("%d ",j); 60 | for (j=1;j<(i*2)-1;j++) 61 | printf("%d ",i); 62 | for (j=i+1;j<=n;j++) 63 | printf("%d ",j); 64 | printf("\n"); 65 | } 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /SmallestPalindrome.C: -------------------------------------------------------------------------------- 1 | Problem Description 2 | 3 | An integer is said to be a palindrome if its value is the same when read from both the right and left side. For a given positive Integer X , write a program that outputs the value of the smallest palindrome greater than X. 4 | 5 | Input 6 | Integer X such that X<10000. 7 | 8 | Output 9 | Output the smallest palindrome larger than X.Note : Numbers are always displayed without leading zeros. 10 | 11 | Test Case 1 12 | Input (stdin) 13 | 606 14 | 15 | Expected Output 16 | 616 17 | 18 | Test Case 2 19 | Input (stdin) 20 | 303 21 | 22 | Expected Output 23 | 313 24 | 25 | 26 | 27 | #include 28 | #include 29 | int main() 30 | {char i,j,len,str[50]; 31 | scanf("%s",str); 32 | len=strlen(str); 33 | j = len; 34 | i = -1; 35 | while(++i <= --j) { 36 | if(str[i] != str[j]) { 37 | break; 38 | } 39 | } 40 | if(j < i) { 41 | if(len & 1) { 42 | if(str[len/2] < '9'){ 43 | str[len/2]++; 44 | printf("%s\n",str); 45 | } 46 | 47 | else { 48 | str[len/2] = '0';} 49 | } 50 | } 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /Special_array_sorting.CPP: -------------------------------------------------------------------------------- 1 | 2 | Sample Input 0 3 | 4 | 2 5 | 4 6 | 1 0 3 2 7 | 3 8 | 2 1 0 9 | Sample Output 0 10 | 11 | Yes 12 | No 13 | 14 | 15 | 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | int main() { 23 | int test; 24 | scanf("%d",&test); 25 | for(int i=0;ia[i+1]){ 35 | if(a[i]-a[i+1]==1 || a[i+1]-a[i]==-1){ 36 | int temp=a[i]; 37 | a[i]=a[i+1]; 38 | a[i+1]=temp; 39 | } 40 | else{ 41 | flag=1; 42 | break; 43 | } 44 | } 45 | } 46 | if(flag==0) 47 | printf("Yes\n"); 48 | else 49 | printf("No\n"); 50 | } 51 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /StringRepetitions.CPP: -------------------------------------------------------------------------------- 1 | Given a string, find the smallest string that can generate the original string by concatenation. See example case for clarity. 2 | 3 | Input Format 4 | 5 | One string. String is guaranteed to contain lowercase letters only. 6 | 7 | Constraints 8 | 9 | 1 <= length of string <= 5000 10 | 11 | Output Format 12 | 13 | Print the smallest string that can be repeated to get the original string. 14 | 15 | Sample Input 0 16 | 17 | abababab 18 | Sample Output 0 19 | 20 | ab 21 | 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | using namespace std; 29 | 30 | 31 | int main() { 32 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 33 | string str; 34 | cin>>str; 35 | string str2="",str3; 36 | int i,j; 37 | for(i=1;i<=str.length();i++){ 38 | str3=""; 39 | str2+=str[i-1]; 40 | //cout< 14 | using namespace std; 15 | bool compare(pair &p1, 16 | pair &p2) 17 | { 18 | if (p1.second == p2.second) 19 | return p1.first < p2.first; 20 | return p1.second > p2.second; 21 | } 22 | void printSorted(int arr[], int n) 23 | { 24 | map m; 25 | for (int i = 0; i < n; i++) 26 | m[arr[i]]++; 27 | int s = m.size(); 28 | pair p[s]; 29 | int i = 0; 30 | for (auto it = m.begin(); it != m.end(); ++it) 31 | p[i++] = make_pair(it->first, it->second); 32 | sort(p, p + s, compare); 33 | 34 | cout << "Elements sorted by frequency are: "; 35 | for (int i = 0; i < s; i++) 36 | { 37 | int freq = p[i].second; 38 | while (freq--) 39 | cout << p[i].first << " "; 40 | } 41 | } 42 | int main() 43 | { 44 | int arr[] = {2, 3, 2, 4, 5, 12, 2, 3, 45 | 3, 3, 12}; 46 | int n = sizeof(arr)/ sizeof(arr[0]); 47 | printSorted(arr, n); 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Square in a circle.CPP: -------------------------------------------------------------------------------- 1 | A square is kept inside a circle. It keeps expanding untill all four of its vertices touch the circumfernce of the circle. Another smaller circle is kept inside the square now and it keeps expanding untill its circumference touches all four sides of the square. The outer and the inner circle form a ring. Find the area of this ring. 2 | 3 | Input 4 | Input consists of multiple test cases. 5 | Each test case contains one integer a denoting the side-length of the square between the two circles. 6 | 7 | Output 8 | Print the area of the ring. 9 | 10 | Sample Input 0 11 | 12 | 3 13 | 3 14 | 4 15 | 5 16 | Sample Output 0 17 | 18 | 7.068583 19 | 12.566371 20 | 19.634954 21 | 22 | 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | using namespace std; 30 | 31 | 32 | int main() { 33 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 34 | int t; 35 | cin>>t; 36 | while(t--) 37 | { 38 | double n; 39 | cin>>n; 40 | double r=n/sqrt(2); 41 | double r1=n/2; 42 | double a=M_PI*(r*r-r1*r1); 43 | printf("%.6f\n",a); 44 | 45 | } 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /LL_insertatanypos.CPP: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | struct node{ 8 | int data; 9 | struct node* next; 10 | }*head; 11 | struct val{ 12 | int p; 13 | int x; 14 | 15 | }; 16 | void insert(int p,int x){ 17 | struct node *n=new node(); 18 | n->data=x; 19 | n->next=NULL; 20 | if(head==NULL) 21 | head=n; 22 | else if(p==0){ 23 | n->next=head; 24 | head=n; 25 | } 26 | else{ 27 | struct node *t=new node(); 28 | t=head; 29 | p-=1; 30 | while(p-->0){ 31 | t=t->next; 32 | } 33 | n->next=t->next; 34 | t->next=n; 35 | } 36 | } 37 | int main() { 38 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 39 | int n; 40 | cin>>n; 41 | head=new node(); 42 | struct val v[n]; 43 | for(int i=0;i>v[i].p>>v[i].x; 45 | insert(v[i].p,v[i].x); 46 | } 47 | 48 | while(head->next->next!=NULL){ 49 | 50 | cout<data<<"->"; 51 | head=head->next; 52 | 53 | } 54 | cout<data; 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /MaximumElementonStack.CPP: -------------------------------------------------------------------------------- 1 | Output Format 2 | 3 | image 4 | 5 | Sample Input 0 6 | 7 | 10 8 | 1 97 9 | 2 10 | 1 20 11 | 2 12 | 1 26 13 | 1 20 14 | 2 15 | 3 16 | 1 91 17 | 3 18 | Sample Output 0 19 | 20 | 26 21 | 91 22 | 23 | 24 | 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | using namespace std; 33 | 34 | 35 | int main() { 36 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 37 | stack s,ma; 38 | int m = -1; 39 | int q,t,v; 40 | cin >> q; 41 | while(q--) 42 | { 43 | cin >> t; 44 | if(t == 1) 45 | { 46 | cin >> v; 47 | if(ma.empty()) 48 | ma.push(v); 49 | else if(v > ma.top()) 50 | { 51 | ma.push(v); 52 | } 53 | s.push(v); 54 | } 55 | else if(t == 2) 56 | { 57 | if(s.top() == ma.top()) 58 | { 59 | ma.pop(); 60 | } 61 | s.pop(); 62 | } 63 | else if(t == 3) 64 | { 65 | cout << ma.top() << endl; 66 | } 67 | } 68 | return 0; 69 | } 70 | 71 | -------------------------------------------------------------------------------- /Domino_Piles.CPP: -------------------------------------------------------------------------------- 1 | You are given a rectangular board of M × N squares. Also you are given an unlimited number of standard domino pieces of 2 × 1 squares. You are allowed to rotate the pieces. You are asked to place as many dominoes as possible on the board so as to meet the following conditions: 2 | 3 | 1.Each domino completely covers two squares. 4 | 5 | 2.No two dominoes overlap. 6 | 7 | 3.Each domino lies entirely inside the board. It is allowed to touch the edges of the board. 8 | 9 | Find the maximum number of dominoes, which can be placed under these restrictions. 10 | 11 | Input Format 12 | 13 | In a single line you are given two integers M and N — board sizes in squares 14 | 15 | Constraints 16 | 17 | 1 ≤ M ≤ N ≤ 16 18 | 19 | Output Format 20 | 21 | Output one number — the maximal number of dominoes, which can be placed. 22 | 23 | Sample Input 0 24 | 25 | 2 4 26 | Sample Output 0 27 | 28 | 4 29 | 30 | 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | using namespace std; 38 | 39 | 40 | int main() { 41 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 42 | int l,a,b; 43 | cin>>a>>b; 44 | l=a*b; 45 | cout< 29 | #include 30 | #include 31 | #include 32 | #include 33 | using namespace std; 34 | 35 | 36 | int main() { 37 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 38 | long long int a,b,c; 39 | cin>>a>>b; 40 | c=a; 41 | if(b==0){ 42 | cout<<"1"; 43 | } 44 | else{ 45 | for(int i=1;i 28 | 29 | using namespace std; 30 | 31 | int MyFunc(int n, int m) { 32 | // Complete this function 33 | string s = to_string(n); 34 | string sum = to_string(n+m); 35 | // cout<> n >> m; 46 | vector array(n); 47 | for(int array_i = 0; array_i < n; array_i++){ 48 | cin >> array[array_i]; 49 | } 50 | int result = MyFunc(n, m); 51 | cout << result << endl; 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /7 divisible_pairs.CPP: -------------------------------------------------------------------------------- 1 | You are given an array A[N] of size N. 2 | We define a 7-Divisible Pair as a pair of integers (i,j) such that Ai + Aj is divisible by 7. 3 | Formally, a pair of integers (i,j) is a 7-Divisible pair if ( Ai + Aj ) % 7 = 0. 4 | Your task is to find the total number of 7-Divisible pairs from the given array. 5 | 6 | INPUT 7 | 8 | First line contains N the size of the array. (1 ≤ N ≤ 105). 9 | Second line contains the array elements that all lie between 1 and 105. 10 | 11 | OUTPUT 12 | 13 | Output a single integer that denotes the number of pairs divisible by 7. 14 | 15 | Sample Input 0 16 | 17 | 5 18 | 9 3 7 4 14 19 | Sample Output 0 20 | 21 | 2 22 | 23 | 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | using namespace std; 31 | 32 | 33 | int main() { 34 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 35 | int n; 36 | cin>>n; 37 | int t; 38 | int rem[7] = {0}; 39 | for(int i=0;i>t; 41 | rem[t%7]++; 42 | } 43 | int count = 0; 44 | count+=(rem[1]*rem[6]); 45 | count+=(rem[2]*rem[5]); 46 | count+=(rem[3]*rem[4]); 47 | count+=(rem[0]*(rem[0]-1)/2); 48 | cout< 22 | #include 23 | using namespace std; 24 | 25 | int gcd(int a, int b) 26 | { 27 | if (a == 0) 28 | return b; 29 | return gcd(b%a, a); 30 | } 31 | int commDiv(int a,int b) 32 | { 33 | // find gcd of a,b 34 | int n = gcd(a, b); 35 | 36 | // Count divisors of n. 37 | int result = 0; 38 | for (int i=1; i<=sqrt(n); i++) 39 | { 40 | // if 'i' is factor of n 41 | if (n%i==0) 42 | { 43 | // check if divisors are equal 44 | if (n/i == i) 45 | result += 1; 46 | else 47 | result += 2; 48 | } 49 | } 50 | return result; 51 | } 52 | 53 | // Driver program to run the case 54 | int main() 55 | { 56 | int a,b; 57 | cin>>a>>b; 58 | 59 | cout << commDiv(a, b); 60 | return 0; 61 | } 62 | 63 | -------------------------------------------------------------------------------- /Pattern_printing9.C: -------------------------------------------------------------------------------- 1 | Given a number N, print a pattern as shown below : 2 | 3 | n = 1 4 | * 5 | 6 | n = 2 7 | * 8 | *** 9 | * 10 | 11 | n = 3 12 | * 13 | *** 14 | ***** 15 | *** 16 | * 17 | and so on.. 18 | Hint : Print the upper triangle and the lower reverse triangle separately. 19 | 20 | Input Format 21 | 22 | Only one integer, the number n. 23 | 24 | Constraints 25 | 26 | 1 <= n <= 100 27 | 28 | Output Format 29 | 30 | The required pattern 31 | 32 | Sample Input 0 33 | 34 | 1 35 | Sample Output 0 36 | 37 | * 38 | 39 | 40 | #include 41 | #include 42 | #include 43 | #include 44 | 45 | int main() { 46 | int n, c, k, space = 1; 47 | 48 | //printf("Enter number of rows\n"); 49 | scanf("%d", &n); 50 | 51 | space = n - 1; 52 | 53 | for (k = 1; k <= n; k++) 54 | { 55 | for (c = 1; c <= space; c++) 56 | printf(" "); 57 | 58 | space--; 59 | 60 | for (c = 1; c <= 2*k-1; c++) 61 | printf("*"); 62 | 63 | printf("\n"); 64 | } 65 | 66 | space = 1; 67 | 68 | for (k = 1; k <= n - 1; k++) 69 | { 70 | for (c = 1; c <= space; c++) 71 | printf(" "); 72 | 73 | space++; 74 | 75 | for (c = 1 ; c <= 2*(n-k)-1; c++) 76 | printf("*"); 77 | 78 | printf("\n"); 79 | } 80 | 81 | return 0; 82 | 83 | } 84 | -------------------------------------------------------------------------------- /Bug and Secret Santa.C: -------------------------------------------------------------------------------- 1 | Bug's friends play Secret Santa. He has n friends. Lets number them from 1 to n. For each friend i he knows pi, the person that i gave a gift to. Bug needs to find out ri for each friend i, which is the person that i received a gift from. 2 | 3 | Input Format 4 | 5 | First line contains one number n. Second line contains n space separated integers. 6 | 7 | Constraints 8 | 9 | 1 <= n <= 100 10 | 11 | Output Format 12 | 13 | Print n space-separated integers: the i-th number should equal the number of the friend who gave a gift to friend number i. 14 | 15 | Sample Input 0 16 | 17 | 4 18 | 2 3 4 1 19 | Sample Output 0 20 | 21 | 4 1 2 3 22 | Explanation 0 23 | 24 | Ouput is 25 | 26 | 4 : because 1 received a gift from 4 27 | 28 | 1 : because 2 received a gift from 1 29 | 30 | 2 : because 3 received a gift from 2 31 | 32 | 3 : because 4 received a gift from 3 33 | 34 | 35 | 36 | #include 37 | #include 38 | #include 39 | #include 40 | 41 | int main() { 42 | int n; 43 | scanf("%d",&n); 44 | int a[n]; 45 | for(int i=0;i 29 | #include 30 | #include 31 | #include 32 | #include 33 | using namespace std; 34 | 35 | 36 | int main() { 37 | int i,n,l,row=0,direction=0; 38 | string str; 39 | cin>>str; 40 | cin>>n; 41 | string a[n]; 42 | l=str.length(); 43 | for(i=0;i 26 | #include 27 | #include 28 | #include 29 | #include 30 | using namespace std; 31 | 32 | 33 | int main() { 34 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 35 | int a,b,c; 36 | long long l,m; 37 | cin>>a>>b>>c; 38 | l=a/c; 39 | if(a%c!=0){ 40 | l++; 41 | } 42 | m=b/c; 43 | if(b%c!=0){ 44 | m++; 45 | } 46 | cout< 1 and A > 0 where A and P both should be integers. Now he wonders, there is a possibility that he does not exist at all because he does not satisfy the A^P expressibility condition. Given the values of A and P, find out if X exists or not. 2 | 3 | Input Format 4 | 5 | Input contains the value of integer X. 6 | 7 | Constraints 8 | 9 | 0 < X < 2x10^9 10 | 11 | Output Format 12 | 13 | Output "yes" if X can be expressed as A^P and "no" otherwise. 14 | 15 | Sample Input 0 16 | 17 | 4 18 | Sample Output 0 19 | 20 | yes 21 | Explanation 0 22 | 23 | Yes as 2^2 = 4 24 | 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | using namespace std; 32 | 33 | 34 | int main() { 35 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 36 | int x,flag=0; 37 | cin>>x; 38 | if (x<2) 39 | {cout<<"yes"; 40 | flag=1;} 41 | 42 | for (auto i = 2; i<=sqrt(x); i++) 43 | { 44 | for (auto j = 2; j<=32; j++) 45 | { 46 | if(pow(i, j)==x) 47 | {cout<<"yes"; 48 | flag=1;} 49 | } 50 | } 51 | if(flag==0) 52 | cout<<"no"; 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /N_as_sum_of_primes.CPP: -------------------------------------------------------------------------------- 1 | Given a number N, print "YES" if N can be expressed as a sum of 2 prime numbers and "NO" otherwise. 2 | 3 | Input Format 4 | 5 | One number, N. 6 | 7 | Constraints 8 | 9 | 1 <= N <= 10^5. 10 | 11 | Output Format 12 | 13 | "YES" or "NO" 14 | 15 | Sample Input 0 16 | 17 | 5 18 | Sample Output 0 19 | 20 | YES 21 | Explanation 0 22 | 23 | 5 = 2 + 3 24 | 25 | Sample Input 1 26 | 27 | 11 28 | Sample Output 1 29 | 30 | NO 31 | 32 | 33 | 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | using namespace std; 40 | 41 | 42 | int power(int a, unsigned int n, int p){ 43 | int res = 1; 44 | a = a % p; 45 | while (n>0){ 46 | if (n & 1) 47 | res = (res*a)%p; 48 | n = n >> 1; 49 | a = (a*a)%p; 50 | } 51 | return res; 52 | } 53 | 54 | bool isPrime(int n, int k){ 55 | if (n==1 || (n%2==0 && n!=2)) 56 | return false; 57 | if (n<=3) 58 | return true; 59 | while (k>0){ 60 | int a = 2 + rand()%(n-4); 61 | if(power(a, n-1, n) != 1) 62 | return false; 63 | k--; 64 | } 65 | return true; 66 | } 67 | 68 | 69 | int main() { 70 | int n; 71 | cin >> n; 72 | if (n%2==0) 73 | cout << "YES"; 74 | else if(isPrime(n-2, 10)) 75 | cout << "YES"; 76 | else 77 | cout << "NO"; 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /Let us xor.CPP: -------------------------------------------------------------------------------- 1 | There are 2 positive integers a and b such that they have a sum of s and the bitwise XOR of the numbers is x. How many possible values exist for the ordered pair (a, b)? 2 | 3 | Input Format 4 | 5 | The first line of the input contains s and x, the sum and bitwise xor of the pair of positive integers, respectively. 6 | 7 | Constraints 8 | 9 | (2 ≤ s ≤ 1e12) where 1eN stands for Nth power of 10 10 | 11 | (0 ≤ x ≤ 1e12) where 1eN stands for Nth power of 10 12 | 13 | Output Format 14 | 15 | Print a single integer, the number of solutions under the given conditions. If no solutions exist, then print 0. 16 | 17 | Sample Input 0 18 | 19 | 9 5 20 | Sample Output 0 21 | 22 | 4 23 | Sample Input 1 24 | 25 | 3 3 26 | Sample Output 1 27 | 28 | 2 29 | Sample Input 2 30 | 31 | 5 2 32 | Sample Output 2 33 | 34 | 0 35 | 36 | 37 | 38 | 39 | #include 40 | #include 41 | using namespace std; 42 | typedef long long ll; 43 | ll sum,XOR,AND; 44 | ll one_bits,res; 45 | 46 | int main() 47 | { 48 | scanf("%lld%lld",&sum,&XOR); 49 | AND=(sum-XOR)/2; 50 | if((sum-XOR)%2!=0||sum>=1; 58 | } 59 | res=(1ll< 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | using namespace std; 38 | 39 | 40 | int main() { 41 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 42 | string s; 43 | cin>>s; 44 | int n=s.length(); 45 | for(int i=0;i 29 | 30 | #define N 100000005 31 | #define long long long 32 | #define max(x, y) ((x > y) ? x : y) 33 | 34 | char comp[N], sieve[N]; 35 | 36 | int main() 37 | { 38 | long a, b, i, j, ans = 0; 39 | scanf("%lld %lld", &a, &b); 40 | for(j = 2; j*j <= b; ++j) 41 | { 42 | if(sieve[j]) 43 | continue; 44 | for(i = j+j; i*i <= b; i += j) 45 | sieve[i] = 1; 46 | for(i = j*max(2, a/j + (a%j > 0)); i <= b; i += j) 47 | comp[i-a+1] = 1; 48 | } 49 | for(i = max(2, a); i <= b; ++i) 50 | if(!comp[i-a+1]) 51 | ans++; 52 | printf("%lld", ans); 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /Lex Characters Unique.CPP: -------------------------------------------------------------------------------- 1 | Given a string consisting of lowercase english letters only, print all the unique characters in it in lexicographically increasing order. 2 | 3 | Input Format 4 | 5 | One string S. 6 | 7 | Constraints 8 | 9 | 1 <= strlen(S) <= 1000 10 | 11 | Output Format 12 | 13 | One string consisting of the unique characters of string S in alphabetic order. 14 | 15 | Sample Input 0 16 | 17 | alohamora 18 | Sample Output 0 19 | 20 | ahlmor 21 | 22 | 23 | 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | using namespace std; 31 | 32 | 33 | int main() { 34 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 35 | string s; 36 | cin>>s; 37 | int flag; 38 | string str=""; 39 | str+=s[0]; 40 | for(int i=1;istr[j]){ 56 | char temp = str[i]; 57 | str[i] = str[j]; 58 | str[j] = temp; 59 | } 60 | } 61 | } 62 | 63 | cout< 52 | #include 53 | #include 54 | #include 55 | #include 56 | #include 57 | using namespace std; 58 | 59 | 60 | int main() { 61 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 62 | int n; 63 | cin >> n; 64 | stack s; 65 | int ans = 0,temp; 66 | for(int i = 0;i < n;i++) 67 | { 68 | cin >> temp; 69 | while(!s.empty() && temp > s.top()) 70 | { 71 | s.pop(); 72 | ans++; 73 | } 74 | if(!s.empty()) ans++; 75 | s.push(temp); 76 | } 77 | cout << ans; 78 | } 79 | -------------------------------------------------------------------------------- /Array/Gfg/Relative_Sorting.CPP: -------------------------------------------------------------------------------- 1 | Given two arrays A1[] and A2[] of size N and M respectively. 2 | The task is to sort A1 in such a way that the relative order among the elements will be same as those in A2. 3 | For the elements not present in A2, append them at last in sorted order. 4 | It is also given that the number of elements in A2[] are smaller than or equal to number of elements in A1[] and A2[] has all distinct elements. 5 | 6 | Note: Expected time complexity is O(N log(N)). 7 | 8 | 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | int main() { 15 | int t; 16 | cin>>t; 17 | while(t--) 18 | { 19 | int n,m; 20 | cin>>n>>m; 21 | vector arr(n); 22 | for(int i=0;i>arr[i]; 24 | vector brr(m); 25 | for(int i=0;i>brr[i]; 27 | map mp; 28 | for(int i=0;isecond) 47 | { 48 | cout<first<<" "; 49 | it->second--; 50 | } 51 | } 52 | 53 | cout< 4 | using namespace std; 5 | 6 | // Returns index of key in arr[l..h] if 7 | // key is present, otherwise returns -1 8 | int search(int arr[], int l, int h, int key) 9 | { 10 | if (l > h) return -1; 11 | 12 | int mid = (l+h)/2; 13 | if (arr[mid] == key) return mid; 14 | 15 | /* If arr[l...mid] is sorted */ 16 | if (arr[l] <= arr[mid]) 17 | { 18 | /* As this subarray is sorted, we can quickly 19 | check if key lies in half or other half */ 20 | if (key >= arr[l] && key <= arr[mid]) 21 | return search(arr, l, mid-1, key); 22 | /*If key not lies in first half subarray, 23 | Divide other half into two subarrays, 24 | such that we can quickly check if key lies 25 | in other half */ 26 | return search(arr, mid+1, h, key); 27 | } 28 | 29 | /* If arr[l..mid] first subarray is not sorted, then arr[mid... h] 30 | must be sorted subarray */ 31 | if (key >= arr[mid] && key <= arr[h]) 32 | return search(arr, mid+1, h, key); 33 | 34 | return search(arr, l, mid-1, key); 35 | } 36 | 37 | // Driver program 38 | int main() 39 | { 40 | int arr[] = {4, 5, 6, 7, 8, 9, 1, 2, 3}; 41 | int n = sizeof(arr)/sizeof(arr[0]); 42 | int key = 6; 43 | int i = search(arr, 0, n-1, key); 44 | 45 | if (i != -1) 46 | cout << "Index: " << i << endl; 47 | else 48 | cout << "Key not found"; 49 | } 50 | -------------------------------------------------------------------------------- /Athletic_Akhil.CPP: -------------------------------------------------------------------------------- 1 | Akhil is an athlete. 2 | 3 | As a part of his training he started working on his jumping skills recently. Currently he's located at point 0. He needs to get to the point x as a part of his training regime. 4 | 5 | To make the training more fruitful, he has decided that he'll first jump by only one unit, and each subsequent jump will be exactly one longer than the previous one. He can go either left or right with each jump. He wonders how many jumps he needs to reach the point x. 6 | 7 | Input Format 8 | 9 | The input data consists of only one integer x 10 | 11 | Constraints 12 | 13 | ( - 1e9 ≤ x ≤ 1e9) where 1eN stands for Nth power of 10 14 | 15 | Output Format 16 | 17 | Output the minimal number of jumps that Akhil requires to reach x. 18 | 19 | Sample Input 0 20 | 21 | 2 22 | Sample Output 0 23 | 24 | 3 25 | Sample Input 1 26 | 27 | 6 28 | Sample Output 1 29 | 30 | 3 31 | Sample Input 2 32 | 33 | 0 34 | Sample Output 2 35 | 36 | 0 37 | 38 | 39 | 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | using namespace std; 46 | 47 | 48 | int main() { 49 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 50 | 51 | 52 | 53 | long long x; 54 | scanf("%lld",&x); 55 | if(x<0)x=-x; 56 | int n= 0; 57 | int current = 0; 58 | while(current 25 | #include 26 | #include 27 | #include 28 | #include 29 | using namespace std; 30 | int main() { 31 | int n; 32 | cin>>n; 33 | int i,arr[n]; 34 | for(i=0;i>arr[i]; 36 | int k=n-1; 37 | int temp=0,index; 38 | int b[n]; 39 | for(i=0;i 25 | #include 26 | #include 27 | #include 28 | #include 29 | using namespace std; 30 | 31 | 32 | int main() { 33 | 34 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 35 | int n,a[100],i; 36 | cin>>n; 37 | for(i=1; i<=n; i++) 38 | { 39 | cin>>a[i]; 40 | } 41 | int f1=1; 42 | a[n+1]=a[0]=1<<30; 43 | int f=2; 44 | while(a[f]>a[f-1]) f++; 45 | while(a[f]==a[f-1]) f++; 46 | while(a[f] 19 | #include 20 | #include 21 | #include 22 | #include 23 | using namespace std; 24 | 25 | 26 | int main() { 27 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 28 | string s; 29 | cin>>s; 30 | int i,c=0; 31 | string s1="Goodbye"; 32 | int len=s.length(); 33 | int k=0; 34 | for(i=0;i 25 | #include 26 | using namespace std; 27 | 28 | int main(){ 29 | string s; 30 | int n,k; 31 | cin>>n; 32 | cin>>s; 33 | for(int i=0;i 35 | using namespace std ; 36 | int a[10] , l , m = 1e9 ; 37 | int main() 38 | { 39 | int n ; 40 | cin >> n ; 41 | for ( int i = 1 ; i<=9 ; i++) 42 | cin >> a[i], m = min( m , a[i] ) ; 43 | l = n/m; 44 | if ( l== 0 ) 45 | { 46 | cout << -1 ; 47 | return 0 ; 48 | } 49 | while ( l-- ) 50 | for ( int i =9 ; i>= 0 ; i--) 51 | if ((n-a[i]) / m == l && n>=a[i]) 52 | { 53 | cout << i ; 54 | n-=a[i] ; 55 | break ; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /OS/FCFS.CPP: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void waitingtime(int p[],int n,int bt[],int wt[],int at[]){ 4 | int ct[n]; 5 | ct[0]=0; 6 | wt[0]=0; 7 | for(int i=1;i=0;i--){ 55 | if(i+k>=n) { 56 | int p=(i+k)%n; 57 | b[p]=a[i]; 58 | }else b[i+k]=a[i]; 59 | } 60 | 61 | 62 | for(int i=0;i 32 | #include 33 | #include 34 | #include 35 | #include 36 | using namespace std; 37 | 38 | 39 | int main() { 40 | long long int n,m; 41 | cin>>n>>m; 42 | long long int i,arr[n],brr[m],count[m],j; 43 | for(i=0;i>arr[i]; 45 | for(i=0;i>brr[i]; 47 | sort(arr,arr+n); 48 | for(i=0;i=arr[j]) 55 | count[i]++; 56 | else 57 | break; 58 | } 59 | } 60 | for(i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | void findMaximumNum(string str, int k, string& max) 10 | { 11 | // return if no swaps left 12 | if(k == 0) 13 | return; 14 | 15 | int n = str.length(); 16 | 17 | // consider every digit 18 | for (int i = 0; i < n - 1; i++) 19 | { 20 | 21 | // and compare it with all digits after it 22 | for (int j = i + 1; j < n; j++) 23 | { 24 | // if digit at position i is less than digit 25 | // at position j, swap it and check for maximum 26 | // number so far and recurse for remaining swaps 27 | if (str[i] < str[j]) 28 | { 29 | // swap str[i] with str[j] 30 | swap(str[i], str[j]); 31 | 32 | // If current num is more than maximum so far 33 | if (str.compare(max) > 0) 34 | max = str; 35 | 36 | // recurse of the other k - 1 swaps 37 | findMaximumNum(str, k - 1, max); 38 | 39 | // backtrack 40 | swap(str[i], str[j]); 41 | } 42 | } 43 | } 44 | } 45 | 46 | // Driver code 47 | int main() 48 | { 49 | string str ; 50 | cin>>str; 51 | 52 | int k = 4; 53 | 54 | string max = str; 55 | findMaximumNum(str, k, max); 56 | 57 | cout << max << endl; 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /Consecutiveprimesum.CPP: -------------------------------------------------------------------------------- 1 | Some prime numbers can be expressed as Sum of other consecutive prime numbers. 2 | 3 | For example 4 | 5 | 5 = 2 + 3 6 | 17 = 2 + 3 + 5 + 7 7 | 41 = 2 + 3 + 5 + 7 + 11 + 13 8 | 9 | Your task is to find out how many prime numbers which satisfy this property are present in the range 3 to N subject to a constraint that summation should always start with number 2. 10 | 11 | Write code to find out number of prime numbers that satisfy the above mentioned property in a given range. 12 | 13 | Input Format: 14 | Each test case contains a number N <= 1000000000 15 | 16 | Output Format: 17 | Print the total number of all such prime numbers which are less than or equal to N. 18 | 19 | 20 | 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | using namespace std; 28 | 29 | 30 | bool isPrime(int n){ 31 | if(n<=1) 32 | return false; 33 | for(int i=2;i<=sqrt(n);i++){ 34 | if(n%i==0) 35 | return false; 36 | } 37 | return true; 38 | } 39 | 40 | int main() { 41 | 42 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 43 | int n; 44 | cin>>n; 45 | int sum=2,count=0; 46 | if(n<5){ 47 | cout<<0; 48 | return 0; 49 | } 50 | for(int i=3;sum<=n;i++){ 51 | if(isPrime(i)){ 52 | sum+=i; 53 | if(sum<=n){ 54 | if(isPrime(sum)){ 55 | count++; 56 | } 57 | } 58 | 59 | } 60 | } 61 | cout< 22 | #include 23 | #include 24 | #include 25 | #include 26 | using namespace std; 27 | 28 | 29 | int main() { 30 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 31 | int n,s; 32 | s=0; 33 | cin>>n; 34 | if(n>=1700&&n<=1917) 35 | { 36 | if(n%4==0) 37 | { 38 | s=s+31+29+31+30+31+30+31+31; 39 | // cout<=1919) 52 | { 53 | if((n%400==0)||(n%4==0 && n%100!=0)) 54 | { 55 | s=s+31+29+31+30+31+30+31+31; 56 | // cout<data = val; 15 | t->next = NULL; 16 | return t; 17 | } 18 | 19 | void push(int val) 20 | { 21 | queueNode* p,*p1; 22 | p=newNode(val); 23 | 24 | if(head==NULL) 25 | head=p; 26 | else 27 | { 28 | p1=head; 29 | while(p1->next!=NULL) 30 | { 31 | p1=p1->next; 32 | } 33 | p1->next=p; 34 | } 35 | } 36 | void pop() 37 | { 38 | queueNode* p1; 39 | 40 | p1=head; 41 | head=head->next; 42 | p1->next=NULL; 43 | } 44 | int top() 45 | { 46 | return head->data; 47 | } 48 | int empty() 49 | { 50 | return (head==NULL); 51 | 52 | } 53 | int main() 54 | { 55 | int n; 56 | scanf("%d", &n); 57 | for (int i = 0; i < n; i++) 58 | { 59 | char s[50]; 60 | int x; 61 | scanf(" %s", s); 62 | if (s[0] == 't') 63 | { 64 | if (empty()) printf("Invalid\n"); 65 | else 66 | printf("%d\n", top()); 67 | } 68 | else if (s[0] == 'p' && s[1] == 'o') 69 | { 70 | if (empty()) printf("Invalid\n"); 71 | else pop(); 72 | } 73 | else 74 | { 75 | scanf(" %d", &x); 76 | push(x); 77 | } 78 | } 79 | return 0; 80 | } 81 | -------------------------------------------------------------------------------- /Eat less.CPP: -------------------------------------------------------------------------------- 1 | Marc loves cupcakes, but he also likes to stay fit. He eats n cupcakes in one sitting, and each cupcake i has a calorie count c[i] . After eating a cupcake with c calories, he must walk at least 2^j * c (where j is the number cupcakes he has already eaten) miles to maintain his weight. 2 | 3 | Given the individual calorie counts for each of the cupcakes, find and print a long integer denoting the minimum number of miles Marc must walk to maintain his weight. Note that he can eat the cupcakes in any order. 4 | 5 | Notes: Has to be done greedily so that you have to burn as less calories as possible 6 | 7 | Input Format 8 | 9 | The first line contains an integer, N , denoting the number of cupcakes. The second line contains N space-separated integers describing the respective calorie counts of each cupcake,c[1]...c[n]. 10 | 11 | Constraints 12 | 13 | 1<=n<=40 1<=ci<=1000 14 | 15 | Output Format 16 | 17 | Print a long integer denoting the minimum number of miles Marc must walk to maintain his weight. 18 | 19 | Sample Input 0 20 | 21 | 3 22 | 1 3 2 23 | Sample Output 0 24 | 25 | 11 26 | 27 | 28 | 29 | 30 | 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | using namespace std; 37 | 38 | 39 | int main() { 40 | long long n; 41 | cin>>n; 42 | long long a[n],i,j=0,s=0; 43 | for(i=0;i>a[i]; 45 | sort(a,a+n,greater<>()); 46 | for(i=0;i 2 | #include 3 | 4 | int digit(char); 5 | 6 | int main(){ 7 | 8 | char roman_Number[1000]; 9 | int i=0; 10 | long int number =0; 11 | 12 | printf("Enter any roman number (Valid digits are I, V, X, L, C, D, M): \n"); 13 | scanf("%s",roman_Number); 14 | 15 | while(roman_Number[i]!=0){ 16 | 17 | if(digit(roman_Number[i]) < 0){ 18 | printf("Invalid roman digit : %c",roman_Number[i]); 19 | return 0; 20 | } 21 | 22 | if((strlen(roman_Number) -i) > 2){ 23 | if(digit(roman_Number[i]) < digit(roman_Number[i+2])){ 24 | printf("Invalid roman number"); 25 | return 0; 26 | } 27 | } 28 | 29 | if(digit(roman_Number[i]) >= digit(roman_Number[i+1])) 30 | number = number + digit(roman_Number[i]); 31 | else{ 32 | number = number + (digit(roman_Number[i+1]) - digit(roman_Number[i])); 33 | i++; 34 | } 35 | i++; 36 | } 37 | 38 | printf("Its decimal value is : %ld",number); 39 | 40 | return 0; 41 | 42 | } 43 | 44 | int digit(char c){ 45 | 46 | int value=0; 47 | 48 | switch(c){ 49 | case 'I': value = 1; break; 50 | case 'V': value = 5; break; 51 | case 'X': value = 10; break; 52 | case 'L': value = 50; break; 53 | case 'C': value = 100; break; 54 | case 'D': value = 500; break; 55 | case 'M': value = 1000; break; 56 | case '\0': value = 0; break; 57 | default: value = -1; 58 | } 59 | 60 | return value; 61 | } 62 | -------------------------------------------------------------------------------- /Electronic_Store_Sort.CPP: -------------------------------------------------------------------------------- 1 | The server is finally happy that he sorted the line and now he gets to sell his portable chargers. But alas, a new person enters with a battery percentage of k comes to the store and stands at the back of the queue. Now he has to fit him in this already ascending ordered queue. He asks for your help. 2 | 3 | Formally, you are given an array of size n where the first n - 1 elements are sorted in ascending order and the last element can be any value. Sort this array by sliding the last element into its correct position. 4 | 5 | INPUT 6 | 7 | The first line of input is n (1≤n≤100), the number of customers in the queue. The second line of input is the battery percentages of the n customers (positive numbers less than 100) 8 | the first n - 1 of which are standing in ascending order according to their battery percentage. 9 | 10 | OUTPUT 11 | 12 | Print the battery percentages of the customers in a line after the new person has been put in his right place in the queue. 13 | 14 | Sample Input 0 15 | 16 | 6 17 | 1 3 5 7 9 4 18 | Sample Output 0 19 | 20 | 1 3 4 5 7 9 21 | 22 | 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | using namespace std; 30 | int main() { 31 | int n; 32 | cin>>n; 33 | int arr[n],i,j; 34 | for(i=0;i>arr[i]; 36 | int key; 37 | for(j=1;j0&&arr[i]>key) 42 | { 43 | arr[i+1]=arr[i]; 44 | i=i-1; 45 | } 46 | arr[i+1]=key; 47 | } 48 | 49 | for(i=0;i ai. 4 | 5 | Input Format 6 | 7 | The first line of the input contains integer n — the number of painting. 8 | 9 | The second line contains the sequence a1, a2, ..., an, where ai means the beauty of the i-th painting. 10 | 11 | Constraints 12 | 13 | 1 <= n <= 1000 14 | 1 <= ai <= 1000 15 | 16 | Output Format 17 | 18 | Print one integer — the maximum possible number of neighbouring pairs, such that ai + 1 > ai, after the optimal rearrangement. 19 | 20 | Sample Input 0 21 | 22 | 4 23 | 200 100 100 200 24 | Sample Output 0 25 | 26 | 2 27 | Explanation 0 28 | 29 | Sample Ordering that gives answer 2 : 30 | 31 | 100 200 100 200 32 | 33 | 34 | 35 | #include 36 | #include 37 | #include 38 | #include 39 | int n,x,a[1005],cnt,ans; 40 | int main(){ 41 | scanf("%d",&n); 42 | //cin>>n; 43 | for (int i=1;i<=n;i++){ 44 | scanf("%d",&x); 45 | //cin>>x; 46 | a[x]++; 47 | } 48 | while (1){ 49 | if (n==0) break; 50 | int cnt=0; 51 | for (int i=1;i<=1000;i++) if (a[i]) cnt++,a[i]--,n--; 52 | ans+=cnt-1; 53 | } 54 | printf("%d\n",ans); 55 | //cout<= 0; j--) { 59 | printf("%d ", stackArr[j]); 60 | }printf("\n"); 61 | } 62 | else { 63 | if (empty()) { 64 | printf("Invalid\n"); 65 | continue; 66 | } 67 | printf("%d\n", peek()); 68 | } 69 | } 70 | return 0; 71 | } 72 | 73 | -------------------------------------------------------------------------------- /Canadian to turn off light bulb.CPP: -------------------------------------------------------------------------------- 1 | Robin Scherbatsky, a proud canadian wants to turn on all the light bulbs. 2 | But since she is a Canadian and she is scared to go into the dark. 3 | 4 | N light bulbs are connected by a wire. Each bulb has a switch associated with it, however due to faulty wiring, a switch also changes the state of all the bulbs to the right of current bulb. Given an initial state of all bulbs, find the minimum number of switches you have to press to turn on all the bulbs. You can press the same switch multiple times. 5 | 6 | Note : 0 represents the bulb is off and 1 represents the bulb is on. 7 | 8 | Input Format 9 | 10 | First line contains an integer N. 11 | Next line contains N binary integers (0 or 1). 12 | 13 | Constraints 14 | 15 | 1 <= N <= 105 16 | 17 | Output Format 18 | 19 | Output the minimum number of switches you'll need to press. 20 | 21 | Sample Input 0 22 | 23 | 4 24 | 0 1 0 1 25 | Sample Output 0 26 | 27 | 4 28 | 29 | 30 | 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | using namespace std; 38 | 39 | 40 | int main() { 41 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 42 | int n, count=0; 43 | cin>>n; 44 | int a[n]; 45 | for(int i=0;i>a[i]; 48 | } 49 | for(int i=0;i 38 | #include 39 | #include 40 | #include 41 | #include 42 | using namespace std; 43 | 44 | 45 | int main() { 46 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 47 | int n,i; 48 | cin>>n; 49 | int ratings[n]; 50 | for(i=0;i>ratings[i]; 52 | int candy[n],sum=0; 53 | for(i=0;iratings[i-1]) 57 | candy[i]=candy[i-1]+1; 58 | for(i=n-1;i>0;i--) 59 | if(ratings[i-1]>ratings[i] && candy[i-1]<=candy[i]) 60 | candy[i-1]=candy[i]+1; 61 | for(i=0;i 41 | using namespace std; 42 | int main() 43 | { 44 | int n; 45 | cin>>n; 46 | int a[n]; 47 | for(int i=0;i>a[i]; 49 | sort(a,a+n); 50 | long long int ans=a[n-1]; 51 | if(n>1) 52 | ans=ans-a[0]; 53 | for(int i=1;i 37 | #include 38 | #include 39 | #include 40 | long m=1e9+7; 41 | long numDecodings(char* A) 42 | { 43 | int *count,i,n; 44 | if(!A) return 0; 45 | n=strlen(A); 46 | if(n==1) return 1; 47 | count = (int*)malloc(sizeof(int)*(n+1)); 48 | count[0] = count[1] = 1; 49 | for (i = 2; i <= n; i++) 50 | { 51 | count[i] = 0; 52 | if (A[i-1] >= '0') count[i] = count[i-1]; 53 | if (A[i-2]=='0'||A[i-2] == '1' || (A[i-2] == '2' && A[i-1] < '6') ) count[i] = (count[i]+count[i-2])%m; 54 | } 55 | return count[n]%m;} 56 | 57 | int main() 58 | { 59 | char ch[1000005]; 60 | scanf("%s",ch); 61 | long x=numDecodings(ch); 62 | printf("%ld",x); 63 | } 64 | -------------------------------------------------------------------------------- /Array/Gfg/Next Greater Element.CPP: -------------------------------------------------------------------------------- 1 | For the input array [4, 5, 2, 25}, the next greater elements for each element are as follows. 2 | 3 | Element NGE 4 | 4 --> 5 5 | 5 --> 25 6 | 2 --> 25 7 | 25 --> -1 8 | 9 | 10 | #include 11 | 12 | using namespace std; 13 | 14 | /* prints element and NGE pair for all 15 | elements of arr[] of size n */ 16 | void printNGE(int arr[], int n) 17 | { 18 | stack s; 19 | unordered_map mp; 20 | 21 | /* push the first element to stack */ 22 | s.push(arr[0]); 23 | 24 | 25 | // iterate for rest of the elements 26 | for (int i = 1; i < n; i++) { 27 | 28 | if (s.empty()) { 29 | s.push(arr[i]); 30 | continue; 31 | } 32 | 33 | /* if stack is not empty, then 34 | pop an element from stack. 35 | If the popped element is smaller 36 | than next, then 37 | a) print the pair 38 | b) keep popping while elements are 39 | smaller and stack is not empty */ 40 | while (s.empty() == false && s.top() < arr[i]) { 41 | mp[s.top()] = arr[i]; 42 | s.pop(); 43 | } 44 | 45 | /* push next to stack so that we can find 46 | next smaller for it */ 47 | s.push(arr[i]); 48 | } 49 | 50 | /* After iterating over the loop, the remaining 51 | elements in stack do not have the next smaller 52 | element, so print -1 for them */ 53 | while (s.empty() == false) { 54 | mp[s.top()] = -1; 55 | s.pop(); 56 | } 57 | 58 | for (int i=0; i " << mp[arr[i]] << endl; 60 | } 61 | 62 | /* Driver program to test above functions */ 63 | int main() 64 | { 65 | int arr[] = { 11, 13, 21, 3 }; 66 | int n = sizeof(arr) / sizeof(arr[0]); 67 | printNGE(arr, n); 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /Algorithms/Water the grass.CPP: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | #define MAX_SIZE 10000 8 | 9 | struct Sprinkler 10 | { 11 | double left; 12 | double right; 13 | bool operator <(const Sprinkler &s) const 14 | { 15 | return left rightmost) break; 27 | for (j = i+1; j sprinklers[i].right) 30 | { 31 | i = j; 32 | } 33 | } 34 | ++count; 35 | rightmost = sprinklers[i].right; 36 | if (rightmost >= l) break; 37 | } 38 | if (rightmost >= l) 39 | { 40 | return count; 41 | } 42 | return -1; 43 | } 44 | 45 | int main(void) 46 | { 47 | int n, l; 48 | double w; 49 | while (cin >> n >> l >> w) 50 | { 51 | w /= 2.0; 52 | int i, m = 0; 53 | for (i = 0; i >p>>r; 57 | if (r > w) 58 | { 59 | double halfCoveredLen = sqrt((double)r*r - w*w); //Attention to double, wrong several times... 60 | sprinklers[m].left = (double)p - halfCoveredLen; 61 | sprinklers[m++].right = (double)p + halfCoveredLen; 62 | } 63 | } 64 | sort(sprinklers, sprinklers+m); 65 | cout < 56 | #include 57 | #include 58 | #include 59 | #include 60 | using namespace std; 61 | 62 | 63 | int main() { 64 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 65 | int n,i,j,k; 66 | cin>>n; 67 | for(i=1;i0;j--) 70 | cout<<" "; 71 | for(j=0;j0;j--) 74 | cout<<"*"; 75 | cout<0;i--) 79 | { 80 | for(j=n-i;j>0;j--) 81 | cout<<" "; 82 | for(j=0;j0;j--) 85 | cout<<"*"; 86 | cout< 28 | #include 29 | #include 30 | #include 31 | #include 32 | using namespace std; 33 | 34 | 35 | void helper(string s1,string s2,string &s,int n) 36 | { 37 | s=""; 38 | for(int i=0;i>s1>>s2; 53 | int n; 54 | string x,y,res; 55 | 56 | if(s1.size() > s2.size()) 57 | { 58 | n = s1.size() - s2.size(); 59 | x=""; 60 | while(n!=0) 61 | { 62 | x = x+"0"; 63 | n--; 64 | } 65 | x=x+s2; 66 | helper(s1,x,s,s1.size()); 67 | } 68 | 69 | else if(s1.size()==s2.size()) 70 | helper(s1,s2,s,s1.size()); 71 | 72 | else 73 | { 74 | n = s2.size() - s1.size(); 75 | x=""; 76 | while(n!=0) 77 | { 78 | x = x+"0"; 79 | n--; 80 | } 81 | x=x+s1; 82 | helper(x,s2,s,s2.size()); 83 | } 84 | cout< 46 | #include 47 | #include 48 | #include 49 | #include 50 | using namespace std; 51 | 52 | 53 | int main() { 54 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 55 | int flag=0,i; 56 | string s; 57 | getline(cin,s); 58 | 59 | for(i=0;i 26 | #include 27 | #include 28 | #include 29 | #include 30 | using namespace std; 31 | 32 | 33 | int main() { 34 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 35 | long long int n,k,max; 36 | cin>>n>>k; 37 | long long int a[n]; 38 | for(long long int i=0;i>a[i]; 40 | } 41 | long long int j=0,index; 42 | while(k){ 43 | max = a[j]; 44 | for(long long int i=j+1;imax){ 46 | max=a[i]; 47 | index = i; 48 | } 49 | } 50 | if(max > a[j]){ 51 | int temp = a[j]; 52 | a[j] = a[index]; 53 | a[index] = temp; 54 | k--; 55 | } 56 | 57 | j++; 58 | if(j==n){ 59 | break; 60 | } 61 | } 62 | for(long long int i=0;i 35 | #include 36 | #include 37 | #include 38 | #include 39 | using namespace std; 40 | 41 | 42 | int main() { 43 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 44 | int a[100001],k,r; 45 | char str[100001]; 46 | cin>>str; 47 | cin>>k; 48 | long long i=0,s=0; 49 | while(str[i]){ 50 | a[i]=(int)str[i]-48; 51 | // cout<0){ 61 | r=tot%10; 62 | tot/=10; 63 | s+=r; 64 | } 65 | //cout< 32 | #include 33 | #include 34 | #include 35 | #include 36 | using namespace std; 37 | 38 | 39 | int main() { 40 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 41 | int n; 42 | cin>>n; 43 | int a[n],i,j,t,s=0; 44 | for(i=0;i>a[i]; 46 | for(i=0;i 2 | using namespace std; 3 | 4 | struct Process 5 | { 6 | int pid; 7 | int bt; 8 | }; 9 | bool comparison(Process a, Process b) 10 | { 11 | return (a.bt < b.bt); 12 | } 13 | void findWaitingTime(Process proc[], int n, int wt[]) 14 | { 15 | wt[0] = 0; 16 | for (int i = 1; i < n ; i++ ) 17 | wt[i] = proc[i-1].bt + wt[i-1] ; 18 | } 19 | void findTurnAroundTime(Process proc[], int n, 20 | int wt[], int tat[]) 21 | { 22 | for (int i = 0; i < n ; i++) 23 | tat[i] = proc[i].bt + wt[i]; 24 | } 25 | void findavgTime(Process proc[], int n) 26 | { 27 | int wt[n], tat[n], total_wt = 0, total_tat = 0; 28 | findWaitingTime(proc, n, wt); 29 | findTurnAroundTime(proc, n, wt, tat); 30 | cout << "\nProcesses "<< " Burst time " 31 | << " Waiting time " << " Turn around time\n"; 32 | for (int i = 0; i < n; i++) 33 | { 34 | total_wt = total_wt + wt[i]; 35 | total_tat = total_tat + tat[i]; 36 | cout << " " << proc[i].pid << "\t\t" 37 | << proc[i].bt << "\t " << wt[i] 38 | << "\t\t " << tat[i] <2->3->4->5 30 | Explanation 0 31 | 32 | Initially the list will be empty and then we will add 5. After that we will add 4 infront of 5. Similarly 3 infront of 4 and so on. 33 | 34 | 35 | 36 | #include 37 | #include 38 | #include 39 | #include 40 | struct node 41 | { 42 | int data; 43 | struct node *next; 44 | }; 45 | void insertbefore(struct node** headref,int data) 46 | { 47 | struct node* newnode=(struct node*)malloc(sizeof(struct node)); 48 | newnode->data = data; 49 | newnode->next = *headref; 50 | *headref = newnode; 51 | } 52 | void print(struct node* Node) 53 | { 54 | while(Node->next!=NULL) 55 | { 56 | printf("%d->",Node->data); 57 | Node = Node->next; 58 | } 59 | printf("%d",Node->data); 60 | } 61 | int main() { 62 | struct node* head = NULL; 63 | int size; 64 | scanf("%d",&size); 65 | int i,val; 66 | for(i=0;i 24 | #include 25 | #include 26 | #include 27 | #include 28 | using namespace std; 29 | int main() { 30 | long int n; 31 | cin>>n; 32 | int i,arr[n]; 33 | for(i=0;i>arr[i]; 35 | int min=arr[0]; 36 | int index; 37 | for(i=0;i 25 | #include 26 | #include 27 | #include 28 | #include 29 | using namespace std; 30 | int main() { 31 | int n; 32 | cin>>n; 33 | int i,arr[n]; 34 | for(i=0;i>arr[i]; 36 | int k; 37 | cin>>k; 38 | int temp=0,index; 39 | int b[n]; 40 | for(i=0;i 43 | #include 44 | #include 45 | #include 46 | #include 47 | using namespace std; 48 | 49 | 50 | int main() { 51 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 52 | int n,i,j,k,l,t,test; 53 | cin>>test; 54 | while(test--){ 55 | cin>>n; 56 | int space = 2*(n-1); 57 | for(i=1;i<=n;i++){ 58 | t=0; 59 | for(k=1;k<=space;k++){ 60 | cout<<" "; 61 | } 62 | 63 | for(j=1;j<=i;j++){ 64 | cout<<"*"<<" "; 65 | t++; 66 | } 67 | for(l=t-1;l>=1;l--){ 68 | cout<<"*"<<" "; 69 | } 70 | space-=2; 71 | cout<=1;i--){ 77 | t=0; 78 | for(k=1;k<=space;k++){ 79 | cout<<" "; 80 | } 81 | for(j=1;j<=i;j++){ 82 | cout<<"*""<<" "; 83 | t++; 84 | } 85 | for(l=t-1;l>=1;l--){ 86 | cout<<""*""<<" "; 87 | } 88 | space+=2; 89 | cout< 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | using namespace std; 34 | 35 | long long maxarea(long long a[],long long n) 36 | { 37 | stacks; 38 | long long area=0; 39 | long long tp; 40 | long long ar; 41 | long long i=0; 42 | while(i>n; 69 | long long a[n],i; 70 | for(i=0;i>a[i]; 72 | cout< 27 | #include 28 | #include 29 | #include 30 | #include 31 | using namespace std; 32 | 33 | 34 | int main() { 35 | int n; 36 | cin>>n; 37 | int i,arr[n]; 38 | for(i=0;i>arr[i]; 40 | int temp,j; 41 | for(i=0;iarr[j+1]) 46 | { 47 | temp=arr[j]; 48 | arr[j]=arr[j+1]; 49 | arr[j+1]=temp; 50 | } 51 | } 52 | } 53 | 54 | for(i=0;i2<==>3<==>4<==>5<==>6<==>7<==>8 with K = 4 8 | Answer : 3<==>2<==>1<==>5<==>6<==>7<==>8 9 | 10 | List : 1<==>5<==>10<==>25<==>10<==>5<==>1 with K = 25 11 | Answer : 10<==>5<==>1<==>10<==>5<==>1 12 | The following is the structure of a linkedlist node : 13 | 14 | struct DoubleLinkedListNode { 15 | int val; 16 | DoubleLinkedListNode* prev; 17 | DoubleLinkedListNode* next; 18 | }; 19 | Your task is to implement the following function : 20 | 21 | LinkedListNode* disintegrate(LinkedListNode* head, int k) 22 | Input 23 | Not Needed 24 | 25 | Output 26 | Not Needed 27 | 28 | Instructions 29 | You are only allowed to edit the Body of the code 30 | The Head and the Tail of the code must remain as it is 31 | You are not responsible for taking any input/output 32 | You must write the function with names exactly as given above 33 | You must not create any global variables 34 | Sample Input 0 35 | 36 | 8 37 | 1 38 | 2 39 | 3 40 | 4 41 | 5 42 | 6 43 | 7 44 | 8 45 | 4 46 | Sample Output 0 47 | 48 | 3 49 | 2 50 | 1 51 | 5 52 | 6 53 | 7 54 | 8 55 | 56 | 57 | 58 | 59 | #include 60 | 61 | int main() 62 | { 63 | int n,i,k,arr[100],pos; 64 | scanf("%d",&n); 65 | for(i=0;i=0;i--) 76 | printf("%d\n",arr[i]); 77 | for(i=pos+1;i 35 | #include 36 | #include 37 | #include 38 | #include 39 | using namespace std; 40 | 41 | 42 | int main() { 43 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 44 | int t; 45 | cin>>t; 46 | int ai[t],pi[t]; 47 | for(int i=0;i>ai[i]>>pi[i]; 50 | } 51 | int cost=0,min; 52 | cost=ai[0]*pi[0]; 53 | min=pi[0]; 54 | for(int i=1;i 31 | #include 32 | #include 33 | #include 34 | #include 35 | using namespace std; 36 | 37 | int josephus(int n, int k) 38 | { 39 | if (n == 1) 40 | return 1; 41 | else 42 | return (josephus(n - 1, k) + k-1) % n + 1; 43 | } 44 | int main() { 45 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 46 | int n,k; 47 | cin>>n>>k; 48 | cout< 49 | #include 50 | 51 | void replace(char *s1) 52 | { 53 | char r[4][10]={"yes","you","today","why"}; 54 | char rep[4][5]={"s","u","2day","y"}; 55 | char buff[500]; 56 | int i; 57 | char *ch; 58 | for(i=0;i<4;i++) 59 | if((ch=strstr(s1,r[i]))) 60 | { 61 | strncpy(buff,s1,ch-s1); 62 | buff[ch-s1]=0; 63 | sprintf(buff+(ch-s1),"%s%s",rep[i],ch+strlen(r[i])); 64 | s1[0]=0; 65 | strcpy(s1,buff); 66 | return replace(s1); 67 | } 68 | 69 | } 70 | int main() 71 | { 72 | char s1[100]; 73 | scanf("%[^\n]%*c", s1); 74 | replace(s1); 75 | printf("%s",s1); 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /Algorithms/Maximizing special nodes.CPP: -------------------------------------------------------------------------------- 1 | You are given a directed acyclic graph. We define a sink node to be a node that has zero edges coming out of it. We define a special node to be a node that has a direct edge to a sink node. 2 | You are given a power to reverse the direction of every single edge. 3 | You can either choose to use this power or not use this power at all. Your goal is to maximize the number of special nodes. 4 | 5 | Write a program to print the maximum number of special nodes that you can get either by reversing all the edges or by leaving the graph as it is. 6 | 7 | Input 8 | First line contains the number of nodes N. 9 | Next N-1 lines contains the information of edges. 10 | Each line contains two integers U and V denoting that there is an edge from U to V. 11 | 12 | Output 13 | Print the maximum number of special nodes that you can get. 14 | 15 | Sample Input 0 16 | 17 | 5 6 18 | 1 2 19 | 1 3 20 | 1 4 21 | 2 5 22 | 3 5 23 | 3 5 24 | Sample Output 0 25 | 26 | 3 27 | Explanation 0 28 | 29 | In both the reverse graph and the original graph the special nodes remain same : 2, 3 and 4. Hence the answer is 3 30 | 31 | 32 | 33 | 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | using namespace std; 40 | vector g[1005],rg[1005]; 41 | int n; 42 | int solve(vectorG[]) 43 | { 44 | int a[1005]={0}; 45 | for(int i=1;i<=n;i++) 46 | { 47 | for(auto node:G[i]){ 48 | if(G[node].size()==0) 49 | a[i]=1; 50 | } 51 | } 52 | int ans=0; 53 | for(int i=1;i<=n;i++) 54 | ans+=a[i]; 55 | return ans; 56 | } 57 | 58 | int main() { 59 | int e,c=0,c1=0; 60 | cin>>n>>e; 61 | for(int i=1;i<=e;i++) 62 | { 63 | int x,y; 64 | cin>>x>>y; 65 | g[x].push_back(y); 66 | rg[y].push_back(x); 67 | } 68 | int a=solve(g); 69 | int b=solve(rg); 70 | cout< 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | using namespace std; 39 | 40 | 41 | int main() { 42 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 43 | int p; 44 | cin>>p; 45 | string s; 46 | cin>>s; 47 | int b[123]; 48 | for(int i=97;i<=122;i++) 49 | b[i]=0; 50 | for(int i=0;i 43 | 44 | using namespace std; 45 | 46 | int gcd(int a, int b) { 47 | while (b) { 48 | int t = a % b; 49 | a = b; 50 | b = t; 51 | } 52 | return a; 53 | } 54 | 55 | int main() { 56 | int l, r, x, y; 57 | cin >> l >> r >> x >> y; 58 | if (y % x != 0){ 59 | cout << 0 << '\n'; 60 | return 0; 61 | } 62 | int ans = 0; 63 | int n = y / x; 64 | for (int d = 1; d * d <= n; ++d) { 65 | if (n % d == 0) { 66 | int c = n / d; 67 | if (l <= c * x && c * x <= r && l <= d * x && d * x <= r && gcd(c, d) == 1) { 68 | if (d * d == n) ++ans; 69 | else ans += 2; 70 | } 71 | } 72 | } 73 | 74 | cout << ans << '\n'; 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /Johny_in_a_matrix.CPP: -------------------------------------------------------------------------------- 1 | You are given a matrix A of size NxN. Every cell has a value A[i][j] associated with it. Johny is standing currently at position (1,1) and he intends to get to cell (N,N). 2 | 3 | The cost of going from cell (i,j) to cell(x,y) is A[i][j] * A[x][y]. 4 | 5 | Johny can either decided to travel in a row-major order or in a column-major order. 6 | 7 | Your task is to find out which method will cost him less. Print "row-major" or "column-major" accordingly. 8 | 9 | Input 10 | First line contains an integer N 11 | Next N lines each contain N integers each denoting the value of the array A[][] 12 | 13 | Output 14 | Print either "row-major" or "column-major". If both ways cost the same then print "row-major". 15 | 16 | Sample Input 0 17 | 18 | 2 19 | 1 2 20 | 3 4 21 | Sample Output 0 22 | 23 | column-major 24 | Explanation 0 25 | 26 | row major cost = 1x2 + 2x3 + 3x4 = 2 + 6 + 12 = 20 27 | col major cost = 1x3 + 3x2 + 2x4 = 3 + 6 + 8 = 17 28 | Hence the answer is column-major 29 | 30 | 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | using namespace std; 38 | 39 | 40 | int main() { 41 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 42 | long long int n,i,j,sum=0,k,rc=0,l,cm=0; 43 | cin>>n; 44 | long long int a[n][n],ar[n*n],ar1[n*n]; 45 | for(i=0;i>a[i][j]; 48 | ar[k]=a[i][j]; 49 | k++; 50 | } 51 | } 52 | for(i=0;i<(n*n)-1;i++){ 53 | rc=rc+ar[i]*ar[i+1]; 54 | } 55 | //cout<cm){ 66 | cout<<"column-major"; 67 | } 68 | else{ 69 | cout<<"row-major"; 70 | } 71 | 72 | //cout< 34 | #include 35 | #include 36 | #include 37 | 38 | struct node{ 39 | int data; 40 | struct node* next; 41 | }; 42 | 43 | int main() { 44 | 45 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 46 | long int t; 47 | int i,val; 48 | scanf("%ld",&t); 49 | struct node* head; 50 | for(i=0;idata=val; 55 | temp1->next=NULL; 56 | if(i==0) 57 | { 58 | head=temp1; 59 | } 60 | else 61 | { 62 | struct node* temp=head; 63 | while(temp->next!=NULL) 64 | { 65 | temp=temp->next; 66 | } 67 | temp->next=temp1; 68 | } 69 | } 70 | scanf("%d",&val); 71 | struct node *temp=head; 72 | while(temp!=NULL) 73 | { 74 | if(temp->data==val) 75 | { 76 | printf("yes"); 77 | return 0; 78 | } 79 | temp=temp->next; 80 | } 81 | printf("no"); 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /Subway_runner.CPP: -------------------------------------------------------------------------------- 1 | Arrnie is playing the new famous game Subway Runners. 2 | In this game there are three lanes and one starts from any one of the three lanes. 3 | Arrnie plays the game in the following manner : 4 | Switch(1,2) 5 | Switch(2,3) 6 | Switch(1,2) 7 | Switch(2,3).. and so on.. 8 | 9 | Switch(x,y) is defined as follows : 10 | if Arrnie is on lane x, he switches to lane y. 11 | if Arrnie is on lany y, he switches to lane x. 12 | if he is neither on lane x nor on y, he stays on the current lane. 13 | 14 | Given the current lane of Arrnie and the number of moves he has made, Print the number of the lane Arrnie started the game in. 15 | 16 | Input Format 17 | 18 | First line contains two integers n and x 19 | 20 | Constraints 21 | 22 | 1 <= n <= 2e9 23 | 1 <= x <= 3 24 | 25 | Output Format 26 | 27 | Output the lane number thar Arrnie started the game in. 28 | 29 | Sample Input 0 30 | 31 | 4 3 32 | Sample Output 0 33 | 34 | 2 35 | 36 | 37 | 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | using namespace std; 44 | 45 | int Switch(int ini,int x,int y){ 46 | if(x==2 && y==3){ 47 | if(ini == 2) 48 | return 3; 49 | else if(ini == 3) 50 | return 2; 51 | else 52 | return ini; 53 | } 54 | if(x==1 && y==2){ 55 | if(ini == 2) 56 | return 1; 57 | else if(ini == 1) 58 | return 2; 59 | else 60 | return ini; 61 | } 62 | return -1; 63 | } 64 | int main() { 65 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 66 | int n, ini; 67 | cin>>n>>ini; 68 | int current =ini; 69 | // checkPath(ini,n); 70 | while(n>0){ 71 | if(n%2 == 0){ 72 | current=Switch(current,2,3); 73 | }else{ 74 | current=Switch(current,1,2); 75 | } 76 | if(current == -1){ 77 | cout<<"term"; 78 | break; 79 | } 80 | n--; 81 | } 82 | cout< 39 | #include 40 | #include 41 | #include 42 | #include 43 | using namespace std; 44 | 45 | int Switch(int ini,int x,int y){ 46 | if(x==2 && y==3){ 47 | if(ini == 2) 48 | return 3; 49 | else if(ini == 3) 50 | return 2; 51 | else 52 | return ini; 53 | } 54 | if(x==1 && y==2){ 55 | if(ini == 2) 56 | return 1; 57 | else if(ini == 1) 58 | return 2; 59 | else 60 | return ini; 61 | } 62 | return -1; 63 | } 64 | int main() { 65 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 66 | int n, ini; 67 | cin>>n>>ini; 68 | int current =ini; 69 | // checkPath(ini,n); 70 | while(n>0){ 71 | if(n%2 == 0){ 72 | current=Switch(current,2,3); 73 | }else{ 74 | current=Switch(current,1,2); 75 | } 76 | if(current == -1){ 77 | cout<<"term"; 78 | break; 79 | } 80 | n--; 81 | } 82 | cout< 43 | using namespace std; 44 | #define INT_BITS 32 45 | int maxSubarrayXOR(int set[], int n) 46 | { 47 | int index = 0; 48 | for (int i = INT_BITS-1; i >= 0; i--) 49 | { 50 | int maxInd = index; 51 | int maxEle = INT_MIN; 52 | for (int j = index; j < n; j++) 53 | { 54 | if ( (set[j] & (1 << i)) != 0 55 | && set[j] > maxEle ) 56 | maxEle = set[j], maxInd = j; 57 | } 58 | if (maxEle == INT_MIN) 59 | continue; 60 | swap(set[index], set[maxInd]); 61 | maxInd = index; 62 | for (int j=0; j>n; 79 | int a[n]; 80 | for(int i =0;i>a[i]; 82 | cout << maxSubarrayXOR(a, n); 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /Bracket Sequence.CPP: -------------------------------------------------------------------------------- 1 | A bracket sequence is called regular, if by inserting '+' and '1' into it we can get a correct mathematical expression. For example, sequences '(())()', '()' and '(()(()))' are regular, while ')(', '(()' and '(()))(' are not. 2 | 3 | Since, ((1+(1+1))+(1+1)) is a valid mathematical expression hence, '((())())' is valid sequence. 4 | 5 | You are given a string of '(' and ')' characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well. 6 | 7 | Input Format 8 | 9 | The first line of the input file contains a non-empty string, consisting of '(' and ')' characters. 10 | 11 | Constraints 12 | 13 | Length of given sequence does not exceed . 14 | 15 | Output Format 16 | 17 | Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1". 18 | 19 | Sample Input 0 20 | 21 | )((())))(()()) 22 | Sample Output 0 23 | 24 | 6 2 25 | Sample Input 1 26 | 27 | ((((()((( 28 | Sample Output 1 29 | 30 | 2 1 31 | 32 | 33 | 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | using namespace std; 41 | 42 | 43 | int main() { 44 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 45 | string s; 46 | cin>>s; 47 | stackst; 48 | st.push(-1); 49 | vectorv; 50 | int num=INT_MIN; 51 | for(int i=0;i 34 | #include 35 | #include 36 | #include 37 | #include 38 | using namespace std; 39 | int main() 40 | { 41 | int n; 42 | while(cin>>n) 43 | { 44 | int ans=0; 45 | for(int i=1;i "cooc" (Palindrome), 6 | Hence Distance("cook") = 1. 7 | 8 | "fluffy" -> "flufff" -> "ffufff" -> "ffffff" (Palindrome), 9 | Hence Distance("fluffy") = 3. 10 | 11 | Now, you are given N number of strings, and your task is to find out which string has the farthest Distance from being a palindrome and print it. 12 | 13 | INPUT 14 | First line contains number of strings N. 15 | Next N lines each contain one string. 16 | 17 | OUTPUT 18 | Print one string that is the farthest out of all strings from being a palindrome. 19 | 20 | CONSTRAINTS 21 | 1 ≤ N ≤ 103 22 | Each string has length of atmost 1000 23 | Strings consists of lowercase english characters only 24 | 25 | Sample Input 0 26 | 27 | 7 28 | philosopher 29 | chamber 30 | prisoner 31 | goblet 32 | phoenix 33 | halfbloodprince 34 | deathlyhallows 35 | Sample Output 0 36 | 37 | halfbloodprince 38 | 39 | 40 | 41 | 42 | 43 | #include 44 | #include 45 | #include 46 | #include 47 | #include 48 | using namespace std; 49 | 50 | 51 | int main() { 52 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 53 | int n,p,m; 54 | cin>>n; 55 | 56 | m=-999999; 57 | string s[n]; 58 | 59 | for(int i=0;i>s[i]; 61 | 62 | for(int w=0;w 32 | #include 33 | #include 34 | #include 35 | #include 36 | using namespace std; 37 | int binarysearch(long int arr[],int n,int k) 38 | { 39 | long int l=0,h=n-1,mid; 40 | while(l<=h) 41 | { 42 | mid=floor((l+h)/2); 43 | if(arr[mid]==k) 44 | return 1; 45 | else if(arr[mid]>n; 55 | long int arr[n]; 56 | for(i=0;i>arr[i]; 58 | cin>>m; 59 | long int can[m]; 60 | for(i=0;i>can[i]; 62 | for(i=0;i 50 | 51 | using namespace std; 52 | 53 | int main() { 54 | int a, b; 55 | scanf("%d%d", &a, &b); 56 | int gcd = __gcd(a, b); 57 | vector div; 58 | for (int i = 1; i*i <= gcd; ++i) { 59 | if (gcd%i == 0) { 60 | div.push_back(i); 61 | if (i != gcd/i) div.push_back(gcd/i); 62 | } 63 | } 64 | sort(div.begin(), div.end(), greater ()); 65 | 66 | int Q; 67 | scanf("%d", &Q); 68 | while (Q--) { 69 | int m, n, res = -1; 70 | scanf("%d%d", &m, &n); 71 | for (int i : div) { 72 | if (i >= m && i <= n) { 73 | res = i; 74 | break; 75 | } 76 | } 77 | printf("%d\n", res); 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /Word_index1.CPP: -------------------------------------------------------------------------------- 1 | Write a C program to find the first occurrence of word in a string using loop 2 | 3 | Refer sample Input and Output: 4 | Example 1: 5 | srm university 6 | university 7 | Output 1: university is found at 4 8 | 9 | The String university is found at string index 4. Array Index starts from 0 10 | 11 | Example 2: 12 | srm university 13 | universit 14 | 15 | Output 2: 16 | universit is not found 17 | 18 | 19 | 20 | APPROACH-1 21 | 22 | #include 23 | #include 24 | int main() 25 | { 26 | char str1[100],str2[100]; 27 | scanf("%[^\n]s",str1); 28 | scanf("%s",str2); 29 | char *res = strstr(str1,str2); 30 | if(res=='\0') 31 | printf("%s is not found",str2); 32 | else 33 | { 34 | int index,i; 35 | for(i=0;str1[i]!='\0';i++) 36 | { 37 | if(res==&str1[i]) 38 | { 39 | index=i; 40 | break; 41 | } 42 | } 43 | printf("%s is found at %d",str2,index); 44 | } 45 | return 0; 46 | } 47 | 48 | 49 | 50 | 51 | APPROACH-2 52 | 53 | 54 | 55 | #include 56 | #include 57 | int main() 58 | { 59 | char str[100], word[100]; 60 | int i, index, found = 0; 61 | 62 | scanf("%[^\t\n]s",str); 63 | // printf("%s",str); 64 | scanf("%s",word); 65 | 66 | index = 0; 67 | while(str[index] != '\0') 68 | { 69 | 70 | if(str[index] == word[0]) 71 | { 72 | i=0; 73 | found = 1; 74 | while(word[i] != '\0') 75 | { 76 | if(str[index + i] != word[i]) 77 | { 78 | found = 0; 79 | break; 80 | } 81 | 82 | i++; 83 | } 84 | } 85 | 86 | if(found == 1) 87 | { 88 | break; 89 | } 90 | 91 | index++; 92 | } 93 | 94 | if(found == 1) 95 | { 96 | printf("\n%s is found at %d", word, index); 97 | } 98 | else 99 | { 100 | printf("\n %s is not found.", word); 101 | } 102 | 103 | return 0; 104 | } 105 | -------------------------------------------------------------------------------- /Play_with_strings.C: -------------------------------------------------------------------------------- 1 | Given are two strings(each not more than 100 characters). We need to find relation between them such that all the characters of the second string must appear in the first string sequentially (not necessarily continuously). The program should : 2 | Print 1: if the characters in the first string appear in the same sequence as that of the second string. 3 | Print 2: if the characters in the first string appear in the opposite sequence as that of second string. 4 | Print 3: if the characters in the first string appear in both ie same as well as opposite sequences of the second string. 5 | Print 0: if the characters in the first string dont appear in sequence of the second string. 6 | The characters in the strings are case-sensitive. 7 | 8 | Input 9 | The first line should give the number of test cases. For each test case, it takes input two strings. 10 | 11 | Output 12 | An integer ( 0,1,2 or 3 ) for each of the test cases. 13 | 14 | Test Case 1 15 | Input (stdin) 16 | 2 17 | njack njk 18 | anwesha awesa 19 | 20 | Expected Output 21 | 1 22 | 1 23 | 24 | Test Case 2 25 | Input (stdin) 26 | 2 27 | wysiwig iwi 28 | abcdef edba 29 | 30 | Expected Output 31 | 3 32 | 2 33 | 34 | 35 | 36 | 37 | #include 38 | #include 39 | int main() 40 | { 41 | int t,i,j,s1=0,s2=0,c=0; 42 | char a[100],b[100]; 43 | scanf("%d",&t); 44 | while(t--) 45 | { 46 | scanf("%s%s",a,b); 47 | j=0; 48 | c=0; 49 | for(i=0;i=0;i--) 68 | { 69 | for(j=j;j 35 | #include 36 | #include 37 | #include 38 | 39 | int main() { 40 | int n; 41 | scanf("%d",&n); 42 | int a[n]; 43 | for(int i=0;i 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | using namespace std; 46 | 47 | long long arr[100005]; 48 | int main() { 49 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 50 | 51 | int n, p; 52 | long long ans; 53 | 54 | cin>>n>>p; 55 | 56 | for(int i=0; i>arr[i]; 59 | arr[i] = ceil((double)arr[i]/(double)p); 60 | } 61 | 62 | sort(arr, arr+n); 63 | 64 | ans = (long long)arr[0]; 65 | 66 | for(int i=1; i 38 | 39 | using namespace std; 40 | 41 | int gcd(int a, int b) { 42 | while (b) { 43 | int t = a % b; 44 | a = b; 45 | b = t; 46 | } 47 | return a; 48 | } 49 | 50 | signed main() { 51 | ios_base::sync_with_stdio(false); 52 | cin.tie(0); 53 | 54 | int l, r, x, y; 55 | cin >> l >> r >> x >> y; 56 | if (y % x != 0){ 57 | cout << 0 << '\n'; 58 | return 0; 59 | } 60 | int ans = 0; 61 | int n = y / x; 62 | for (int d = 1; d * d <= n; ++d) { 63 | if (n % d == 0) { 64 | int c = n / d; 65 | if (l <= c * x && c * x <= r && l <= d * x && d * x <= r && gcd(c, d) == 1) { 66 | if (d * d == n) ++ans; 67 | else ans += 2; 68 | } 69 | } 70 | } 71 | 72 | cout << ans << '\n'; 73 | return 0; 74 | } 75 | 76 | -------------------------------------------------------------------------------- /Bag of Gold again.C: -------------------------------------------------------------------------------- 1 | You are given an empty bag that is supposed to be filled with gold, and it can carry at max W kgs of gold in it. 2 | 3 | You are given N samples of gold, with the ith of them weighing Wi and having value Vi. 4 | 5 | Your task is to fill the bag with atmost W kgs of gold such that the total value of the gold inside the bag is maximum. 6 | 7 | Unlike last time, this time whenever you choose to pick a sample of gold, you have to take it completely, i.e. you cannot break down samples. You either pick the entire sample of gold, or you dont pick the sample at all. 8 | 9 | INPUT 10 | First line contains two integers, N and W. 11 | Second onwards, there are N lines with each of them containing two integers, first one being the weight of the sample and the second one being the value of the sample 12 | 13 | OUTPUT 14 | Print one number, the maximum attainable value when the bag is filled with atmost W kgs of gold. 15 | 16 | CONSTRAINTS 17 | 1 ≤ N ≤ 103 18 | 1 ≤ W ≤ 103 19 | 1 ≤ weights, values ≤ 103 20 | 21 | Sample Input 0 22 | 23 | 1 24 | 3 50 25 | 10 60 26 | 20 100 27 | 30 120 28 | Sample Output 0 29 | 30 | 220 31 | 32 | 33 | 34 | 35 | 36 | #include 37 | #include 38 | #include 39 | #include 40 | int max(int a, int b) { return (a > b)? a : b; } 41 | 42 | // Returns the maximum value that can be put in a knapsack of capacity W 43 | int knapSack(int W, int wt[], int val[], int n) 44 | { 45 | int i, w; 46 | int K[n+1][W+1]; 47 | 48 | for (i = 0; i <= n; i++) 49 | { 50 | for (w = 0; w <= W; w++) 51 | { 52 | if (i==0 || w==0) 53 | K[i][w] = 0; 54 | else if (wt[i-1] <= w) 55 | K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]); 56 | else 57 | K[i][w] = K[i-1][w]; 58 | } 59 | } 60 | 61 | return K[n][W]; 62 | } 63 | int main() 64 | { 65 | int val[10000],wt[10000],n,t,W; 66 | scanf("%d",&t); 67 | for(int i=1;i<=t;++i) 68 | { 69 | scanf("%d %d",&n,&W); 70 | for(int j=0;j 37 | #include 38 | #include 39 | #include 40 | struct Date{ 41 | int d,m,y; 42 | }; 43 | int main() { 44 | int t,i,j; 45 | scanf("%d",&t); 46 | struct Date date[t]; 47 | for(i=0;i date[j].y){ 53 | struct Date temp = date[i]; 54 | date[i] = date[j]; 55 | date[j] = temp; 56 | } 57 | if(date[i].y == date[j].y){ 58 | if(date[i].m>date[j].m){ 59 | struct Date temp = date[i]; 60 | date[i] = date[j]; 61 | date[j] = temp; 62 | } 63 | } 64 | if(date[i].y == date[j].y && date[i].m == date[j].m){ 65 | if(date[i].d>date[j].d){ 66 | struct Date temp = date[i]; 67 | date[i] = date[j]; 68 | date[j] = temp; 69 | } 70 | } 71 | } 72 | } 73 | for(i=0;i 44 | #include 45 | #include 46 | #include 47 | #include 48 | using namespace std; 49 | 50 | 51 | int main() { 52 | long int n; 53 | cin>>n; 54 | long int i,ar[n],mul=1; 55 | for(i=0;i>ar[i]; 58 | } 59 | double num=0; 60 | for(i=0;i 33 | #include 34 | #include 35 | #include 36 | #include 37 | using namespace std; 38 | long long int binarysearch(long long int arr[],long long int l,long long int r,long long int x) 39 | { 40 | while (l <= r) 41 | { 42 | long long int m = l + (r - l) / 2; 43 | if (arr[m] == x) 44 | return 1; 45 | if (arr[m] < x) 46 | l = m + 1; 47 | else 48 | r = m - 1; 49 | } 50 | return 0; 51 | } 52 | long long int print(long long int arr[],long long int n,long long int sum) 53 | { 54 | long long int i,l=0,r=n-1,c=0,key; 55 | sort(arr,arr+n); 56 | for(i=0;i>n; 67 | long long int i,arr[n]; 68 | for(i=0;i>arr[i]; 70 | long long int key; 71 | cin>>key; 72 | cout< 38 | #include 39 | #include 40 | #include 41 | int max(int a, int b) { return (a > b)? a : b; } 42 | 43 | // Returns the maximum value that can be put in a knapsack of capacity W 44 | int knapSack(int W, int wt[], int val[], int n) 45 | { 46 | int i, w; 47 | int K[n+1][W+1]; 48 | 49 | for (i = 0; i <= n; i++) 50 | { 51 | for (w = 0; w <= W; w++) 52 | { 53 | if (i==0 || w==0) 54 | K[i][w] = 0; 55 | else if (wt[i-1] <= w) 56 | K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]); 57 | else 58 | K[i][w] = K[i-1][w]; 59 | } 60 | } 61 | 62 | return K[n][W]; 63 | } 64 | int main() 65 | { 66 | int val[10000],wt[10000],n,t,W; 67 | scanf("%d",&t); 68 | for(int i=1;i<=t;++i) 69 | { 70 | scanf("%d %d",&n,&W); 71 | for(int j=0;j 2 -> 7 -> 8 -> 9 -> NULL 9 | Non-Terminating Lists(Loop in the list) : 10 | 11 | 1 -> 2 -> 5 -> 15 -> 7-------- 12 | ^ | 13 | | | 14 | ---- 3 <- 10 <- 9<-- 15 | In the second type of list, you can traverse forever and never reach the end. 16 | 17 | Modify the find_loop function so that given the head of the linked list, it returns 1 if the list has a loop and 0 otherwise. 18 | 19 | Your code's memory consumption must not be dependant on the size of the linked list. 20 | 21 | Input Format 22 | 23 | Not Needed 24 | 25 | Constraints 26 | 27 | None 28 | 29 | Output Format 30 | 31 | Not Needed 32 | 33 | 34 | 35 | 36 | #include "stdio.h" 37 | #include "stdlib.h" 38 | #include "math.h" 39 | #include "string.h" 40 | 41 | struct Node{ 42 | int value; 43 | struct Node* next; 44 | }; 45 | typedef struct Node Node; 46 | Node links[105]; 47 | 48 | void add_link(int from, int to){ 49 | links[from].next = &links[to]; 50 | } 51 | void set_value(int index, int val){ 52 | links[index].value = val; 53 | } 54 | 55 | 56 | //BODY BEGINS HERE 57 | 58 | int find_loop(struct Node* head){ 59 | /*** 60 | Your Code Here 61 | It must return 1 if a loop exists and zero otherwise. 62 | ***/ 63 | struct Node *slow = head; 64 | struct Node *fast = head; 65 | while(fast != NULL && fast->next != NULL) 66 | { 67 | slow = slow->next; 68 | fast = fast->next->next; 69 | if (slow==fast) 70 | return 1; 71 | } 72 | return 0; 73 | } 74 | /*** 75 | Your Code Here 76 | 77 | It must return 1 if a loop exists and zero otherwise. 78 | ***/ 79 | 80 | //BODY ENDS HERE 81 | 82 | //TAIL BEGINS HERE 83 | int main(){ 84 | int i,n,m,tmp,tmp1,tmp2; 85 | scanf("%d %d", &n, &m); 86 | for(i = 0; i < n; i++){ 87 | scanf("%d", &tmp); 88 | set_value(i, tmp); 89 | } 90 | for(i = 0; i < m; i++){ 91 | scanf("%d %d", &tmp1, &tmp2); 92 | add_link(tmp1,tmp2); 93 | } 94 | if(find_loop(&links[0]) == 1) printf("Loop Found"); 95 | else printf("No Loop Found"); 96 | return 0; 97 | } 98 | -------------------------------------------------------------------------------- /One teacher Two classes.CPP: -------------------------------------------------------------------------------- 1 | Jessy, the P.E instructor for Middle Vale High School has to arrange his students in line for aerobics. Thankfully, his class of students already came to the court in a line of increasing order of heights. Not so thankfully, James, another P.E instructor is on leave again. So she has to take care of his class right now as well. The second class of students also came in a line of increasing order of heights. But Jessy feels that is useless since she has to make both classes stand together in a single line for her exercises. But you, the janitor (and also a computer science graduate with no other job), sees that this is the right place to use the merge technique. So write a program to help Jessy arrange the students in ascending order of their heights. 2 | 3 | Formally, given two sorted arrays, merge them into one sorted array and print the result. 4 | 5 | INPUT 6 | 7 | The first line of input is n (1 8 | 9 | OUTPUT 10 | 11 | Print the heights of the students in a row after both the lines have been merged. 12 | 13 | Sample Input 0 14 | 15 | 5 16 | 2 4 6 8 10 17 | 5 18 | 1 3 5 7 9 19 | Sample Output 0 20 | 21 | 1 2 3 4 5 6 7 8 9 10 22 | 23 | 24 | 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | using namespace std; 32 | void mergeArrays(long long int arr1[],long long int arr2[],long long int n1, 33 | long long int n2,long long int arr3[]) 34 | { 35 | int i = 0, j = 0, k = 0; 36 | while (i>n; 52 | long long int a[n]; 53 | for(i=0;i>a[i]; 55 | cin>>m; 56 | long long int b[m]; 57 | for(i=0;i>b[i]; 59 | long long int c[n+m]; 60 | mergeArrays(a, b, n, m, c); 61 | for (int i=0; i < n+m; i++) 62 | cout << c[i] << " "; 63 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /Pyramidbynumericals.CPP: -------------------------------------------------------------------------------- 1 | Identify the logic behind the series 2 | 3 | 6 28 66 120 190 276.... 4 | 5 | The numbers in the series should be used to create a Pyramid. The base of the Pyramid will be the widest and will start converging towards the top where there will only be one element. Each successive layer will have one number less than that on the layer below it. The width of the Pyramid is specified by an input parameter N. In other words there will be N numbers on the bottom layer of the pyramid. 6 | 7 | The Pyramid construction rules are as follows 8 | 1. First number in the series should be at the top of the Pyramid 9 | 2. Last N number of the series should be on the bottom-most layer of the Pyramid, with Nth number being the right-most number of this layer. 10 | 3. Numbers less than 5-digits must be padded with zeroes to maintain the sanctity of a Pyramid when printed. Have a look at the examples below to get a pictorial understanding of what this rule actually means. 11 | 12 | Example 13 | 14 | If input is 2, output will be 15 | 16 | 00006 17 | 00028 00066 18 | 19 | If input is 3, output will be 20 | 21 | 00006 22 | 00028 00066 23 | 00120 00190 00276 24 | Input Format 25 | 26 | First line of input will contain number N that corresponds to the width of the bottom-most layer of the Pyramid 27 | 28 | Constraints 29 | 30 | 0 < N <= 14 31 | 32 | Output Format 33 | 34 | The Pyramid constructed out of numbers in the series as per stated construction rules 35 | 36 | Sample Input 0 37 | 38 | 2 39 | Sample Output 0 40 | 41 | 00006 42 | 00028 00066 43 | Sample Input 1 44 | 45 | 3 46 | Sample Output 1 47 | 48 | 00006 49 | 00028 00066 50 | 00120 00190 00276 51 | 52 | 53 | 54 | 55 | #include 56 | #include 57 | #include 58 | #include 59 | 60 | int main() { 61 | 62 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 63 | int i,j,n,k=2; 64 | scanf("%d",&n); 65 | int space=n-1; 66 | for(i=1;i<=n;i++) 67 | { 68 | for(j=1;j<=space;j++) 69 | printf(" "); 70 | for(j=1;j<=i;j++) 71 | { 72 | printf("%1.5d ",(k*(2*k-1))); 73 | 74 | k=k+2; 75 | 76 | 77 | } 78 | space--; 79 | printf("\n"); 80 | 81 | } 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /Into the forest.C: -------------------------------------------------------------------------------- 1 | A tree is defined as a group of nodes such that for every pair of nodes in the tree it is possible to reach from one node to another. Note that this definition is applicable when the edges are bidirectional. 2 | 3 | Size of a tree is defined as the number of nodes in a tree. 4 | 5 | You are given a forest. A forest is a collection of multiple trees. Your task is to gather information about this forest. 6 | 7 | Formally, you are supposed to find out three characteristics : 8 | * Size of the biggest tree in the forest * Size of the smallest tree in the forest * Number of trees in the forest 9 | 10 | Input 11 | First line contains two integers N and M where N denotes the number of nodes and M denotes the number of edges. 12 | The nodes are numbered from 1 to N. 13 | Next M lines each contain a pair of integers U and V denoting that there is an edge between the node with index U and the node with index V. 14 | 15 | Output 16 | Print three integers, the max size of a tree, the min size of a tree and the number of trees in the forest 17 | 18 | Notes 19 | 1 ≤ N, M< ≤ 1000 20 | There may be multiple edges or self edges 21 | Sample Input 0 22 | 23 | 5 3 24 | 1 2 25 | 2 3 26 | 4 5 27 | Sample Output 0 28 | 29 | 3 2 2 30 | 31 | 32 | 33 | 34 | 35 | 36 | #include 37 | #include 38 | #include 39 | #include 40 | int *v; 41 | int min=1001,max=0,c=1,n; 42 | void dfs(int i,int a[][n]) 43 | { 44 | v[i]=1; 45 | for(int j=0;jmax)max=c; 54 | 55 | } 56 | int main() { 57 | int m; 58 | scanf("%d%d",&n,&m); 59 | v=(int *)malloc(sizeof(int)*n); 60 | for(int i=0;imax)max=c; 81 | if(x==1)min=max; 82 | printf("%d %d %d",max,min,x); 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /Intothewater.CPP: -------------------------------------------------------------------------------- 1 | Aditi takes place in a bike race. The race takes place along the roads at the shores of the Hussain Sagar Lake. As everyone knows, the boundaries of the Hussain Sagar Lake consists only of straight lines that are either horizontal with respect to the equator or vertical as seen from the space. 2 | 3 | Assume a 2D co-ordinate system with the hussain sagar lake being a polygon in it with all its side either parallel to the X-axis or the Y-axis. The starting point of the race is the southernmost position of the boundary. Since all sections are straight, Aditi is always travelling in one of the four directions : East, West, North or South. Every turn is a 90 degree turn i.e. every two consecutive roads will be perpendicular to each other. 4 | 5 | Aditi is still naive so she is not very confident at some turns. Name, Aditi feels insecure at a turn there is a possibility that she can fall into the Hussain Sagar Lake. In formal words, Aditi considers a turn dangerous if she can fall directly into the water upon ignoring the turn. 6 | 7 | Help Aditi get ready for the competition -- determine the number of dangerous turns. 8 | 9 | Input Format 10 | 11 | The first line contains integer N, the number of straight sections of the boundary. 12 | Next N+1 lines contains pairs of integers (xi, yi). The first of these points is the starting position. The ith straight section begins at point (xi, yi) and ends at (xi+1, yi+1). 13 | It is guaranteed that : 14 | 15 | The first section is directed towards north. 16 | The southernmost point is the starting point. 17 | The last point coincides with the first point. 18 | Any pairs of straight lines share no points except the neighbouring tracks that share one end point. 19 | No pair of points except the first and the last is same. 20 | Constraints 21 | 22 | 4 <= n <= 100 23 | -10000 <= xi,yi <= 10000 24 | 25 | Output Format 26 | 27 | Print a single integer — the number of dangerous turns on the track. 28 | 29 | Sample Input 0 30 | 31 | 6 32 | 0 0 33 | 0 1 34 | 1 1 35 | 1 2 36 | 2 2 37 | 2 0 38 | 0 0 39 | Sample Output 0 40 | 41 | 1 42 | 43 | 44 | 45 | #include 46 | #include 47 | #include 48 | #include 49 | 50 | int main() { 51 | 52 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 53 | int n; 54 | cin>>n; 55 | cout<<(n-4)/2; 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /Algorithms/Ebola curve.CPP: -------------------------------------------------------------------------------- 1 | An antidote has been developed for the ebola crisis. This antidote is conjuctive, i.e. If it is given to a person, then the person who is connected to this person will also be cured. Two people X and Y are connected if X and Y are adjacent or if there exists a person Z such that X and Z are connected and Z and Y are connected. 2 | 3 | So, for every connected group of people we need one antidote. This antidotes strength must be equal to the highest infected person in the group. 4 | 5 | Your task is to calculate the number of antidotes of different strengths needed. 6 | 7 | Input 8 | First line contains the size of the grid N. 9 | Next contains the NxN grid. 10 | 11 | Output 12 | Print 4 integers, the number of antidotes required of strength 2, 3, 4 and 5. 13 | 14 | Sample Input 0 15 | 16 | 5 17 | 0 0 0 1 5 18 | 1 1 0 1 1 19 | 3 1 0 0 0 20 | 1 1 0 1 1 21 | 4 4 0 3 1 22 | Sample Output 0 23 | 24 | 0 1 1 1 25 | 26 | 27 | 28 | #include 29 | using namespace std; 30 | 31 | int vis(vector > &a,vector > &v,int i,int j){ 32 | v[i][j]=1; 33 | int mx=a[i][j]; 34 | if(i-1>=0 && !v[i-1][j] && a[i-1][j]) mx=max(mx,vis(a,v,i-1,j)); 35 | if(j-1>=0 && !v[i][j-1] && a[i][j-1]) mx=max(mx,vis(a,v,i,j-1)); 36 | if(i+1=0 && j-1>=0 && !v[i-1][j-1] && a[i-1][j-1]) mx=max(mx,vis(a,v,i-1,j-1)); 40 | if(j-1>=0 && i+1=0 && !v[i-1][j+1] && a[i-1][j+1]) mx=max(mx,vis(a,v,i-1,j+1)); 43 | return mx; 44 | } 45 | 46 | int main() { 47 | int n; cin>>n; 48 | vector > a(n,vector (n)); 49 | for(int i=0;i>a[i][j]; 51 | 52 | vector > v(n,vector (n,0)); 53 | vector c(6,0); 54 | for(int i=0;i 62 | #include 63 | #include 64 | #include 65 | #include 66 | using namespace std; 67 | 68 | 69 | int main() { 70 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 71 | int n,i,j,k,l,t,test; 72 | cin>>test; 73 | while(test--){ 74 | cin>>n; 75 | int space = 2*(n-1); 76 | for(i=1;i<=n;i++){ 77 | t=0; 78 | for(k=1;k<=space;k++){ 79 | cout<<" "; 80 | } 81 | 82 | for(j=1;j<=i;j++){ 83 | cout<=1;l--){ 87 | cout<=1;i--){ 96 | t=0; 97 | for(k=1;k<=space;k++){ 98 | cout<<" "; 99 | } 100 | for(j=1;j<=i;j++){ 101 | cout<=1;l--){ 105 | cout< 46 | #include 47 | #include 48 | 49 | #define N 1100 50 | #define M 1000000007 51 | 52 | using namespace std; 53 | 54 | long long n, m, a[N], f[N][N], p[N], ans = 1, s; 55 | 56 | int main(){ 57 | cin >> n >> m; 58 | for (int i = 0; i < m; i++) cin >> a[i]; 59 | 60 | for (int i = 0; i <= n + 1; i++) f[0][i] = 1; 61 | for (int i = 1; i <= n + 1; i++) 62 | for (int j = 1; j <= n + 1; j++) 63 | f[i][j] = (f[i - 1][j] + f[i][j - 1]) % M; 64 | p[0] = 1; 65 | for (int i = 1; i <= n; i++) p[i] = (p[i - 1] * 2) % M; 66 | 67 | sort(a, a + m); 68 | s = a[0] - 1; 69 | for (int i = 1; i < m; i++){ 70 | int l = a[i] - a[i - 1] - 1; 71 | if (l == 0) continue; 72 | ans = (ans * f[l][s + 1]) % M; 73 | ans = (ans * p[l - 1]) % M; 74 | s += l; 75 | } 76 | ans = (ans * f[n - a[m - 1]][s + 1]) % M; 77 | 78 | cout << ans; 79 | } 80 | -------------------------------------------------------------------------------- /Gas Stations.CPP: -------------------------------------------------------------------------------- 1 | There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. 2 | 3 | You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations. 4 | 5 | Print the minimum starting gas station’s index if you can travel around the circuit once, otherwise print -1. 6 | 7 | You can only travel in one direction. i to i+1, i+2, ... n-1, 0, 1, 2.. Completing the circuit means starting at i and ending up at i again. 8 | 9 | Input Format 10 | 11 | First line contains the number of gas stations N. 12 | Second line contains N integers, the ith of them denoting gas[i]. 13 | Third line contains N integers, the ith of them denoting cost[i]. 14 | 15 | Constraints 16 | 17 | 1 <= N <= 105 18 | 19 | Output Format 20 | 21 | Print the minimum starting gas station’s index if you can travel around the circuit once, otherwise print -1. 22 | 23 | Sample Input 0 24 | 25 | 2 26 | 1 2 27 | 2 1 28 | Sample Output 0 29 | 30 | 1 31 | 32 | 33 | 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | using namespace std; 40 | 41 | 42 | int main() { 43 | int n; 44 | cin>>n; 45 | long long int gas[n],cost[n]; 46 | for(int i=0;i>gas[i]; 48 | for(int i=0;i>cost[i]; 50 | int k=0; 51 | while(k 50 | #include 51 | #include 52 | #include 53 | #include 54 | #include 55 | using namespace std; 56 | 57 | struct Employee{ 58 | char name[55]; 59 | int salary; 60 | }; 61 | 62 | int main() { 63 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 64 | int n,i,j; 65 | struct Employee a[1000],temp; 66 | cin>>n; 67 | for(i=0;i>a[i].name>>a[i].salary; 69 | for(i=0;i a[j].salary){ 72 | temp = a[i]; 73 | a[i] = a[j]; 74 | a[j] = temp; 75 | }else if(a[i].salary == a[j].salary && strcmp(a[i].name,a[j].name)>0){ 76 | temp = a[i]; 77 | a[i] = a[j]; 78 | a[j] = temp; 79 | }else 80 | continue; 81 | } 82 | } 83 | for(i=0;i 37 | using namespace std; 38 | const int maxn = 1e5+5; 39 | const int mod = 998244353; 40 | int a[maxn]; 41 | long long dp[maxn][205][3]; 42 | int main() { 43 | int n; 44 | scanf("%d", &n); 45 | for(int i = 0; i < n; ++i) 46 | scanf("%d", &a[i]); 47 | if(a[0] != -1) 48 | dp[0][a[0]][0] = 1; 49 | else { 50 | for(int i = 1; i <= 200; ++i) 51 | dp[0][i][0] = 1; 52 | } 53 | for(int i = 1; i < n; ++i) { 54 | int sum = 0; 55 | for(int j = 1; j <= 200; ++j) { 56 | if(a[i] == -1 || j == a[i]) { 57 | dp[i][j][0] = sum; 58 | } 59 | 60 | sum = (sum+dp[i-1][j][1]+dp[i-1][j][0]+dp[i-1][j][2])%mod; 61 | } 62 | sum = 0; 63 | for(int j = 1; j <= 200; ++j) { 64 | if(a[i] == -1 || j == a[i]) { 65 | dp[i][j][1] = (dp[i-1][j][0]+dp[i-1][j][1]+dp[i-1][j][2])%mod; 66 | } 67 | 68 | } 69 | for(int j = 200; j >= 1; --j) { 70 | if(a[i] == -1 || j == a[i]) { 71 | dp[i][j][2] = sum; 72 | } 73 | sum = (sum+dp[i-1][j][2]+dp[i-1][j][1])%mod; 74 | } 75 | } 76 | long long ans = 0; 77 | for(int i = 1; i <= 200; ++i) { 78 | ans = (ans+dp[n-1][i][1]+dp[n-1][i][2])%mod; 79 | } 80 | printf("%lld\n", ans); 81 | return 0; 82 | } 83 | -------------------------------------------------------------------------------- /Pucca and the card game.CPP: -------------------------------------------------------------------------------- 1 | Pucca and Garu are playing a card game that is explained as follows : 2 | 1. N number of cards are placed in a straight line. 3 | 2. Cards are numbered 1 to N from left to right and the ith card has a number Ai written on it. 4 | 3. Pucca plays first, then Garu, then Pucca and so on. 5 | 4. In each turn a player can choose one card, either the rightmost card or the leftmost card. 6 | 5. Whatever card a player chooses, the number on that card is added to the players score and the card is removed from the game. 7 | 6. The game ends when there are no other cards to pick. 8 | 7. At the end of the game, the player with the maxmimum score wins. 9 | 8. If both players have equal scores, then Pucca wins. 10 | 11 | Pucca and Garu are feeling lazy on this sunday afternoon and have asked you to write a program to determine who the winner of the game will be. 12 | 13 | INPUT 14 | 15 | First line contains the number of cards N. (1 <= N <= 105) 16 | Second line contains N space separated integers, ith of them denoting Ai. (1 <= Ai <= 106) 17 | 18 | OUTPUT 19 | 20 | Print "Pucca" (without quotes) if Pucca wins or "Garu" (without quotes) if Garu wins. 21 | 22 | Sample Input 0 23 | 24 | 5 25 | 9 3 7 4 8 26 | Sample Output 0 27 | 28 | Pucca 29 | Explanation 0 30 | 31 | Turn 1 : Pucca Takes 9 (Pucca - 9 || Garu - 0 ) 32 | Turn 2 : Garu Takes 8 (Pucca - 9 || Garu - 8 ) 33 | Turn 3 : Pucca Takes 4 (Pucca - 13 || Garu - 8 ) 34 | Turn 4 : Garu Takes 7 (Pucca - 13 || Garu - 15) 35 | Turn 5 : Pucca Takes 3 (Pucca - 16 || Garu - 15) 36 | Pucca wins 37 | 38 | 39 | 40 | 41 | 42 | #include 43 | #include 44 | #include 45 | #include 46 | #include 47 | using namespace std; 48 | 49 | 50 | int main() { 51 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 52 | long long int n; 53 | cin>>n; 54 | int a[n]; 55 | for(int i=0;i>a[i]; 57 | } 58 | int i=0,j=n-1; 59 | int k=1; 60 | int b[2]={0}; 61 | while(i<=j){ 62 | if(k==1) 63 | k=0; 64 | else 65 | k=1; 66 | if(a[i]=b[1]){ 77 | cout<<"Pucca"; 78 | } 79 | else 80 | cout<<"Garu"; 81 | return 0; 82 | } 83 | -------------------------------------------------------------------------------- /PrimeOfPrimes.CPP: -------------------------------------------------------------------------------- 1 | A number is super prime if the sum of all its divisors except itself is a prime number. 2 | A prime number is a number which can only be divided by two numbers, 1 and itself. 3 | 1 is neither prime nor composite. 4 | For this task, you are to write a program to find out whether a given number is super prime or not. 5 | See the sample case for clarity 6 | 7 | Input 8 | Input consists of multiple test cases. 9 | First line contains T the number of test cases. 10 | Then T lines follow. Each test case contains one integer only, N. 11 | 12 | Output 13 | For every test case print "yes" if N is a super prime and "no" otherwise. 14 | 15 | Notes 16 | 1 ≤ T ≤ 10000 17 | 1 ≤ N, ≤ 106 18 | Your code must be efficient and optimal 19 | Sample Input 0 20 | 21 | 4 22 | 1 23 | 2 24 | 4 25 | 6 26 | Sample Output 0 27 | 28 | no 29 | no 30 | yes 31 | no 32 | Explanation 0 33 | 34 | 1 : sum = 0 : not prime 35 | 2 : sum = 1 : not prime 36 | 4 : sum = 1 + 2 = 3 : prime 37 | 6 : sum = 1 + 2 + 3 = 6 : not prime 38 | 39 | 40 | 41 | #include 42 | #include 43 | #include 44 | #include 45 | 46 | int divSum(int num) 47 | { 48 | // Final result of summation of divisors 49 | int result = 0; 50 | 51 | // find all divisors which divides 'num' 52 | for (int i=2; i<=sqrt(num); i++) 53 | { 54 | // if 'i' is divisor of 'num' 55 | if (num%i==0) 56 | { 57 | // if both divisors are same then add 58 | // it only once else add both 59 | if (i==(num/i)) 60 | result += i; 61 | else 62 | result += (i + num/i); 63 | } 64 | } 65 | 66 | // Add 1 to the result as 1 is also a divisor 67 | return (result + 1); 68 | } 69 | 70 | int isprime(long long int x) 71 | { 72 | long long int i; 73 | if(x==0 || x==1) return 0; 74 | else { 75 | for (int i=2; i*i<=x; i++) 76 | if (x%i == 0) 77 | return 0; 78 | return 1; 79 | } 80 | } 81 | 82 | int main() { 83 | 84 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 85 | long int t; 86 | scanf("%ld", &t); 87 | while(t--){ 88 | int a; 89 | scanf("%d", &a); 90 | int sum = divSum(a); 91 | if(isprime(sum)) 92 | printf("yes\n"); 93 | else 94 | printf("no\n"); 95 | } 96 | return 0; 97 | } 98 | -------------------------------------------------------------------------------- /Jason fights the monster.CPP: -------------------------------------------------------------------------------- 1 | Jason is trapped in a forest with n hungry monsters and must use his trusty blaster to defend himself! Each monster i has a health value,h(i) . 2 | Jason can discharge his blaster at a monster once per second and reduce its health points by hit units. Once a monster's health points become <=0, it dies. 3 | Given the health values for each monster and an integer, t, can you determine the maximum number of monsters he can kill in t seconds? Assume Jason always hits his target! 4 | 5 | Input Format 6 | 7 | The first line consists of three space-separated integers describing the respective values of n,hit , and t. 8 | The second line consists of space-separated integers describing the values of h(0),h(1),...,h(n-1). 9 | 10 | Constraints 11 | 12 | Output Format 13 | 14 | Print an integer denoting the maximum number of monsters Jason can kill in seconds. 15 | 16 | Sample Input 0 17 | 18 | 7 8 6 19 | 16 19 7 11 23 8 16 20 | Sample Output 0 21 | 22 | 4 23 | Explanation 0 24 | 25 | We want to find the maximum number of monsters we can kill in t=6 seconds using a blaster that does hit=8 units of damage per second. The diagram below depicts the array of initial health values, : 26 | h=[16,19,7,11,23,8,16] 27 | The optimal approach is as follows: 28 | 29 | Shoot monster 2 so h=7-8=-1, monster 2 dies, and becomes [16,19,-1,11,23,8,16]: 30 | Shoot monster 5 so h=8-8=0, monster dies, and becomes [16,19,-1,11,23,0,16]: 31 | Shoot monster 3 so h=11-8=3, monster dies, and becomes [16,19,-1,3,23,0,16]: 32 | Shoot monster 3 so h=3-8=-5, monster dies, and becomes [16,19,-1,-5,23,0,16]: 33 | Shoot monster 0 so h=16-8=8, monster dies, and becomes [8,19,-1,-5,23,0,16]: 34 | Shoot monster 0 so h=8-8=0, monster dies, and becomes [0,19,-1,-5,23,0,16]: 35 | 36 | Thus, we print 4 as the maximum number of monsters we can kill in the given time period. 37 | 38 | 39 | 40 | 41 | #include 42 | 43 | using namespace std; 44 | 45 | const int maxn = 100005; 46 | 47 | int a[maxn],n,l,t; 48 | 49 | int main() 50 | { 51 | scanf("%d%d%d", &n, &l, &t); 52 | for(int i = 1;i <= n;i ++) scanf("%d", &a[i]); 53 | sort(a + 1,a + n + 1); 54 | int ans = 0; 55 | for(int i = 1;i <= n;i ++) 56 | { 57 | int ned = a[i] / l; 58 | if (l * ned < a[i]) ++ ned; 59 | t -= ned; 60 | if (t < 0) break; 61 | ans ++; 62 | } 63 | printf("%d\n", ans); 64 | return 0; 65 | } 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /KefaHatesCoins.C: -------------------------------------------------------------------------------- 1 | Kefa hates coins and thinks that coins are a currency of the poor. 2 | 3 | She has N coins with her, ith of them having the value Ai. 4 | She goes to a shop to buy M gifts, with the price of ith gift being Pi. 5 | 6 | She wants to get rid of as many coins as she can and she buys the gift one by one from the 1st one to the Mth one. She adopts the following strategy while buying any particular gift i : She wants to give the shopkeeper a total of X coins whose value sum up to K such that - 7 | 8 | K ≥ Pi 9 | X is maximized 10 | K - Pi is minimized 11 | She does not consider other gifts while buying a particular gift i.e. while buying every gift she tries her best to satisfy the above conditions for that gift. 12 | 13 | Your task is, given the array A and the array P, for every gift, find and print the values of X and K, or print -1 -1 if she will not be able to buy the gift at all. 14 | 15 | INPUT 16 | First line contains two numbers N and M. 17 | Second line contains the array A of size N. 18 | Third line contains the array P of size M. 19 | 20 | INPUT 21 | Print M lines of output, for every gift two integers on a new line. 22 | The two integers should denote the values of X and K while buying that gift optimally. 23 | If it is not possible to buy a particular gift with available coins, print "-1 -1" 24 | 25 | CONSTRAINTS 26 | 1 ≤ N, M ≤ 105 27 | 1 ≤ Ai, Pi ≤ 105 28 | 29 | Sample Input 0 30 | 31 | 10 5 32 | 1 2 3 4 5 1 2 3 4 5 33 | 6 34 | 6 35 | 6 36 | 6 37 | 6 38 | Sample Output 0 39 | 40 | 4 6 41 | 2 6 42 | 2 8 43 | 2 10 44 | -1 -1 45 | 46 | 47 | 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | int cmp(const void *a,const void *b){ 54 | return(*(int*)a-*(int*)b); 55 | } 56 | int main() { 57 | int m,n; 58 | scanf("%d%d",&n,&m); 59 | int a[n]; 60 | for(int i=0;i 45 | #include 46 | #include 47 | #include 48 | #include 49 | using namespace std; 50 | void swap(int *a, int *b){ 51 | int temp = *a; 52 | *a = *b; 53 | *b = temp; 54 | } 55 | int partition (int a[], int l, int r) 56 | { 57 | int x = a[r]; 58 | int i = (l - 1); 59 | for (int j = l; j <= r-1; j++) 60 | { 61 | if (a[j] <= x) 62 | { 63 | i++; 64 | swap (&a[i], &a[j]); 65 | } 66 | } 67 | swap (&a[i + 1],&a[r]); 68 | return (i + 1); 69 | } 70 | void Quicksort(int a[], int l, int r) 71 | { 72 | int i,p; 73 | if (l < r) 74 | { 75 | p = partition(a, l, r); 76 | cout<>n; 89 | int a[n],i; 90 | for(i=0;i>a[i]; 92 | Quicksort(a,0,n-1); 93 | 94 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 95 | return 0; 96 | } 97 | -------------------------------------------------------------------------------- /TurningBinaryMatrix.CPP: -------------------------------------------------------------------------------- 1 | Consider a binary matrix A of size N X N. 2 | Now, consider the following matrices : 3 | A90 - obtained by rotating A clockwise by 90 degrees. 4 | A180 - obtained by rotating A clockwise by 180 degrees. 5 | A270 - obtained by rotating A clockwise by 270 degrees. 6 | 7 | Note : Binary matrix implies that every element will be either 0 or 1. 8 | 9 | Your task is to construct another binary matrix B of size N X N such that : 10 | B(i,j) = 1 iff either A(i,j) = 1 OR A90(i,j) = 1 OR A180(i,j) = 1 OR A270(i,j) = 1 11 | B(i,j) = 0 otherwise 12 | 13 | 14 | INPUT 15 | 16 | First line contains the size of the matrix N (1 ≤ N ≤ 100) 17 | Next N lines contain N integers each (Only 0 or 1) denoting the matrix A 18 | 19 | 20 | OUTPUT 21 | 22 | Print N X N integers, denoting the matrix B. 23 | 24 | Sample Input 0 25 | 26 | 4 27 | 0 0 0 0 28 | 0 0 0 0 29 | 0 0 1 0 30 | 1 0 0 0 31 | Sample Output 0 32 | 33 | 1 0 0 1 34 | 0 1 1 0 35 | 0 1 1 0 36 | 1 0 0 1 37 | 38 | 39 | 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | using namespace std; 46 | int arr[2]; 47 | void rotate(int i,int j, int n, int angle){ 48 | if(angle==0){ 49 | arr[0]=i; 50 | arr[1]=j; 51 | } 52 | else{ 53 | arr[0]=n-j-1; 54 | arr[1]=i; 55 | rotate(arr[0],arr[1],n,angle-90); 56 | } 57 | } 58 | int main() { 59 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 60 | int n,i,j; 61 | cin>>n; 62 | int mat[n][n]; 63 | for(i=0;i>mat[i][j]; 66 | } 67 | } 68 | int barr[n][n]; 69 | for(i=0;i