├── back-tracking ├── question4.c ├── question2.c └── question1.c ├── stacks-and-queues ├── divide-and-conquor ├── a.exe ├── question8.c ├── question9.c ├── question2.c ├── question11.c └── question3.c ├── .gitignore ├── trees ├── a.exe ├── question3.c ├── question16.c ├── question22.c ├── question2.c ├── question17.c ├── question14.c ├── question8.c ├── question18.c └── question10.c ├── arrays ├── a.exe ├── question13.c ├── question23.c ├── question4.c ├── question1.c ├── question2.c ├── question6.c ├── question7.c ├── question14.c ├── question5.c ├── question29.c ├── question12.c ├── question9.c ├── question3.c ├── question19.c └── question28.c ├── strings ├── a.exe ├── question5.c ├── question6.c ├── question8.c ├── question3.c ├── question4.c └── question9.c ├── graphs ├── question8.c ├── question18.c ├── question15.c └── question13.c ├── divide-and-conquer ├── a.exe ├── question2.c ├── question7.c ├── question10.c ├── question3.c └── question11.c ├── dynamic-programming ├── a.exe ├── question31.c ├── question23.c ├── question30.c ├── question37.c ├── question5.c ├── question36.c ├── question27.c ├── question32.c ├── question9.c ├── question14.c ├── question22.c ├── question33.c ├── question34.c ├── question7.c ├── question1.c ├── question35.c └── question18.c ├── misc ├── question25.c ├── question27.c ├── question54.c ├── question63.c ├── question48.c ├── question13.c ├── question31.c ├── question47.c ├── question4.c ├── question3.c ├── question20.c ├── question62.c ├── question67.c ├── question8.c ├── question18.c ├── question46.c ├── question65.c ├── question19.c ├── question10.c ├── question17.c ├── question24.c ├── question64.c ├── question51.c ├── question21.c ├── question33.c ├── question40.c ├── question26.c ├── question2.c ├── question7.c ├── question55.c ├── question1.c ├── question56.c ├── question14.c ├── question6.c ├── question60.c ├── question11.c ├── question50.c ├── question49.c ├── question12.c ├── question28.c ├── question39.c ├── question41.c ├── question42.c ├── question35.c ├── question57.c ├── question52.c ├── question59.c ├── question32.c ├── question9.c ├── question22.c ├── question53.c └── question43.c ├── programs ├── enumeration.c ├── hello-world.c ├── count-string-constant.c ├── star-3.c ├── even-odd.c ├── remove-char.c ├── fibonacci.c ├── check-prime.c ├── put-get-char.c ├── fahrenheit-celsius.c ├── simple-calculator.c ├── armstrong.c ├── check-palindrome.c ├── factorial.c └── string-functions.c ├── linked-lists ├── question16.c ├── question3.c ├── question5.c ├── question2.c ├── question4.c └── question11.c ├── greedy ├── question4.c ├── question6.c ├── question3.c ├── question1.c ├── question2.c ├── question10.c └── question11.c ├── general ├── question3.c ├── question6.c ├── question7.c ├── question1.c ├── question2.c ├── question9.c ├── question4.c ├── question25.c ├── question19.c ├── question5.c ├── question13.c ├── question11.c ├── question23.c ├── question16.c ├── question10.c ├── question8.c ├── question18.c ├── question20.c ├── question15.c ├── question14.c ├── question24.c ├── question21.c └── question12.c ├── test1.c ├── bit-manipulation ├── question1.c └── question2.c ├── heaps ├── question10.c ├── question4.c ├── question9.c ├── question3.c ├── question8.c └── question1.c ├── pattern-matching ├── question4.c └── question1.c ├── complex-datastructures ├── making-a-tree.c └── making-a-graph.c ├── hashing └── question1.c └── hacker-rank └── question1.c /back-tracking/question4.c: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /stacks-and-queues/divide-and-conquor: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # output files in C 2 | */*.out 3 | *.out 4 | .swp 5 | *.o 6 | test.c 7 | *.txt 8 | -------------------------------------------------------------------------------- /trees/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Intervue/Data-structures-algorithms-for-interviews/HEAD/trees/a.exe -------------------------------------------------------------------------------- /arrays/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Intervue/Data-structures-algorithms-for-interviews/HEAD/arrays/a.exe -------------------------------------------------------------------------------- /strings/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Intervue/Data-structures-algorithms-for-interviews/HEAD/strings/a.exe -------------------------------------------------------------------------------- /graphs/question8.c: -------------------------------------------------------------------------------- 1 | /* 2 | Detect a cycle in a directed graph 3 | 4 | Will be done once back edges are done 5 | */ 6 | 7 | -------------------------------------------------------------------------------- /stacks-and-queues/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Intervue/Data-structures-algorithms-for-interviews/HEAD/stacks-and-queues/a.exe -------------------------------------------------------------------------------- /divide-and-conquer/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Intervue/Data-structures-algorithms-for-interviews/HEAD/divide-and-conquer/a.exe -------------------------------------------------------------------------------- /dynamic-programming/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Intervue/Data-structures-algorithms-for-interviews/HEAD/dynamic-programming/a.exe -------------------------------------------------------------------------------- /misc/question25.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main(){ 8 | return 0; 9 | } -------------------------------------------------------------------------------- /trees/question3.c: -------------------------------------------------------------------------------- 1 | /* 2 | Program to make a mirror tree of a binary tree 3 | 4 | 5 | 6 | */ 7 | #include 8 | #include 9 | 10 | -------------------------------------------------------------------------------- /dynamic-programming/question31.c: -------------------------------------------------------------------------------- 1 | /* 2 | Given a string of digits, find the length of the longest substring of a string, such that the length of 3 | the substring is '2k' digits and sum of left k digits is equal to the sum of right k digits 4 | 5 | Do yourself 6 | */ 7 | 8 | -------------------------------------------------------------------------------- /programs/enumeration.c: -------------------------------------------------------------------------------- 1 | /* Program to show enumeration datatype */ 2 | 3 | #include 4 | 5 | int main(){ 6 | //this will interpret no as 0 and yes as 1. So these can now be used conditionally 7 | enum boolean {no, yes}; 8 | 9 | if(yes){ 10 | printf( "hello world\n"); 11 | }else{ 12 | printf( "nothing\n"); 13 | } 14 | 15 | } -------------------------------------------------------------------------------- /divide-and-conquer/question2.c: -------------------------------------------------------------------------------- 1 | /* 2 | Given two array with nuts and bolts each, and given that each nut only maps to a single bolt and vice 3 | versa, find each pair 4 | 5 | METHOD: 6 | sort and then compare 7 | Time complexity: O(nlogn) 8 | 9 | METHOD: 10 | quick sort way?? 11 | http://www.geeksforgeeks.org/nuts-bolts-problem-lock-key-problem/ 12 | */ -------------------------------------------------------------------------------- /linked-lists/question16.c: -------------------------------------------------------------------------------- 1 | /* 2 | Apply merge sort on linked list 3 | 4 | T(N) - total time taken to solve algo (sort) 5 | 6 | Time complexity: 7 | 1) Find middle of linked list O(N) 8 | 2) Sort first half //T(N)/2 9 | 3) Sort second half //T(N)/2 10 | 4) Merge them O(N) //we have already seen this 11 | 12 | Total = 2T(N)/2 + KN = O(NlogN) 13 | 14 | */ -------------------------------------------------------------------------------- /programs/hello-world.c: -------------------------------------------------------------------------------- 1 | /* Program to print Hello world in C */ 2 | 3 | //pre-process fetching contents of library stdio.h which contains predefined functions in C 4 | #include 5 | 6 | //default function that is run by C everytime 7 | int main(){ 8 | //predefined function from the library which prints the output given to it 9 | printf("Hello world \n"); 10 | } -------------------------------------------------------------------------------- /dynamic-programming/question23.c: -------------------------------------------------------------------------------- 1 | /* 2 | Longest non-overlapping repeating sub string 3 | 4 | Total number of sub strings of a given string are n^2. 5 | Now, once we have a given string, we need to see whether that substring is a part of the remaining string or 6 | not. This can be done using KMP algorithm in O(n) time 7 | Therefore total time complexity: O(n^3) 8 | 9 | Better method: 10 | 11 | 12 | */ 13 | -------------------------------------------------------------------------------- /programs/count-string-constant.c: -------------------------------------------------------------------------------- 1 | /* Program to count the number of characters in a string constant */ 2 | 3 | #include 4 | 5 | //strlen is the name of the function which is taking character as argument 6 | int strlen(char s[]){ 7 | 8 | int i = 0; 9 | //this loop increments i until end of string is reached 10 | while(s[i] != '\0'){ 11 | i = i + 1; 12 | } 13 | 14 | return i; 15 | 16 | } 17 | 18 | //need something to convert string to array in main() -------------------------------------------------------------------------------- /trees/question16.c: -------------------------------------------------------------------------------- 1 | /* 2 | Print bottom view of a binary tree 3 | 4 | Same method using double linked list will be applied here as done in previous question and question12. 5 | Instead of chaining the elements using SLL we maintain only one node, and keep replacing it 6 | if any other node comes up at that distance. Time and space complexity remains the same. 7 | 8 | This cannot be done using min and max without space complexity method as done in question15 9 | 10 | */ -------------------------------------------------------------------------------- /greedy/question4.c: -------------------------------------------------------------------------------- 1 | /* 2 | Optimal merge patterns 3 | 4 | In this some files with sorted number of records are given. We need to make an algo such that 5 | there is min record movement when merging them in sorted order into single file 6 | 7 | Same algo as that of Huffman codes. File with lowest records is moved more so is kept at the bottom 8 | away from the root. Files with higher number of records are placed closer to the root i.e. merged 9 | later 10 | 11 | Time complexity: O(nlogn) 12 | 13 | */ -------------------------------------------------------------------------------- /general/question3.c: -------------------------------------------------------------------------------- 1 | /* 2 | Program to swap two variables without using any other variable 3 | */ 4 | 5 | #include 6 | 7 | int main(){ 8 | 9 | int num1,num2; 10 | 11 | printf("enter the two variables needed to be swapped: \n"); 12 | scanf("%d %d", &num1, &num2); 13 | 14 | printf("num1 %d\t num2 %d\n", num1, num2); 15 | 16 | num1 = num1 + num2; 17 | 18 | num2 = num1 - num2; 19 | 20 | num1 = num1 - num2; 21 | 22 | printf("After swapping\n num1 %d\t num2 %d\n", num1, num2); 23 | 24 | } -------------------------------------------------------------------------------- /programs/star-3.c: -------------------------------------------------------------------------------- 1 | /* Program to print star 3 pattern */ 2 | 3 | //pre-process fetching contents of library stdio.h which contains predefined functions in C 4 | #include 5 | 6 | //default function that is run by C everytime 7 | int main(){ 8 | 9 | for(int i = 0; i < 5; i++){ 10 | 11 | for(int j = 5; j >=i; j--){ 12 | printf(" "); 13 | } 14 | 15 | for(int k = 0; k <= 2*i; k++){ 16 | printf("*"); 17 | } 18 | 19 | printf("\n"); 20 | 21 | } 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /general/question6.c: -------------------------------------------------------------------------------- 1 | /* 2 | Use ternary condition operator to find min and max among three integers 3 | output: min and max of three 4 | */ 5 | 6 | #include 7 | 8 | int main(){ 9 | 10 | int a,b,c, max, min; 11 | 12 | printf("enter 3 integers:\n"); 13 | scanf("%d %d %d", &a,&b,&c); 14 | 15 | max = (a>b)?((a>c)?a:c):((b>c)?b:c); //oneway 16 | min = (a < b && a < c)? a: (b < c)? b: c; //another way 17 | 18 | printf("max number is: %d\n", max); 19 | printf("min number is: %d\n", min); 20 | 21 | } -------------------------------------------------------------------------------- /misc/question27.c: -------------------------------------------------------------------------------- 1 | /* 2 | Multiply two strings 3 | http://practice.geeksforgeeks.org/problems/multiply-two-strings/1 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | int toint(char c){ 11 | return c-'0'; 12 | } 13 | 14 | void multiply(int *str1, int *str2){ 15 | 16 | 17 | } 18 | 19 | int main(){ 20 | int cases; 21 | scanf("%d",&cases); 22 | while(cases > 0){ 23 | char str1[101],str2[101]; 24 | scanf("%s %s", str1, str2); 25 | 26 | multiply(arr1,arr2); 27 | } 28 | return 0; 29 | } -------------------------------------------------------------------------------- /programs/even-odd.c: -------------------------------------------------------------------------------- 1 | /* Program see if a number is even or odd C */ 2 | 3 | //pre-process fetching contents of library stdio.h which contains predefined functions in C 4 | #include 5 | 6 | //default function that is run by C everytime 7 | int main(){ 8 | int num; 9 | 10 | printf("enter some number: \n"); 11 | scanf("%d", &num); //address of num will be returned when we write &num 12 | 13 | if(num%2 == 0){ 14 | printf("number is even\n"); 15 | }else{ 16 | printf("number is odd\n"); 17 | } 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /greedy/question6.c: -------------------------------------------------------------------------------- 1 | /* 2 | Program for KRUSKALS algorithm 3 | 4 | To find the min cost spanning tree of undirected weighted graph 5 | 6 | METHOD: 7 | In this case rather then picking the min edge for once and then choosing neighbours which are min, we 8 | choose min numbers from all over the graph each time. Thus there wont be a single tree that is growing 9 | but different forests will be there which will be connected in the end. A condition to keep in mind 10 | is that there should be no cycle formed when picking edges like that. 11 | */ -------------------------------------------------------------------------------- /misc/question54.c: -------------------------------------------------------------------------------- 1 | /* 2 | http://practice.geeksforgeeks.org/problems/power-of-2/0 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | int isPower(unsigned long long int num) { 9 | return (num && !(num & (num - 1))); 10 | } 11 | 12 | int main(){ 13 | int cases; 14 | int i; 15 | scanf("%d", &cases); 16 | for(i=0;i 10 | #include 11 | #include 12 | #define size 11 13 | 14 | int main(){ 15 | int a; 16 | 17 | srand(time(NULL)); 18 | 19 | a = 1+rand()%size; 20 | 21 | if(a < 3){ 22 | printf("%s\n", "a"); 23 | }else if(a < 6){ 24 | printf("%s\n", "b"); 25 | }else if(a > 9){ 26 | printf("%s\n", "c"); 27 | }else{ 28 | printf("%s\n", "d"); 29 | } 30 | 31 | } 32 | 33 | 34 | -------------------------------------------------------------------------------- /test1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(){ 5 | int cases; 6 | int i; 7 | scanf("%d",&cases); 8 | for(i=0;i 6 | #include 7 | 8 | int dig[] = {1, 1, 2, 6, 4, 2, 2, 4, 2, 8}; 9 | 10 | int lastNon0Digit(int n) { 11 | if(n < 10) { 12 | return dig[n]; 13 | }else{ 14 | int a = 6; 15 | int b = 4; 16 | if (((n/10)%10)%2 == 0) 17 | return (6*lastNon0Digit(n/5)*dig[n%10]) % 10; 18 | else 19 | return (4*lastNon0Digit(n/5)*dig[n%10]) % 10; 20 | } 21 | } 22 | 23 | int main() { 24 | 25 | int cases; 26 | scanf("%d",&cases); 27 | int i; 28 | for(i=0;i 5 | 6 | void squeeze(char s[], int c){ 7 | 8 | int i, j; 9 | 10 | for(i=j=0; s[i] != '\0'; i++){ 11 | 12 | if(s[i] != c){ //it will replace that letter with the next letter and the increment j 13 | s[j++] = s[i]; 14 | } 15 | 16 | } 17 | 18 | s[j] = '\0'; 19 | 20 | printf("%s\n", s); 21 | } 22 | //default function that is run by C everytime 23 | int main(){ 24 | 25 | char s[] = "rachulc"; 26 | 27 | squeeze(s, 'c'); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /general/question1.c: -------------------------------------------------------------------------------- 1 | /* 2 | Question description: 3 | 4 | printing nth term of arithmetic series 5 | input: first term, common difference and value of n 6 | output: nth and element and sum of n elements 7 | */ 8 | 9 | #include 10 | 11 | int main(){ 12 | 13 | int firstterm, d, n, nth_element, sum; 14 | 15 | printf("enter the first term, common difference and value of n\n"); 16 | scanf("%d %d %d", &firstterm, &d, &n); 17 | 18 | nth_element = firstterm + (n-1)*d; 19 | sum = (n/2)*(2*firstterm+(n-1)*d); 20 | 21 | printf("nth term of the series is: %d\n", nth_element); 22 | printf("and the sum of the series is: %d\n", sum); 23 | 24 | } -------------------------------------------------------------------------------- /misc/question48.c: -------------------------------------------------------------------------------- 1 | /* 2 | http://practice.geeksforgeeks.org/problems/collecting-trees/0 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | int findTrees(int n){ 10 | 11 | int count = 0; 12 | int power; //this will basically store the value and not the power 13 | while(n > 0){ 14 | power = pow(2,floor(log(n)/log(2))); 15 | n = n - power; 16 | count++; 17 | } 18 | 19 | return count; 20 | } 21 | 22 | 23 | 24 | int main(){ 25 | 26 | int cases; 27 | scanf("%d",&cases); 28 | int i; 29 | for(i=0;i 10 | 11 | int main(){ 12 | 13 | int a[] = {2,5,1,9,6,7,3,4}; 14 | int length = sizeof(a)/sizeof(a[0]); 15 | 16 | int i=0,j=length-1,temp; 17 | 18 | while(i 10 | #include 11 | 12 | int setBits(int num){ 13 | int rem; 14 | 15 | int count = 0; 16 | while(num){ 17 | 18 | 19 | rem = num%2; 20 | if(rem){ 21 | count++; 22 | } 23 | 24 | num = num/2; 25 | 26 | 27 | 28 | } 29 | 30 | return count; 31 | } 32 | 33 | 34 | int main(){ 35 | 36 | int n; 37 | printf("enter the number\n"); 38 | scanf("%d",&n); 39 | 40 | printf("num set bits are: %d\n", setBits(n)); 41 | 42 | } -------------------------------------------------------------------------------- /heaps/question10.c: -------------------------------------------------------------------------------- 1 | /* 2 | Convert BST to max heap 3 | 4 | METHOD1: 5 | Since BST is generally not stored in the array, so we cannot use the data structure to convert directly 6 | into the max heap as we did in min to max heap or vice versa conversion. Therefore we do a INORDER traversal 7 | of BST. It will give us numbers in ascending order. Now reversing the array will be descending order which is 8 | nothing but a MAX HEAP 9 | Time complexity: O(n) 10 | Space complexity: O(1) 11 | 12 | Note in the method above we could have simply done reverse INORDER TRAVERSAL in order to avoid reversing the 13 | array. In that we traverse the RST print and then traverse LST 14 | */ -------------------------------------------------------------------------------- /programs/fibonacci.c: -------------------------------------------------------------------------------- 1 | /* Program to print fibonacci series in C */ 2 | 3 | //pre-process fetching contents of library stdio.h which contains predefined functions in C 4 | #include 5 | 6 | //default function that is run by C everytime 7 | int main(){ 8 | //predefined function from the library which prints the output given to it 9 | int num, prev, last, sum; 10 | 11 | printf("enter a number\n"); 12 | scanf("%d", &num); 13 | 14 | for(int i=0; i <=num; i++){ 15 | 16 | if(i < 2){ 17 | prev = 0; 18 | last = 1; 19 | } 20 | 21 | sum = prev + last; 22 | 23 | printf("%d ", sum); 24 | 25 | prev = last; 26 | last = sum; 27 | 28 | 29 | 30 | } 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /arrays/question23.c: -------------------------------------------------------------------------------- 1 | /* 2 | Find the duplicate in O(n) time and O(1) extra space 3 | Only valid for numbers in range 1 to N 4 | 5 | Method: In this we visit a number lets say its 3, we go to index 3 and make the number there 6 | negative. Then if 3 is repeated we will again go to 3 and check if its already negative. 7 | Time complexity: O(n) 8 | Space complexity: O(1) 9 | */ 10 | 11 | #include 12 | #include 13 | 14 | int main(){ 15 | 16 | int a[] = {1,2,3,5,8,1,1,2}; 17 | int size = sizeof(a)/sizeof(a[0]); 18 | 19 | for(int i=0;i 0){ 21 | a[abs(a[i])] = -1*a[abs(a[i])]; 22 | }else{ 23 | printf("%d\n", abs(a[i])); 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /general/question2.c: -------------------------------------------------------------------------------- 1 | /* 2 | Question description: 3 | A square is made by bending a single piece of wire. If we make a circle of this wire, 4 | what will be the radius and area of the circle? 5 | input = lenght of side of square 6 | */ 7 | 8 | #include 9 | 10 | int main(){ 11 | 12 | int length_side_square; 13 | double radius, area; 14 | 15 | printf("enter the length of side of the square: \n"); 16 | scanf("%d", &length_side_square); 17 | 18 | radius = (4*length_side_square)/(2*3.14); //now total length will be the circumference 19 | 20 | area = 3.14*radius*radius; 21 | 22 | printf("radius of the square is %f\n", radius); 23 | printf("area of the square is %f\n", area); 24 | 25 | } -------------------------------------------------------------------------------- /programs/check-prime.c: -------------------------------------------------------------------------------- 1 | /* Program to check if a number if prime or not C */ 2 | 3 | //pre-process fetching contents of library stdio.h which contains predefined functions in C 4 | #include 5 | 6 | //default function that is run by C everytime 7 | int main(){ 8 | 9 | int num; 10 | 11 | int isPrime = 1; 12 | 13 | printf("enter a number\n"); 14 | scanf("%d", &num); 15 | 16 | for(int i = 2; i <=num/2; i++){ 17 | 18 | if(num%i == 0){ 19 | isPrime = 0; 20 | break;//breaking out of the loop once out check is satisfied 21 | } 22 | 23 | } 24 | 25 | if(isPrime == 0){ 26 | printf("number is not prime\n"); 27 | }else{ 28 | printf("number is prime\n"); 29 | } 30 | 31 | 32 | } -------------------------------------------------------------------------------- /programs/put-get-char.c: -------------------------------------------------------------------------------- 1 | /* Program to print whatever character is given as input from keyboard at runtime */ 2 | 3 | //pre-process fetching contents of library stdio.h which contains predefined functions in C 4 | #include 5 | 6 | //default function that is run by C everytime 7 | int main(void){ 8 | //declaring a variable c that will take the input given 9 | int c; 10 | //assigning input using this function to variable c 11 | c = getchar(); 12 | //this will keep printing until end of file is there 13 | while(c != EOF){ 14 | //this function will print output on the screen 15 | putchar(c); 16 | //after printing the output it takes input until while loop ends 17 | c = getchar(); 18 | } 19 | } -------------------------------------------------------------------------------- /misc/question13.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX 60 4 | 5 | int cmpfnc(const void *a, const void *b){ 6 | return (*(int *)a - *(int *)b); 7 | } 8 | 9 | struct node{ 10 | int index; 11 | int count; 12 | int value; 13 | }; 14 | 15 | void freqSort(int *arr, int size){ 16 | 17 | struct node ref[size]; 18 | 19 | int i; 20 | 21 | for(i=0; i<) 22 | 23 | 24 | 25 | } 26 | 27 | 28 | int main(){ 29 | int cases; 30 | scanf("%d",&cases); 31 | 32 | int i; 33 | for(i=0;i 8 | #include 9 | 10 | void display(int *arr, int n){ 11 | int i; 12 | for(i=0;i 12 | int main(){ 13 | 14 | int num, remainder, sum=0; 15 | 16 | printf("enter a number: \n"); 17 | scanf("%d", &num); 18 | 19 | while(num > 0){ 20 | 21 | remainder = num%10; 22 | 23 | sum += remainder; 24 | 25 | num /= 10; 26 | 27 | } 28 | 29 | printf("the sum is %d\n", sum); 30 | 31 | } 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /strings/question5.c: -------------------------------------------------------------------------------- 1 | /* 2 | Reverse a given string 3 | 4 | METHOD: 5 | take two pointers, one at start and one at the end and swap the two values, then advance the pointers to the 6 | right and left resp. and swap again. Keep repeating until the whole string is swapped 7 | Time complexity: O(n) 8 | Space complexity: O(1) 9 | */ 10 | #include 11 | #include 12 | #include 13 | 14 | void reverse(char *arr,int size){ 15 | int j = size-1; 16 | int i=0; 17 | while(i<=j){ 18 | int temp = arr[i]; 19 | arr[i] = arr[j]; 20 | arr[j] = temp; 21 | i++; 22 | j--; 23 | } 24 | } 25 | 26 | int main(){ 27 | char arr[] = "rahul"; 28 | int size = strlen(arr); 29 | reverse(arr,size); 30 | printf("the reverse string is %s\n", arr); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /misc/question31.c: -------------------------------------------------------------------------------- 1 | /* 2 | Number of paths 3 | http://practice.geeksforgeeks.org/problems/number-of-paths/0 4 | */ 5 | #include 6 | #include 7 | 8 | int totalPaths(int rows, int columns){ 9 | int dp[rows][columns]; 10 | 11 | int i,j; 12 | dp[0][0] = 0; 13 | for(i=1;i 9 | 10 | int main(){ 11 | 12 | int fahr, celsius; 13 | 14 | int lower, upper, step; 15 | 16 | lower = 0; // this is the lower limit of temperature table 17 | upper = 300; // this is the upper limit of temperature table 18 | step = 20; //step size 19 | 20 | fahr = lower; //initialized with lower value 21 | 22 | while(fahr <= upper){ 23 | 24 | celsius = 5*(fahr - 32)/9; //here the precedence and associativity can be seen 25 | 26 | //will print the result in table format 27 | printf("%d\t %d\n", fahr, celsius); 28 | 29 | fahr += step; //incrementing by step size 30 | 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /stacks-and-queues/question9.c: -------------------------------------------------------------------------------- 1 | /* 2 | Check whether parenthesis are balanced or not 3 | 4 | In this we push each parenthesis on to stack and whenever there is a parenthesis we compare it with the top 5 | of the stack, if it is same, we pop it off. Keep doing that for every parenthesis. 6 | In the end if stack is empty, then parenthesis is balanced 7 | 8 | Time complexity: O(n) 9 | Space complexity: O(n) 10 | */ 11 | 12 | #include 13 | #include 14 | 15 | int main(){ 16 | char input[100]; 17 | int step; 18 | while(1){ 19 | printf("1. Enter set of characteristics\n"); 20 | printf("1. Exit\n"); 21 | 22 | switch(step){ 23 | case 1: gets(input); 24 | 25 | break; 26 | case 2: exit(1); 27 | break; 28 | } 29 | 30 | } 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /misc/question47.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | 6 | int matchNow(char *pattern, char *text){ 7 | 8 | int plen = strlen(pattern); 9 | int tlen = strlen(text); 10 | 11 | int i=0,j=0; 12 | 13 | while(i 10 | 11 | int main(){ 12 | 13 | int t1[2], t2[2], t3[2], i, t1_length, t2_length, t3_length; 14 | 15 | for(i = 0; i < 3; i++){ 16 | 17 | printf("enter the x and y coordinates for triangle %d\n", i+1); 18 | if(i==0){ 19 | scanf("%d %d", &t1[0], &t1[1]); 20 | } 21 | if(i==1){ 22 | scanf("%d %d", &t2[0], &t2[1]); 23 | } 24 | if(i==2){ 25 | scanf("%d %d", &t3[0], &t3[1]); 26 | } 27 | 28 | 29 | } 30 | 31 | //@TODO 32 | 33 | 34 | 35 | 36 | } -------------------------------------------------------------------------------- /programs/simple-calculator.c: -------------------------------------------------------------------------------- 1 | /* Program to show enumeration datatype */ 2 | 3 | #include 4 | 5 | int main(){ 6 | 7 | char operator; 8 | double a,b; 9 | printf("enter an operator (+, -, *, /)\n"); 10 | scanf("%c",&operator); 11 | 12 | printf("enter two operators \n"); 13 | //lf is the format specifier for double 14 | scanf("%lf %lf",&a, &b); 15 | 16 | switch(operator){ 17 | //representing operators in single quotes will convert them to integers (ascii values) 18 | case '+': printf("additon of a and b: %lf\n", a+b); break; 19 | case '-': printf("subtraction of a and b: %lf\n", a-b); break; 20 | case '/': printf("division of a and b: %lf\n", a/b); break; 21 | case '*': printf("multiplication of a and b: %lf\n", a*b); break; 22 | default: printf("please enter a valid operator\n"); break; 23 | } 24 | 25 | 26 | } -------------------------------------------------------------------------------- /misc/question4.c: -------------------------------------------------------------------------------- 1 | /**/ 2 | #include 3 | #include 4 | 5 | int findMin(int a, int b, int c){ 6 | return (a < b)? ((a < c)?a:c): ((b < c)?b:c); 7 | } 8 | 9 | int calculate(int start,int target, int a, int b){ 10 | if(start >= target){ 11 | return 0; 12 | } 13 | int x,y,z; 14 | if(start == 0){ 15 | x = b; 16 | }else{ 17 | x = b + calculate(2*start, target, a, b); 18 | y = a + calculate(start + 1, target, a, b); 19 | z = a + calculate(start -1 ,target, a, b); 20 | } 21 | return findMin(x,y,z); 22 | } 23 | 24 | int main(){ 25 | int cases; 26 | scanf("%d",&cases); 27 | int i; 28 | int a,b,n; 29 | for(i=0;i 14 | #include 15 | 16 | int isPowerOfTwo(int n){ 17 | return n && !(n & (n-1)); 18 | } 19 | 20 | int main(){ 21 | int n; 22 | printf("enter the number\n"); 23 | scanf("%d",&n); 24 | 25 | if(isPowerOfTwo(n)){ 26 | printf("number is pwr\n"); 27 | }else{ 28 | printf("no.\n"); 29 | } 30 | return 0; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /dynamic-programming/question37.c: -------------------------------------------------------------------------------- 1 | /* 2 | Collect max coins from the grid in two traversals 3 | 4 | Given a matrix where every cell represents points. How to collect maximum points using two traversals 5 | under following conditions? 6 | 7 | Let the dimensions of given grid be R x C. 8 | 9 | 1) The first traversal starts from top left corner, i.e., (0, 0) and should reach left bottom corner, 10 | i.e., (R-1, 0). The second traversal starts from top right corner, i.e., (0, C-1) and should reach bottom 11 | right corner, i.e., (R-1, C-1)/ 12 | 13 | 2) From a point (i, j), we can move to (i+1, j+1) or (i+1, j-1) or (i+1, j) 14 | 15 | 3) A traversal gets all points of a particular cell through which it passes. If one traversal has already 16 | collected points of a cell, then the other traversal gets no points if goes through that cell again. 17 | */ 18 | 19 | -------------------------------------------------------------------------------- /misc/question3.c: -------------------------------------------------------------------------------- 1 | /* 2 | Additive sequence 3 | */ 4 | #include 5 | #include 6 | #include 7 | 8 | int isAdditive(char *str){ 9 | int length = strlen(str); 10 | 11 | int isAdditive = 1; 12 | 13 | int i; 14 | 15 | 16 | 17 | printf("%d\n", str[length-1]-'0'); 18 | // for(i=2;i 12 | #include 13 | #include 14 | 15 | int findMax(int arr[], int size){ 16 | int index = floor(size/2) + 1; 17 | int max = arr[index]; 18 | for(int i=index+1;imax){ 20 | max = arr[i]; 21 | } 22 | } 23 | 24 | return max; 25 | 26 | } 27 | 28 | int main(){ 29 | int minHeap[] = {1,3,5,7,9,8,10,11,12}; 30 | int size = sizeof(minHeap)/sizeof(minHeap[0]); 31 | int max = findMax(minHeap, size); 32 | 33 | printf("max element in the min heap is %d\n", max); 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /pattern-matching/question4.c: -------------------------------------------------------------------------------- 1 | /* 2 | Rabin Karp string matching algorithm 3 | 4 | In this algorithm we compute the hash value (using some hash function) of the pattern string 5 | 6 | Now in the text string we keep taking substrings of length equal to pattern string and compute there 7 | hash value. To efficiently compute the hash value (assume length of pattern string is 3) 8 | 9 | text: abbecd 10 | pattern: abb 11 | 12 | initially abb's hash value will be found then for bba, we can simly subtract a and add e's hash 13 | value to make this happen in constant time, 14 | 15 | One hash value is a match with pattern string hash value, then we can compare the substring and the pattern 16 | string just to be sure. 17 | 18 | Time complexity: O(mn) //comparing hash and substring 19 | Space complexity: O(1) 20 | 21 | Disadvantage: Making the hash function is not that easy 22 | */ -------------------------------------------------------------------------------- /programs/armstrong.c: -------------------------------------------------------------------------------- 1 | /* Program to check if a number is armstrong or not in C */ 2 | 3 | //pre-process fetching contents of library stdio.h which contains predefined functions in C 4 | #include 5 | #include 6 | 7 | //default function that is run by C everytime 8 | int main(){ 9 | 10 | int num, result=0, originalnumber, n=0, remainder; 11 | 12 | printf("enter a number\n"); 13 | scanf("%d", &num); 14 | originalnumber = num; 15 | 16 | while(originalnumber != 0){ 17 | originalnumber = originalnumber/10; 18 | n++; 19 | } 20 | originalnumber = num; 21 | while(originalnumber != 0){ 22 | remainder = originalnumber%10; 23 | result += pow(remainder, n); 24 | originalnumber /= 10; 25 | } 26 | 27 | if(result == num){ 28 | printf("this is armstrong number"); 29 | }else{ 30 | printf("this is not an armstrong number"); 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /general/question25.c: -------------------------------------------------------------------------------- 1 | /* 2 | GCD and LCM of two numbers. 3 | 4 | The logic is that GCD of two numbers does not change even if smaller number is subtracter from 5 | larger, so we can keep doing it until one of them reaches 0 or becomes equal to the other. 6 | 7 | LCM can be found out by multiplying two numbers and dividing by HCF. 8 | 9 | */ 10 | 11 | #include 12 | #include 13 | 14 | int gcd(int a, int b){ 15 | if(a == 0 || b== 0){ 16 | return 0; 17 | } 18 | if(a == b){ 19 | return a; 20 | } 21 | if(a > b){ 22 | return gcd(a-b,b); 23 | } 24 | return gcd(a,b-a); 25 | } 26 | 27 | int findlcm(int hcf, int a, int b){ 28 | return (a*b)/hcf; 29 | } 30 | 31 | int main(){ 32 | int a = 20; 33 | int b = 10; 34 | 35 | int hcf = gcd(a,b); 36 | int lcm = findlcm(hcf,a,b); 37 | 38 | printf("lcm is: %d hcf is: %d\n",lcm, hcf); 39 | 40 | return 0; 41 | } -------------------------------------------------------------------------------- /misc/question20.c: -------------------------------------------------------------------------------- 1 | /* 2 | http://practice.geeksforgeeks.org/problems/buildings-receiving-sunlight/0 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | int totalBuildings(int *arr, int size){ 10 | int max = arr[0]; 11 | int i; 12 | 13 | int count = 1; 14 | 15 | for(i=1;i 5 | 6 | //default function that is run by C everytime 7 | int main(){ 8 | //predefined function from the library which prints the output given to it 9 | int num, lastdigit, result = 0; 10 | printf("enter a number: \n"); 11 | scanf("%d", &num); 12 | 13 | int originalnum = num; 14 | 15 | while(originalnum !=0){ //making that number in reverse 16 | lastdigit = originalnum%10; 17 | 18 | //concatenate 19 | result = result*10 + lastdigit; //to make that number in reverse 20 | 21 | originalnum /= 10; 22 | } 23 | 24 | if(num == result){ 25 | printf("number is palindrome\n"); 26 | }else{ 27 | printf("number is not palindrome\n"); 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /dynamic-programming/question5.c: -------------------------------------------------------------------------------- 1 | /* 2 | Find a subset in an array whose sum is w 3 | 4 | METHOD: 5 | Since number of subsets possible are 2^n, by brute force we can solve it 6 | 7 | Better method: 8 | we cannot use greedy here, as it will always pick the smallest or largest number which wont give the 9 | right answer always, hence we use DP. 10 | DP in this case is similar to 0/1 knapsack problem. Therefore we need a sum of S and at any point if 11 | we include the ith element, it will be 12 | S(i,n) = s(i,n-ai) || s(i-1,n) ;n>ai //by taking first i elements is the sum of n possible 13 | //either include it || dont include it 14 | = s(i-1,n) ; n 9 | #include 10 | 11 | struct node{ 12 | int data; 13 | struct node *link; 14 | }; 15 | 16 | void printList(struct node *t){ 17 | if(t){ 18 | //interchanging these lines will print it in reverse order 19 | printf("%d\n", t->data); 20 | printList(t->link); 21 | } 22 | } 23 | 24 | int main(){ 25 | 26 | struct node *head = (struct node *)malloc(sizeof(struct node)); 27 | 28 | struct node *t = head; 29 | 30 | int counter = 1; 31 | while(counter<=5){ 32 | 33 | t->data = counter*10; 34 | if(counter == 5){ 35 | t->link = NULL; 36 | }else{ 37 | t->link = (struct node *)malloc(sizeof(struct node)); 38 | } 39 | 40 | t = t->link; 41 | 42 | counter++; 43 | } 44 | t = head; 45 | printList(t); 46 | 47 | } -------------------------------------------------------------------------------- /programs/factorial.c: -------------------------------------------------------------------------------- 1 | /* Program to find factorial of a number in C */ 2 | 3 | //pre-process fetching contents of library stdio.h which contains predefined functions in C 4 | #include 5 | 6 | //default function that is run by C everytime 7 | int main(){ 8 | 9 | int num; 10 | 11 | printf("enter a number\n"); 12 | scanf("%d", &num); 13 | 14 | if(num < 0){ 15 | printf("factorial for negative numbers does not exist\n"); 16 | } 17 | 18 | //since factorial of a number if always positive it is better to use unsigned for a bigger range (0 - range) 19 | // and not (-range to range) 20 | unsigned long long fact = 1; //integer having bigger size compared to normal integer 21 | 22 | for(int i = 2; i <= num; i++){ 23 | 24 | fact = fact*i; 25 | 26 | } 27 | 28 | printf("factorial is %d = %llu\n", num, fact); //llu is format specifier for long long int 29 | 30 | return 0; 31 | 32 | } -------------------------------------------------------------------------------- /general/question19.c: -------------------------------------------------------------------------------- 1 | /* 2 | 2. Write a C program to find the average of "n" integers entered by user. 3 | To perform this task, allocate memory dynamically, and do not use 4 | any statically declared large array. 5 | Input: value of "n", and the "n" integers 6 | Output: The average of the "n" integers entered [2] 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | 13 | int main(){ 14 | 15 | int n; float avg = 0; 16 | printf("enter value of n\n"); 17 | scanf("%d",&n); 18 | 19 | int *a = (int *)malloc(n*sizeof(int)); 20 | 21 | for(int i=0; i 7 | #include 8 | 9 | int main(){ 10 | 11 | int x1,y1,x2,y2,x3,y3, slope1, slope2, slope3; 12 | 13 | printf("enter values of x and y for 3 points in a cartesian plane:\n"); 14 | 15 | scanf("%d %d %d %d %d %d", &x1, &y1, &x2, &y2, &x3, &y3); 16 | 17 | if(abs(y2-y1) == 0){ 18 | slope1 = 0; 19 | }else{ 20 | slope1 = abs(x2-x1)/abs(y2-y1); 21 | } 22 | 23 | if(abs(y3-y1) == 0){ 24 | slope2 = 0; 25 | }else{ 26 | slope2 = abs(x3-x1)/abs(y3-y1); 27 | } 28 | 29 | if(abs(y2-y3) == 0){ 30 | slope3 = 0; 31 | }else{ 32 | slope3 = abs(x2-x3)/abs(y2-y3); 33 | } 34 | 35 | if(slope1 == slope2 && slope1==slope3){ 36 | 37 | printf("%s\n", "no, cirlce cannot be drawn"); 38 | }else{ 39 | printf("%s\n", "yes, circle can be drawn"); 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /general/question13.c: -------------------------------------------------------------------------------- 1 | /* 2 | Write a C function to check whether a positive integer is a palindrome or not, 3 | using the C function "void is_palindrome (int number)". 4 | A "palindromic number" is a number that remains the same when its digits are reversed. Eg: 16461 5 | Input: A positive integer. 6 | Output : "yes" if the number is a palindrome, else "no". [3 7 | */ 8 | #include 9 | 10 | void is_palindrome(int a){ 11 | 12 | int reverse = 0; 13 | int originalnum = a; 14 | 15 | while(a){ 16 | reverse = reverse*10 + a%10; 17 | a /= 10; 18 | } 19 | 20 | if(originalnum == reverse){ 21 | printf("number is a palindrome\n"); 22 | }else{ 23 | printf("number is not a palindrome\n"); 24 | } 25 | 26 | } 27 | 28 | int main(){ 29 | 30 | int num; 31 | printf("enter a number\n"); 32 | scanf("%d",&num); 33 | is_palindrome(num); 34 | 35 | } 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /strings/question6.c: -------------------------------------------------------------------------------- 1 | /* 2 | Check whether given string is palindrome or not 3 | 4 | METHOD: 5 | Take two pointers, one at start and one at the end and start comparing the values in it till they meet each 6 | other, at every point value should be same 7 | 8 | Time complexity: O(n) 9 | Space complexity: O(1) 10 | */ 11 | #include 12 | #include 13 | #include 14 | 15 | int checkPalindrome(char *arr,int size){ 16 | int j = size-1; 17 | int i=0; 18 | int temp = 1; 19 | while(i<=j){ 20 | if(arr[i] != arr[j]){ 21 | temp = 0; 22 | break; 23 | } 24 | i++; 25 | j--; 26 | } 27 | return temp; 28 | } 29 | 30 | int main(){ 31 | char arr[] = "arora"; 32 | int size = strlen(arr); 33 | int isPalindrome = checkPalindrome(arr,size); 34 | if(isPalindrome){ 35 | printf("The given string is a palindrome \n"); 36 | }else{ 37 | printf("The given string is not a palindrome\n"); 38 | } 39 | 40 | return 0; 41 | 42 | } -------------------------------------------------------------------------------- /arrays/question4.c: -------------------------------------------------------------------------------- 1 | /* 2 | PARTITION PROCEDURE 3 | 4 | Time complexity: O(n) 5 | Space complexity: O(1) 6 | 7 | Can be remembered as partitioning everytime choosing an element 8 | left all small, right all big 9 | */ 10 | #include 11 | #include 12 | 13 | void partition(int arr[],int p, int r){ 14 | int key = arr[r]; 15 | int i, j=-1, temp; 16 | for(i=p; i 5 | #include 6 | 7 | unsigned long long int f[101]; 8 | 9 | void calFib(int n) { 10 | unsigned long long int a = 1; 11 | unsigned long long int b = 1; 12 | f[0] = a; 13 | f[1] = b; 14 | int i; 15 | for(i=2;i<=n;i++){ 16 | unsigned long long int d = a + b; 17 | f[i] = d; 18 | a = b%1000000007; 19 | b = d%1000000007; 20 | } 21 | } 22 | 23 | unsigned long long int fib(int n) { 24 | if(f[n] == -1) { 25 | if(n < 2) { 26 | f[n] = 1; 27 | }else{ 28 | f[n] = fib(n-1) + fib(n-2); 29 | } 30 | } 31 | return f[n]%1000000007; 32 | } 33 | 34 | int main() { 35 | calFib(100); 36 | int cases; 37 | int i; 38 | // for(i=0;i<1000;i++){ 39 | // printf("%llu\n", f[i]); 40 | // } 41 | scanf("%d",&cases); 42 | for(i=0;i 12 | 13 | void is_power_of_two (unsigned int x){ 14 | while (((x & 1) == 0) && x > 1) /* While x is even and > 1 */ 15 | x >>= 1; 16 | if (x == 1){ 17 | printf("the number is a power of two\n"); 18 | }else{ 19 | printf("the number is not a power of two\n"); 20 | } 21 | } 22 | 23 | int main(){ 24 | int num, remainder, binary = 0, i = 1; 25 | 26 | printf("Enter a number\n"); 27 | scanf("%d", &num); 28 | 29 | is_power_of_two(num); 30 | } 31 | 32 | -------------------------------------------------------------------------------- /general/question23.c: -------------------------------------------------------------------------------- 1 | /* 2 | A file "input.txt" contains a some lines of text. Collect the name of the file from the user, and then 3 | use the C "system()" function, and the standard UNIX command "wc", to find the number 4 | of lines, words and characters in the file. Ensure that the file is readable before trying the "system()" 5 | function. 6 | INPUT: read file name from command line as: ./a.out input.txt 7 | OUTPUT: the number of lines, words characters in the file "input.txt" [2 marks] 8 | 9 | */ 10 | #include 11 | #include 12 | #include 13 | 14 | int main(int argc, char *argv[]){ 15 | 16 | char *str = malloc(sizeof(char)*100); 17 | 18 | strcpy(str, "wc "); 19 | strcat(str, argv[1]); 20 | 21 | printf("%s\n", str); 22 | 23 | if(argc > 1){ 24 | FILE *fp = fopen(argv[1],"r"); 25 | if(fp != NULL){ 26 | system(str); 27 | } 28 | fclose(fp); 29 | } 30 | 31 | } -------------------------------------------------------------------------------- /complex-datastructures/making-a-tree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node{ 5 | int data; 6 | struct node *left; 7 | struct node *right; 8 | }; 9 | 10 | struct node *newNode(int data){ 11 | struct node *temp = (struct node *)malloc(sizeof(struct node)); 12 | temp->data = data; 13 | temp->left = NULL; 14 | temp->right = NULL; 15 | return temp; 16 | } 17 | 18 | void inorder(struct node *root){ 19 | if(root){ 20 | inorder(root->left); 21 | printf("%d ", root->data); 22 | inorder(root->right); 23 | } 24 | } 25 | 26 | void displayTree(struct node *root){ 27 | struct node *temp = root; 28 | inorder(temp); 29 | } 30 | 31 | int main(){ 32 | 33 | struct node *root = newNode(10); 34 | 35 | root->left = newNode(20); 36 | root->right = newNode(30); 37 | root->left->left = newNode(40); 38 | root->left->right = newNode(50); 39 | 40 | root->right->left = newNode(60); 41 | root->right->right = newNode(70); 42 | 43 | displayTree(root); 44 | 45 | return 0; 46 | } -------------------------------------------------------------------------------- /greedy/question3.c: -------------------------------------------------------------------------------- 1 | /* 2 | Make a program to sequence given jobs with deadlines to maximize profits 3 | 4 | Here some jobs will be given along with some profits associated to each job if the job is done in 5 | given deadline. 6 | METHOD: 7 | First we find the job with the max deadline. That is the max slot that will be available to us. Therefore 8 | array will be of that size. Now, we arrange them in decreasing order of profits. Once that is done we pick 9 | max profit job and place it as far away as possible such that it is meeting its deadline. Like this we 10 | keep scanning the empty slot to place a job as far as possible such that it meets its deadline. Like this 11 | we maximize profit. 12 | Note: if the deadline is very very high as compared to the no. of jobs, always limit the array size 13 | with the number of jobs given. So compare both. 14 | Time complexity: O(n^2) //in worst case we will end up scanning all the cells in the array 15 | Space complexity: O(n) //array size or O(max deadline) 16 | */ -------------------------------------------------------------------------------- /misc/question67.c: -------------------------------------------------------------------------------- 1 | /* 2 | Given two strings a and b print whether they contain any common subsequence (non empty) or not. 3 | 4 | Hash table and check if any alphabet of other string is already greater than 0; 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | int hash[26]; 11 | 12 | int commonSub(char *str1, char *str2){ 13 | int len1 = strlen(str1); 14 | int len2 = strlen(str2); 15 | int i; 16 | for(i=0;i<26;i++){ 17 | hash[i] = 0; 18 | } 19 | 20 | for(i=0;i 0){ 26 | return 1; 27 | } 28 | } 29 | return 0; 30 | } 31 | 32 | 33 | int main() { 34 | int cases; 35 | scanf("%d",&cases); 36 | int i; 37 | for(i=0;i 2 | #include 3 | 4 | int subsetSum(int *arr, int n, int sum){ 5 | int dp[n+1][sum + 1]; 6 | 7 | int i,j; 8 | 9 | dp[0][0] = 1; 10 | 11 | for(i=1;i<=sum;i++){ 12 | dp[0][i] = 0; 13 | } 14 | 15 | for(i=1;i<=n;i++){ 16 | dp[i][0] = 1; 17 | } 18 | 19 | int max = 0; 20 | 21 | for(i=1; i<=n; i++){ 22 | for(j=1;j<=sum;j++){ 23 | if(arr[i-1] <= j){ 24 | dp[i][j] = dp[i-1][j-arr[i-1]] + dp[i-1][j]; 25 | }else{ 26 | dp[i][j] = dp[i-1][j]; 27 | } 28 | if(j == sum){ 29 | if(max < dp[i][j]){ 30 | max = dp[i][j]; 31 | } 32 | } 33 | 34 | } 35 | } 36 | 37 | return max; 38 | 39 | } 40 | 41 | 42 | int main(){ 43 | int cases; 44 | scanf("%d", &cases); 45 | int i, sum; 46 | 47 | for(i=0;i 18 | 19 | void print_array(int a[], int n){ 20 | for(int i = 0; i=0 && key < arr[j]){ 33 | arr[j+1] = arr[j]; 34 | j--; 35 | } 36 | arr[j+1]=key; 37 | //j has been decremented so in the end will be at one less than the position desired, so adding one 38 | } 39 | 40 | printf("the sorted array is:\n"); 41 | print_array(arr,10); 42 | } 43 | 44 | -------------------------------------------------------------------------------- /strings/question8.c: -------------------------------------------------------------------------------- 1 | /* 2 | Run length encoding 3 | 4 | Run length encoding means, traversing through the given character array and displaying which character 5 | is repeating how many times along with the character as output. 6 | Eg: SSMMMAAARRT => S2M3A3R2T1 7 | 8 | Applications: File compression 9 | 10 | METHOD: 11 | Just traverse and maintain a counter to solve the algo 12 | Time complexity: O(n) 13 | Space complexity: O(1) 14 | */ 15 | #include 16 | #include 17 | #include 18 | 19 | void runLengthEncoding(char *arr, int size){ 20 | int i,j=0; 21 | // char *finalStr = (char *)malloc(sizeof(char)*(size*2 + 1)); 22 | int counter = 1; 23 | printf("%s\n", arr); 24 | for(i=0;i 9 | #include 10 | 11 | void print_array(int a[], int n){ 12 | for(int i = 0; i 10 | #include 11 | #include 12 | 13 | int minCoins(int *arr, int n, int amount){ 14 | 15 | int dp[amount+1]; 16 | dp[0] = 0; 17 | int i,j; 18 | for(i=1;i<=amount;i++){ 19 | dp[i] = 2000; 20 | } 21 | 22 | for(j=0;j= arr[j]){ 25 | if(dp[i-arr[j]] + 1 < dp[i]){ 26 | dp[i] = dp[i-arr[j]] + 1; 27 | } 28 | } 29 | } 30 | } 31 | 32 | 33 | if(dp[amount] == 2000){ 34 | return -1; 35 | } 36 | 37 | return dp[amount]; 38 | } 39 | 40 | int main(){ 41 | int cases; 42 | scanf("%d", &cases); 43 | int i; 44 | for(i=0;i 6 | #include 7 | 8 | int maxOnes(int *arr, int size){ 9 | int i,j; 10 | 11 | int ref[size]; 12 | int totalOnes = 0; 13 | 14 | if(arr[0] == 1){ 15 | ref[0] = 0; 16 | totalOnes++; 17 | }else{ 18 | ref[0] = 1; 19 | } 20 | 21 | int max_so_far = 0; 22 | 23 | for(i=1;i 6 | #include 7 | #include 8 | 9 | int findMin(int a, int b) { 10 | return a < b ? a : b; 11 | } 12 | 13 | int numEdges(int n) { 14 | 15 | int i,j; 16 | 17 | int dp[n+1][n+1]; 18 | 19 | for(i=0;i<=n;i++){ 20 | dp[0][i] = 0; 21 | dp[i][0] = 0; 22 | } 23 | 24 | for(i=1;i<=n;i++){ 25 | for(j=1;j<=n;j++) { 26 | if(j-i-1 >= 0 && j-3*i >= 0){ 27 | dp[i][j] = 1 + findMin(dp[i][j-i-1],dp[i][j-3*i]); 28 | }else if(j-3*i < 0 && j-i-1 >=0){ 29 | dp[i][j] = 1 + dp[i][j-i-1]; 30 | }else{ 31 | dp[i][j] = 200; 32 | } 33 | } 34 | } 35 | 36 | for(i=0;i<=n;i++){ 37 | for(j=0;j<=n;j++){ 38 | printf("%d\t", dp[i][j]); 39 | } 40 | printf("\n"); 41 | } 42 | 43 | return dp[1][n]; 44 | 45 | } 46 | 47 | int main() { 48 | int cases; 49 | int i; 50 | scanf("%d",&cases); 51 | for(i=0;i 15 | #include 16 | #include 17 | 18 | unsigned long long int f[100001]; 19 | 20 | void findAns(int plots){ 21 | 22 | unsigned long long int a = 2; 23 | unsigned long long int b = 3; 24 | unsigned long long int d; 25 | 26 | f[1] = 2; 27 | f[2] = 3; 28 | 29 | int i; 30 | for(i=3; i<=100001 ;i++){ 31 | d = a + b; 32 | f[i] = d; 33 | a = b%1000000007; 34 | b = d%1000000007; 35 | } 36 | } 37 | 38 | int main(){ 39 | int cases; 40 | scanf("%d", &cases); 41 | int i; 42 | 43 | findAns(10001); 44 | 45 | for(i=0;i 17 | #include 18 | 19 | int main(){ 20 | 21 | int a[] = {9,6,5,4,0,7,3,5,11}; 22 | int length = sizeof(a)/sizeof(a[0]); 23 | int swapped,i,j; 24 | for(i=0; i a[j+1]){ 28 | int temp = a[j+1]; 29 | a[j+1] = a[j]; 30 | a[j] = temp; 31 | swapped = 1; 32 | } 33 | } 34 | if(swapped == 0){ 35 | break; 36 | } 37 | } 38 | 39 | printf("sorted array is\n"); 40 | for(int z=0; z 12 | 13 | void swap(int *aptr, int *bptr){ 14 | 15 | *aptr = *aptr + *bptr; 16 | *bptr = *aptr - *bptr; 17 | *aptr = *aptr - *bptr; 18 | 19 | } 20 | 21 | void reverse(int arr[], int b){ 22 | 23 | int i,j,size = b; 24 | j = size - 1; 25 | i = 0; 26 | while(1){ 27 | 28 | swap(&arr[i], &arr[j]); 29 | i++; 30 | j--; 31 | if(j==i || j 8 | #include 9 | #include 10 | 11 | int checkSmurfs(int rcount, int gcount, int bcount, char *str){ 12 | int n = rcount + gcount + bcount; 13 | if(rcount == n || gcount == n || bcount == n){ 14 | return n; 15 | } 16 | 17 | if(((rcount & 1) && (bcount & 1) && (gcount & 1)) || (!(rcount & 1) && !(bcount & 1) && !(gcount & 1))){ 18 | return 2; 19 | } 20 | return 1; 21 | } 22 | 23 | int main(){ 24 | 25 | int cases; 26 | scanf("%d",&cases); 27 | int i; 28 | for(i=0;i 6 | #include 7 | 8 | struct node{ 9 | int data; 10 | struct node *link; 11 | }; 12 | 13 | struct node *head; 14 | 15 | void printList(struct node *t){ 16 | if(t){ 17 | //interchanging these lines will print it in reverse order 18 | printf("%d\n", t->data); 19 | printList(t->link); 20 | } 21 | } 22 | 23 | void reverse(struct node *prev, struct node *curr){ 24 | if(curr){ 25 | reverse(curr, curr->link); 26 | curr->link = prev; 27 | }else{ 28 | head = prev; 29 | } 30 | } 31 | 32 | int main(){ 33 | 34 | head = (struct node *)malloc(sizeof(struct node)); 35 | 36 | struct node *t = head; 37 | 38 | int counter = 1; 39 | while(counter<=5){ 40 | 41 | t->data = counter*10; 42 | if(counter == 5){ 43 | t->link = NULL; 44 | }else{ 45 | t->link = (struct node *)malloc(sizeof(struct node)); 46 | } 47 | 48 | t = t->link; 49 | 50 | counter++; 51 | } 52 | t = head; 53 | printList(t); 54 | t = head; 55 | 56 | reverse(NULL,head); 57 | t = head; 58 | printList(t); 59 | } -------------------------------------------------------------------------------- /misc/question17.c: -------------------------------------------------------------------------------- 1 | /* 2 | http://practice.geeksforgeeks.org/problems/chinky-and-diamonds/0 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | int maxDiamonds(int size, int min, int *arr){ 10 | 11 | int i,j; 12 | int sum = 0, max, index; 13 | 14 | for(i=0;i 6 | #include 7 | #include 8 | #include 9 | 10 | int multiply(int num, int *arr, int size){ 11 | int i; 12 | int carry = 0; 13 | for(i=0;i=0;i--){ 41 | printf("%d", arr[i]); 42 | } 43 | printf("\n"); 44 | } 45 | 46 | int main(){ 47 | int cases,i; 48 | scanf("%d",&cases); 49 | for(i=0;i 12 | #include 13 | 14 | int main(){ 15 | 16 | int num, count = 0, prev, current, sum; 17 | 18 | printf("enter the number of terms:\n"); 19 | scanf("%d", &num); 20 | 21 | prev = 0; 22 | current = 1; 23 | 24 | while(1){ 25 | 26 | sum = current + prev; 27 | prev = current; 28 | current = sum; 29 | 30 | if(current - prev > 1){ 31 | 32 | for(int i= (prev + 1); i < current && i<= num; i++){ 33 | printf("%d\n", i); 34 | } 35 | 36 | } 37 | 38 | if(sum > num){ 39 | break; 40 | } 41 | 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /misc/question64.c: -------------------------------------------------------------------------------- 1 | /* 2 | http://practice.geeksforgeeks.org/problems/minimum-number-of-jumps/0 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | int minJumps(int *arr, int size) { 10 | 11 | int i; 12 | int ref[size]; 13 | for(i=0; i=0;i--) { 20 | 21 | int value = arr[i]; 22 | int j = i; 23 | int min = 2000; 24 | while(j < size && value) { 25 | j++; 26 | if(min > ref[j]) { 27 | min = ref[j]; 28 | } 29 | value--; 30 | } 31 | // printf("value of min is %d\n", min); 32 | ref[i] += min; 33 | // printf("ref[%d] is %d\n",i, ref[i]); 34 | } 35 | 36 | if(ref[0] >= 2000) { 37 | return -1; 38 | } 39 | 40 | return ref[0]; 41 | 42 | } 43 | 44 | int main() { 45 | 46 | int cases; 47 | int i; 48 | scanf("%d",&cases); 49 | for(i=0;i 14 | #include 15 | #include 16 | 17 | 18 | void count_sort(int a[], int b[], int range, int min, int length){ 19 | int size = range+1; 20 | int c[size]; 21 | int i,j; 22 | 23 | for(i=0;i=0;j--){ 33 | b[c[a[j]-min]-1]=a[j]; //-1 is done to match with 0 index 34 | c[a[j]-min]--; 35 | } 36 | //printing the array 37 | for(i=0; i 18 | int main(){ 19 | 20 | int i,j,k,q,rows; 21 | 22 | printf("enter the number of rows:\n"); 23 | scanf("%d", &rows); 24 | 25 | int prev[rows]; 26 | 27 | for(i=0; i< rows; i++){ 28 | 29 | int temp[rows]; 30 | 31 | temp[0] = 1; 32 | 33 | for(j=rows-i; j > 0; j--){ 34 | printf("%s"," "); 35 | } 36 | 37 | for(k = 1; k < i+1; k++){ 38 | 39 | temp[k] = prev[k] + prev[k-1]; 40 | 41 | } 42 | 43 | temp[i] = 1; 44 | 45 | for(q = 0; q< i+1; q++){ 46 | printf("%d %s", temp[q], " "); 47 | prev[q] = temp[q]; 48 | } 49 | 50 | printf("\n"); 51 | 52 | } 53 | 54 | } 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /heaps/question9.c: -------------------------------------------------------------------------------- 1 | /* 2 | Print out all integers in the form of a^3+b^3 where a and b are integers between 0 and N in sorted order 3 | 4 | METHOD1: 5 | Here all integers that lie between 0 and N will, combinations of those will be made with each other and 6 | a3+b3 will be computed for each and presented in a sorted order. Imagine it has a matrix lets say the 7 | range is between 0 to 3, then combinations are 00,01,02,03,10,11,12,13 and so on. Therefore 0^3+0^3 and so 8 | on is computed and each time lets say value is in a two dimensional array. Now the entire column of 9 | the array is picked and a min heap is made out of it. Each time min is deleted and new element from the 10 | row in serial order is inserted and minified. This way we will get all elements in sorted order 11 | Time complexity: O(n^2)logn //total elements are n^2 inserted and deleting from the min heap takes logn 12 | time. 13 | Space complexity: O(n) to store n elements in a heap. matrix wont be taken into consideration because it 14 | will not be a matrix, its just for understanding purposes. In actual implementation everything can be done 15 | on runtime 16 | 17 | */ -------------------------------------------------------------------------------- /misc/question51.c: -------------------------------------------------------------------------------- 1 | /* 2 | http://practice.geeksforgeeks.org/problems/shortest-path-from-1-to-n/0 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | struct Node { 9 | int data; 10 | struct Node *right; 11 | struct Node *left; 12 | }; 13 | 14 | struct Node *newNode(int data){ 15 | struct Node *temp = (struct Node *)malloc(sizeof(struct Node)); 16 | temp->data = data; 17 | temp->left = NULL; 18 | temp->right = NULL; 19 | return temp; 20 | } 21 | 22 | struct Node *LCA(struct Node *root, int n1, int n2){ 23 | 24 | if(!root){ 25 | return NULL; 26 | } 27 | int data = root->data; 28 | if((n1 <= data && n2 >= data) || (n1 >= data && n2 <= data)){ 29 | return root; 30 | }else if(n1 < data && n2 < data){ 31 | return LCA(root->left, n1, n2); 32 | } 33 | return LCA(root->right, n1, n2); 34 | } 35 | 36 | int main(){ 37 | struct Node *root = newNode(5); 38 | 39 | root->left = newNode(4); 40 | root->left->left = newNode(3); 41 | 42 | root->right = newNode(6); 43 | root->right->right = newNode(7); 44 | root->right->right->right = newNode(8); 45 | 46 | printf("%d ", LCA(root, 7,8)->data); 47 | 48 | return 0; 49 | } -------------------------------------------------------------------------------- /misc/question21.c: -------------------------------------------------------------------------------- 1 | /* 2 | http://practice.geeksforgeeks.org/problems/tricky-subset-problem/0 3 | */ 4 | 5 | #include 6 | 7 | int canBeFormed(int *arr, int *ref, int n, int s, int x){ 8 | ref[0] = s; 9 | int temp = s; 10 | int i; 11 | for(i=1;i=0;j--){ 20 | if(x-ref[j] > 0){ 21 | x = x - ref[j]; 22 | } 23 | for(k = 0; k<=j;k++){ 24 | if(x == ref[k]){ 25 | return 1; 26 | break; 27 | } 28 | } 29 | 30 | } 31 | return 0; 32 | 33 | } 34 | 35 | 36 | int main(){ 37 | int cases; 38 | scanf("%d",&cases); 39 | while(cases>0){ 40 | int s,n,x; 41 | scanf("%d %d %d",&s,&n,&x); 42 | int arr[100000], ref[100000]; 43 | 44 | if(canBeFormed(arr,ref,s,n,x)){ 45 | printf("yes\n"); 46 | }else{ 47 | printf("no\n"); 48 | } 49 | cases--; 50 | } 51 | } -------------------------------------------------------------------------------- /misc/question33.c: -------------------------------------------------------------------------------- 1 | /* 2 | http://practice.geeksforgeeks.org/problems/array-of-alternate-ve-and-ve-nos/0 3 | 4 | Arrays 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | void displayResult(int *arr, int size){ 11 | 12 | int arr1[50],arr2[50]; 13 | int i; 14 | for(i=0; i<50;i++){ 15 | arr1[i] = -2000; 16 | arr2[i] = -2000; 17 | } 18 | 19 | 20 | int j=0,k=0; 21 | for(i=0;i= 0){ 23 | arr1[j] = arr[i]; 24 | j++; 25 | } 26 | if(arr[i] < 0){ 27 | arr2[k] = arr[i]; 28 | k++; 29 | } 30 | 31 | } 32 | 33 | 34 | int count = 0; 35 | i=0; 36 | while(count < size){ 37 | if(arr1[i] != -2000){ 38 | printf("%d ", arr1[i]); 39 | count++; 40 | } 41 | if(arr2[i] != -2000){ 42 | printf("%d ", arr2[i]); 43 | count++; 44 | } 45 | i++; 46 | } 47 | printf("\n"); 48 | } 49 | 50 | int main(){ 51 | int cases; 52 | scanf("%d",&cases); 53 | int i; 54 | for(i=0;i 2 | #include 3 | #include 4 | 5 | int findMax(int a, int b){ 6 | return a>b?a:b; 7 | } 8 | 9 | void strrev(char *str){ 10 | int len = strlen(str); 11 | 12 | int i=0,j=len-1; 13 | while(i 22 | #include 23 | 24 | int main(){ 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /linked-lists/question2.c: -------------------------------------------------------------------------------- 1 | /* 2 | Move the last node to the begining of a linked list 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | struct node{ 9 | int data; 10 | struct node *link; 11 | 12 | }; 13 | 14 | void printList(struct node *t, struct node *head){ 15 | //printing 16 | while(t){ 17 | printf("%d --> ", t->data); 18 | t=t->link; 19 | } 20 | printf("\n"); 21 | //head points to 22 | printf("head is at: %d\n", head->data); 23 | } 24 | 25 | int main(){ 26 | 27 | struct node *head = (struct node *)malloc(sizeof(struct node)); 28 | 29 | struct node *t; 30 | t = head; 31 | 32 | int counter = 1; //creating a list dynamically 33 | while(counter <=5){ 34 | t->data = counter*10; 35 | if(counter == 5){ 36 | t->link = NULL; 37 | }else{ 38 | t->link = (struct node *)malloc(sizeof(struct node)); 39 | } 40 | t = t->link; 41 | counter++; 42 | } 43 | 44 | t = head; 45 | printList(t,head); 46 | struct node *p; 47 | 48 | while(t->link){ 49 | p = t; 50 | t=t->link; 51 | } 52 | 53 | free(t); 54 | 55 | t->link = head; 56 | p->link = NULL; 57 | head = t; 58 | 59 | printList(t,head); 60 | 61 | } -------------------------------------------------------------------------------- /misc/question26.c: -------------------------------------------------------------------------------- 1 | /* 2 | http://practice.geeksforgeeks.org/problems/swap-all-odd-and-even-bits/0 3 | 4 | Swap all odd and even bits 5 | */ 6 | 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int modify(int n){ 13 | int arr[8]; 14 | 15 | int temp = n; 16 | 17 | int rem = temp%2; 18 | int quo; 19 | 20 | if(rem == 0){ 21 | quo = temp/2; 22 | }else{ 23 | quo = (temp-1)/2; 24 | } 25 | int i = 0; 26 | arr[i] = rem; 27 | i++; 28 | 29 | while(quo > 0){ 30 | rem = quo%2; 31 | arr[i] = rem; 32 | quo = (quo%2 == 0)? quo/2: (quo-1)/2; 33 | i++; 34 | } 35 | arr[i] = quo; 36 | int j = i; 37 | for(j=i+1;i<8;i++){ 38 | arr[i] = 0; 39 | } 40 | 41 | for(k=0;k<8;k++){ 42 | int b = arr[k]; 43 | arr[k] = arr[k+1]; 44 | arr[k+1] = b; 45 | k++; 46 | } 47 | 48 | int num = 0; 49 | 50 | for(j=0;j<8;j++){ 51 | num += arr[j]*pow(2,j); 52 | } 53 | 54 | return num; 55 | 56 | } 57 | 58 | 59 | int main(){ 60 | int cases; 61 | scanf("%d",&cases); 62 | while(cases > 0){ 63 | int n; 64 | scanf("%d",&n); 65 | printf("%d\n", modify(n)); 66 | cases--; 67 | } 68 | return 0; 69 | } -------------------------------------------------------------------------------- /misc/question2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int hash[256]; 7 | 8 | int cal(char *str){ 9 | 10 | int k; 11 | for(k=0;k<256;k++){ 12 | hash[k] = 0; 13 | } 14 | 15 | int length = strlen(str); 16 | 17 | int allEqual = true; 18 | 19 | int mid = length/2; 20 | 21 | int max = 0; 22 | 23 | int i = 0; 24 | int j = mid; 25 | 26 | int count = 0; 27 | 28 | while(i 2 | #include 3 | #include 4 | 5 | int hash[256]; 6 | 7 | int checkChain(int n, char arr[n][31]){ 8 | 9 | int i,j; 10 | 11 | if(n == 1 && strlen(arr[0]) == 1){ 12 | return 1; 13 | } 14 | 15 | if(n <= 1){ 16 | return 0; 17 | } 18 | 19 | char str[n*2 + 1]; 20 | int i = 0; 21 | for(j=0; j= 3){ 41 | 42 | } 43 | } 44 | } 45 | return 1; 46 | } 47 | 48 | int main(){ 49 | int cases; 50 | scanf("%d", &cases); 51 | int i; 52 | for(i=0;i 6 | #include 7 | 8 | int max(int a, int b) { 9 | return a > b? a: b; 10 | } 11 | 12 | int maxValue(int *weights, int *values, int size, int cap){ 13 | 14 | int dp[size+1][cap+1]; 15 | 16 | int i,j; 17 | for(i=0;i<=cap;i++){ 18 | dp[0][i] = 0; 19 | } 20 | for(i=0;i<=size;i++){ 21 | dp[i][0] = 0; 22 | } 23 | 24 | for(i=1;i<=size;i++){ 25 | for(j=1;j<=cap;j++){ 26 | if(weights[i-1] <= j) { 27 | dp[i][j] = max(values[i-1] + dp[i][j-weights[i-1]], dp[i-1][j]); 28 | }else { 29 | dp[i][j] = dp[i-1][j]; 30 | } 31 | } 32 | } 33 | 34 | return dp[size][cap]; 35 | } 36 | 37 | int main(){ 38 | int cases; 39 | int i; 40 | scanf("%d", &cases); 41 | for(i=0;i 2 | #include 3 | #include 4 | #define MAX 1000 5 | 6 | int hash1[256], hash2[256]; 7 | 8 | 9 | int cal(char *str1, char *str2){ 10 | 11 | int j; 12 | 13 | for(j=0;j<256;j++){ 14 | hash1[j] = 0; 15 | hash2[j] = 0; 16 | } 17 | 18 | int len1 =strlen(str1); 19 | int len2 = strlen(str2); 20 | 21 | if(len1 != len2){ 22 | return 0; 23 | } 24 | int i; 25 | for(i=0;i 6 | #include 7 | #define MAX 3000 8 | 9 | int min(int a, int b) { 10 | return a < b? a: b; 11 | } 12 | 13 | int minHours(int hours) { 14 | 15 | int arr[] = {10,12}; 16 | int size = sizeof(arr)/sizeof(arr[0]); 17 | int dp[size+1][hours+1]; 18 | 19 | int i,j; 20 | 21 | for(i=0;i<=size;i++) { 22 | for(j=0;j<=hours; j++) { 23 | dp[i][j] = MAX; 24 | } 25 | } 26 | 27 | for(i=1;i<=hours;i++){ 28 | dp[0][i] = MAX; 29 | } 30 | 31 | for(i=0;i<=size;i++) { 32 | dp[i][0] = 0; 33 | } 34 | 35 | for(i=1;i<=size;i++){ 36 | for(j=1;j<=hours;j++) { 37 | if(arr[i-1] <= j) { 38 | dp[i][j] = min(1 + dp[i][j-arr[i-1]], dp[i-1][j]); 39 | }else { 40 | dp[i][j] = dp[i-1][j]; 41 | } 42 | } 43 | } 44 | 45 | if(dp[size][hours] == MAX) { 46 | return -1; 47 | } 48 | 49 | return dp[size][hours]; 50 | 51 | } 52 | 53 | int main(){ 54 | int cases; 55 | int i; 56 | scanf("%d", &cases); 57 | for(i=0;i 7 | #include 8 | 9 | void floydWarshall(int v, int graph[v][v]) { 10 | 11 | int i,j,k; 12 | 13 | int result[v][v]; 14 | 15 | for(i=0;i 11 | #include 12 | 13 | int main(){ 14 | 15 | char str1[10], str2[10], counter = 0; 16 | 17 | printf("enter two strings\n"); 18 | scanf("%s %s",str1, str2); 19 | 20 | int a[26]={0}; 21 | 22 | int str2_len = strlen(str2); 23 | int str1_len = strlen(str1); 24 | for(int i=0;i 3 | #include 4 | #include 5 | 6 | int findMin(int a, int b, int c){ 7 | return (a < b)? ((a 8 | #include 9 | void remove_non_chars(char *str,int n){ 10 | char *new_str=(char*)malloc(n*sizeof(char)); 11 | int k=0; 12 | for(int i=0;i= 'a' && str[i] <='z')||(str[i] >= 'A' && str[i] <='Z') || (str[i] >= '0' && str[i] <='9')){ 14 | new_str[k++]=str[i]; 15 | } 16 | } 17 | printf("After removal of non chars the new string is:%s\n",new_str); 18 | } 19 | int main(){ 20 | char *str; 21 | int n; 22 | printf("Enter the length of string:\n"); 23 | scanf("%d",&n); 24 | str=(char*)malloc(n*sizeof(char)); 25 | printf("Enter string\n"); 26 | scanf("%s",str); 27 | remove_non_chars(str,n); 28 | free(str); 29 | return 0; 30 | } 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /misc/question6.c: -------------------------------------------------------------------------------- 1 | /* 2 | http://practice.geeksforgeeks.org/problems/pick-values/0 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | int findMin(int start, int end, int *arr){ 9 | int min = arr[start]; 10 | int i; 11 | for(i=start + 1; i <= end; i++){ 12 | if(min > arr[i]){ 13 | min = arr[i]; 14 | } 15 | } 16 | return min; 17 | } 18 | 19 | int calculate(int *arr, int n){ 20 | int dp[n]; 21 | 22 | if(n == 1){ 23 | return arr[0]; 24 | } 25 | if(n == 2){ 26 | return findMin(0,1,arr); 27 | } 28 | if(n == 3){ 29 | return findMin(0,2,arr); 30 | } 31 | if(n == 4){ 32 | return findMin(0,3,arr); 33 | } 34 | 35 | dp[0] = arr[0]; 36 | dp[1] = arr[1]; 37 | dp[2] = arr[2]; 38 | dp[3] = arr[3]; 39 | 40 | int i; 41 | for(i=4; i 20 | #include 21 | #define MAX 100 22 | 23 | int f[MAX]; 24 | 25 | int fib(int n){ 26 | if(f[n] == -1){ 27 | if(n<2){ 28 | f[n]=n; 29 | }else{ 30 | f[n] = fib(n-1) + fib(n-2); 31 | } 32 | } 33 | return f[n]; 34 | } 35 | 36 | void initialize(int *arr, int n){ 37 | int i; 38 | for(i=0;i<=n;i++){ 39 | f[i] = -1; 40 | } 41 | } 42 | 43 | int main(){ 44 | int n; 45 | printf("enter the value of n\n"); 46 | scanf("%d",&n); 47 | 48 | initialize(f,n); 49 | 50 | int ans = fib(n); 51 | printf("%dth fibonacci number is or number of ways to fill the tile is %d\n", n,ans); 52 | } -------------------------------------------------------------------------------- /misc/question60.c: -------------------------------------------------------------------------------- 1 | /* 2 | http://practice.geeksforgeeks.org/problems/cutted-segments/0 3 | 4 | This is similar to coin sum problem just that max coins are to be found 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | int max(int a, int b){ 13 | return a > b? a:b; 14 | } 15 | 16 | int cuttedSegments(int length, int x, int y, int z) { 17 | 18 | int arr[] = {x,y,z}; 19 | int size = sizeof(arr)/sizeof(arr[0]); 20 | 21 | int dp[size+1][length+1]; 22 | int i,j; 23 | for(i=0;i<=size;i++){ 24 | dp[i][0] = 0; 25 | } 26 | 27 | for(i=0;i<=length;i++){ 28 | dp[0][i] = INT_MIN; 29 | } 30 | 31 | dp[0][0] = 0; 32 | 33 | for(i=1;i<=size;i++){ 34 | for(j=1;j<=length;j++){ 35 | if(arr[i-1] <= j){ 36 | dp[i][j] = max(1+dp[i][j-arr[i-1]], dp[i-1][j]); 37 | }else{ 38 | dp[i][j] = dp[i-1][j]; 39 | } 40 | } 41 | } 42 | 43 | return dp[size][length]; 44 | 45 | 46 | } 47 | 48 | int main(){ 49 | 50 | int cases; 51 | scanf("%d",&cases); 52 | int i; 53 | for(i=0;i 5 | #include 6 | #define vertices 5 7 | 8 | int isSafe(int index, int graph[vertices][vertices], int *path, int pos) { 9 | if(graph[path[pos-1]][index] == 0) { 10 | return 0; 11 | } 12 | int i; 13 | for(i=0;i 8 | #include 9 | 10 | unsigned long long int findMax(unsigned long long int a, unsigned long long int b){ 11 | return a>b?a:b; 12 | } 13 | 14 | unsigned long long int findProd(int lenA, int lenB, unsigned long long int *arrayA, unsigned long long int *arrayB){ 15 | 16 | int i,j; 17 | unsigned long long int dp[lenA + 1][lenB + 1]; 18 | 19 | for(i=0;i<=lenA;i++){ 20 | dp[i][0] = 0; 21 | } 22 | for(i=0;i<=lenB;i++){ 23 | dp[0][i] = 0; 24 | } 25 | 26 | for(i=1;i<=lenA;i++){ 27 | for(j=1;j<=lenB;j++){ 28 | dp[i][j] = findMax(arrayA[i-1]*arrayB[j-1] + dp[i-1][j-1], dp[i-1][j]); 29 | } 30 | } 31 | 32 | return dp[lenA][lenB]; 33 | 34 | } 35 | 36 | int main(){ 37 | 38 | int cases; 39 | scanf("%d",&cases); 40 | int i; 41 | for(i=0;i 6 | #include 7 | 8 | 9 | int top = -1; 10 | 11 | struct pair{ 12 | int value; 13 | int index; 14 | }; 15 | 16 | struct pair stack[201]; 17 | 18 | void push(int data, int index){ 19 | ++top; 20 | stack[top].value = data; 21 | stack[top].index = index; 22 | } 23 | 24 | struct pair pop(){ 25 | return stack[top--]; 26 | } 27 | 28 | void findSpan(int *arr, int size){ 29 | 30 | int j; 31 | int ref[size]; 32 | int i; 33 | for(i=0;i= 0 && arr[i] >= stack[top].value){ 43 | struct pair temp = pop(); 44 | count += ref[temp.index]; 45 | } 46 | ref[i] += count; 47 | push(arr[i],i); 48 | } 49 | 50 | for(i=0;i 12 | 13 | void MaxMin(int arr[], int size, int *max_ptr, int*min_ptr){ 14 | 15 | printf("working\n"); 16 | 17 | int j = 1;; 18 | max_ptr = arr; 19 | min_ptr = arr; 20 | 21 | printf("max %d\n", *max_ptr); 22 | printf("min %d\n", *min_ptr); 23 | 24 | for(int i=1; i arr[j]){ 33 | min_ptr = arr+j; 34 | } 35 | 36 | } 37 | 38 | printf("max number is %d\n", *max_ptr); 39 | printf("min number is %d\n", *min_ptr); 40 | 41 | } 42 | 43 | int main(){ 44 | 45 | int a[]={1,124,21,4,5,8,3,56,52,0}; 46 | int size=sizeof(a)/sizeof(a[0]); 47 | 48 | MaxMin(a, size, 0, 0); 49 | 50 | } 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /arrays/question14.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Given an array A, find two elements whose sum is closest to zero 4 | 5 | METHOD1: 6 | Sorting and then iterating with two pointers one from beginning and one from ending 7 | 8 | NOTE: 9 | Hash maps cannot be applied in this case as number can even be negative and it can get confusing even if normalized 10 | 11 | Time complexity: O(nlogn) 12 | Space complexity: O(1) 13 | */ 14 | 15 | //METHOD1 16 | #include 17 | #include 18 | #include 19 | 20 | int compar(void const *a, void const *b){ 21 | return (*(int*)a-*(int*)b); 22 | } 23 | 24 | int main(){ 25 | long unsigned int f = INFINITY; 26 | 27 | int a[] = {5,10,-8,2,3,-5}; 28 | int length = sizeof(a)/sizeof(a[0]); 29 | qsort(a, length, sizeof(int),compar); 30 | int leftIndex=0, rightIndex=length-1,minSum = a[leftIndex] + a[rightIndex], sum = f; 31 | int elm1 = a[leftIndex], elm2 = a[rightIndex]; 32 | 33 | while(leftIndex < rightIndex){ 34 | if(abs(minSum-0) > abs(sum - 0)){ 35 | minSum = sum; 36 | elm1 = a[leftIndex]; 37 | elm2 = a[rightIndex]; 38 | } 39 | if(sum > 0){ 40 | rightIndex--; 41 | }else{ 42 | leftIndex++; 43 | } 44 | sum = a[leftIndex] + a[rightIndex]; 45 | } 46 | 47 | printf("min sum is %d\n", minSum); 48 | printf("two numbers are %d and %d \n", elm1, elm2); 49 | 50 | } -------------------------------------------------------------------------------- /misc/question49.c: -------------------------------------------------------------------------------- 1 | /* 2 | http://practice.geeksforgeeks.org/problems/tree-from-postorder-and-inorder/1 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | struct node{ 9 | int data; 10 | struct node *left; 11 | struct node *right; 12 | }; 13 | 14 | struct node *newNode(int data){ 15 | struct node *temp = (struct node *)malloc(sizeof(struct node)); 16 | temp->data = data; 17 | temp->left = NULL; 18 | temp->right = NULL; 19 | } 20 | 21 | int search(int *inorder, int key, int size){ 22 | int i; 23 | for(i=0;ileft = inorder(index/2); 37 | root->right = inorder(index/2); 38 | 39 | } 40 | 41 | 42 | void findTree(int *postorder, int *inorder, int size){ 43 | 44 | struct node *root; 45 | 46 | root = constructTree(root); 47 | 48 | } 49 | 50 | int main(){ 51 | int cases; 52 | int i; 53 | scanf("%d",&cases); 54 | for(i=0;i 17 | #include 18 | 19 | int partition(int arr[],int p, int r){ 20 | int key = arr[r]; 21 | int i, j=p-1, temp; 22 | 23 | for(i=p; i 5 | #include 6 | 7 | struct node{ 8 | int data; 9 | struct node *link; 10 | }; 11 | 12 | void reverse(struct node **head){ 13 | //reversing the list 14 | struct node *curr, *prev,*nextNode; 15 | curr = *head; 16 | 17 | prev = NULL; 18 | while(curr){ 19 | nextNode = curr->link; 20 | curr->link = prev; 21 | prev = curr; 22 | curr = nextNode; 23 | 24 | } 25 | *head = prev; //assigning the address the updated value 26 | } 27 | 28 | void printList(struct node *t){ 29 | if(t){ 30 | //interchanging these lines will print it in reverse order 31 | printf("%d\n", t->data); 32 | printList(t->link); 33 | } 34 | } 35 | 36 | int main(){ 37 | 38 | struct node *head = (struct node *)malloc(sizeof(struct node)); 39 | 40 | struct node *t = head; 41 | 42 | int counter = 1; 43 | while(counter<=5){ 44 | 45 | t->data = counter*10; 46 | if(counter == 5){ 47 | t->link = NULL; 48 | }else{ 49 | t->link = (struct node *)malloc(sizeof(struct node)); 50 | } 51 | 52 | t = t->link; 53 | 54 | counter++; 55 | } 56 | t = head; 57 | printList(t); 58 | t = head; 59 | 60 | reverse(&head); //passing the address of the pointer pointing to the first address of the linked list 61 | printList(head); 62 | 63 | } -------------------------------------------------------------------------------- /misc/question12.c: -------------------------------------------------------------------------------- 1 | /* 2 | Good pairs 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | int cmpfnc(const void *a, const void *b){ 9 | return (*(int *)a - *(int *)b); 10 | } 11 | 12 | 13 | int goodPairs(int *arr, int size){ 14 | int i; 15 | 16 | int dp[size]; 17 | 18 | dp[0] = 0; 19 | 20 | for(i=size-1; i>0; i--){ 21 | if(arr[i] > arr[i-1]){ 22 | dp[i] = i; 23 | }else{ 24 | dp[i] = 0; 25 | } 26 | } 27 | 28 | // printf("......\n"); 29 | // for(i=0;i dp[i-1]){ 37 | dp[i] = i; 38 | }else{ 39 | dp[i] = dp[i-1]; 40 | } 41 | } 42 | 43 | int sum = 0; 44 | // printf("......\n"); 45 | // for(i=0;i 6 | #include 7 | #include 8 | #include 9 | 10 | int findMin(int a,int b){ 11 | return a>b?a:b; 12 | } 13 | 14 | int totalWays(int stairs, int x, int y){ 15 | int arr[100000]; 16 | int size = 0; 17 | 18 | int i = 1; 19 | 20 | int num1 = x; 21 | int num2 = y; 22 | 23 | while(num1 <= stairs || num2 <= stairs){ 24 | if(num1 <= stairs){ 25 | arr[i-1] = num1; 26 | size++; 27 | num1 = num1*x; 28 | i++; 29 | } 30 | if(num2 <= stairs){ 31 | arr[i-1] = num2; 32 | size++; 33 | num2 = num2*y; 34 | i++; 35 | } 36 | } 37 | 38 | int dp[stairs+1]; 39 | dp[0] = 0; 40 | int j; 41 | 42 | for(j=0;j= arr[j]){ 48 | if(dp[i-arr[j]] + 1 < dp[i]){ 49 | dp[i] = dp[i-arr[j]] + 1; 50 | } 51 | } 52 | } 53 | } 54 | 55 | 56 | if(dp[stairs] == 1000001){ 57 | return -1; 58 | } 59 | 60 | return dp[stairs]; 61 | } 62 | 63 | int main(){ 64 | int cases; 65 | scanf("%d",&cases); 66 | int i; 67 | for(i=0;i 10 | #include 11 | 12 | void maxHeapify(int arr[],int i,int size){ 13 | if(size < 1){ 14 | return; 15 | } 16 | int left = 2*i+1,right=2*i+2,largest, temp, heapSize = size; 17 | if(left <= heapSize-1 && arr[left] > arr[i]){ 18 | largest = left; 19 | }else{ 20 | largest = i; 21 | } 22 | if(right <= heapSize-1 && arr[right] > arr[largest]){ 23 | largest = right; 24 | } 25 | 26 | if(largest <= heapSize-1 && largest != i){ 27 | temp = arr[largest]; 28 | arr[largest] = arr[i]; 29 | arr[i]=temp; 30 | maxHeapify(arr,largest,size); 31 | } 32 | } 33 | 34 | void display(int arr[],int size){ 35 | for(int i=0;i=1;i--){ 44 | temp = arr[0]; 45 | arr[0] = arr[i]; 46 | arr[i]=temp; 47 | maxHeapify(arr,0,i); 48 | } 49 | display(arr,size); 50 | } 51 | 52 | int main(){ 53 | int heap[] = {100,20,30,10,15,7,16}; 54 | int size = sizeof(heap)/sizeof(heap[0]); 55 | 56 | heapSort(heap,size); 57 | 58 | return 0; 59 | } -------------------------------------------------------------------------------- /arrays/question29.c: -------------------------------------------------------------------------------- 1 | /* 2 | Program to implement binary search in an array 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | int findElmIterative(int *arr, int start, int end, int elm){ 9 | while(start < end){ 10 | int mid = (start+end)/2; 11 | if(arr[mid] > elm){ 12 | end = mid - 1; 13 | }else if(arr[mid] < elm){ 14 | start = mid + 1; 15 | }else{ 16 | return mid; 17 | } 18 | } 19 | return -1; 20 | } 21 | 22 | int findElmRecursive(int *arr, int start, int end, int elm){ 23 | if(start >= end){ 24 | return -1; 25 | } 26 | int mid = (start + end)/2; 27 | if(arr[mid] > elm){ 28 | return findElmRecursive(arr,start,mid-1,elm); 29 | } 30 | if(arr[mid] < elm){ 31 | return findElmRecursive(arr,mid+1,end,elm); 32 | } 33 | return mid; 34 | } 35 | 36 | int main(){ 37 | int arr[] = {2,5,8,12,16,23,38,56,72,91}; 38 | int size = sizeof(arr)/sizeof(arr[0]); 39 | int elm = 23; 40 | int step; 41 | printf("1. Do Iteration\n"); 42 | printf("2. Do Recursion\n"); 43 | scanf("%d",&step); 44 | int index; 45 | switch(step){ 46 | case 1: index = findElmIterative(arr,0, size-1, elm); 47 | break; 48 | case 2: index = findElmRecursive(arr,0,size-1,elm); 49 | break; 50 | } 51 | if(index < 0){ 52 | printf("no such element is present\n"); 53 | }else{ 54 | printf("element is present at index %d\n", index); 55 | } 56 | 57 | return 0; 58 | } -------------------------------------------------------------------------------- /strings/question4.c: -------------------------------------------------------------------------------- 1 | /* 2 | Reverse the words in a given sentence 3 | 4 | METHOD1: 5 | First reverse the given letters in the sentence fully, then in the output received, reverse only the letters 6 | in each word. A single function can be made to reverse the string which takes the starting and the ending 7 | index. A loop can be used to identify the white spaces in the string and start and end index can be passed 8 | to the function each time 9 | Time complexity:O(n) //for full string of n characters it will take O(n) time and since words are part of 10 | the sentence itself, overall time complexity will stay as O(n) 11 | Space complexity:O(1) 12 | */ 13 | #include 14 | #include 15 | #include 16 | 17 | void reverse(char *arr,int start, int end){ 18 | int j=end; 19 | int i=start; 20 | while(i <=j){ 21 | int temp = arr[i]; 22 | arr[i] = arr[j]; 23 | arr[j] = temp; 24 | i++; 25 | j--; 26 | } 27 | } 28 | 29 | void reverseWords(char *arr, int size){ 30 | reverse(arr,0,size-1); 31 | int start = 0, end; 32 | for(int i=0; i 12 | #include 13 | #include 14 | 15 | int hash[256]; 16 | 17 | void initHash(){ 18 | int i; 19 | for(i=0;i<256;i++){ 20 | hash[i] = 0; 21 | } 22 | } 23 | 24 | void outputResult(char *result){ 25 | int i, length = strlen(result); 26 | 27 | for(i=0;ib?a:b; 16 | } 17 | 18 | int cutRod(int *arr, int size){ 19 | int length = size; 20 | 21 | int dp[size+1][size+1]; 22 | 23 | int i,j; 24 | 25 | 26 | for(i=0;i<=size;i++){ 27 | for(j=0;j<=size;j++){ 28 | dp[i][j] = 0; 29 | } 30 | 31 | } 32 | 33 | for(i=1;i<=size;i++){ 34 | for(j=1;j<=size;j++){ 35 | if(i <= j) { 36 | dp[i][j] = max(dp[i-1][j], arr[i-1] + dp[i][j-i]); 37 | }else{ 38 | dp[i][j] = dp[i-1][j]; 39 | } 40 | } 41 | } 42 | 43 | for(i=0;i<=size;i++){ 44 | for(j=0;j<=size;j++){ 45 | printf("%d ", dp[i][j]); 46 | } 47 | printf("\n"); 48 | } 49 | 50 | return dp[size][size]; 51 | 52 | } 53 | 54 | int main() 55 | { 56 | int arr[] = {1,5,8,9}; 57 | int size = sizeof(arr)/sizeof(arr[0]); 58 | printf("Maximum Obtainable Value is %d\n", cutRod(arr, size)); 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /trees/question2.c: -------------------------------------------------------------------------------- 1 | /* 2 | Check whether two trees are identical or not 3 | 4 | Time complexity: O(n) //visit each node 5 | Space complexity: O(n) worst case skewed tree 6 | 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | struct node{ 13 | int data; 14 | struct node *left; 15 | struct node *right; 16 | }; 17 | 18 | struct node *newNode(int data){ 19 | struct node *temp = (struct node *)malloc(sizeof(struct node)); 20 | temp->data = data; 21 | temp->left = NULL; 22 | temp->right = NULL; 23 | return temp; 24 | } 25 | 26 | int checkIdentical(struct node *t1, struct node *t2){ 27 | if(t1 == NULL && t2 == NULL){ 28 | return 1; 29 | } 30 | if(t1->data == t2->data){ 31 | return checkIdentical(t1->left, t2->left) && checkIdentical(t1->right, t2->right); 32 | } 33 | return 0; 34 | } 35 | 36 | int main(){ 37 | struct node *t1 = NULL; 38 | t1 = newNode(10); 39 | t1->left = newNode(20); 40 | t1->right = newNode(30); 41 | t1->left->left = newNode(40); 42 | t1->right->left = newNode(50); 43 | 44 | struct node *t2 = NULL; 45 | t2 = newNode(10); 46 | t2->left = newNode(20); 47 | t2->right = newNode(30); 48 | t2->left->left = newNode(40); 49 | t2->right->left = newNode(50); 50 | 51 | int isIdentical = checkIdentical(t1,t2); 52 | 53 | if(isIdentical){ 54 | printf("both the trees are identical\n"); 55 | }else{ 56 | printf("trees are not identical\n"); 57 | } 58 | } -------------------------------------------------------------------------------- /arrays/question12.c: -------------------------------------------------------------------------------- 1 | /* 2 | Separate 0's and 1's from a given array 3 | 4 | METHOD1: 5 | Counting sort 6 | Time complexity: O(n) 7 | Space complexity: O(1) 8 | 9 | Partition algo swapping: two pointers will be used to traverse from opposite directions and 1's and 0's will be swapped 10 | Time complexity: O(n) 11 | Space complexity: O(1) 12 | 13 | */ 14 | 15 | 16 | //METHOD1 17 | #include 18 | 19 | int main(){ 20 | 21 | int a[] = {1,0,1,1,0,1,0}; 22 | int length = sizeof(a)/sizeof(a[0]); 23 | int c[2]; 24 | 25 | int i,j; 26 | 27 | for(i=0; i<2; i++){ 28 | c[i] = 0; 29 | } 30 | 31 | for(i=0; i 53 | 54 | int main(){ 55 | 56 | int a[] = {1,0,1,1,0,1,0}; 57 | int length = sizeof(a)/sizeof(a[0]); 58 | 59 | int i=0,j=length-1, temp; 60 | 61 | while(i<=j){ 62 | 63 | if(a[i] == 1){ 64 | if(a[j]==0){ 65 | temp = a[j]; 66 | a[j] = a[i]; 67 | a[i] = temp; 68 | i++; 69 | }else{ 70 | j--; 71 | } 72 | }else{ 73 | i++; 74 | } 75 | } 76 | 77 | for(int z=0; z 16 | #include 17 | 18 | int getMaxSum(int *arr, int i){ 19 | int index, sum = 0, maxSum = 0; 20 | for(index = i-1; index >=0; index--){ 21 | if(arr[i] > arr[index]){ 22 | sum = arr[i]+arr[index]; 23 | } 24 | if(sum > maxSum){ 25 | maxSum = sum; 26 | } 27 | } 28 | return maxSum; 29 | } 30 | 31 | void maxIncreaseSubSequence(int *arr, int size){ 32 | int sum; 33 | int cumulative[size]; 34 | int i, j, index; 35 | int maxSum = 0; 36 | cumulative[0] = arr[0]; 37 | for(i=1; i 21 | #include 22 | #include 23 | #define size 256 24 | 25 | void checkAnagram(char *str1, char *str2, int l1, int l2){ 26 | int hash[size] = {0}; 27 | int i; 28 | for(i=0;i 0){ 36 | printf("two strings are NOT anagrams\n"); 37 | return; 38 | } 39 | } 40 | printf("two strings are anagrams of each other\n"); 41 | } 42 | 43 | int main(){ 44 | char str1[] = "heater"; 45 | char str2[] = "reheaa"; 46 | int l1 = strlen(str1); 47 | int l2 = strlen(str2); 48 | if(l1 != l2){ 49 | printf("two strings not NOT anagrams\n"); 50 | return 0; 51 | } 52 | checkAnagram(str1,str2, l1, l2); 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /dynamic-programming/question14.c: -------------------------------------------------------------------------------- 1 | /* 2 | Find the longest decreasing sub sequence in a given array 3 | 4 | METHOD: 5 | It remains the same as that of increasing sub sequence (question13), here either we can reverse 6 | the array and apply that or reverse the loop 7 | 8 | Time complexity: O(n^2) 9 | Space complexity: O(n) 10 | */ 11 | #include 12 | #include 13 | 14 | int lonDecreasingSub(int *arr, int size){ 15 | int *sol = (int *)malloc(sizeof(int)*size); 16 | int i,j; 17 | int maxLength = 1; 18 | sol[size-1] = 1; 19 | for(i=size-2;i>=0;i--){ 20 | int max = 1; 21 | for(j=i+1;j arr[j]){ 24 | key = key + sol[j]; 25 | // printf("key value for i = %d and j= %d is %d\n",i,j,key); 26 | if(key > max){ 27 | // printf("updating max to %d\n", key); 28 | max = key; 29 | } 30 | } 31 | } 32 | // printf("sol of i = %d is now %d\n", i, max); 33 | // printf("==================================\n"); 34 | sol[i] = max; 35 | if(maxLength < sol[i]){ 36 | maxLength = sol[i]; 37 | } 38 | } 39 | return maxLength; 40 | } 41 | 42 | int main(){ 43 | int *arr, size = 8; 44 | arr = (int *)malloc(sizeof(int)*size); 45 | 46 | int i; 47 | for(i=0;i 5 | #include 6 | #define ROW 5 7 | #define COL 5 8 | 9 | int isValid(int i,int j, int visited[ROW][COL], int arr[ROW][COL]){ 10 | if(i>=0 && i=0 && j 16 | #include 17 | 18 | int countOnes(int *arr, int start, int end,int size){ 19 | if(start > end){ 20 | return -1; 21 | } 22 | int mid = (start + end)/2; 23 | if(arr[mid] && !arr[mid-1]){ 24 | return (size-mid); 25 | } 26 | if(arr[mid] && arr[mid-1]){ 27 | return countOnes(arr,start,mid-1, size); 28 | } 29 | return countOnes(arr,mid+1,end,size); 30 | } 31 | 32 | int countOnesIterative(int *arr, int start, int end, int size){ 33 | while(start <= end){ 34 | int mid = (start + end)/2; 35 | if(arr[mid] && !arr[mid-1]){ 36 | return (size-mid); 37 | } 38 | if(arr[mid] && arr[mid-1]){ 39 | start = start; 40 | end = mid-1; 41 | }else{ 42 | start = mid + 1; 43 | end = end; 44 | } 45 | } 46 | } 47 | 48 | int main(){ 49 | int arr[] = {0,0,0,0,1,1,1,1,1}; 50 | int size = sizeof(arr)/sizeof(arr[0]); 51 | int count = countOnesIterative(arr,0,size-1, size); 52 | if(count < 0){ 53 | printf("there are no 1s in the array\n"); 54 | }else{ 55 | printf("number of 1s are %d\n", count); 56 | } 57 | return 0; 58 | } -------------------------------------------------------------------------------- /dynamic-programming/question22.c: -------------------------------------------------------------------------------- 1 | /* 2 | Given n-stairs, how many number of ways a person can climb to top 3 | from bottom using 1 step or 2 steps 4 | 5 | Naive approach: 6 | Lets say the stairs are given as 1 2 3 4 5 6... n 7 | Generate all the sub sequences of the numbers from 1 to n. Now examine if each subsequence is posible 8 | or not. 9 | Eg: 1 4 5 is not possible as user can only take a max of 2 steps. 10 | but 1 2 3 4 5 is valid. 11 | Therefore it will take another O(n) time to examine such subsequences 12 | 13 | Time complexity: O(2^n)n //which is exponential 14 | 15 | METHOD: 16 | DP: 17 | Here if we start from number of ways it will take to reach nth step, it will be equal to number of ways to reach 18 | n-1 + number of way to reach n-2. 19 | which is f(n)=f(n-1) + f(n-2) 20 | Therefore is is nothing but fibonacci series 21 | 22 | Time complexity: O(n) 23 | Space complexity: O(n) 24 | 25 | */ 26 | 27 | #include 28 | #include 29 | #define MAX 100 30 | 31 | int f[MAX]; 32 | 33 | int fib(int n){ 34 | if(f[n] == -1){ 35 | if(n < 2){ 36 | f[n] = 1; 37 | }else{ 38 | f[n] = fib(n-1) + fib(n-2); 39 | } 40 | } 41 | return f[n]; 42 | } 43 | 44 | void initialize(int n){ 45 | int i; 46 | for(i=0; i<=n;i++){ 47 | f[i] = -1; 48 | } 49 | } 50 | 51 | int main(){ 52 | //lets say value of n is 20 53 | int n = 9; 54 | initialize(n); 55 | printf("number of ways to reach the %dth step is %d\n", n,fib(n)); 56 | 57 | return 0; 58 | } 59 | 60 | -------------------------------------------------------------------------------- /misc/question41.c: -------------------------------------------------------------------------------- 1 | /* 2 | http://practice.geeksforgeeks.org/problems/sorting-elements-of-an-array-by-frequency/0 3 | */ 4 | #include 5 | #include 6 | 7 | struct node{ 8 | int index; 9 | int value; 10 | }; 11 | 12 | struct hash{ 13 | int index; 14 | int freq; 15 | int value; 16 | }; 17 | 18 | int cmpfnc1(const void *a, const void *b){ 19 | const struct node *p1 = a; 20 | const struct node *p2 = b; 21 | 22 | if(p1->value < p2->value){ 23 | return -1; 24 | }else if(p1->value > p2->value){ 25 | return 1; 26 | }else{ 27 | return 0; 28 | } 29 | } 30 | 31 | void sortByFreq(int *arr, int size){ 32 | 33 | 34 | 35 | struct node ref[size]; 36 | int i; 37 | for(i=0;i 2 | #include 3 | 4 | void display(int f[2][2]){ 5 | int i,j; 6 | printf("-------------------\n"); 7 | for(i=0;i<2;i++){ 8 | for(j=0;j<2;j++){ 9 | printf("%d ", f[i][j]); 10 | } 11 | printf("\n"); 12 | } 13 | printf("----------------\n"); 14 | } 15 | 16 | void mul(int a[2][2], int b[2][2]){ 17 | int p = a[0][0]*b[0][0] + a[0][1]*b[1][0]; 18 | int q = a[0][0]*b[0][1] + a[0][1]*b[1][1]; 19 | int r = a[1][0]*b[0][0] + a[1][1]*b[1][0]; 20 | int s = a[1][0]*b[0][1] + a[1][1]*b[1][1]; 21 | 22 | a[0][0] = p; 23 | a[0][1] = q; 24 | a[1][0] = r; 25 | a[1][1] = s; 26 | } 27 | 28 | 29 | void ipow(int f[2][2], int power){ 30 | 31 | if(power <= 1){ 32 | return; 33 | } 34 | 35 | int base[2][2] = { 36 | {1,1}, 37 | {1,0} 38 | }; 39 | 40 | mul(f,f); 41 | 42 | ipow(f,power/2); 43 | 44 | if(power%2 != 0){ 45 | mul(f,base); 46 | } 47 | 48 | } 49 | 50 | void ipowIterative(int f[2][2],int power){ 51 | 52 | int base[2][2] = { 53 | {1,1}, 54 | {1,0} 55 | }; 56 | 57 | while(power > 1){ 58 | mul(f,f); 59 | if(power%2 !=0){ 60 | mul(f,base); 61 | } 62 | power = power/2; 63 | } 64 | 65 | } 66 | 67 | int fib(int n){ 68 | int f[2][2] = { 69 | {1,1}, 70 | {1,0} 71 | }; 72 | printf("calling pow with power %d\n", n-1); 73 | ipowIterative(f,n-1); 74 | 75 | return f[0][0]; 76 | } 77 | 78 | int main(){ 79 | 80 | int n = 11; 81 | 82 | printf("answer is %d\n", fib(n)); 83 | 84 | return 0; 85 | } 86 | 87 | 88 | -------------------------------------------------------------------------------- /general/question14.c: -------------------------------------------------------------------------------- 1 | /* 2 | You are given an array "A" of "n" integers. It is given that the elements of "A" satisfy the following inequalities: 3 | A[0] < A[1] < ... < A[m - 1] < A[m] > A[m + 1] > A[m + 2] > ... > A[n - 1] 4 | for some (unknown) index "m" in the range [1,n-2]. 5 | Let us call such an array a "hill-valued" array. The sequence A[0], A[1], ... , A[m - 1], A[m] 6 | is called the "ascending part" of the hill, and the remaining part A[m], A[m+1], . . ., A[n - 1] is called the 7 | "descending part" of the hill. The element A[m] is the "peak" of the hill and is the 8 | largest element in the array. Your task is to locate the peak, i.e., to determine the values of "m" and "A[m]". 9 | Write a C function "int binarysearchpeak (int array[], int first_index, int last_index)" 10 | to perform the task, by performing a (non-recursive) binary search. The initial call to the function is of the form 11 | "m = binarysearchpeak(A, 0, n-1)" where "A" is a hill-valued array holding "n" integers. The function returns the 12 | value of "m". Write a complete C program to demonstrate the working of the function. You might 13 | have a hard-coded hill-valued array of (say) 10 integers inside your "main()" body. 14 | Input: Nothing 15 | Output : the value of "m" and the value of "A[m]", printed from inside "main()". [3] 16 | */ 17 | 18 | #include 19 | 20 | void is_palindrome(int a){ 21 | 22 | 23 | 24 | } 25 | 26 | int main(){ 27 | 28 | } 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /misc/question42.c: -------------------------------------------------------------------------------- 1 | /* 2 | http://practice.geeksforgeeks.org/problems/minimum-points-to-reach-destination/0 3 | 4 | 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | int min(int a, int b){ 13 | return ab?a:b; 18 | } 19 | 20 | int findPoints(int rows, int cols, int arr[rows][cols]){ 21 | 22 | int dp[rows][cols]; 23 | 24 | int i,j; 25 | for(i=0;i 0)?1: abs(arr[rows-1][cols-1]) + 1; 32 | 33 | // printf("total is ...%d \n", total); 34 | 35 | for(i=rows-2;i>=0;i--){ 36 | dp[i][cols-1] = max(dp[i+1][cols-1] - arr[i][cols-1], 1); 37 | } 38 | 39 | for(j=cols-2;j>=0;j--){ 40 | dp[rows-1][j] = max(dp[rows-1][j+1] - arr[rows-1][j], 1); 41 | } 42 | 43 | for(i=rows-2;i>=0;i--){ 44 | for(j=cols-2;j>=0;j--){ 45 | int minpts = min(dp[i][j+1],dp[i+1][j]); 46 | dp[i][j] = max(minpts - arr[i][j],1); 47 | } 48 | } 49 | 50 | return dp[0][0]; 51 | 52 | } 53 | 54 | int main(){ 55 | int cases; 56 | scanf("%d",&cases); 57 | int i; 58 | for(i=0;i i <= n-m 13 | Therefore it can be anywhere from o to n-m, that is n-m+1 values. 14 | Therefore total time complexity will be (n-m+1)*m //substrings*comparsions for each 15 | which is nm. 16 | If length is equal to n/2 or higher 17 | worst case time complexity will be O(n^2) 18 | 19 | Space complexity: O(1) 20 | 21 | 22 | METHOD2: 23 | */ 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | void search(char *text, char *pattern){ 30 | int len1 = strlen(text); 31 | int len2 = strlen(pattern); 32 | 33 | int i,j; 34 | 35 | for(i=0;i<=len1-len2;i++){ 36 | for(j=0;j 6 | #include 7 | #define MAX 100 8 | 9 | struct adjListNode{ 10 | int data; 11 | struct adjListNode *next; 12 | }; 13 | 14 | struct adjList{ 15 | struct adjListNode *head; 16 | }; 17 | 18 | struct graph{ 19 | int vertices; 20 | struct adjList *arr; 21 | }; 22 | 23 | struct adjListNode *newNode(int data){ 24 | struct adjListNode *temp = (struct adjListNode *)malloc(sizeof(struct adjListNode)); 25 | temp->data = data; 26 | temp->next = NULL; 27 | return temp; 28 | } 29 | 30 | void createEdge(int start, int end, struct graph *newGraph){ 31 | struct adjListNode *temp = newNode(end); 32 | if(newGraph->arr[start].head){ 33 | temp->next = newGraph->arr[start].head; 34 | newGraph->arr[start].head = temp; 35 | }else{ 36 | newGraph->arr[start].head = temp; 37 | } 38 | } 39 | 40 | void dfs(int index, struct graph *newGraph, int *visited){ 41 | 42 | visited[index] = 1; 43 | 44 | struct adjListNode *temp = newGraph->arr[index].head; 45 | while(temp){ 46 | if(visited[temp->data] == 0){ 47 | dfs(temp->data, newGraph, visited); 48 | } 49 | temp = temp->next; 50 | } 51 | } 52 | 53 | void init(int *visited, int vertices){ 54 | int i; 55 | for(i=0;i 11 | #include 12 | #define MAX 100 13 | 14 | void push(int arr[], int *front, int *rear, int elm){ 15 | *rear = (*rear+1)%MAX; //modulus after incrementing is imp. 16 | if(*rear == *front){ 17 | printf("Overflow\n"); 18 | if(*rear == 0){ 19 | *rear = MAX - 1; 20 | }else{ 21 | *rear = *rear-1; 22 | } 23 | return; 24 | } 25 | arr[*rear] = elm; 26 | return; 27 | } 28 | 29 | int pop(int arr[], int *front, int *rear){ 30 | if(*front == *rear){ //this is where we started from when both were pointing to zero 31 | return -1; 32 | } 33 | *front = (*front + 1)%MAX; //modulus after incrementing is imp. 34 | int data = arr[*front]; 35 | return data; 36 | } 37 | 38 | int main(){ 39 | int arr[MAX]; 40 | int front,rear = 0, step, elm; 41 | 42 | while(1){ 43 | printf("1. PUSH element\n"); 44 | printf("2. POP element\n"); 45 | printf("3.EXIT \n"); 46 | scanf("%d",&step); 47 | 48 | switch(step){ 49 | case 1: printf("Enter the number to be pushed\n"); 50 | scanf("%d",&elm); 51 | push(arr, &front, &rear, elm); 52 | break; 53 | case 2: elm = pop(arr,&front,&rear); 54 | if(elm == -1){ 55 | printf("Already empty\n"); 56 | }else{ 57 | printf("%d was popped\n", elm); 58 | } 59 | 60 | break; 61 | case 3: exit(1); 62 | break; 63 | } 64 | } 65 | 66 | return 0; 67 | } -------------------------------------------------------------------------------- /graphs/question15.c: -------------------------------------------------------------------------------- 1 | /* 2 | Print all jumping numbers smaller than or equal to a given value 3 | 4 | Number is jumping if the diff between the digits of a number is 1. 5 | 90 is not a jumping number 6 | */ 7 | 8 | #include 9 | #include 10 | #define MAX 150000 11 | 12 | int queue[MAX]; 13 | int start = 0, rear= 0; 14 | 15 | int elms = 0; 16 | 17 | void enqueue(int data){ 18 | queue[++rear] = data; 19 | elms++; 20 | } 21 | 22 | int dequeue(){ 23 | elms--; 24 | // if(start + 1 == MAX){ 25 | // start = 0, rear = 0; 26 | // } 27 | return queue[start++]; 28 | } 29 | 30 | int isQueueEmpty(){ 31 | return elms == 0; 32 | } 33 | 34 | void bfs(int limit, int index){ 35 | 36 | enqueue(index); 37 | 38 | while(!isQueueEmpty()){ 39 | int data = dequeue(); 40 | 41 | if(data <= limit){ 42 | 43 | printf("%d ", data); 44 | 45 | int last_digit = data%10; 46 | if(last_digit == 0){ 47 | enqueue((data*10) + (last_digit + 1)); 48 | } 49 | else if(last_digit == 9){ 50 | enqueue((data*10) + (last_digit - 1)); 51 | }else{ 52 | enqueue((data*10) + (last_digit - 1)); 53 | enqueue((data*10) + (last_digit + 1)); 54 | } 55 | } 56 | 57 | } 58 | 59 | } 60 | 61 | void displayJumping(int limit){ 62 | int i; 63 | printf("%d ",0); 64 | for(i=1;i<=9 && i<=limit;i++){ 65 | bfs(limit, i); 66 | } 67 | 68 | } 69 | 70 | int main(){ 71 | int cases; 72 | scanf("%d",&cases); 73 | int i; 74 | for(i=0;i 8 | #include 9 | #include 10 | 11 | void makeRef(int *ref, char *pattern, int length){ 12 | int i=1,len=0; 13 | ref[0] = 0; 14 | 15 | while(i < length) { 16 | if(pattern[i] == pattern[len]){ 17 | len++; 18 | ref[i] = len; 19 | i++; 20 | }else{ 21 | if(len == 0){ 22 | i++; 23 | }else{ 24 | len = ref[len-1]; 25 | } 26 | } 27 | } 28 | 29 | } 30 | 31 | void searchString(char *text, char *pattern){ 32 | int tlen = strlen(text); 33 | int plen = strlen(pattern); 34 | 35 | int ref[plen]; 36 | 37 | int i; 38 | for(i=0;i 21 | #include 22 | 23 | int main(){ 24 | 25 | int index,voter, votes=0, counter=0; 26 | int a[] = {2,2,5,6,2,2}; 27 | int length = sizeof(a)/sizeof(a[0]); 28 | //checking voter and votes 29 | for(index = 0; index= 1){ 43 | 44 | for(index=0; index length/2){ 51 | printf("the element that occurs more than n/2 times is %d\n", voter); 52 | }else{ 53 | printf("no element found\n"); 54 | } 55 | 56 | } 57 | 58 | } -------------------------------------------------------------------------------- /misc/question52.c: -------------------------------------------------------------------------------- 1 | /* 2 | LCA for binary tree 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | struct node { 9 | int data; 10 | struct node *right; 11 | struct node *left; 12 | }; 13 | 14 | struct node *newNode(int data){ 15 | struct node *temp = (struct node *)malloc(sizeof(struct node)); 16 | temp->data = data; 17 | temp->left = NULL; 18 | temp->right = NULL; 19 | return temp; 20 | } 21 | 22 | int LCA(struct node *root, int n1, int n2){ 23 | if(!root){ 24 | return -1; 25 | } 26 | int data = root->data; 27 | if(data == n1){ 28 | return n1; 29 | } 30 | if(data == n2){ 31 | return n2; 32 | } 33 | int left = LCA(root->left, n1, n2); 34 | int right = LCA(root->right, n1, n2); 35 | if(left == n1 && right == n2){ 36 | return root->data; 37 | } 38 | return (left > 0)?left: right; 39 | } 40 | 41 | int main(){ 42 | 43 | struct node *root = newNode(1); 44 | root->left = newNode(2); 45 | root->right = newNode(3); 46 | root->right->left = newNode(7); 47 | root->right->right = newNode(8); 48 | root->right->right->left = newNode(13); 49 | root->right->right->right = newNode(14); 50 | root->left->left = newNode(4); 51 | root->left->right = newNode(5); 52 | root->left->left->left = newNode(9); 53 | root->left->left->right = newNode(10); 54 | root->left->right->left = newNode(11); 55 | root->left->right->right = newNode(12); 56 | 57 | int res = LCA(root,8,13); 58 | 59 | if(res > 0){ 60 | printf("LCA is....%d \n", res); 61 | }else{ 62 | printf("not present\n"); 63 | } 64 | 65 | 66 | 67 | return 0; 68 | } -------------------------------------------------------------------------------- /divide-and-conquer/question10.c: -------------------------------------------------------------------------------- 1 | /* 2 | Find the maximum element index in an array which is first increasing and then decreasing 3 | 4 | It means that there is an increasing sequence till i and then after that i+1 to n is a decreasing 5 | sequence. We need to find the value of i 6 | 7 | METHOD1: 8 | Linear search 9 | Time complexity: O(n) 10 | Space complexity: O(1) 11 | 12 | METHOD2: 13 | Binary search. 14 | Here we find middle and if the element at the middle lies in the increasing sequence 15 | search the right array else search the left array. Also for successful condition we see the the middle 16 | element has its immediate right and left lesser than it, then that is the required number. 17 | Time complexity: O(logn) //because T(n/2) + O(1) 18 | Space complexity: O(1) or O(logn) //iterative or recursive 19 | */ 20 | #include 21 | #include 22 | 23 | int findIndex(int *arr,int start, int end){ 24 | if(start > end){ 25 | return -1; 26 | } 27 | int mid = (start+end)/2; 28 | if(arr[mid] > arr[mid-1] && arr[mid] > arr[mid+1]){ 29 | return mid; 30 | } 31 | if(arr[mid] < arr[mid+1] && arr[mid] > arr[mid-1]){ 32 | return findIndex(arr,mid+1,end); //increasing sequence search the right one 33 | } 34 | return findIndex(arr,start,mid-1); 35 | } 36 | 37 | int main(){ 38 | int arr[] = {1,2,3,4,5,6,5,4,3,2,1}; 39 | int size = sizeof(arr)/sizeof(arr[0]); 40 | int index = findIndex(arr,0,size-1); 41 | if(index < 0){ 42 | printf("no such element exists\n"); 43 | }else{ 44 | printf("index with peak is %d\n", index); 45 | } 46 | return 0; 47 | } -------------------------------------------------------------------------------- /trees/question17.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Print left view of a binary tree 4 | 5 | METHOD: 6 | Left view means whatever is visible from the left. Therefore height is the main parameter here. We can check 7 | if we have visited that much height before or not in pre order traversal by keep variable max height and 8 | current height. If max height is lesser than current height, we print the node else we dont. 9 | 10 | Time complexity: O(n) //skewed tree 11 | Space complexity: O(n) //skewed tree recursion 12 | */ 13 | 14 | #include 15 | #include 16 | 17 | struct node{ 18 | int data; 19 | struct node *left; 20 | struct node *right; 21 | }; 22 | 23 | struct node *newNode(int data){ 24 | struct node *temp = (struct node *)malloc(sizeof(struct node)); 25 | temp->data = data; 26 | temp->left = temp->right= NULL; 27 | return temp; 28 | } 29 | 30 | void printLeft(struct node *root, int height, int *max){ 31 | if(!root){ 32 | return; 33 | } 34 | if(*max < height){ 35 | printf("%d\n", root->data); 36 | *max = height; 37 | } 38 | printLeft(root->left,height+1,max); 39 | printLeft(root->right,height+1,max); 40 | } 41 | 42 | int main(){ 43 | int height = 0, max_height = -1; 44 | struct node *root = newNode(10); 45 | root->left = newNode(12); 46 | root->left->left = newNode(14); 47 | root->left->right = newNode(16); 48 | root->right = newNode(20); 49 | root->right->left = newNode(22); 50 | root->right->left->left = newNode(122); 51 | root->right->left->left->left = newNode(132); 52 | root->right->right = newNode(26); 53 | 54 | printLeft(root, height, &max_height); 55 | 56 | return 0; 57 | } -------------------------------------------------------------------------------- /general/question24.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3. The median of a finite list of numbers can be found by arranging all the numbers from the lowest value to 3 | the highest value, and then picking the middle one (e.g., the median of {3, 3, 5, 9, 11} is 5). Your task is to take 4 | "N" real values, probably containing repitions, through command-line, and find the median of those numbers. 5 | Remember that all commnad line arguments are of "char *" type, hence you need to use the "atof()" function. 6 | INPUT: Example: ./a.out 2.355 4.5 3.6 2.12 3.6 6.4 2.355 7 | OUTPUT: 3.6 (median of the given numbers) 8 | */ 9 | #include 10 | #include 11 | #include 12 | 13 | void insertionSort(int arr[], int n){ 14 | int i, key, j; 15 | for (i = 1; i < n; i++){ 16 | key = arr[i]; 17 | j = i-1; 18 | while (j >= 0 && arr[j] > key){ 19 | arr[j+1] = arr[j]; 20 | j = j-1; 21 | } 22 | arr[j+1] = key; 23 | } 24 | } 25 | 26 | void print_array(int array[],int n){ 27 | for (int i = 0; i < n; ++i){ 28 | printf("%d\t",array[i] ); 29 | } 30 | printf("\n"); 31 | } 32 | 33 | int main(int argc, char const *argv[]){ 34 | 35 | int n = argc-1; 36 | int array[argc]; 37 | for(int i=1;i 6 | #include 7 | 8 | struct node{ 9 | int data; 10 | struct node *next; 11 | }; 12 | 13 | struct node *newNode(int data){ 14 | struct node *temp = (struct node *)malloc(sizeof(struct node)); 15 | temp->data = data; 16 | temp->next= NULL; 17 | return temp; 18 | } 19 | 20 | int chooseNum(int n, int k) { 21 | struct node *temp = newNode(1); 22 | struct node *head = temp; 23 | int i; 24 | for(i=1;inext = newNode(i+1); 26 | temp = temp->next; 27 | } 28 | 29 | temp->next = head; //making it circular 30 | 31 | 32 | int remainingElem = n; 33 | struct node *p = head; 34 | struct node *prev = temp; 35 | // printf("here is prev %d\n", prev->data); 36 | 37 | struct node *help; 38 | int counter = 1; 39 | 40 | if(counter == k){ 41 | help = p; 42 | p = p->next; 43 | prev->next = p; 44 | free(help); 45 | counter = 1; 46 | remainingElem--; 47 | } 48 | 49 | 50 | 51 | while(remainingElem > 1) { 52 | prev = p; 53 | 54 | p = p->next; 55 | 56 | counter++; 57 | if(counter == k) { 58 | help = p; 59 | p = p->next; 60 | prev->next = p; 61 | free(help); 62 | counter = 1; 63 | remainingElem--; 64 | } 65 | 66 | } 67 | 68 | // printf("\nreturing %d\n", prev->data); 69 | return prev->data; 70 | } 71 | 72 | int main() { 73 | int cases; 74 | scanf("%d", &cases); 75 | int i; 76 | for(i=0;i 12 | #include 13 | #include 14 | int main(){ 15 | 16 | char *s[5], *t; 17 | printf("enter any five strings: \n"); 18 | for(int i=0; i<5;i++){ 19 | int len; 20 | printf("Enter the number of char's in %dth string\n",i); 21 | scanf("%d",&len); 22 | s[i] = (char *)malloc(len*sizeof(char)); 23 | scanf("%s",s[i]); 24 | } 25 | 26 | for(int i=1;i<5;i++){ 27 | for(int j=1;j<5;j++){ 28 | if(strcmp(s[j-1],s[j]) > 0){ 29 | t = (char *)malloc(strlen(s[j-1])*sizeof(char)); 30 | strcpy(t,s[j-1]); 31 | strcpy(s[j-1],s[j]); 32 | strcpy(s[j],t); 33 | free(t); 34 | } 35 | } 36 | } 37 | 38 | printf("Strings in order are : \n"); 39 | 40 | for(int i=0;i<5;i++){ 41 | printf("%s\n", s[i]); 42 | } 43 | for(int j=0;j<5;j++){ 44 | free(s[j]); 45 | } 46 | return 0; 47 | 48 | } 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /general/question12.c: -------------------------------------------------------------------------------- 1 | /* 2 | 2. Write a C Program to find the Greatest Common Divisor (GCD) of two positive integers, 3 | using a function "int GCD (int a, int b)" which returns the GCD of two 4 | positive integers "a" and "b" passed as arguments. Note that the values of "a" and "b" might be 5 | arbitrary, and proper arrangements should be made to ensure that the 6 | function works for both the cases: "a >= b" and "a < b". 7 | Input : two positive integers in the "main()" portion of the program. 8 | Output: the GCD, as returned by the "GCD()" function. [2] 9 | */ 10 | #include 11 | 12 | int GCD(int a, int b){ 13 | 14 | int min, value; 15 | min = (a < b)?a:b; 16 | 17 | if(a%min == 0 && b%min == 0){ 18 | value = min; 19 | }else{ 20 | for(int i= (min-1); i>1;i--){ 21 | if(a%i ==0 && b%i ==0){ 22 | value = i; 23 | break; 24 | } 25 | } 26 | } 27 | return value; 28 | } 29 | 30 | int main(){ 31 | 32 | int num1, num2, ans; 33 | 34 | printf("Enter two positive integers:\n"); 35 | scanf("%i %i", &num1, &num2); 36 | 37 | ans = GCD(num1, num2); 38 | 39 | printf("GCD for %d and %d is %d\n",num1, num2, ans); 40 | 41 | } 42 | 43 | //Alternate way 44 | // #include 45 | // int main(){ 46 | // int n1, n2; 47 | // printf("Enter two positive integers: "); 48 | // scanf("%d %d",&n1,&n2); 49 | // while(n1!=n2){ 50 | // if(n1 > n2) 51 | // n1 -= n2; 52 | // else 53 | // n2 -= n1; 54 | // } 55 | // printf("GCD = %d",n1); 56 | // return 0; 57 | // } 58 | 59 | 60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /heaps/question8.c: -------------------------------------------------------------------------------- 1 | /* 2 | Given k-sorted lists, Find the minimum range to which at least one number belongs from every list 3 | 4 | METHOD1: 5 | Naive approach: 6 | find all the intervals first that will be nc2 if total elements from all the k lists are n and see which 7 | one is the min and fits the criteria 8 | Time complexity: O(n^2) 9 | Space complexity: O(1) 10 | 11 | METHOD2: 12 | Start from all the lower elements or all the higher elements. Lets say lower. Choose the interval by picking 13 | the least amongst the lower elements and highest amongst the lower elements. Now keep squeezing the interval 14 | by shifting the lower bound. If at any point shifting the lower bound impacts losing a list completely, include 15 | the next higher element from that list. Including that may increase the upper bound as well sometimes. 16 | Repeat the steps for all the n elements. Keep track of the min interval during these steps. 17 | Time complexity: O(kn) //for n times we will have to find the min and max from the k elements chosen by 18 | shifting the bound 19 | Space complexity: O(k) //storing the k elements 20 | */ 21 | 22 | //METHOD2 23 | #include 24 | #include 25 | 26 | void findInterval(int arr[], int k, int size){ 27 | 28 | 29 | 30 | } 31 | 32 | int main(){ 33 | int k, size, arr[], elm; 34 | printf("enter the value if k\n"); 35 | printf("enter the size of each array\n"); 36 | scanf("%d",size); 37 | scanf("%d",&k); 38 | for(int i=0;i 15 | #include 16 | 17 | struct hash{ 18 | int count; 19 | struct node *head; 20 | }; 21 | 22 | struct node{ 23 | int value; 24 | struct node *next; 25 | }; 26 | 27 | int searchInHash(struct hash *hashTable, int key, int size){ 28 | int hashIndex = key%size; 29 | if(!hashTable[hashIndex].head){ 30 | return 0; 31 | } 32 | 33 | struct node *t = hashTable[hashIndex].head; 34 | while(t){ 35 | if(t->value==key){ 36 | return 1; 37 | } 38 | t=t->next; 39 | } 40 | 41 | return 0; 42 | } 43 | 44 | void insertNewElement(int key,struct node *hashTable, int oldKey, int size){ 45 | 46 | int hashIndex = key%size; 47 | //search and then delete 48 | //and then insert new one 49 | 50 | } 51 | 52 | int findDuplicates(int arr[],int size, int k){ 53 | struct hash *hashTable = (struct hash *)calloc(k,sizeof(struct hash)); 54 | for(int i=2; i 7 | #include 8 | 9 | int findDifferenceinBits(int a, int b){ 10 | 11 | // printf("finding bits for %d and %d\n", a,b); 12 | 13 | int arr1[4], arr2[4]; 14 | 15 | int k; 16 | for(k=0;k<4;k++){ 17 | arr1[k] = 0; 18 | arr2[k] = 0; 19 | } 20 | 21 | int rem, i = 0; 22 | // printf("for %d it is....\n",a); 23 | while(a > 1){ 24 | rem = a%2; 25 | arr1[i] = rem; 26 | a = a/2; 27 | i++; 28 | } 29 | arr1[i] = a; 30 | 31 | 32 | // int l; 33 | // for(l=0;l<4;l++){ 34 | // printf("%d ", arr1[l]); 35 | // } 36 | // printf("\n"); 37 | 38 | i = 0; 39 | // printf("for %d it is....\n",b); 40 | while(b > 1){ 41 | rem = b%2; 42 | arr2[i] = rem; 43 | b = b/2; 44 | i++; 45 | } 46 | arr2[i] = b; 47 | 48 | 49 | 50 | 51 | // for(l=0;l<4;l++){ 52 | // printf("%d ", arr2[l]); 53 | // } 54 | // printf("\n"); 55 | 56 | 57 | int sum = 0; 58 | int j = 3; 59 | for(i=3;i>=0;i--){ 60 | 61 | sum += abs(arr1[j] - arr2[i]); 62 | j--; 63 | } 64 | return sum; 65 | } 66 | 67 | int sumIs(int *arr, int size){ 68 | 69 | int sum = 0; 70 | 71 | int i,j; 72 | for(i=0;i 19 | #include 20 | #include 21 | 22 | void maxHeapify(int arr[],int i, int size){ 23 | int left = 2*i+1, right = 2*i+2; 24 | int heapSize = size, largest, temp; 25 | 26 | if(left <= heapSize-1 && arr[i] > arr[left]){ 27 | largest = i; 28 | }else{ 29 | largest = left; 30 | } 31 | if(right <= heapSize-1){ 32 | if(arr[largest] < arr[right]){ 33 | largest = right; 34 | } 35 | } 36 | if(largest <= heapSize && largest != i){ 37 | temp = arr[largest]; 38 | arr[largest] = arr[i]; 39 | arr[i] = temp; 40 | maxHeapify(arr,largest,size); 41 | } 42 | } 43 | 44 | void display(int arr[],int size){ 45 | for(int i=0; i=0;i--){ 54 | maxHeapify(arr,i,size); 55 | } 56 | } 57 | 58 | int main(){ 59 | int arr[] = {9,6,5,0,8,2,1,3}; 60 | int size = sizeof(arr)/sizeof(arr[0]); 61 | 62 | makeHeap(arr,size); 63 | display(arr,size); 64 | return 0; 65 | } -------------------------------------------------------------------------------- /trees/question14.c: -------------------------------------------------------------------------------- 1 | /* 2 | Check whether the given binary tree is sum tree or not 3 | 4 | Sum tree is the one where sum of values in the LST and sum of values in the RST is equal to the root. 5 | This is valid for all the nodes except the tree 6 | 7 | METHOD: 8 | We do post order traversal of the tree where first we compute LST, then RST then we compare if root value 9 | is equal to LST value and RST value sum else we return -1. If equal we return sum of RST, LST and root->data 10 | 11 | Time complexity: O(n) 12 | Space complexity: O(n) 13 | 14 | */ 15 | #include 16 | #include 17 | 18 | struct node{ 19 | int data; 20 | struct node *left; 21 | struct node *right; 22 | }; 23 | 24 | struct node *newNode(int data){ 25 | struct node *temp = (struct node *)malloc(sizeof(struct node)); 26 | temp->data = data; 27 | temp->left = temp->right= NULL; 28 | return temp; 29 | } 30 | 31 | int checkSum(struct node *root){ 32 | if(!root){ 33 | return 0; 34 | } 35 | if(!root->left && !root->right){ 36 | return root->data; 37 | } 38 | int left = checkSum(root->left); 39 | int right = checkSum(root->right); 40 | 41 | if(root->data == left + right){ 42 | return 2*root->data; 43 | } 44 | return -1; 45 | } 46 | 47 | int main(){ 48 | 49 | struct node *root = newNode(50); 50 | root->left = newNode(15); 51 | root->left->left = newNode(10); 52 | root->left->right = newNode(5); 53 | root->right = newNode(10); 54 | root->right->left = newNode(7); 55 | root->right->right = newNode(3); 56 | 57 | int isSumTree = checkSum(root); 58 | 59 | if(isSumTree > 0){ 60 | printf("given tree is a sum tree\n"); 61 | }else{ 62 | printf("given tree is not a sum tree\n"); 63 | } 64 | 65 | return 0; 66 | } -------------------------------------------------------------------------------- /divide-and-conquer/question3.c: -------------------------------------------------------------------------------- 1 | /* 2 | Custom C function to implement a pow function 3 | */ 4 | 5 | /* 6 | The function is buggy because it can overflow, cannot work for negative value of power. Also it cannot work 7 | for decimals. It is also slow because it is doing a particular multiplications so many times. 8 | It also does not handle fractional power 9 | */ 10 | 11 | // #include 12 | // #include 13 | 14 | // int customPow(int base, int power){ 15 | // int i; 16 | // int temp = 1; 17 | // for(i=0;i 33 | #include 34 | 35 | //iterative 36 | int ipow(int base, int exp){ 37 | int result = 1; 38 | while(exp){ 39 | if(exp & 1){ //will return 0 if number is even and will return 1 if number if odd 40 | //therefore odd value is taken out of the bracket and remaning operations are done on 41 | //even value 42 | result *= base; 43 | } 44 | exp >>= 1; //used to convert exp value to half 45 | base *= base; 46 | } 47 | return result; 48 | } 49 | 50 | //recursive 51 | int ipowRecursive(int base, int exp){ 52 | if(exp == 0){ 53 | return 1; 54 | } 55 | int result = ipowRecursive(base,exp/2); 56 | if(exp & 1){ 57 | return result*result*base; 58 | } 59 | return result*result; 60 | } 61 | 62 | int main(){ 63 | // int a = 50; 64 | printf("value is %d\n", ipowRecursive(10,4)); 65 | return 0; 66 | } -------------------------------------------------------------------------------- /greedy/question2.c: -------------------------------------------------------------------------------- 1 | /* 2 | Program to implement Huffman encoding 3 | 4 | METHOD: 5 | In case of huffman encoding the condition is that elements should not be uniformly distributed. 6 | There the element that is repeating most number of times, we represent it using least number of bits 7 | and the element that is repeating least number of times, we use max bits. Therefore in this case we can 8 | represent this thing using tree. 9 | The algo is to make a min heap out of the given elements (as we are required to find min each time) and also 10 | insertion is to be done (therefore sorting is not used). 11 | 12 | Now each time min is extracted twice. A new node is made which will contain the value equal to the sum 13 | of the two mins (freq). The left node smaller one will be the left child of the newly created node and the 14 | bigger of the two will be the right child. Like this we keep repeating this process for n-1 elements 15 | to build the tree. 16 | Now we can fix one pattern that is making left edge of each tree as 0 and right as 1 or vice versa down 17 | to the bottom and then we traverse for each node to generate the huffman codes. 18 | 19 | Note that in this each node will have its own unique pattern which wont be present in any of the nodes. 20 | Also note that node which is smaller has to be on the left and each time there is a possibility that not 21 | always there will be a tree, many a times there can be forests also and then you combine two search forests. 22 | This may happen because least two values will be different from the current sequence 23 | 24 | Time complexity: O(nlogn) //finding min n times (twice) and inserting the sum back to the tree O(logn) which 25 | is done n-1 times again 26 | Space complexity: O(n) //tree 27 | */ -------------------------------------------------------------------------------- /trees/question8.c: -------------------------------------------------------------------------------- 1 | /* 2 | Find the diameter of the given binary tree 3 | 4 | Diameter of the binary tree is the max distance between any two nodes. 5 | 6 | METHOD: 7 | Using recursion we can find the height of the LST and the RST at each node. We can maintain a global 8 | variable for diameter which can be updated if the new computed values is greater than the existing value. 9 | 10 | Time complexity: O(n) 11 | Space complexity: O(n) 12 | */ 13 | 14 | #include 15 | #include 16 | 17 | struct node{ 18 | int data; 19 | struct node *left; 20 | struct node *right; 21 | }; 22 | 23 | struct node *newNode(int data){ 24 | struct node *temp = (struct node *)malloc(sizeof(struct node)); 25 | temp->data = data; 26 | temp->left = temp->right= NULL; 27 | return temp; 28 | } 29 | 30 | int max(int left, int right){ 31 | return (left < right)?right: left; 32 | } 33 | 34 | int findDiameter(struct node *root, int *max_dia){ 35 | if(!root){ 36 | return 0; 37 | } 38 | int left = findDiameter(root->left, max_dia); 39 | int right = findDiameter(root->right, max_dia); 40 | int dia = left+right; 41 | if(*max_dia < dia){ 42 | *max_dia = dia; 43 | } 44 | return 1 + max(left,right); 45 | } 46 | 47 | int main(){ 48 | struct node *root = NULL; 49 | int max_dia = 0; 50 | root = newNode(2); 51 | root->left = newNode(3); 52 | root->right = newNode(4); 53 | root->left->right = newNode(5); 54 | root->left->right->right = newNode(6); 55 | root->left->right->right->right = newNode(7); 56 | root->left->left = newNode(8); 57 | root->left->left->right = newNode(10); 58 | root->left->left->left = newNode(9); 59 | 60 | findDiameter(root, &max_dia); 61 | printf("max dia is...%d\n", max_dia); 62 | return 0; 63 | } 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /arrays/question3.c: -------------------------------------------------------------------------------- 1 | /* 2 | MERGE SORT 3 | 4 | Space complexity: stack O(logn) + space for merge procedure O(n) 5 | Therefore total = O(n) 6 | 7 | Time complexity: time taken to merge O(n) + time taken to sort by masters theorem O(nlogn) 8 | Therefore: O(nlogn) 9 | 10 | */ 11 | #include 12 | #include 13 | #include 14 | 15 | 16 | void merge(int *arr, int start, int mid, int end) { 17 | 18 | int len1 = mid - start + 2; 19 | int len2 = end - mid + 1; 20 | 21 | int left[len1], right[len2]; 22 | 23 | int i; 24 | for(i=0; i 15 | #include 16 | 17 | struct node{ 18 | int data; 19 | struct node *left; 20 | struct node *right; 21 | }; 22 | 23 | struct node *newNode(int data){ 24 | struct node *temp = (struct node *)malloc(sizeof(struct node)); 25 | temp->data = data; 26 | temp->left = temp->right= NULL; 27 | return temp; 28 | } 29 | 30 | struct node *deletePaths(struct node *root, int k){ 31 | if(!root){ 32 | return NULL; 33 | } 34 | if(k == 0){ 35 | return root; 36 | } 37 | root->left = deletePaths(root->left, k-1); 38 | root->right = deletePaths(root->right, k-1); 39 | 40 | if(!root->left && !root->right){ 41 | free(root); 42 | return NULL; 43 | } 44 | return root; 45 | } 46 | 47 | void inorder(struct node *root){ 48 | if(root){ 49 | inorder(root->left); 50 | printf("%d\n", root->data); 51 | inorder(root->right); 52 | } 53 | } 54 | 55 | int main(){ 56 | int k = 3; 57 | struct node *root = newNode(10); 58 | root->left = newNode(12); 59 | root->left->left = newNode(14); 60 | root->left->left->left = newNode(15); 61 | root->left->right = newNode(16); 62 | root->right = newNode(20); 63 | root->right->left = newNode(22); 64 | root->right->right = newNode(26); 65 | 66 | root = deletePaths(root, k); 67 | 68 | inorder(root); 69 | 70 | } -------------------------------------------------------------------------------- /misc/question9.c: -------------------------------------------------------------------------------- 1 | /* 2 | Word break problem 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | int checkPresent(int i, int j, char *given, int n, char dict[n][16]){ 10 | int k; 11 | char str[j-i+2]; 12 | int m = 0; 13 | for(k=i;k<=j;k++){ 14 | str[m] = given[k]; 15 | m++; 16 | } 17 | str[m] = 0; 18 | for(k=0;k 17 | #include 18 | 19 | struct node{ 20 | int data; 21 | struct node *left; 22 | struct node *right; 23 | }; 24 | 25 | struct node *newNode(int data){ 26 | struct node *temp = (struct node *)malloc(sizeof(struct node)); 27 | temp->data = data; 28 | temp->left = temp->right= NULL; 29 | return temp; 30 | } 31 | 32 | void printElementsByLevel(struct node *root, int k, int distance){ 33 | if(root){ 34 | if(distance == k){ 35 | printf("%d\n", root->data); 36 | return; 37 | } 38 | printElementsByLevel(root->left,k,distance+1); 39 | printElementsByLevel(root->right,k,distance+1); 40 | } 41 | } 42 | 43 | int main(){ 44 | struct node *root = newNode(10); 45 | root->left = newNode(12); 46 | root->left->left = newNode(14); 47 | root->left->right = newNode(16); 48 | root->right = newNode(20); 49 | root->right->left = newNode(22); 50 | root->right->right = newNode(26); 51 | 52 | int k = 2; 53 | int distance = 0; 54 | 55 | printElementsByLevel(root,k,distance); 56 | 57 | return 0; 58 | } -------------------------------------------------------------------------------- /linked-lists/question11.c: -------------------------------------------------------------------------------- 1 | /* 2 | Alternating split of a given linked list. 3 | 4 | We can use a counter a split at even and odd numbers and move them to new list, but then we have 5 | dont have to create any new list but use the existing one and split it 6 | 7 | METHOD: 8 | take two pointers one pointing to head and one to head->next, then keep assigning them the next 9 | node as per the pattern until one of them reaches null. Make the other one also null in the end 10 | Time complexity: O(n) 11 | Space complexity: O(1) 12 | */ 13 | 14 | //METHOD 15 | #include 16 | #include 17 | 18 | struct node{ 19 | int data; 20 | struct node *link; 21 | }; 22 | 23 | void makeList(struct node *t, int maxCounter, int mul){ 24 | int counter = 1; 25 | while(counter <=maxCounter){ 26 | t->data = counter*mul; 27 | if(counter == maxCounter){ 28 | t->link = NULL; 29 | }else{ 30 | t->link = (struct node *)malloc(sizeof(struct node));; 31 | } 32 | t = t->link; 33 | counter++; 34 | } 35 | } 36 | 37 | void printList(struct node *head){ 38 | if(head){ 39 | printf("%d-->", head->data); 40 | printList(head->link); 41 | } 42 | printf("\n"); 43 | } 44 | 45 | void alternateList(struct node *head1, struct node *head2){ 46 | for(;head1 && head2 && head1->link && head2->link && head2->link->link; 47 | head1=head1->link, head2=head2->link){ 48 | 49 | head1->link = head2->link; 50 | head2->link = head2->link->link; 51 | } 52 | head1->link = head2->link = NULL; 53 | } 54 | 55 | int main(){ 56 | struct node *head1 = (struct node *)malloc(sizeof(struct node)); 57 | struct node *t = head1; 58 | makeList(t,8,10); 59 | struct node *head2 = head1->link; 60 | printList(head1); 61 | alternateList(head1,head2); 62 | printList(head1); 63 | printList(head2); 64 | 65 | } -------------------------------------------------------------------------------- /complex-datastructures/making-a-graph.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct adjListNode{ 5 | int data; 6 | struct adjListNode *next; 7 | }; 8 | 9 | struct adjList{ 10 | struct adjListNode *head; 11 | }; 12 | 13 | struct graph{ 14 | int vertices; 15 | struct adjList *arr; 16 | }; 17 | 18 | struct adjListNode *newNode(int data){ 19 | struct adjListNode *temp = (struct adjListNode *)malloc(sizeof(struct adjListNode)); 20 | temp->data = data; 21 | temp->next = NULL; 22 | return temp; 23 | } 24 | 25 | void addEdge(struct graph *newGraph,int start, int end){ 26 | struct adjListNode *temp = newNode(start); 27 | 28 | if(newGraph->arr[end].head){ 29 | temp->next = newGraph->arr[end].head; 30 | newGraph->arr[end].head = temp; 31 | 32 | }else{ 33 | newGraph->arr[end].head = temp; 34 | } 35 | 36 | temp = newNode(end); 37 | 38 | if(newGraph->arr[start].head){ 39 | temp->next = newGraph->arr[start].head; 40 | newGraph->arr[start].head = temp; 41 | }else{ 42 | newGraph->arr[start].head = temp; 43 | } 44 | } 45 | 46 | void display(struct graph *newGraph){ 47 | int i; 48 | struct adjListNode *temp; 49 | for(i=0;ivertices;i++){ 50 | temp = newGraph->arr[i].head; 51 | printf("%d --> ", i); 52 | while(temp){ 53 | printf("%d ", temp->data); 54 | temp = temp->next; 55 | } 56 | printf("\n"); 57 | } 58 | } 59 | 60 | int main(){ 61 | 62 | struct graph *newGraph = (struct graph *)malloc(sizeof(struct graph)); 63 | int vertices = 4; 64 | newGraph->vertices = 4; 65 | 66 | newGraph->arr = (struct adjList *)calloc(vertices,sizeof(struct adjList)); 67 | 68 | addEdge(newGraph, 0, 1); 69 | addEdge(newGraph, 0, 2); 70 | addEdge(newGraph, 1, 3); 71 | addEdge(newGraph, 2, 3); 72 | 73 | display(newGraph); 74 | 75 | return 0; 76 | } -------------------------------------------------------------------------------- /stacks-and-queues/question11.c: -------------------------------------------------------------------------------- 1 | /* 2 | Implement a queue using linked list 3 | 4 | //The circular array case rear was always incremented before inserting element and front was incrementing 5 | before deleting element, to save the space. In this case both rear and front can point to the same 6 | element in the begining and while removing no need to increment front for removal. Front is incremented 7 | after removal 8 | */ 9 | 10 | #include 11 | #include 12 | 13 | struct node{ 14 | int data; 15 | struct node *prev; 16 | struct node *next; 17 | } *rear = NULL,*front = NULL; 18 | 19 | struct node *newNode(int data){ 20 | struct node *temp = (struct node *)malloc(sizeof(struct node)); 21 | temp->data = data; 22 | temp->prev = NULL; 23 | temp->next = NULL; 24 | return temp; 25 | } 26 | 27 | void enqueue(int data){ 28 | struct node *temp = newNode(data); 29 | if(!rear){ 30 | rear = front = temp; 31 | return; 32 | } 33 | rear->next = temp; 34 | rear = rear->next; 35 | } 36 | 37 | int dequeue(){ 38 | if(!front){ 39 | return -1; 40 | } 41 | struct node *temp = front; 42 | front = front->next; 43 | if(!front){ 44 | front = rear= NULL; 45 | } 46 | return temp->data; 47 | } 48 | 49 | int main(){ 50 | int step; int elm; 51 | int data; 52 | 53 | while(1){ 54 | printf("1. Enqueue\n"); 55 | printf("2. Dequeue\n"); 56 | printf("3. Exit\n"); 57 | scanf("%d",&step); 58 | switch(step){ 59 | case 1: printf("enter element\n"); 60 | scanf("%d", &elm); 61 | enqueue(elm); 62 | break; 63 | case 2: data = dequeue(); 64 | if(data < 0){ 65 | printf("queue is now empty\n"); 66 | }else{ 67 | printf("%d was removed\n", data); 68 | } 69 | break; 70 | case 3: exit(1); 71 | break; 72 | } 73 | 74 | } 75 | return 0; 76 | } -------------------------------------------------------------------------------- /misc/question22.c: -------------------------------------------------------------------------------- 1 | /* 2 | http://practice.geeksforgeeks.org/problems/min-sum-formed-by-digits/0 3 | */ 4 | 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | int findMin(int a,int b){ 12 | return a < b?a:b; 13 | } 14 | 15 | int cmpfnc(void const *a, void const *b){ 16 | return (*(int *)a - *(int *)b ); 17 | } 18 | 19 | int findSum(int *arr, int size){ 20 | 21 | qsort(arr,size,sizeof(int),cmpfnc); 22 | 23 | int count = 0; 24 | 25 | int i = 0,j = size-1; 26 | 27 | int a=0,b=0; 28 | 29 | while(j>=0){ 30 | //extract numbers 31 | int temp1 = arr[j]*pow(10,i); 32 | j--; 33 | int temp2; 34 | if(j < 0){ 35 | temp2 = 0; 36 | }else{ 37 | temp2 = arr[j]*pow(10,i); 38 | } 39 | j--; 40 | 41 | // printf("temp1 is %d\n",temp1); 42 | // printf("temp2 is %d\n",temp2); 43 | 44 | if(temp1 + a + temp2 + b < temp1 + b + temp2 + a){ 45 | a += temp1; 46 | b += temp2; 47 | }else{ 48 | a+= temp2; 49 | b+= temp1; 50 | } 51 | 52 | // printf("a is now %d\n",a); 53 | // printf("b is now %d\n",b); 54 | 55 | // printf("there sum is %d\n",a+b); 56 | // printf("----------------------\n"); 57 | 58 | i++; 59 | } 60 | 61 | return a + b; 62 | } 63 | 64 | int main(){ 65 | int cases; 66 | scanf("%d",&cases); 67 | 68 | int i; 69 | for(i=0;i 6 | #include 7 | #define MAX 10000 8 | 9 | int queue[MAX]; 10 | int start = 0, rear = 0; 11 | int elmCount = 0; 12 | 13 | 14 | void enqueue(int data){ 15 | queue[rear++] = data; 16 | elmCount++; 17 | } 18 | 19 | int dequeue(){ 20 | elmCount--; 21 | return queue[start++]; 22 | } 23 | 24 | int isQueueEmpty(){ 25 | return elmCount == 0; 26 | } 27 | 28 | int minEdges(int vertices, int arr[vertices][vertices], int index, int *visited, int count){ 29 | visited[index] = 1; 30 | enqueue(index); 31 | int i; 32 | int value; 33 | while(!isQueueEmpty()){ 34 | int tempIndex = dequeue(); 35 | if(arr[tempIndex][vertices-1] == 1){ 36 | count++; 37 | printf("count is now...%d\n", count); 38 | return count; 39 | } 40 | for(i=0;i 18 | #include 19 | #include 20 | 21 | void maxProductSubArray(int *arr, int size){ 22 | int i; 23 | int countNegatives = 0; 24 | for(i=0;i max){ 37 | max = curr; 38 | } 39 | 40 | if(arr[i] < 0){ 41 | countNegatives--; 42 | } 43 | 44 | if((curr < 0 && countNegatives <= 0) || (curr == 0)){ 45 | curr = 1; 46 | } 47 | } 48 | 49 | curr = 1; 50 | countNegatives = lastCountNegativesValues; 51 | 52 | for(i=size-1;i>=0;i--){ 53 | curr = curr*arr[i]; 54 | if(curr > max){ 55 | max = curr; 56 | } 57 | 58 | if(arr[i] < 0){ 59 | countNegatives--; 60 | } 61 | 62 | if((curr < 0 && countNegatives <= 0) || (curr == 0)){ 63 | curr = 1; 64 | } 65 | } 66 | 67 | printf("%d\n", max); 68 | } 69 | 70 | int main(){ 71 | int cases; 72 | scanf("%d",&cases); 73 | int i; 74 | for(i=0;i 6 | #include 7 | 8 | struct node{ 9 | int data; 10 | struct node *left; 11 | struct node *right; 12 | }; 13 | 14 | struct node *newNode(int data){ 15 | struct node *temp = (struct node *)malloc(sizeof(struct node)); 16 | temp->data = data; 17 | temp->left = NULL; 18 | temp->right = NULL; 19 | return temp; 20 | } 21 | 22 | struct node *stack[1000]; 23 | int top = -1; 24 | 25 | struct node *queue[10000]; 26 | int start = 0, rear = 0; 27 | int totalElem = 0; 28 | 29 | void enqueue(struct node *root){ 30 | queue[rear++] = root; 31 | totalElem++; 32 | } 33 | 34 | struct node *dequeue(){ 35 | totalElem--; 36 | return queue[start++]; 37 | } 38 | 39 | int isQueueEmpty(){ 40 | return totalElem == 0; 41 | } 42 | 43 | int isStackEmpty(){ 44 | return top == -1; 45 | } 46 | 47 | void push(struct node *root){ 48 | stack[++top] = root; 49 | } 50 | 51 | struct node *pop(){ 52 | return stack[top--]; 53 | } 54 | 55 | void reverseLevelOrder(struct node *root){ 56 | if(!root){ 57 | return; 58 | } 59 | enqueue(root); 60 | while(!isQueueEmpty()){ 61 | struct node *temp = dequeue(); 62 | 63 | push(temp); 64 | 65 | if(temp->right){ 66 | enqueue(temp->right); 67 | } 68 | if(temp->left){ 69 | enqueue(temp->left); 70 | } 71 | } 72 | 73 | while(!isStackEmpty()){ 74 | printf("%d ", pop()->data); 75 | } 76 | } 77 | 78 | int main(){ 79 | 80 | struct node *root = newNode(1); 81 | 82 | root->left = newNode(2); 83 | root->right = newNode(3); 84 | root->right->left = newNode(6); 85 | root->right->right = newNode(7); 86 | root->left->left = newNode(4); 87 | root->left->right = newNode(5); 88 | 89 | reverseLevelOrder(root); 90 | 91 | return 0; 92 | } -------------------------------------------------------------------------------- /hacker-rank/question1.c: -------------------------------------------------------------------------------- 1 | /* 2 | https://www.hackerrank.com/challenges/pylons 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int minRemovals(int size, int k, int *arr){ 10 | 11 | int left[size]; 12 | int right[size]; 13 | int i; 14 | for(i=0;i= 0){ 29 | i = i-k-k+1; 30 | right[i] = 1; 31 | } 32 | int changesL = 0; 33 | int changesR = 0; 34 | 35 | for(i=0;i changesL){ 58 | return changesL; 59 | } 60 | return changesR; 61 | } 62 | return -1; 63 | 64 | } 65 | 66 | int main() { 67 | int num, k; 68 | scanf("%d %d",&num, &k); 69 | int arr[num]; 70 | int j; 71 | for(j=0; j 31 | #include 32 | 33 | int main(){ 34 | int arr[4][4] = { 35 | {0,1,2,3}, 36 | {4,5,6,7}, 37 | {8,9,10,11}, 38 | {12,13,14,15} 39 | }; 40 | int size = sizeof(arr)/sizeof(arr[0]); 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /programs/string-functions.c: -------------------------------------------------------------------------------- 1 | /* Various string functions in C */ 2 | 3 | //pre-process fetching contents of library stdio.h which contains predefined functions in C 4 | #include 5 | 6 | //str copy function self made 7 | char *strcpynew(char *d, char *s){ 8 | char *saved = d; 9 | while ((*d++ = *s++) != '\0'); 10 | 11 | return saved; //returning starting address of s1 12 | } 13 | 14 | char *strcatnew(char *d, char *s){ 15 | 16 | char *saved = d; 17 | 18 | while(*d != '\0'){ 19 | *d++; //finding end of d string; 20 | } 21 | 22 | while((*d++ = *s++) != '\0'); 23 | // *d = 0; 24 | return saved; 25 | 26 | } 27 | 28 | int strcmpnew(char *d, char *s){ 29 | 30 | while((*d == *s)){ 31 | if(*s == '\0'){ 32 | return 0; 33 | } 34 | *d++; 35 | *s++; 36 | } 37 | 38 | return *d - *s; 39 | 40 | } 41 | 42 | int strlennew(char *s){ 43 | 44 | int total; //to increase range we can take unsigned long long int 45 | 46 | while(*s != 0){ 47 | total++; 48 | } 49 | 50 | return total; 51 | } 52 | 53 | //default function that is run by C everytime 54 | int main(){ 55 | 56 | //for STRCPY 57 | char s1[] = "rahul"; //initializing strings 58 | char s2[] = "arora"; //initializing strings 59 | strcpynew(s1, s2); 60 | printf("strcpy: %s\n", s1); //updated string after strcpy 61 | 62 | //for STRCAT 63 | char s3[] = "rahul"; //initializing strings 64 | char s4[] = "arora"; //initializing strings 65 | strcatnew(s3, s4); 66 | printf("strcat: %s\n", s3); //updated string after strcat 67 | 68 | //for STRCMP 69 | char s5[] = "a"; //initializing strings 70 | char s6[] = "a"; //initializing strings 71 | printf("strcmp: %d\n", strcmpnew(s5, s6)); //updated string after strmp 72 | 73 | //for counting 74 | char s7[] = "rahul"; 75 | printf("strlen: %d\n", strlennew(s7)); 76 | } -------------------------------------------------------------------------------- /dynamic-programming/question7.c: -------------------------------------------------------------------------------- 1 | /* 2 | All pair shortest path algorithm 3 | 4 | In this algorithm every vertex is a node and a destination, so we need find the shortest path from a 5 | vertex to every other node. 6 | 7 | Therefore we can apply Dijkstra algorithm (kind of greedy algo only) on each vertex. 8 | For one vertex (single source) time complexity was O(Elogv) therefore for all it will be 9 | O(VElogv) which for dense graphs is equal to O(v^3 logv). 10 | Also if we apply bellman ford algorithm for -ve cycle it is O(VE) which for every vetex is 11 | O(V^2 E) which for densse graphs is O(V^4). 12 | Both the algos are expensive, so we will try to apply DP now. 13 | 14 | Floyd-warshall algorithm. 15 | Here we relax one node at a time one by one and make matrices corresponding to that node. We then use 16 | the recently computed matrix to further optimize it by relaxing the other node. Keep repeating this 17 | exercise until all the nodes are relaxed and n such matrices are made. The last matrix is the final 18 | answer. 19 | 20 | The analogy behind this is cost from 1 to 2 can be from 1 to 2 or 1 to 3 to 2 or 1 to 3 to 4 to 2. 21 | Therefore whichever is minimum we have to take that and every node has to be a source and a destination 22 | for which computation has to be done. Hence this method is applied 23 | 24 | Time complexity: Since n times n^2 sub problems are solved. Total sub problems are n^3 and time to solve 25 | one is constant. Hence time complexity is O(n^3) 26 | 27 | Space complexity: O(n^3), can be optimized further if two matrices are used and values are overriden for 28 | the next operation once done 29 | 30 | Logic: At any point 1 to 2 can be achieved either by going directly or from some other edge. Similarly 31 | 2 to 3 and all other combinations. Hence min(directly, relax some other) is always taken into 32 | consideration 33 | 34 | */ -------------------------------------------------------------------------------- /dynamic-programming/question1.c: -------------------------------------------------------------------------------- 1 | /* 2 | Algorithm to find optimized soln for matrix chain multiplication 3 | 4 | METHOD1: bottom up approach. 5 | In this the input given is an array of sizes of matrix and then we create two 2d matrices of size nXn. 6 | The first one will store the number of operations (most optimized) required to multiply each each combination. 7 | This will be computed using a for loop inside two other for loops which will actually traverse this matrix. 8 | This inner for loop will actually be used to find each partition of the given number of matrices (the ways 9 | that they can be multiplied). The second matrix is going to store the place where the partition was done 10 | such that we got least number of operations. 11 | 12 | METHOD2: Top down approach: to be done later 13 | */ 14 | #include 15 | #include 16 | #include 17 | 18 | int findOptimizedSolution(int arr[], int size){ 19 | int num_matrices = size-1; 20 | int m[num_matrices][num_matrices], s[num_matrices][num_matrices]; 21 | int i,j,k,l; 22 | for(i=0; i 13 | #include 14 | #define MAX 50 15 | 16 | void push(int arr[],int data, int *ptr){ 17 | if(*ptr == MAX-1){ 18 | printf("overflow\n"); 19 | return; 20 | } 21 | arr[++(*ptr)] = data; 22 | } 23 | 24 | int pop(int arr[],int *ptr){ 25 | if(*ptr == -1){ 26 | printf("underflow\n"); 27 | return -1; 28 | } 29 | int data = arr[(*ptr)--]; 30 | return data; 31 | } 32 | 33 | void enqueue(int arr[],int data, int *top1){ 34 | push(arr,data,top1); 35 | } 36 | 37 | int dequeue(int arr1[], int arr2[],int *top1, int *top2){ 38 | int data, result; 39 | if(*top2 == -1 && *top1 == -1){ 40 | return -1; 41 | }else{ 42 | while(*top1 != -1){ 43 | data = pop(arr1,top1); 44 | push(arr2,data,top2); 45 | } 46 | result = pop(arr2,top2); 47 | } 48 | return result; 49 | } 50 | 51 | int main(){ 52 | int stack1[MAX],stack2[MAX]; 53 | int top1 = -1, top2 = -1; 54 | int step, elm, result; 55 | 56 | while(1){ 57 | printf("1. PUSH element\n"); 58 | printf("2. POP element\n"); 59 | printf("3.EXIT \n"); 60 | scanf("%d",&step); 61 | 62 | switch(step){ 63 | case 1: printf("enter element to be pushed\n"); 64 | scanf("%d",&elm); 65 | enqueue(stack1,elm, &top1); 66 | break; 67 | case 2: result = dequeue(stack1, stack2,&top1, &top2); 68 | if(result < 0){ 69 | printf("already empty\n"); 70 | }else{ 71 | printf("%d was deleted\n", result); 72 | } 73 | break; 74 | case 3: exit(1); 75 | break; 76 | } 77 | } 78 | 79 | return 0; 80 | } -------------------------------------------------------------------------------- /graphs/question13.c: -------------------------------------------------------------------------------- 1 | /* 2 | Dijkstra algorithm for adj matrix 3 | */ 4 | 5 | 6 | #include 7 | #include 8 | #include 9 | #define vertices 9 10 | 11 | int minDistance(int *dist, int *visited) { 12 | int min = INT_MAX, min_index; 13 | int i; 14 | for(i=0;i 18 | int main(){ 19 | int a[] = {10,3,4,5,7,1,3,2}; 20 | int length = sizeof(a)/sizeof(a[0]); 21 | int elm1, counter; 22 | for(int i=0; i a[j]){ 27 | counter++; 28 | } 29 | } 30 | printf("%d ", counter); 31 | } 32 | } 33 | 34 | //METHOD2 35 | #include 36 | #include 37 | 38 | struct node{ 39 | int data,height,size; 40 | struct node *left, *right; 41 | } 42 | 43 | int height(struct node *root){ 44 | return !root?0: root->height; 45 | } 46 | 47 | int size(struct node *root){ 48 | return !root?0: root->size; 49 | } 50 | 51 | int max(int a, int b){ 52 | return (a>b)?a:b; 53 | } 54 | 55 | struct node *newNode(int data){ 56 | 57 | } 58 | 59 | struct node *rightRotate(struct node *root){ 60 | 61 | } 62 | 63 | struct node *leftRotate(struct node *root){ 64 | 65 | } 66 | 67 | int getBalance(struct node *root){ 68 | return !root?0: height(root->left)-height(root->right); 69 | } 70 | 71 | struct node *insert(struct node *root, int data, int *count){ 72 | 73 | } 74 | 75 | void countSmallerArray(int *arr, int *smaller, int size){ 76 | 77 | } 78 | 79 | void printArray(int *arr, int size){ 80 | for(int i=0; i 30 | #include 31 | 32 | int findMinNum(int arr[], int size){ 33 | int sum = 1; //assume 34 | for(int i=0;i 19 | #include 20 | 21 | int cmpfunc(const void *a, const void *b){ 22 | return (*(int *)a - *(int *)b); 23 | } 24 | 25 | struct node{ 26 | int left; 27 | int right; 28 | }; 29 | 30 | int maxIntervals(struct node *arr, int size){ 31 | int counter = 1; 32 | for(int i=0; i 17 | #include 18 | #include 19 | #include 20 | 21 | int findMin(int a, int b, int c){ 22 | return (a 21 | #include 22 | 23 | int calculatePlatforms(int arr[], int dep[],int size){ 24 | int counter = 0; 25 | int max = 0; 26 | int i = 0,j=0; 27 | while(i < size && j < size){ 28 | // printf("arrival is %d\n", arr[i]); 29 | // printf("dep is %d\n", dep[j]); 30 | if(arr[i] 28 | #include 29 | 30 | int fib(int n){ 31 | int f[n+1]; 32 | int i; 33 | f[0] = 0; 34 | f[1] = 1; 35 | for(i=2;i<=n;i++){ 36 | f[i] = f[i-1] + f[i-2]; 37 | } 38 | return f[n]; 39 | } 40 | 41 | int main(){ 42 | int n = 9; 43 | printf("total here is: %d\n", fib(n)); 44 | return 0; 45 | } 46 | 47 | 48 | //RECURSIVE 49 | #include 50 | #include 51 | #define MAX 100 52 | 53 | int f[MAX]; 54 | 55 | int fibRecursive(int n){ 56 | if(f[n] == -1){ //if does not exist then only compute 57 | if(n < 2){ 58 | f[n] = n; 59 | } 60 | else{ 61 | f[n] = fibRecursive(n-1) + fibRecursive(n-2); 62 | } 63 | } 64 | return f[n]; 65 | } 66 | 67 | void initialize(int n){ 68 | int i; 69 | for(i=0; i<=n;i++){ 70 | f[i] = -1; 71 | } 72 | } 73 | 74 | int main(){ 75 | int n = 9; 76 | initialize(n); 77 | printf("total here is: %d\n", fibRecursive(n)); 78 | return 0; 79 | } --------------------------------------------------------------------------------