├── APSAssignment ├── Assignment3 │ ├── Assignment 3.pdf │ ├── a3q5.c │ ├── a3q1.c │ ├── a3q4.c │ ├── a3q2.c │ └── a3q3.c ├── Assignment4 │ ├── Assignment4.pdf │ ├── c.cpp │ ├── b.cpp │ └── a.cpp ├── Assignment1 │ ├── APS Assignment1.pdf │ ├── a1q6.c │ ├── a1q3.c │ ├── a1q1.c │ ├── a1q5.c │ ├── a1q2.c │ ├── a1q3.inv_modulo.c │ └── a1q4.c ├── Assignment2 │ ├── APS Assignment 2.pdf │ ├── a2q3.c │ ├── a2q1.c │ ├── a2q6.c │ ├── a2q4.c │ ├── a2q2.c │ └── a2q5.c └── Assignment5 │ ├── e.cpp │ ├── d.cpp │ ├── f.cpp │ ├── a.cpp │ ├── b.cpp │ ├── c.cpp │ └── Assignment5.txt ├── README.md └── APSLab ├── LabTest ├── e.c ├── a.c ├── c.c └── b.c ├── LabTest2 ├── c.cpp ├── b.cpp ├── a.cpp └── d.cpp ├── Lab7 ├── a.cpp └── b.cpp ├── Lab2 ├── prime_num.c ├── hex_to_bin.c └── indo-pak-war.c ├── Lab1 ├── coprime.c ├── stairs.c └── trip.c ├── Lab5 ├── b.c └── a.c ├── Lab3 ├── c.c ├── b.c └── a.c ├── Lab4 ├── 2.cpp ├── 1.c └── 3.c └── Lab6 ├── a.c └── b.cpp /APSAssignment/Assignment3/Assignment 3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arpitbbhayani/aps_src/HEAD/APSAssignment/Assignment3/Assignment 3.pdf -------------------------------------------------------------------------------- /APSAssignment/Assignment4/Assignment4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arpitbbhayani/aps_src/HEAD/APSAssignment/Assignment4/Assignment4.pdf -------------------------------------------------------------------------------- /APSAssignment/Assignment1/APS Assignment1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arpitbbhayani/aps_src/HEAD/APSAssignment/Assignment1/APS Assignment1.pdf -------------------------------------------------------------------------------- /APSAssignment/Assignment2/APS Assignment 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arpitbbhayani/aps_src/HEAD/APSAssignment/Assignment2/APS Assignment 2.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | C Programming Questions : 2 | These are the questions that are on various algorithms and optimizations. 3 | The questiona are mentioned in the PDF file and solutions are the .c and .cpp files correspondingly. 4 | -------------------------------------------------------------------------------- /APSLab/LabTest/e.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define SIZE 1000001 5 | 6 | int main ( int argc , char *argv[] ) { 7 | 8 | //int * array = (int *) calloc ( SIZE , sizeof(int )) ; 9 | int i , t ,a; 10 | 11 | scanf("%d" , &t); 12 | 13 | while ( t-- ) { 14 | scanf("%d" , &i ); 15 | a= 0; 16 | if ( i & 1 ) 17 | a++; 18 | a += (i>>1); 19 | 20 | printf("%d\n" , a); 21 | } 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /APSLab/LabTest/a.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int (compar)(const void *a, const void *b) { 5 | int x = *((int *) (a)); 6 | int y = *((int *) (b)); 7 | return x-y; 8 | } 9 | 10 | int main ( int argc , char * argv[] ) { 11 | 12 | int t , i; 13 | int array[4]; 14 | scanf( "%d" , &t ); 15 | 16 | while ( t-- ) { 17 | for ( i = 0 ; i < 4 ; i++ ) { 18 | scanf("%d" , &array[i] ); 19 | } 20 | 21 | qsort ( array , 4 , sizeof(int ) , compar ); 22 | 23 | printf("%d\n" , array[3] + array[2] ); 24 | 25 | } 26 | return 0; 27 | 28 | } 29 | -------------------------------------------------------------------------------- /APSAssignment/Assignment1/a1q6.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define BIG_NUM 1000000007 5 | 6 | long int eval(long long int); 7 | 8 | int main( int argc , char *argv []) { 9 | 10 | long long int n; 11 | 12 | while ( 1 ) { 13 | scanf("%lld" , &n); 14 | if( n == 0 ) 15 | break; 16 | eval(n); 17 | } 18 | 19 | return 0; 20 | 21 | } 22 | 23 | long int eval( long long int n) { 24 | long long int i = 1; 25 | long long int sum = 0; 26 | //long long int pro = 0; 27 | long long int k=1; 28 | 29 | for(i = 1 ; i <= n ; ) { 30 | k++; 31 | sum = (sum + i)%BIG_NUM; 32 | i=k*k; 33 | } 34 | printf("%lld %lld\n" , k-1 , sum); 35 | return (0); 36 | 37 | } 38 | -------------------------------------------------------------------------------- /APSLab/LabTest2/c.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define SIZE 1000000 5 | int array[1000010]; 6 | 7 | int main( int argc , char * argv[] ) { 8 | char str[16]; 9 | 10 | for ( int i = 1 ; i < SIZE ; i++ ) { 11 | sprintf( str , "%d" , i ); 12 | long long int sum = i; 13 | for ( int j = 0 ; str[j] != '\0' ; j++ ) { 14 | sum += (str[j] - '0'); 15 | } 16 | if ( array[sum] == 1 ) { 17 | continue; 18 | } 19 | else { 20 | array[sum] = 1; 21 | } 22 | } 23 | 24 | for ( int i = 1 ; i < SIZE ; i++ ) { 25 | //cout << " i : " << i << " " << array[i] << endl; 26 | if ( array[i] != 1 ) 27 | cout << i << endl; 28 | } 29 | 30 | return 0; 31 | 32 | 33 | } 34 | -------------------------------------------------------------------------------- /APSLab/Lab7/a.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main ( int argc , char * argv[] ) { 6 | 7 | int n; 8 | cin >> n; 9 | 10 | int array[n] , max[n]; 11 | int global_max , local_max; 12 | 13 | for ( int i = 0 ; i < n ; i++ ) { 14 | cin >> array[i]; 15 | } 16 | 17 | if ( n == 0 ) { 18 | cout << "0" << endl; 19 | return 0; 20 | } 21 | 22 | max[0] = 1; 23 | global_max = 1; 24 | 25 | for ( int i = 0 ; i < n ; i++ ) { 26 | 27 | local_max = 1; 28 | 29 | for ( int j = i-1 ; j >= 0 ; j-- ) { 30 | if ( max[j] + 1 > local_max && array[i] > array[j] ) { 31 | local_max = max[j] + 1; 32 | } 33 | } 34 | 35 | max[i] = local_max; 36 | 37 | if ( local_max > global_max ) { 38 | global_max = local_max; 39 | } 40 | 41 | } 42 | 43 | cout << global_max << endl; 44 | 45 | return 0; 46 | 47 | } 48 | -------------------------------------------------------------------------------- /APSLab/LabTest/c.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int readline( char *str ) { 4 | 5 | char ch; 6 | int i = 0; 7 | while ( ( ch = getchar() ) != '\n' ) { 8 | *str++ = ch; 9 | i++; 10 | } 11 | *str = '\0'; 12 | return i; 13 | } 14 | 15 | 16 | int main ( int argc , char * argv[] ) { 17 | 18 | char * n_ptr; 19 | char * bin = (char * ) calloc ( 100001 , 1); 20 | 21 | int t = 0 , i = 0; 22 | long long int divisor = 0; 23 | long long int dividend = 0; 24 | 25 | scanf("%d%lld" , &t , &divisor ); 26 | getchar(); 27 | i = 0; 28 | while ( i != t ) { 29 | i ++; 30 | readline ( bin ); 31 | dividend = strtoll ( bin , &n_ptr , 2 ); 32 | long long int rem = dividend % divisor; 33 | if ( rem == 0 ) 34 | printf("Case %d: Yes\n" , i); 35 | else 36 | printf("Case %d: No\n" , i); 37 | } 38 | 39 | return 0; 40 | 41 | } 42 | -------------------------------------------------------------------------------- /APSAssignment/Assignment1/a1q3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define BIG_NUM 1000000007 4 | #define SIZE 1002 5 | 6 | int main() { 7 | 8 | int n , m ; 9 | int i , j; 10 | long int array[SIZE][SIZE]; 11 | 12 | for( i = 0 ; i < SIZE ; i++ ) { 13 | array[0][i] = 1; 14 | array[i][0] = 1; 15 | } 16 | 17 | for( i = 1 ; i < SIZE ; i++ ) { 18 | for( j = 1 ; j < SIZE - i ; j ++ ) { 19 | array[i][j] = (array[i-1][j] + array[i][j-1])% BIG_NUM; 20 | } 21 | } 22 | 23 | for( i = 0 ; i < SIZE ; i++ ) { 24 | for( j = 0 ; j < SIZE - i ; j ++ ) { 25 | //printf("%ld " , array[i][j]); 26 | } 27 | //printf("\n"); 28 | } 29 | 30 | while ( 1 ) { 31 | scanf("%d%d" , &n,&m); 32 | if( n == -1 && m == -1 ) 33 | break; 34 | 35 | printf("%ld\n" , array[ n - m ][m]); 36 | 37 | } 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /APSLab/LabTest/b.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define SIZE 1000001 4 | int main( int argc , char * argv[] ) { 5 | 6 | int n = 0 , sum = 0 , i = 0 , j = 0; 7 | int t= 0 ; 8 | int * array = (int *) calloc ( SIZE , sizeof(int) ); 9 | 10 | for ( j = SIZE - 1 ; j > 0 ; j-- ) { 11 | n = j; 12 | 13 | for( i = 2 ; i * i <= n ; i++ ) { 14 | 15 | if( n % i == 0 ) { 16 | if( i > (n/i) ) 17 | break; 18 | if( i == (n/i) ) { 19 | sum += i; 20 | break; 21 | } 22 | array[i] = 1; 23 | array[ n / i ] = 1; 24 | } 25 | 26 | } 27 | 28 | } 29 | 30 | scanf("%d" , &t ); 31 | 32 | while ( t-- ) { 33 | 34 | int num = 0; 35 | scanf("%d" , &num ); 36 | int count = 0; 37 | for ( i = num ; i >= 2 ; i++ ) { 38 | if ( array[i] == 1 ) 39 | count++; 40 | } 41 | printf("%d" , count ); 42 | 43 | } 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /APSAssignment/Assignment5/e.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main ( int argc , char * argv[] ) { 6 | 7 | int t; 8 | long long int sum[10010] , max_ = 0; 9 | 10 | cin >> t; 11 | 12 | for(int l = 0 ; l < t ; l++ ) { 13 | 14 | int n; 15 | cin >> n ; 16 | 17 | for ( int i = 0 ; i < n ; i++ ) { 18 | cin >> sum[i]; 19 | } 20 | 21 | if ( n == 1 ) { 22 | max_ = sum[n-1]; 23 | } 24 | else { 25 | sum[1] = max(sum[0] , sum[1]); 26 | 27 | for ( int i = 2 ; i < n ; i++ ) { 28 | 29 | long long int sum_y = sum[i-2] + sum[i]; 30 | long long int sum_n = sum[i-1]; 31 | sum[i] = max ( sum_y , sum_n ); 32 | } 33 | //for ( int i = 0 ; i < n ; i++ ) { 34 | // cout << " " << sum[i]; 35 | //} 36 | max_ = sum[n-1]; 37 | } 38 | 39 | cout << "Case " << l+1 << ": " << max_ << endl; 40 | 41 | } 42 | 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /APSLab/LabTest2/b.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef long long LL; 8 | 9 | int a[1000001]; 10 | 11 | LL compute(LL x) 12 | { 13 | if(x<=11) 14 | return x; 15 | else 16 | { 17 | if(x<=1000000) 18 | { 19 | if(a[x]==0) 20 | { 21 | a[x]=max(x,(compute(x/2)+compute(x/3)+compute(x/4))); 22 | return a[x]; 23 | } 24 | else 25 | return a[x]; 26 | } 27 | else 28 | return(max(x,(compute(x/2)+compute(x/3)+compute(x/4)))); 29 | } 30 | } 31 | int main() 32 | { 33 | LL x; 34 | int t; 35 | scanf("%d" , &t); 36 | while(t--) 37 | { 38 | scanf("%lld" , &x); 39 | cout< 2 | #include 3 | #define SIZE 10000001 4 | #define P_SIZE 700000 5 | int * array , * prime; 6 | 7 | int p; 8 | int main( int argc , char * argv[] ) { 9 | 10 | long long int a = 10 , b = 100; 11 | long long int i , j , t , count = 0; 12 | 13 | array = (int *) calloc ( SIZE , sizeof(int) ); 14 | array[0] = array[1] = 0; 15 | 16 | i = 2; 17 | while (i < SIZE) { 18 | 19 | if( array[i] == 0 ) { 20 | count++; 21 | array[i] = count; 22 | } 23 | else { 24 | array[i] = count; 25 | i++; 26 | continue; 27 | } 28 | 29 | for (j = i * i ; j < SIZE ; j+=i) { 30 | array[j] = -1; 31 | } 32 | 33 | i++; 34 | } 35 | 36 | while ( 1 ) { 37 | scanf("%lld%lld",&a,&b); 38 | 39 | if(a == 0 && b == 0) 40 | break; 41 | 42 | if( a > b ) { 43 | t = a; 44 | a = b; 45 | b = t; 46 | } 47 | 48 | if( a > 1 ) 49 | a--; 50 | 51 | printf("%d\n" , array[b] - array[a]); 52 | } 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /APSAssignment/Assignment3/a3q5.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main( int argc , char * argv[] ) { 5 | 6 | int t ; 7 | 8 | int * array1 , * array2; 9 | 10 | scanf("%d" , &t); 11 | 12 | while( t-- ) { 13 | 14 | int num , index1 , index2; 15 | int i = 0; 16 | int count = 0 , t_count = 0; 17 | 18 | scanf("%d" , &num ); 19 | 20 | array1 = (int *) malloc ( num * sizeof(int) ); 21 | array2 = (int *) malloc ( num * sizeof(int) ); 22 | 23 | for( i = 0 ; i < num ; i++) 24 | scanf("%d" , &array1[i] ); 25 | 26 | for( i = 0 ; i < num ; i++) 27 | scanf("%d" , &array2[i] ); 28 | 29 | index1 = index2 = num - 1 ; 30 | 31 | for( i = 0 ; i < num ; i++ ) { 32 | if( array1[ index1 ] == array2[ index2 ] ) { 33 | count += t_count; 34 | t_count = 0; 35 | index2--; 36 | } 37 | else { 38 | t_count++; 39 | } 40 | index1--; 41 | } 42 | 43 | count += t_count; 44 | printf("%d\n" , count ); 45 | 46 | free ( array1 ); 47 | free ( array2 ); 48 | 49 | } 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /APSAssignment/Assignment5/d.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main ( int argc ,char * argv[] ) { 8 | 9 | int t; 10 | scanf("%d" , &t); 11 | 12 | map< long long int , long long int > sum_p; 13 | map< long long int , long long int>::iterator itr; 14 | 15 | while ( t-- ) { 16 | 17 | 18 | long long int count = 0; 19 | long long int n; 20 | 21 | scanf("%lld" , &n); 22 | 23 | long long int array[n]; 24 | 25 | scanf("%lld" , &array[0]); 26 | 27 | sum_p[ array[0] ] ++; 28 | 29 | for ( int i = 1 ; i < n ; i++ ) { 30 | scanf("%lld" , &array[i]); 31 | array[i] += array[i-1]; 32 | sum_p[ array[i] ] ++; 33 | } 34 | 35 | 36 | for ( itr = sum_p.begin() ; itr != sum_p.end() ; itr++ ) { 37 | long long int a,b; 38 | 39 | a = itr->first; 40 | b = itr->second; 41 | 42 | if ( a != 0 ) { 43 | count += b * (b-1) / 2; 44 | } 45 | else { 46 | count += b + (b * (b-1)/2 ); 47 | } 48 | } 49 | 50 | printf("%lld\n" , count); 51 | sum_p.clear(); 52 | } 53 | 54 | return 0; 55 | 56 | } 57 | -------------------------------------------------------------------------------- /APSLab/LabTest2/a.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | 10 | int main() 11 | { 12 | int m,k; 13 | cin>>m>>k;//k-hours 14 | int d[m+1];//distance (i --> i+1) 15 | int s[m+1];//fuel supply s[i] denote the i-th 16 | for(int i=1;i<=m;i++) 17 | cin>>d[i]; 18 | for(int i=1;i<=m;i++) 19 | cin>>s[i]; 20 | int sup_max=-1; 21 | int fuel=0; 22 | int ans=0,temp,time,factor; 23 | 24 | /* i-th city ----> (i+1)-th city */ 25 | for(int i=1;i<=m;i++) 26 | { 27 | sup_max=max(sup_max,s[i]); 28 | fuel=fuel+s[i]; 29 | if(fuel>=d[i]) 30 | { 31 | fuel=fuel-d[i]; 32 | ans+=d[i]; 33 | } 34 | else{ 35 | temp=d[i]-fuel; 36 | time=0; 37 | factor=0; 38 | if(temp 19 | #include 20 | 21 | int main( int argc , char * argv[] ) { 22 | 23 | int n = 0 , sum = 0 , i = 0; 24 | 25 | while ( 1 ) { 26 | scanf("%d" , &n); 27 | 28 | 29 | if( n == 1 ) { 30 | printf("0\n"); 31 | continue; 32 | } 33 | 34 | 35 | if( n == -1 ) 36 | break; 37 | 38 | sum = 0; 39 | 40 | for( i = 2 ; i * i <= n ; i++ ) { 41 | 42 | if( n % i == 0 ) { 43 | if( i > (n/i) ) 44 | break; 45 | if( i == (n/i) ) { 46 | sum += i; 47 | break; 48 | } 49 | sum += i; 50 | sum += (n / i); 51 | } 52 | 53 | } 54 | sum = sum + 1; 55 | printf("%d\n" , sum); 56 | 57 | } 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /APSLab/Lab1/coprime.c: -------------------------------------------------------------------------------- 1 | /* 2 | Co-primes 3 | 4 | Time to check your math aptitude. 5 | 6 | Two numbers are relatively co-primes if their greatest common divisor is 1. For example, gcd(14,15) =1. Given M and N as input, Write a program to tell whether they are co-primes or not. 7 | 8 | Constraints: 9 | 10 | 11 | M <= 10^8 and N <= 10^8 12 | Input Specification: 13 | 14 | The input format is: M(space)N. End of the input would be determined when both M and N are zero. 15 | 16 | Output Specification: 17 | 18 | Print "Yes" if number are relatively prime, otherwise "No".(Without quotes). Followed by a newline character. 19 | 20 | Sample Input 21 | 22 | 3 4 23 | 5 10 24 | 14 15 25 | 0 0 26 | Sample Output 27 | 28 | Yes 29 | No 30 | Yes 31 | */ 32 | 33 | #include 34 | 35 | int getGcd(int, int); 36 | 37 | int main( int argc , char * argv[] ) { 38 | 39 | int m , n , gcd = 0; 40 | 41 | do { 42 | 43 | scanf("%d %d" , &m , &n); 44 | if( m == 0 && n == 0) 45 | break; 46 | gcd = getGcd(m,n); 47 | if( gcd == 1 ) 48 | printf("Yes\n"); 49 | else 50 | printf("No\n"); 51 | } while (1); 52 | return 0; 53 | } 54 | 55 | int getGcd(int a , int b) { 56 | while( a != b ) { 57 | if( a > b ) 58 | a = a - b; 59 | if( b > a ) 60 | b = b - a; 61 | } 62 | return a; 63 | } 64 | 65 | -------------------------------------------------------------------------------- /APSAssignment/Assignment2/a2q3.c: -------------------------------------------------------------------------------- 1 | /* 2 | Find the number of binary sequences of length N possible with no consecutive zeroes and 3 | without leading zeroes. 4 | N<=1000000, T<=10 5 | INPUT: 6 | First line contains T, no of test cases 7 | Following T lines contain an integer N (length of binary sequence) 8 | OUTPUT: 9 | Output the number of possible binary sequences for each test case. 10 | Output can be large , so print (output mod 1000000007) 11 | INPUT: 12 | 2 13 | 2 14 | 3 15 | OUTPUT: 16 | 2 17 | 3 18 | Explanation: 19 | For input 1 , {1} is only possible . So output is 1. 20 | For input 2, {10,11} are possible so output is 2. 21 | For input 3, {101,110,111} are possible so output is 3. 22 | 23 | */ 24 | 25 | #include 26 | #include 27 | #define BIG_NUM 1000000007 28 | #define SIZE 1000001 29 | 30 | int main( int argc , char * argv[] ) { 31 | 32 | long int num = 0 , *array , n = 0 , i = 0; 33 | 34 | array = (long int *) malloc( SIZE * sizeof(long int) ); 35 | 36 | array[0] = 1; 37 | array[1] = 2; 38 | 39 | for(i = 2 ; i < SIZE ; i++ ) { 40 | array[i] = (array[i-1] + array[i-2]) % BIG_NUM; 41 | } 42 | 43 | scanf("%ld",&num); 44 | 45 | while( num-- ) { 46 | scanf("%ld" , &n); 47 | if( n <= 0 ) 48 | printf("0\n"); 49 | else 50 | printf("%ld\n" , array[n-1]); 51 | } 52 | 53 | return 0; 54 | } 55 | 56 | -------------------------------------------------------------------------------- /APSAssignment/Assignment5/f.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void swap ( int *a , int *b ) { 6 | int t = *a; 7 | *a = *b; 8 | *b = t; 9 | } 10 | 11 | void sort ( int * a , int * b , int n ) { 12 | for ( int i = 0 ; i < n ; i++ ) { 13 | for ( int j = i + 1 ; j < n ; j++ ) { 14 | if ( a[i] > a[j] ) { 15 | swap(&a[i] , &a[j]); 16 | swap(&b[i] , &b[j]); 17 | } 18 | else if ( a[i] == a[j] ) { 19 | if ( b[i] > b[j] ) { 20 | swap( &b[i] , &b[j] ); 21 | } 22 | } 23 | 24 | } 25 | } 26 | } 27 | 28 | int main ( int argc , char * argv[] ) { 29 | 30 | int t; 31 | cin >> t; 32 | 33 | int a[10010] , b[10010] , long_seq[10010]; 34 | 35 | while ( t-- ) { 36 | 37 | int n; 38 | cin >> n; 39 | 40 | for(int i = 0 ; i < n ; i++ ) { 41 | cin >> a[i]; 42 | } 43 | 44 | for(int i = 0 ; i < n ; i++ ) { 45 | cin >> b[i]; 46 | } 47 | sort ( a , b , n ); 48 | 49 | long_seq[0] = 1; 50 | int max = 1; 51 | 52 | for ( int i = 1 ; i < n ; i++ ) { 53 | long_seq[i] = 1; 54 | for ( int j = i-1 ; j >= 0 ; j-- ) { 55 | if ( b[j] <= b[i] ) { 56 | if ( long_seq[j] + 1 > long_seq[i] ) { 57 | long_seq[i] = long_seq[j] + 1; 58 | } 59 | } 60 | if ( long_seq [i] > max ) { 61 | max = long_seq[i]; 62 | } 63 | } 64 | } 65 | 66 | cout << max << endl; 67 | 68 | } 69 | 70 | return 0; 71 | } -------------------------------------------------------------------------------- /APSAssignment/Assignment5/a.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main ( int argc , char * argv[] ) { 9 | 10 | int t; 11 | 12 | cin >> t; 13 | 14 | while ( t-- ) { 15 | int n; 16 | cin >> n; 17 | 18 | list node[n]; 19 | int visited[n]; 20 | 21 | int p; 22 | cin >> p; 23 | 24 | for ( int i = 0 ; i < n ; i++ ) { 25 | visited[i] = 0; 26 | 27 | /*cout << "Node : " << i << " -> "; 28 | for ( vector::iterator itr = node[i].begin() ; itr != node[i].end() ; itr++ ) { 29 | cout << *itr << " "; 30 | } 31 | cout << endl; 32 | */ 33 | 34 | } 35 | 36 | 37 | while ( p-- ) { 38 | int a , b; 39 | cin >> a >> b; 40 | 41 | node[a].push_back(b); 42 | node[b].push_back(a); 43 | } 44 | 45 | 46 | queue q; 47 | int count = 0; 48 | for ( int i = 0 ; i < n ; i++ ) { 49 | 50 | if ( visited[i] == 1 ) 51 | continue; 52 | 53 | visited[i] = 1; 54 | 55 | q.push(i); 56 | 57 | while ( !q.empty() ) { 58 | int t = q.front(); 59 | q.pop(); 60 | 61 | for ( list::iterator itr = node[t].begin() ; itr != node[t].end() ; itr++ ) { 62 | int child = *itr; 63 | 64 | if ( visited[child] == 1 ) 65 | continue; 66 | 67 | visited[child] = 1; 68 | q.push(child); 69 | } 70 | 71 | } 72 | 73 | count ++; 74 | 75 | 76 | } 77 | 78 | cout << count << endl; 79 | 80 | 81 | } 82 | 83 | return 0; 84 | 85 | } 86 | 87 | -------------------------------------------------------------------------------- /APSLab/Lab5/b.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct stack_t { 4 | int stack[500000]; 5 | int top; 6 | }; 7 | 8 | int is_empty( struct stack_t * s ) { 9 | if ( s->top > -1 ) { 10 | return 0; 11 | } 12 | return 1; 13 | } 14 | 15 | int get_top ( struct stack_t * s ) { 16 | return s->stack[ s->top ]; 17 | } 18 | 19 | void init ( struct stack_t * s ) { 20 | s->top = -1; 21 | } 22 | 23 | int main ( int argc , char * argv[] ) { 24 | 25 | struct stack_t s; 26 | 27 | int temp , _top , first_ele; 28 | long int i , size , t; 29 | long long int count; 30 | 31 | scanf ("%ld" , &t ); 32 | 33 | while ( t-- ) { 34 | 35 | scanf("%ld" , &size ); 36 | 37 | count = 0; 38 | init ( &s ); 39 | 40 | for ( i = 0 ; i < size ; i++ ) { 41 | 42 | scanf ( "%d" , &first_ele ); 43 | 44 | while ( (!is_empty(&s)) && first_ele > get_top ( &s ) ) { 45 | 46 | temp = get_top ( &s ); 47 | _top = s.top ; 48 | 49 | while ( !is_empty( &s ) && ( temp == get_top( &s ) )) { 50 | count += size -1 - i; 51 | s.top --; 52 | } 53 | 54 | if ( ! is_empty( &s ) ) { 55 | count += ( _top - s.top ) * ( s.top ); 56 | } 57 | } 58 | 59 | s.stack[++s.top] = first_ele; 60 | } 61 | 62 | while ( !is_empty( &s ) ) { 63 | 64 | temp = get_top ( &s ); 65 | _top = s.top; 66 | 67 | while ( !is_empty( &s ) && ( temp == get_top ( &s ) ) ) 68 | s.top --; 69 | 70 | if ( !is_empty( &s ) ) 71 | count += ( _top - s.top ) * ( s.top ); 72 | } 73 | 74 | printf("%lld\n" , count); 75 | 76 | } 77 | 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /APSAssignment/Assignment3/a3q1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int merge ( int * array , int l , int m , int h ) { 5 | 6 | int b_array[100001]; 7 | int i = 0 , j = 0 , k = 0; 8 | int count = 0; 9 | 10 | i = k = l; 11 | j = m+1; 12 | while ( i <= m && j <= h ) { 13 | if ( array[i] <= array[j] ) { 14 | b_array[k++] = array[i++]; 15 | } 16 | else { 17 | count += ( m - i + 1 ); 18 | b_array[k++] = array[j++]; 19 | } 20 | 21 | } 22 | if ( i > m ) { 23 | while ( j <= h ) { 24 | b_array[k++] = array[j++]; 25 | } 26 | } 27 | else if ( j > h ) { 28 | while ( i <= m ) { 29 | b_array[k++] = array[i++]; 30 | } 31 | } 32 | 33 | k = l; 34 | 35 | while ( k <= h ) { 36 | array[k] = b_array[k]; 37 | k++; 38 | } 39 | return count; 40 | } 41 | 42 | int count_inv ( int * array , int start , int end ) { 43 | 44 | 45 | if ( start >= end ) return 0; 46 | 47 | int mid = (start + end ) / 2; 48 | 49 | int count_l = count_inv ( array , start , mid ); 50 | 51 | int count_r = count_inv ( array , mid+1 , end ); 52 | 53 | int count_m = merge ( array , start , mid , end ); 54 | 55 | return count_l + count_r + count_m; 56 | } 57 | 58 | int main ( int argc , char * argv[] ) { 59 | 60 | int t , n , i; 61 | int *array; 62 | 63 | scanf("%d" , &t); 64 | 65 | while ( t-- ) { 66 | 67 | scanf("%d" , &n); 68 | array = (int *) malloc ( n * sizeof(int) ); 69 | 70 | for ( i = 0 ; i < n ; i++ ) { 71 | scanf("%d" , &array[i]); 72 | } 73 | printf("%d\n" , count_inv( array , 0 , n-1 ) ); 74 | 75 | free(array); 76 | } 77 | 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /APSAssignment/Assignment1/a1q5.c: -------------------------------------------------------------------------------- 1 | /* 2 | Kabeer has a diamond rod which is “n” meter long. He lost a bet with Pankaj so he is 3 | required to pay 1 meter of that rod to Pankaj everyday for “n” days. As cutting a rod is 4 | very difficult and Kabeer is very lazy, he wants to minimize his efforts for cutting the rod. 5 | Kabeer realized that it was not necessary to cut exactly 1m rod everyday. For example, on 6 | the third day, he can give a 3 meter rod and can have 2 meter rod back which pankaj has 7 | already taken from him. Can you help your TA to minimize his efforts by telling the 8 | minimum cuts required so that he can pay from 1st to nth day to make his daily payment 9 | of 1m rod. 10 | At the end of nth day he would have given the whole rod to Pankaj. 11 | Input 12 | Input would be a positive integer denoting the length of the rod(as well as no of days). 0 < 13 | n < 1000000. Terminate your program by a 0. 14 | Output 15 | For each “n” print the minimum number of cuts required so that Kabeer can fulfill his bet 16 | by paying 1 meter daily. 17 | Example 18 | Input: 19 | 3 20 | 5 21 | 1 22 | 0 23 | Output: 24 | 1 25 | 2 26 | 0 27 | */ 28 | 29 | #include 30 | #include 31 | 32 | int eval(int); 33 | 34 | int main(int argc , char * argv[]) { 35 | 36 | int n , result; 37 | 38 | while( 1 ) { 39 | scanf("%d" , &n); 40 | if ( n == 0 ) 41 | break; 42 | result = eval(n); 43 | printf("%d\n" , result); 44 | } 45 | 46 | return 0; 47 | } 48 | 49 | int eval(int n) { 50 | int c = 0; 51 | while(n) { 52 | c++; 53 | n = n >> 1; 54 | } 55 | return c-1; 56 | } 57 | -------------------------------------------------------------------------------- /APSLab/Lab7/b.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main ( int argc , char * argv[] ) { 9 | 10 | int t = 1; 11 | 12 | while ( t-- ) { 13 | int n; 14 | cin >> n; 15 | 16 | list node[n]; 17 | int visited[n]; 18 | 19 | int p; 20 | cin >> p; 21 | 22 | int edge = p; 23 | 24 | for ( int i = 0 ; i < n ; i++ ) { 25 | visited[i] = 0; 26 | 27 | /*cout << "Node : " << i << " -> "; 28 | for ( vector::iterator itr = node[i].begin() ; itr != node[i].end() ; itr++ ) { 29 | cout << *itr << " "; 30 | } 31 | cout << endl; 32 | */ 33 | 34 | } 35 | 36 | 37 | while ( p-- ) { 38 | int a , b; 39 | cin >> a >> b; 40 | 41 | node[a-1].push_back(b-1); 42 | node[b-1].push_back(a-1); 43 | } 44 | 45 | 46 | queue q; 47 | int count = 0; 48 | for ( int i = 0 ; i < n ; i++ ) { 49 | 50 | if ( visited[i] == 1 ) 51 | continue; 52 | 53 | visited[i] = 1; 54 | 55 | q.push(i); 56 | 57 | while ( !q.empty() ) { 58 | int t = q.front(); 59 | q.pop(); 60 | 61 | for ( list::iterator itr = node[t].begin() ; itr != node[t].end() ; itr++ ) { 62 | int child = *itr; 63 | 64 | if ( visited[child] == 1 ) 65 | continue; 66 | 67 | visited[child] = 1; 68 | q.push(child); 69 | } 70 | 71 | } 72 | 73 | count ++; 74 | 75 | 76 | } 77 | 78 | if ( count == 1 && n == edge + 1 ) { 79 | cout << "YES" << endl; 80 | } 81 | else { 82 | cout << "NO" << endl; 83 | } 84 | 85 | 86 | } 87 | 88 | return 0; 89 | 90 | } 91 | 92 | -------------------------------------------------------------------------------- /APSAssignment/Assignment3/a3q4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int readline ( char * str ) { 5 | int i = 0; 6 | char ch = 0; 7 | while ( (ch = getchar() ) != '\n' ) { 8 | str[i] = ch; 9 | i++; 10 | } 11 | str[i] = '\0'; 12 | return i; 13 | } 14 | 15 | int main ( int argc , char * argv[] ) { 16 | 17 | int len = 0 , i = 0 ; 18 | char * str = (char *) calloc ( 5010 , sizeof(char) ); 19 | long long int * array = (long long int *) calloc ( 5010 , sizeof(long long int) ); 20 | 21 | char string[3] ; 22 | 23 | len = readline ( str ); 24 | 25 | while ( str[0] != '0' ) { 26 | 27 | if ( len == 1 ) { 28 | printf("1\n"); 29 | len = readline ( str ); 30 | continue; 31 | } 32 | 33 | 34 | array [len] = 1 ; 35 | 36 | for ( i = len-1 ; i >= 0 ; i-- ) { 37 | 38 | if ( i == len-1 ) { 39 | if ( str[i] == '0' ) 40 | array[i] = 0; 41 | else 42 | array[i] = 1; 43 | continue; 44 | } 45 | 46 | if ( str[i] == '0' ) { 47 | array[i] = 0; 48 | continue; 49 | } 50 | 51 | string[0] = str[i]; string[1] = str[i+1]; string[2] = '\0'; 52 | 53 | //printf("Checking for string : %s\n" , string ); 54 | 55 | int number = atoi ( string ); 56 | 57 | if ( number <= 26 ) { 58 | if ( i == len-1 ) 59 | array[i] = array[i+1] + 1; 60 | else 61 | array[i] = array[i+1] + array[i+2]; 62 | } 63 | else { 64 | array[i] = array[i+1] ; 65 | } 66 | 67 | /*int j = 0 ; 68 | for ( j = 0 ; j < len ; j++ ) { 69 | printf("%lld " , array[j]); 70 | } 71 | printf("\n"); 72 | */ 73 | } 74 | printf("%lld\n" , array[0]); 75 | //printf("%lld\n" , f); 76 | len = readline ( str ); 77 | } 78 | 79 | return 0; 80 | } 81 | -------------------------------------------------------------------------------- /APSLab/Lab1/stairs.c: -------------------------------------------------------------------------------- 1 | /* 2 | Rishit didn't prepare any question for the APS lab. So while coming to teaching lab in Nilgiri building when he saw the staircase, he came up with one of his wonderful questions. He can move up either 1 or 2 stairs at a time, as he's afraid of jumping more than 2 stairs at a time. 3 | Given the no of stairs, you should tell Rishit the number of ways he can reach the top. Initially he is standing on ground. 4 | 5 | Constraints: 6 | 7 | 1 <= n <= 1000000 , 1 <= t <= 10 8 | 9 | Input: 10 | 11 | First line contains t, number of testcases. Next t lines contain value of n (number of stairs) 12 | 13 | Output: 14 | 15 | Output the result for test case.(Number of ways you can reach top of Staircase). Followed by a newline character. 16 | As the result can be large , print ( output mod 1000000007 ) 17 | 18 | Example: 19 | 20 | Input: 21 | 2 22 | 1 23 | 2 24 | Output: 25 | 1 26 | 2 27 | 28 | Timelimit : 1s 29 | */ 30 | 31 | #include 32 | 33 | #define BIG_NUM 1000000007 34 | 35 | long int eval(int); 36 | 37 | int main( int argc , char * argv[] ) { 38 | 39 | long int num = 0 , array[10000] , stair = 0 , i = 0; 40 | 41 | scanf("%ld",&num); 42 | 43 | for(i = 0 ; i < num ; i++ ) { 44 | scanf("%ld",&stair); 45 | array[i] = eval(stair); 46 | } 47 | 48 | for(i = 0 ; i < num ; i++) { 49 | printf("%ld\n",array[i]); 50 | } 51 | 52 | return 0; 53 | } 54 | 55 | long int eval(int n) { 56 | 57 | long int f1 = 1 , f2 = 2 , f = 0; 58 | 59 | if( n == 1 ) 60 | return f1; 61 | if ( n == 2 ) 62 | return f2; 63 | 64 | while( n > 1 ) { 65 | f = (f1 + f2) % BIG_NUM; 66 | f1 = f2 % BIG_NUM; 67 | f2 = f % BIG_NUM; 68 | n--; 69 | } 70 | 71 | return f1; 72 | } 73 | -------------------------------------------------------------------------------- /APSAssignment/Assignment3/a3q2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* 5 | Eg : for 798 6 | 7 -> 1 -> 1 * 3 ^ (3-1) -> 9 7 | 9 -> 2 -> 2 * 3 ^ (2-1) -> 6 8 | 8 -> 2 -> 2 * 3 ^ (1-1) -> 2 9 | Ans = 17 + uptill 100 = 17 + 12 = 29 10 | 11 | For number starting with non lucky digits : 12 | change number to 0000000..... 13 | */ 14 | 15 | int lesser[] = { 0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 , 2 , 2 }; 16 | long long int _power[16]; 17 | 18 | int main ( int argc , char * argv[] ) { 19 | 20 | long long int n; 21 | char num[64]; 22 | 23 | long long int count = 0; 24 | long long int i = 0; 25 | 26 | int lucky = 1; 27 | 28 | _power[0] = 1; 29 | for ( i = 1 ; i < 16 ; i++ ) { 30 | _power[i] = _power[i-1] * 3; 31 | } 32 | 33 | scanf("%lld" , &n ); 34 | 35 | while ( n != 0 ) { 36 | 37 | count = i = 0; 38 | 39 | sprintf(num , "%lld" , n ); 40 | int num_of_digits = strlen( num ); 41 | 42 | count = ( (_power [ num_of_digits ] - 1 ) / 2 ) - 1 ; 43 | 44 | 45 | for ( i = 0 ; num[i] != '\0' ; i++ ) { 46 | if ( num[i] != '3' && num[i] != '7' && num[i] != '9' ) { 47 | break; 48 | } 49 | } 50 | if ( num[i] != '\0' ) { 51 | for ( i = i + 1 ; num[i] != '\0' ; i++ ) { 52 | num[i] = '0'; 53 | } 54 | } 55 | 56 | //printf("Number = %s\n" , num); 57 | 58 | lucky = 1; 59 | 60 | for ( i = 0 ; num[i] != '\0' ; i++ ) { 61 | 62 | count += ( lesser[num[i] - '0'] * _power [num_of_digits - 1 - i] ); 63 | if ( num[i] != '3' && num[i] != '7' && num[i] != '9' ) { 64 | lucky = 0; 65 | } 66 | } 67 | 68 | if ( lucky == 1 ) { 69 | count++; 70 | } 71 | 72 | printf("%lld\n" , count); 73 | scanf("%lld" , &n ); 74 | 75 | } 76 | 77 | return 0; 78 | } 79 | -------------------------------------------------------------------------------- /APSAssignment/Assignment1/a1q2.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Write a program to calculate the sum of digits of the expression 2^X where x is between 1 3 | * and 1000. The value of X is given as input. For example, 2^7=128, sum of digits=1+2+8=11. 4 | * Example 5 | * INPUT: 6 | * 7 7 | * 4 8 | * -1 9 | * OUTPUT: 10 | * 11 11 | * 7 12 | */ 13 | 14 | /* 15 | Store first base number in an array and then do operation on it . at the end sum all the numbers. 16 | i.e. implement myinteger with multiplication. 17 | */ 18 | 19 | #include 20 | #include 21 | 22 | long int sodIn2x(int); 23 | //void printDigits(char *); 24 | 25 | int main( int argc , char * argv[] ) { 26 | 27 | int n = 0; 28 | 29 | while( 1 ) { 30 | scanf("%d", &n); 31 | if( n == -1 ) 32 | break; 33 | 34 | printf("%ld\n" , sodIn2x(n)); 35 | 36 | } 37 | 38 | return 0; 39 | } 40 | 41 | long int sodIn2x(int n) { 42 | 43 | long int sum = 0; 44 | int i = 0; 45 | char carry = '0' , temp; 46 | char * digits = (char *) malloc ( 17000 * sizeof(char) ); 47 | char * ptr = digits; 48 | 49 | digits[0] = '2'; 50 | digits[1] = 'a'; 51 | 52 | for( i = 1 ; i < n ; i++ ) { 53 | 54 | ptr = digits; 55 | 56 | while( *ptr != 'a' ) { 57 | 58 | temp = *ptr; 59 | 60 | (*ptr) = ((2 * ((temp) - '0') + (carry - '0')) % 10) + '0'; 61 | (carry) = ((2 * ((temp) - '0') + (carry - '0')) / 10) + '0'; 62 | 63 | ptr++; 64 | } 65 | if( carry == '1' ) { 66 | *ptr = '1'; 67 | ptr ++; 68 | *ptr = 'a'; 69 | carry = '0'; 70 | } 71 | 72 | } 73 | 74 | ptr = digits; 75 | while( (*ptr) != 'a' ) 76 | sum += (*ptr++)-'0'; 77 | 78 | //printDigits(digits); 79 | free(digits); 80 | 81 | return sum; 82 | } 83 | 84 | /*void printDigits ( char *str ) { 85 | int i = 0; 86 | printf("Array : "); 87 | while ( 1 ) { 88 | if(str[i] == 'a') { 89 | printf("\n"); 90 | return; 91 | } 92 | printf("%c" , str[i]); 93 | i++; 94 | } 95 | }*/ 96 | -------------------------------------------------------------------------------- /APSLab/Lab2/hex_to_bin.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main( int argc , char * argv[] ) { 5 | 6 | char ch = 'a'; 7 | int n = 0 , i = 0; 8 | int flag = 0; 9 | scanf("%d\n" , &n); 10 | while( n-- ) { 11 | i = 0; 12 | flag = 0; 13 | while((ch = getchar()) != '\n') { 14 | //printf("ch = %d\n" , ch); 15 | 16 | /*while( (ch = getchar()) == '0' );*/ 17 | //printf("AAYA %d" , ch); 18 | if( i == 0 && ch == '0' ) 19 | continue; 20 | else 21 | flag = 1; 22 | 23 | switch(ch) { 24 | //printf("CASE %d \n" , ch); 25 | case 'A': 26 | printf("1010"); 27 | break; 28 | case 'B': 29 | printf("1011"); 30 | break; 31 | case 'C': 32 | printf("1100"); 33 | break; 34 | case 'D': 35 | printf("1101"); 36 | break; 37 | case 'E': 38 | printf("1110"); 39 | break; 40 | case 'F': 41 | printf("1111"); 42 | break; 43 | case '0': 44 | if(i != 0) { 45 | printf("0000"); 46 | } 47 | else { 48 | printf("0"); 49 | } 50 | break; 51 | case '1': 52 | //printf("1 ki case me \n"); 53 | if(i != 0)printf("0001"); else printf("1"); 54 | break; 55 | case '2': 56 | if(i != 0)printf("0010"); else printf("10"); 57 | break; 58 | case '3': 59 | if(i != 0)printf("0011"); else printf("11"); 60 | break; 61 | case '4': 62 | if(i != 0)printf("0100"); else printf("100"); 63 | break; 64 | case '5': 65 | if(i != 0)printf("0101"); else printf("101"); 66 | break; 67 | case '6': 68 | if(i != 0)printf("0110"); else printf("110"); 69 | break; 70 | case '7': 71 | if(i != 0)printf("0111"); else printf("111"); 72 | break; 73 | case '8': 74 | printf("1000"); 75 | break; 76 | case '9': 77 | printf("1001"); 78 | break; 79 | } 80 | if(flag == 1) 81 | i=1; 82 | } 83 | if(i == 0) 84 | printf("0"); 85 | printf("\n"); 86 | } 87 | 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /APSLab/Lab3/c.c: -------------------------------------------------------------------------------- 1 | /* 2 | Dictionary 3 | 4 | Its time to make our own dictionary with 5 letters only.These letters are k,u,m,a,r. 5 | But,this dictionary is different from general english one. 6 | In this 'k' comes before 'u','u' comes before 'm', 'm' comes before 'a', and 'a' comes before 'r'.T 7 | 8 | Thus according to our dictionary 9 | 1st word is k 10 | 2nd word is u 11 | 3rd word is m 12 | 4th word is a 13 | 5th word is r 14 | 6th word is kk 15 | 7th word is ku 16 | 17 | and so on.... 18 | 19 | 20 | You will be given a value of n(1<=n<=10^18).Write the nth word in our dictionary 21 | 22 | 23 | Input Specification: 24 | 25 | First line will contain the number of test cases (t<=100) Next t lines will contain a value n (1<=n<=10^18) 26 | 27 | 28 | Output Specification: 29 | 30 | For each case print the nth word of our dictionary on new line. 31 | 32 | 33 | Sample Input: 34 | 35 | 36 | Input : 37 | 38 | 5 39 | 1 40 | 2 41 | 3 42 | 10 43 | 31 44 | 45 | Sample Output: 46 | 47 | 48 | 49 | Output 1 50 | 51 | k 52 | u 53 | m 54 | kr 55 | kkk 56 | 57 | 58 | Timelimit: 1s 59 | */ 60 | #include 61 | 62 | int main( int argc , char * argv[] ) { 63 | 64 | int t , b , j; 65 | long long int n , i; 66 | int array[100000]; 67 | 68 | scanf("%d" , &t); 69 | 70 | while( t -- ) { 71 | i = 0; 72 | scanf("%lld" , &n); 73 | if( n == 0 ) { 74 | array[i++] = 1; 75 | } 76 | while ( n != 0 ) { 77 | b = n % 5; 78 | if( b == 0 ) { 79 | array[i++] = 5; 80 | n /= 5; 81 | n--; 82 | continue; 83 | } 84 | else { 85 | array[i++] = b; 86 | } 87 | n /= 5; 88 | } 89 | 90 | for( j = i-1 ; j >=0 ; j-- ) { 91 | if( array[j] == 1 ) 92 | printf("k"); 93 | if( array[j] == 2 ) 94 | printf("u"); 95 | if( array[j] == 3 ) 96 | printf("m"); 97 | if( array[j] == 4 ) 98 | printf("a"); 99 | if( array[j] == 5 ) 100 | printf("r"); 101 | } 102 | printf("\n"); 103 | 104 | } 105 | 106 | return 0; 107 | } 108 | -------------------------------------------------------------------------------- /APSLab/Lab4/2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Game of Thrones 3 | 4 | Bran Stark wants Hodor to learn problem solving. The first problem he gave is as following 5 | 6 | "Given an array of numbers print the next greater number for every number. 7 | The Next greater number for a number n is the first greater number on the right side of n in array. 8 | Numbers for which no greater number exist, consider next greater number as -1." 9 | 10 | We all know that Hodor knows nothing except calling his own name. You need to help him to solve this problem. 11 | 12 | INPUT 13 | The first line of the input contains an integer N denoting the number of elements in the given array. 14 | The second line contains N space-separated integers A1, A2, ..., AN denoting the given array. 15 | 16 | OUTPUT 17 | Print the array with each number replaced by its next greater number. 18 | 19 | CONSTRAINTS 20 | 1 ≤ N ≤ 10^6 21 | 0≤ Ai ≤ 10^8 22 | 23 | SAMPLE 24 | 25 | Input 26 | 5 27 | 10 4 15 6 21 28 | Output 29 | 15 15 21 21 -1 \n 30 | 31 | Input 32 | 7 33 | 3 0 8 8 5 29 12 34 | Output 35 | 8 8 29 29 29 -1 -1 \n 36 | 37 | Timelimit: 1s 38 | */ 39 | 40 | #include 41 | #include 42 | #include 43 | #include 44 | 45 | using namespace std; 46 | 47 | int array[1000000]; 48 | struct node { 49 | int val; 50 | int index; 51 | }; 52 | 53 | int main( int argc , char *argv[] ) { 54 | 55 | int t = 0; 56 | int i , temp; 57 | 58 | stack s; 59 | 60 | scanf("%d" , &t); 61 | 62 | scanf("%d" , &temp); 63 | struct node n; 64 | n.val = temp; 65 | n.index = 0; 66 | s.push(n); 67 | 68 | for( i = 1 ; i < t ; i++ ) { 69 | scanf("%d" , &temp); 70 | 71 | struct node a; 72 | a.val = temp; 73 | a.index = i; 74 | 75 | if( !s.empty() ) { 76 | while( s.top().val < temp ) { 77 | array[s.top().index] = temp; 78 | 79 | s.pop(); 80 | if( s.empty() ) 81 | break; 82 | } 83 | } 84 | s.push(a); 85 | 86 | } 87 | 88 | while ( !s.empty() ) { 89 | array[s.top().index] = -1; 90 | s.pop(); 91 | } 92 | for( i = 0 ; i < t ; i++ ) { 93 | printf("%d " , array[i]); 94 | } 95 | printf("\n"); 96 | return 0; 97 | } 98 | -------------------------------------------------------------------------------- /APSLab/Lab6/a.c: -------------------------------------------------------------------------------- 1 | /* 2 | PostOrder from Preorder 3 | Given a Preorder traversal of a Binary Search Tree of size N, Print the Kth element in Postorder of the same. 4 | You will be given Q queries of input K and you have to print Kth element in Postorder of the BST. 5 | 6 | Input: 7 | First Line contains N, number of nodes in a tree (1<=N<=300000). 8 | Second line will have N INTEGERS separated by space which represents Preorder traversal of the BST. 9 | Third Line contains Q, number of queries(1<=Q<=10). 10 | Fourth line would have Q integers separated by space representing K(1<=K<=N). 11 | 12 | Output: 13 | Q lines each with printed Kth element in Postorder traversal Of BST. 14 | 15 | Sample test case: 16 | Input: 17 | 7 18 | 5 3 2 4 8 7 9 19 | 3 20 | 1 3 5 21 | Output: 22 | 2 23 | 3 24 | 9 25 | 26 | Explaination: 27 | Postorder traversal for the the BST would be 2 4 3 7 9 8 5. 28 | So for K=1 => ans=2 , K=3 => ans=3, K=5 => ans=9. 29 | */ 30 | 31 | #include 32 | #include 33 | 34 | struct tree_el { 35 | int val; 36 | struct tree_el * right, * left; 37 | }; 38 | 39 | typedef struct tree_el node; 40 | 41 | void insert(node ** tree, node * item) { 42 | if(!(*tree)) { 43 | *tree = item; 44 | return; 45 | } 46 | if(item->val<(*tree)->val) 47 | insert(&(*tree)->left, item); 48 | else if(item->val>(*tree)->val) 49 | insert(&(*tree)->right, item); 50 | } 51 | int k = 0; 52 | void printout(node * tree , int * array) { 53 | if(tree->left) printout(tree->left , array); 54 | if(tree->right) printout(tree->right , array); 55 | array[k++] = tree->val; 56 | } 57 | 58 | int main() { 59 | node * curr, * root; 60 | int i; 61 | int n; 62 | 63 | scanf("%d" , &n); 64 | 65 | root = NULL; 66 | 67 | int a; 68 | for(i=1;i<=n;i++) { 69 | scanf("%d" , &a); 70 | curr = (node *)malloc(sizeof(node)); 71 | curr->left = curr->right = NULL; 72 | curr->val = a; 73 | insert(&root, curr); 74 | } 75 | int * array = (int *) malloc ( n * sizeof(int) ); 76 | printout(root , array); 77 | scanf("%d" , &n); 78 | 79 | for ( i = 0 ; i < n ; i++ ) { 80 | scanf("%d" , &a); 81 | printf("%d\n" , array[a-1]); 82 | } 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /APSAssignment/Assignment3/a3q3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int readline ( char *str , int * all_9) { 4 | int i = 1; 5 | char ch; 6 | while ( ( ch = getchar() ) != '\n' ) { 7 | str[i] = ch; 8 | if ( str[i] != '9' ) 9 | *all_9 = 0; 10 | i++; 11 | } 12 | str[0] = '0'; 13 | str[i] = 0; 14 | return i; 15 | } 16 | 17 | int main ( int argc , char * argv[] ) { 18 | 19 | int t , len = 0 , i = 0 ; 20 | char array[1000010]; 21 | int carry = 0; 22 | int a , b; 23 | int all_9 = 1; 24 | 25 | scanf("%d" , &t); 26 | getchar(); 27 | 28 | while ( t-- ) { 29 | 30 | len = readline ( array , &all_9 ); 31 | 32 | if ( all_9 == 1 ) { 33 | printf("1"); 34 | for ( i = 1 ; i < len-1 ; i++ ) { 35 | printf("0"); 36 | } 37 | printf("1\n"); 38 | continue; 39 | } 40 | 41 | carry = 0; 42 | 43 | int mid = ((len+1)/2) - 1; 44 | 45 | a = mid; 46 | b = len - a; 47 | 48 | while ( a > 0 && b < len && array[a] == array[b] ) { 49 | a--; 50 | b++; 51 | } 52 | 53 | int flag = 0; 54 | if ( (array[a]-'0') < (array[b]-'0') || a == 0) { 55 | flag = 1; 56 | } 57 | 58 | for ( i = mid ; i > 0 ; i-- ) { 59 | a = i; 60 | b = len - i; 61 | array[b] = array[a]; 62 | } 63 | 64 | if ( flag == 1 ) { 65 | carry = 1; 66 | if ( (len-1) & 1 ) { 67 | array[mid+1] = (array[mid+1] - '0') + carry + '0'; 68 | carry = (array[mid+1] - '0') / 10; 69 | array[mid+1] = ((array[mid+1] - '0') % 10) + '0'; 70 | } 71 | for ( i = mid ; i > 0 ; i -- ) { 72 | int _a = i; 73 | int _b = len - i; 74 | array[_a] = (array[_a] - '0') + carry + '0'; 75 | carry = (array[_a] - '0') / 10; 76 | array[_a] = ((array[_a] - '0') % 10) + '0'; 77 | array[_b] = array[_a]; 78 | if ( carry == 0 ) 79 | break; 80 | } 81 | } 82 | 83 | if ( carry == 1 ) { 84 | array[0] = '1'; 85 | array[len - 1 ] = '1'; 86 | } 87 | 88 | array[len] = '\0'; 89 | if ( array[0] != '0' ) 90 | printf("%s\n" , array ); 91 | else 92 | printf("%s\n" , &array[1]); 93 | //for ( i = 1 ; i < len ; i++ ) { 94 | // printf("%d" , array[i]); 95 | //} 96 | //printf("\n"); 97 | } 98 | 99 | return 0; 100 | } 101 | -------------------------------------------------------------------------------- /APSLab/Lab3/b.c: -------------------------------------------------------------------------------- 1 | /*Matrix sum 2 | 3 | You will be given a matrix A of size m*n . 4 | 5 | Your task is to find sum of all the numbers(which can be negative as well) between two corners(north-west and south-east). 6 | 7 | Input Specification: 8 | 9 | 3 integers in first line : m,n,t 10 | where m*n is the size of matrix 11 | and t is the number of test cases. 12 | 13 | Next m lines contains n integers(which can be negative). 14 | 15 | After that,next t line will follow 4 integers x1,y1,x2,y2 where (x1,y1) is the coordinates of north-west and (x2,y2) is the coordinates of south-east corners. 16 | Output Specification: 17 | 18 | One line denoting the sum of the numbers between the two given corners. 19 | 20 | Constraints: 21 | 22 | 1<=m,n<=1000 23 | 24 | 1<=t<=5000 25 | 26 | 1<=x1<=x2<=1000 27 | 1<=y1<=y2<=1000 28 | -1000<=a[i][j]<=1000 29 | 30 | Sample Input: 31 | 32 | 33 | Input 1 34 | 35 | 3 3 1 36 | 1 2 3 37 | 4 5 6 38 | 7 8 9 39 | 1 1 2 2 40 | 41 | Sample Output: 42 | 43 | 44 | 45 | Output 1 46 | 47 | 12\n 48 | 49 | Timelimit: 1s 50 | */ 51 | #include 52 | #include 53 | 54 | int main( int argc , char * argv[] ) { 55 | 56 | int m , n , t , test; 57 | int i , j; 58 | int ** array = NULL; 59 | int a,b,c,d; 60 | int total = 0; 61 | 62 | scanf("%d%d" , &m , &n); 63 | 64 | array = (int **) calloc( m , sizeof(int*) ); 65 | 66 | for( i =0 ; i < m ;i++ ) { 67 | array[i] = (int *) malloc( n * sizeof(int)); 68 | } 69 | 70 | scanf("%d" , &test); 71 | 72 | for( i = 0 ; i < m ; i++) { 73 | total = 0; 74 | for( j = 0 ; j < n ; j++ ) { 75 | scanf("%d" , &t); 76 | total += t; 77 | array[i][j] = total; 78 | } 79 | } 80 | 81 | while( test-- ) { 82 | scanf("%d%d%d%d" , &a , &b , &c , &d); 83 | a--;b--;c--;d--; 84 | if ( d > n ) 85 | d = n - 1; 86 | if( c > m ) 87 | c = m - 1; 88 | if( a < 0 ) 89 | a = 0; 90 | if( b < 0 ) 91 | b = 0; 92 | total = 0; 93 | for( i = a ; i <= c ; i++ ) { 94 | if( b > 0 ) 95 | total += (array[i][d] - array[i][b-1]); 96 | else 97 | total += (array[i][d]); 98 | } 99 | 100 | printf("%d\n" , total); 101 | 102 | } 103 | 104 | return 0; 105 | } 106 | -------------------------------------------------------------------------------- /APSAssignment/Assignment2/a2q1.c: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array A, you now know how to find a Sub-Array sum i.e., A[i]+ A[i+1] + ... + A[j]. A 3 | sub-array is defined by its starting index i and its ending index j ( 0 <= i <= j < N ). Can you find 4 | the maximum sum of a sub-array. 5 | Input: 6 | First line contains N, number of elements in the array ( 1 <= N <= 100000 ). 7 | Second line contains N integers in range [-10^4,10^4], separated by spaces. 8 | Output: 9 | Print the maximum sum possible for a sub-array of A. (Followed by a '\n' which is just for a 10 | newline, no need to display) 11 | Sample Cases: 12 | Input: 13 | 4 14 | 1 -2 1 2 15 | Output: 16 | 3\n 17 | Explanation: 18 | Sum(0..0) = 1, Sum(0..1) = -1, Sum(0..2) = 0, Sum(0..3) = 2, . . . , Sum(2..3) = 3, . . . , Sum(3,3) = 2 19 | Among all possible SubArray sums, Sum(2..3) = 3 has the maximum value. 20 | */ 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | int main( int argc , char * argv[] ) { 27 | 28 | /* 29 | Case 1 : All numbers are negative 30 | Case 2 : All numbers are positive 31 | Case 3 : There is a mixture of positive and negative 32 | */ 33 | 34 | int n; 35 | int * array = NULL; 36 | 37 | int i = 0 , flag_p = 1 , flag_n = 1 , sum = 0 , max = INT_MIN; 38 | 39 | scanf("%d" , &n); 40 | 41 | array = (int *) malloc ( n * sizeof(int) ); 42 | 43 | for( i = 0 ; i < n ; i++ ) { 44 | scanf("%d" , &array[i]); 45 | 46 | if( array[i] < 0 ) { 47 | flag_p = 0; 48 | } 49 | else if( array[i] >= 0 ) { 50 | flag_n = 0; 51 | } 52 | 53 | sum += array[i]; 54 | if( array[i] > max ) 55 | max = array[i]; 56 | 57 | } 58 | 59 | if( flag_p == 1 ) { 60 | /* All numbers are positive */ 61 | printf("%d\n" , sum); 62 | return 0; 63 | } 64 | else if( flag_n == 1 ) { 65 | /* All numbers are negative */ 66 | printf("%d\n" , max); 67 | //sum = min; 68 | return 0; 69 | } 70 | else { 71 | /* The numbers are mixed */ 72 | /* sum[i] -> sum till ith element */ 73 | /* array[i] = max( sum[i-1] + array[i] , array[i] ); */ 74 | 75 | sum = 0; 76 | int end = 0 , cur_sum = 0; 77 | 78 | for( end = 0 ; end < n ; end++ ) { 79 | cur_sum += array[end]; 80 | if( cur_sum > sum ) { 81 | sum = cur_sum; 82 | } 83 | if( cur_sum < 0 ) { 84 | cur_sum = 0; 85 | } 86 | } 87 | printf("%d\n" , sum); 88 | } 89 | 90 | 91 | return 0; 92 | } 93 | -------------------------------------------------------------------------------- /APSAssignment/Assignment5/b.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | struct node { 8 | long long int cost; 9 | int d; 10 | }; 11 | 12 | list nodes[50010]; 13 | int visited[50010]; 14 | long long int dist[50010]; 15 | 16 | int last_node ( int n , int source ) { 17 | 18 | queue q; 19 | 20 | visited[source] = 1; 21 | dist[source] = 0; 22 | 23 | q.push(source); 24 | 25 | while ( !q.empty() ) { 26 | int parent = q.front(); 27 | q.pop(); 28 | 29 | for ( list::iterator itr = nodes[parent].begin() ; itr != nodes[parent].end() ; itr++ ) { 30 | 31 | int child = (*itr)->d; 32 | 33 | if ( visited[child] == 1 ) 34 | continue; 35 | 36 | long long int t = dist[parent] + (*itr)->cost; 37 | 38 | if ( t > dist[child] ) 39 | dist[child] = t; 40 | 41 | visited[child] = 1; 42 | q.push(child); 43 | } 44 | 45 | } 46 | 47 | long long int max = -1; 48 | int last_node = 0; 49 | for ( int i = 0 ; i < n ; i++) { 50 | 51 | //cout << "distance node " << i << " : " << dist[i] << endl; 52 | if ( dist[i] > max ) { 53 | last_node = i; 54 | max = dist[i]; 55 | } 56 | } 57 | 58 | return last_node; 59 | } 60 | 61 | int main ( int argc , char * argv[] ) { 62 | 63 | int t; 64 | 65 | cin >> t; 66 | 67 | while ( t-- ) { 68 | int n; 69 | cin >> n; 70 | 71 | for ( int i = 0 ; i < n ; i++ ) { 72 | dist[i] = 0; 73 | visited[i] = 0; 74 | nodes[i].clear(); 75 | } 76 | 77 | for( int i = 0 ; i < n-1 ; i++ ) { 78 | long long int a , b , c; 79 | cin >> a >> b >> c; 80 | 81 | struct node * n = new struct node; 82 | n -> d = b-1; 83 | n -> cost = c; 84 | nodes[a-1].push_back(n); 85 | 86 | struct node * m = new struct node; 87 | m -> d = a-1; 88 | m -> cost = c; 89 | nodes[b-1].push_back(m); 90 | 91 | } 92 | 93 | /*for ( int i = 0 ; i < n ; i++ ) { 94 | cout << "Node " << i << " : "; 95 | for ( list::iterator itr = nodes[i].begin() ; itr != nodes[i].end() ; itr++ ) { 96 | cout << (*itr)->d << " "<< (*itr) -> cost << " ---- "; 97 | } 98 | cout << endl; 99 | }*/ 100 | 101 | int last = last_node( n , 0 ); 102 | 103 | for ( int i = 0 ; i < n ; i++ ) { 104 | visited[i] = 0; 105 | dist[i] = 0; 106 | } 107 | 108 | last = last_node( n , last ); 109 | 110 | cout << dist[last] << endl; 111 | 112 | } 113 | 114 | return 0; 115 | } 116 | -------------------------------------------------------------------------------- /APSAssignment/Assignment2/a2q6.c: -------------------------------------------------------------------------------- 1 | /*The Last Digit 2 | 3 | 4 | Its time for some maths fun. 5 | Given two integers p(0<=p<=20) and q(0<=q<=2000000000). 6 | Find the last digit of p^q. 7 | Input Specification: 8 | First line contains T i.e number of test cases ( 1 <= T<= 30 ) 9 | Next T lines contains p and q seperated by space. 10 | 11 | 12 | Output Specification: 13 | 14 | The last digit of p^q. 15 | 16 | Sample Input: 17 | 18 | 19 | 2 20 | 8 2 21 | 10 3 22 | Sample Output: 23 | 24 | 25 | 4\n 26 | 0\n 27 | (\n in the above output represents newline character. This is not an explicit "\n" ) 28 | Timelimit: 1s 29 | */ 30 | 31 | 32 | #include 33 | #include 34 | 35 | struct node { 36 | int values[4]; 37 | int size; 38 | }; 39 | 40 | void populate(struct node *); 41 | 42 | int main( int argc , char * argv[] ) { 43 | 44 | struct node array[10]; 45 | int p , a , b; 46 | long long int q = 0; 47 | int n; 48 | 49 | populate(array); 50 | scanf("%d" , &n ); 51 | 52 | while( n-- ) { 53 | scanf("%d%lld" , &p , &q ); 54 | a = p % 10; 55 | b = q % array[a].size; 56 | 57 | if( q == 0) { 58 | printf("1\n"); 59 | continue; 60 | } 61 | 62 | if( b == 0 ) 63 | printf("%d\n" , array[a].values[array[a].size - 1]); 64 | else 65 | printf("%d\n" , array[a].values[b - 1]); 66 | 67 | } 68 | 69 | return 0; 70 | } 71 | 72 | void populate(struct node * array) { 73 | 74 | array[0].values[0] = 0; 75 | array[0].size = 1; 76 | 77 | array[1].values[0] = 1; 78 | array[1].size = 1; 79 | 80 | array[2].values[0] = 2; 81 | array[2].values[1] = 4; 82 | array[2].values[2] = 8; 83 | array[2].values[3] = 6; 84 | array[2].size = 4; 85 | 86 | array[3].values[0] = 3; 87 | array[3].values[1] = 9; 88 | array[3].values[2] = 7; 89 | array[3].values[3] = 1; 90 | array[3].size = 4; 91 | 92 | 93 | array[4].values[0] = 4; 94 | array[4].values[1] = 6; 95 | array[4].size = 2; 96 | 97 | array[5].values[0] = 5; 98 | array[5].size = 1; 99 | 100 | 101 | array[6].values[0] = 6; 102 | array[6].size = 1; 103 | 104 | 105 | array[7].values[0] = 7; 106 | array[7].values[1] = 9; 107 | array[7].values[2] = 3; 108 | array[7].values[3] = 1; 109 | array[7].size = 4; 110 | 111 | 112 | array[8].values[0] = 8; 113 | array[8].values[1] = 4; 114 | array[8].values[2] = 2; 115 | array[8].values[3] = 6; 116 | array[8].size = 4; 117 | 118 | 119 | array[9].values[0] = 9; 120 | array[9].values[1] = 1; 121 | array[9].size = 2; 122 | } 123 | 124 | 125 | -------------------------------------------------------------------------------- /APSAssignment/Assignment4/c.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define LEFT 0 6 | #define RIGHT 1 7 | 8 | using namespace std; 9 | 10 | struct node { 11 | int val; 12 | struct node * child[2]; 13 | }; 14 | 15 | struct node * create_node ( int val ) { 16 | struct node * temp = new struct node; 17 | temp -> val = val; 18 | temp -> child[LEFT] = NULL; 19 | temp -> child[RIGHT] = NULL; 20 | 21 | return temp; 22 | } 23 | 24 | int index_preorder = 0; 25 | 26 | struct node * construct_tree ( vector inorder , vector preorder , int start , int end ) { 27 | 28 | if ( start > end ) { 29 | return NULL; 30 | } 31 | 32 | struct node * t = create_node ( preorder[index_preorder++] ); 33 | 34 | if ( start == end ) { 35 | return t; 36 | } 37 | 38 | int search_index = distance ( inorder.begin() , find ( inorder.begin() , inorder.end() , t->val )); 39 | 40 | t -> child[LEFT] = construct_tree ( inorder , preorder , start , search_index - 1 ); 41 | t -> child[RIGHT] = construct_tree ( inorder , preorder , search_index + 1 , end ); 42 | 43 | return t; 44 | } 45 | 46 | void preorder_r ( struct node * root ) { 47 | if ( root == NULL ) 48 | return; 49 | cout << root->val << " "; 50 | preorder_r ( root->child[LEFT] ); 51 | preorder_r ( root->child[RIGHT] ); 52 | } 53 | 54 | void inorder_r ( struct node * root ) { 55 | if ( root == NULL ) 56 | return; 57 | inorder_r ( root->child[LEFT] ); 58 | cout << root->val << " "; 59 | inorder_r ( root->child[RIGHT] ); 60 | } 61 | 62 | int diameter_r ( struct node * root , int * height ) { 63 | 64 | int left_height = 0 , right_height = 0; 65 | int left_diameter = 0 , right_diameter = 0; 66 | 67 | if ( root == NULL ) { 68 | *height = 0; 69 | return 0; 70 | } 71 | 72 | left_diameter = diameter_r ( root-> child[LEFT] , &left_height ); 73 | right_diameter = diameter_r ( root-> child[RIGHT] , &right_height ); 74 | 75 | *height = max ( left_height , right_height ) + 1; 76 | 77 | return max( left_height + right_height + 1 , max ( left_diameter , right_diameter ) ) ; 78 | } 79 | 80 | int main( int argc , char * argv[] ) { 81 | 82 | int n , a , height; 83 | vector preorder, inorder; 84 | 85 | cin >> n; 86 | 87 | for ( int i = 0 ; i < n ; i++ ) { 88 | cin >> a; 89 | preorder.push_back( a ); 90 | inorder.push_back( a ); 91 | } 92 | 93 | sort( inorder.begin() , inorder.end() ); 94 | 95 | struct node * root = construct_tree ( inorder , preorder , 0 , n - 1 ); 96 | 97 | cout << diameter_r( root , &height ) - 1 << endl; 98 | 99 | return 0; 100 | } 101 | -------------------------------------------------------------------------------- /APSLab/Lab3/a.c: -------------------------------------------------------------------------------- 1 | /*Light Bulb 2 | 3 | Given a grid of lights(R X C), our goal is to turn on all the lights. 1 = on, 0 = off. 4 | Flipping a blub means, changing its state ( from on to off, or off to on ). 5 | If we flip a bulb(i,j), all the bulbs in a matrix consisting of rows [0..i] and columns [0..j] also get flipped. 6 | 7 | eg: 8 | 0 1 1 0 9 | 0 0 * 1 10 | 0 1 0 1 11 | 1 1 0 1 12 | 13 | lets say bulb (1,2), marked as * is actually 0 and if you flip it the grid becomes 14 | 15 | 1 0 0 0 16 | 1 1 1 1 17 | 0 1 0 1 18 | 1 1 0 1 19 | 20 | (i.e., all (0,0) to (i,j) flipped ). 21 | 22 | Objective : You have to turn on all lights using minimum flips. 23 | Input Specification: 24 | 25 | First line contains R(1<=R<=100) and C(1<=C<=100) 26 | following line contains the matrix(R X C) input 27 | 28 | Output Specification: 29 | 30 | Print the answer in a single line, followed by a '\n' ( just print a new line, no need to display '\n' ) 31 | 32 | Constraints: 33 | 34 | 1 <= R,C <= 100 35 | 36 | 37 | Sample Input: 38 | 39 | 40 | Input 1 41 | 42 | 2 3 43 | 1 1 1 44 | 1 0 1 45 | 46 | Input 2 47 | 48 | 3 3 49 | 1 1 1 50 | 0 0 0 51 | 0 0 0 52 | Sample Output: 53 | 54 | 55 | 56 | Output 1 57 | 58 | 4\n 59 | 60 | Output 2 61 | 62 | 2\n 63 | 64 | Timelimit: 1s 65 | */ 66 | #include 67 | #include 68 | 69 | int main ( int argc, char * argv[] ) { 70 | 71 | int m , n , flag , i , j; 72 | int count = 0; 73 | int k , l; 74 | int **array; 75 | 76 | scanf("%d%d" , &m , &n); 77 | 78 | array = (int **) malloc ( m * sizeof(int *) ); 79 | 80 | for( i = 0 ; i < m ; i++ ) { 81 | array[i] = (int *) malloc( n * sizeof(int) ); 82 | } 83 | 84 | for( i = 0 ; i < m ; i++ ) { 85 | for ( j = 0 ; j < n ; j++) { 86 | scanf("%d" , &array[i][j]); 87 | } 88 | } 89 | flag = 0; 90 | count = 0; 91 | for( i = m-1 ; i>= 0 ; i-- ) { 92 | if( flag == 1 ) { 93 | for( j = 0 ; j < n ; j++ ) { 94 | if(array[i][j] == 0) { 95 | count++; 96 | for( k = 0 ; k <= i ; k++ ) { 97 | for(l = 0 ; l <= j ;l++) { 98 | if(array[k][l] == 0 )array[k][l] = 1; 99 | else array[k][l] = 0; 100 | } 101 | } 102 | } 103 | } 104 | } 105 | else { 106 | for( j = n-1 ; j>=0 ; j-- ) { 107 | if(array[i][j] == 0) { 108 | count++; 109 | for( k = 0 ; k <= i ; k++ ) { 110 | for(l = 0 ; l <= j ;l++) { 111 | if(array[k][l] == 0 )array[k][l] = 1; 112 | else array[k][l] = 0; 113 | } 114 | } 115 | } 116 | } 117 | } 118 | flag = 0; 119 | 120 | 121 | } 122 | printf("%d\n" , count); 123 | 124 | return 0; 125 | } 126 | -------------------------------------------------------------------------------- /APSLab/Lab2/indo-pak-war.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void merge(int a[],int l, int m, int h); 5 | int partition(int a[], int l, int h); 6 | void mergesort(int a[], int l, int h); 7 | 8 | void mergesort(int a[], int l, int h) 9 | { 10 | int m; 11 | if (l >= h) return; 12 | m = partition(a, l, h); 13 | mergesort(a, l, m); 14 | mergesort(a, m+1, h); 15 | merge(a, l, m, h); 16 | } 17 | int partition(int a[], int l, int h) 18 | { 19 | assert(l+1 <= h); 20 | return (l+h)/2; 21 | } 22 | void merge(int a[],int l, int m, int h) 23 | { 24 | int b[10000]; 25 | int i, j, k; 26 | k = l; 27 | i = l; 28 | j = m + 1; 29 | while (i <= m && j <= h) 30 | { 31 | if (a[i] < a[j]) 32 | b[k++] = a[i++]; 33 | else 34 | b[k++]= a[j++]; 35 | } 36 | if (i > m ) 37 | while(j <= h) 38 | b[k++] = a[j++]; 39 | else if (j > h) 40 | while (i <= m) 41 | b[k++] = a[i++]; 42 | k = l; 43 | while(k <= h) 44 | { 45 | a[k] = b[k]; 46 | k++; 47 | } 48 | } 49 | 50 | 51 | int comp(const void * a,const void * b) 52 | { 53 | if (*(int*)a==*(int*)b) 54 | return 0; 55 | else if (*(int*)a < *(int*)b) 56 | return -1; 57 | else 58 | return 1; 59 | } 60 | 61 | int main() 62 | { 63 | int x , y , n; 64 | int a[10000] , i , j; 65 | int b[10000]; 66 | 67 | scanf("%d" , &n); 68 | 69 | while( n-- ) { 70 | scanf("%d" , &x); 71 | for (i = 0; i < x; i++) { 72 | scanf ("%d", &a[i]); 73 | } 74 | scanf("%d" , &y); 75 | for (i = 0; i < y; i++) { 76 | scanf ("%d", &b[i]); 77 | } 78 | 79 | mergesort(a,0,x-1) ; 80 | mergesort(b,0,y-1) ; 81 | 82 | i = 0; j = 0; 83 | 84 | int * inc; 85 | int minimum = abs(a[0]-b[0]); 86 | while (minimum > 0 && (i < (x - 1) || j < (y - 1))) { 87 | 88 | int z; 89 | if ( i == x-1) 90 | { 91 | inc = &j; 92 | z = abs(a[i]-b[j+1]); 93 | } 94 | else if ( j == y -1 ) 95 | { 96 | inc = &i; 97 | z = abs(a[i+1]-b[j]); 98 | } 99 | else 100 | { 101 | int zi = abs(a[i+1]-b[j]); 102 | int zj = abs(a[i]-b[j+1]); 103 | if ( zi < zj) 104 | { 105 | inc = &i; 106 | z = zi; 107 | } 108 | else 109 | { 110 | inc = &j; 111 | z = zj; 112 | } 113 | } 114 | if ( z < minimum) 115 | { 116 | minimum = z; 117 | } 118 | 119 | (*inc)++; 120 | } 121 | printf ("%d\n", minimum); 122 | } 123 | return 0; 124 | 125 | } 126 | -------------------------------------------------------------------------------- /APSAssignment/Assignment2/a2q4.c: -------------------------------------------------------------------------------- 1 | /* 2 | Here is a very easy linear recurrence for you to solve, 3 | F(k) = 2.F(k-1) + 5.F(k-2) + 3 4 | Initial Cases: F(0) = 1, F(1) = 2. 5 | Input: Given n and M 6 | Constraints: 0 <= n <= 10^9 , 2 <= M <= 10^9 7 | Output: find the value of F(n) % M 8 | Sample Case: 9 | 1) 10 | Input: 11 | 4 15 12 | Output: 13 | 2\n 14 | 2) 15 | Input: 16 | 3 40 17 | Output: 18 | 37\n 19 | */ 20 | 21 | #include 22 | #include 23 | #define SIZE 3 24 | 25 | long long int multiply ( long long int ** , long long int ** , long long int ); 26 | 27 | int main( int argc , char * argv[] ) { 28 | long long int m , n , i , sum = 0 ; 29 | long long int ** array1 , **array2; 30 | 31 | scanf("%lld%lld", &n , &m); 32 | 33 | if( n == 0 ) { 34 | printf("1\n"); 35 | return 0; 36 | } 37 | 38 | array1 = (long long int **) malloc ( SIZE * sizeof(long long int *) ); 39 | 40 | for( i = 0 ; i < SIZE ; i++ ) { 41 | array1[i] = (long long int *) malloc ( SIZE * sizeof(long long int)); 42 | } 43 | 44 | array1[0][0] = 2; array1[0][1] = 5; array1[0][2] = 1; array1[1][0] = 1; 45 | array1[1][1] = 0; array1[1][2] = 0; array1[2][0] = 0; array1[2][1] = 0; 46 | array1[2][2] = 1; 47 | 48 | array2 = (long long int **) malloc( SIZE * sizeof(long long int *) ); 49 | for( i = 0 ; i < SIZE ; i++ ) { 50 | array2[i] = (long long int *) malloc ( SIZE * sizeof(long long int) ); 51 | } 52 | 53 | array2[0][0] = 1; array2[0][1] = 0; array2[0][2] = 0; array2[1][0] = 0; 54 | array2[1][1] = 1; array2[1][2] = 0; array2[2][0] = 0; array2[2][1] = 0; 55 | array2[2][2] = 1; 56 | 57 | n--; 58 | 59 | while( n > 0 ) { 60 | 61 | if( n % 2 == 1 ) 62 | multiply( array2 , array1, m ); 63 | 64 | multiply( array1 , array1 , m ); 65 | 66 | n /= 2; 67 | } 68 | 69 | sum = ( array2[0][0] * 2 + array2[0][1] * 1 + array2[0][2] * SIZE) % m; 70 | 71 | printf("%lld\n",sum); 72 | 73 | return 0; 74 | } 75 | 76 | long long int multiply ( long long int ** array2 , long long int ** array1 , long long int m ) { 77 | 78 | long long int ** temp = (long long int **) malloc ( SIZE * sizeof(long long int * ) ); 79 | int i , j , k ; 80 | 81 | for( i = 0 ; i < SIZE ; i++ ) { 82 | temp[i] = (long long int *) malloc ( SIZE * sizeof(long long int)); 83 | } 84 | 85 | for( i = 0 ; i < SIZE ; i++ ) { 86 | for( j = 0 ; j < SIZE ; j++ ) { 87 | 88 | temp[i][j] = 0; 89 | 90 | for( k = 0 ; k < SIZE ; k++ ) { 91 | temp[i][j] += (( array2[i][k] * array1[k][j] ) % m); 92 | } 93 | } 94 | } 95 | 96 | for( i = 0 ; i < SIZE ; i++ ) { 97 | for( j = 0 ; j < SIZE ; j++ ) { 98 | array2[i][j] = temp[i][j]; 99 | } 100 | } 101 | 102 | return 0; 103 | } 104 | -------------------------------------------------------------------------------- /APSAssignment/Assignment1/a1q3.inv_modulo.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define BIG_NUM 1000000007 5 | 6 | long int *factorial; 7 | 8 | long int ncr ( int , int ); 9 | long int getFactorial(int); 10 | long int getInverse(long int); 11 | 12 | int main ( int argc , char * argv[] ) { 13 | 14 | int n , m ; 15 | long int result = 0; 16 | 17 | factorial = (long int *) calloc(1001 , sizeof(long int)); 18 | 19 | factorial[0] = factorial[1] = 1; 20 | 21 | while ( 1 ) { 22 | scanf("%d%d" , &n,&m); 23 | if( n == -1 && m == -1 ) 24 | break; 25 | 26 | if ( n < m ) { 27 | printf("%ld\n" , (long int)0); 28 | continue; 29 | } 30 | 31 | if ( n == m) { 32 | printf("%ld\n" , (long int) 1); 33 | continue; 34 | } 35 | 36 | if ( m == 0) { 37 | printf("%ld\n" , (long int) 1); 38 | continue; 39 | } 40 | 41 | if ( n == 0 ) { 42 | printf("%ld\n" , (long int)0); 43 | continue; 44 | } 45 | 46 | ////printf("For n = %d and r = %d\n" , n , m); 47 | if( m > (n-m) ) { 48 | ////printf("Evaluating: n = %d and r = %d\n" , n , n-m); 49 | result = ncr(n,n-m); 50 | } 51 | else { 52 | ////printf("Evaluating: n = %d and r = %d\n" , n , m); 53 | result = ncr(n,m); 54 | } 55 | 56 | printf("%ld\n", result); 57 | 58 | } 59 | 60 | free(factorial); 61 | return 0; 62 | } 63 | 64 | long int ncr ( int n , int r ) { 65 | 66 | long int _n = 1 ; 67 | long int _r = 1 ; 68 | long int _nr = 1 , _d = 1; 69 | 70 | _n = getFactorial(n); 71 | _r = getFactorial(r); 72 | _nr = getFactorial(n-r); 73 | 74 | _d = getInverse( (_r * _nr)% BIG_NUM ); 75 | 76 | //printf("INverse of %ld is %ld\n", (_r*_nr) % BIG_NUM , _d); 77 | 78 | return (_n * _d)%BIG_NUM; 79 | //return (_n)%BIG_NUM; 80 | 81 | } 82 | 83 | long int getInverse(long int n) { 84 | long int ex = BIG_NUM-2, result = 1; 85 | while (ex > 0) { 86 | if (ex % 2 == 1) { 87 | result = (result*n) % BIG_NUM; 88 | } 89 | n = (n*n) % BIG_NUM; 90 | ex /= 2; 91 | } 92 | return result; 93 | 94 | } 95 | 96 | long int getFactorial(int n) { 97 | 98 | int i = 0; 99 | 100 | //printf("In Factorial : n = %d and factorial(n) = %ld\n", n , factorial[n]); 101 | 102 | if( factorial[n] ) { 103 | return factorial[n]; 104 | } 105 | 106 | //printf("Factorial does not exist for n = %d\n", n); 107 | for( i = n - 1 ; i >= 1 ; i-- ) { 108 | if( factorial[i] != 0 ) 109 | break; 110 | } 111 | //printf("Factorial available for n = %d\n", i); 112 | 113 | for( ; i <= n ; i++) { 114 | factorial[i] = (factorial[i-1] * i) % BIG_NUM; 115 | //printf("Evaluating factorial for n = %d using fact(i-1) = %ld and answer = %ld\n" , i , factorial[i-1] , factorial[i]); 116 | } 117 | 118 | return factorial[n]; 119 | } 120 | -------------------------------------------------------------------------------- /APSLab/LabTest2/d.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | int compar(const void * a , const void * b) { 11 | int x = *(int *)a; 12 | int y = *(int *)b; 13 | return y-x; 14 | } 15 | 16 | int main( int argc , char * argv[] ) { 17 | 18 | int n , m; 19 | 20 | scanf("%d%d" , &n , &m ); 21 | 22 | int * degree = new int[n]; 23 | 24 | memset( degree , 0 , sizeof(int) * n); 25 | 26 | for ( int i = 0 ; i < m ; i++ ) { 27 | int a,b; 28 | scanf("%d%d" , &a , &b); 29 | 30 | degree[a-1] ++; 31 | degree[b-1] ++; 32 | 33 | } 34 | 35 | qsort(degree , n , sizeof(int) , compar); 36 | 37 | if ( degree[0] == n-1 && degree[1] == 1 ) { 38 | /*int not_star = 0; 39 | for ( int i = 1 ; i < n ; i++) { 40 | if ( degree[i] != 1 ) { 41 | not_star = 1; 42 | break; 43 | } 44 | } 45 | if ( not_star == 0 ) { 46 | cout << "star topology\n"; 47 | return 0; 48 | }*/ 49 | cout << "star topology"; 50 | return 0; 51 | } 52 | if ( degree[0] == degree[n-1] && degree[0] == 2 ) { 53 | /*int not_ring = 0; 54 | for ( int i = 0 ; i < n-1 ; i++) { 55 | if ( degree[i] != degree[i+1] ) { 56 | not_ring = 1; 57 | break; 58 | } 59 | } 60 | if ( not_ring == 0 ) { 61 | cout << "ring topology\n"; 62 | return 0; 63 | }*/ 64 | cout << "ring topology"; 65 | return 0; 66 | } 67 | if ( (degree[n-1] == degree[n-2]) && degree[n-1] == 1 && degree[0] == 2 ) { 68 | /*int not_bus = 0; 69 | for ( int i = 0 ; i < n-2 ; i++) { 70 | if ( degree[i] != 2 ) { 71 | not_bus = 1; 72 | break; 73 | } 74 | } 75 | if ( not_bus == 0 ) { 76 | cout << "bus topology\n"; 77 | return 0; 78 | }*/ 79 | cout << "bus topology"; 80 | return 0; 81 | } 82 | 83 | 84 | /*for ( int i = 0 ; i < n ; i++) { 85 | cout << degree[i] << " "; 86 | } 87 | cout << endl; 88 | 89 | return 0;*/ 90 | cout << "unknown topology"; 91 | return 0; 92 | } 93 | -------------------------------------------------------------------------------- /APSLab/Lab1/trip.c: -------------------------------------------------------------------------------- 1 | /* 2 | Trip 3 | 4 | Rakesh took APS class students on a trip. Students were unaware, that on the trip Rakesh plans to give them some of the hardest coding problems, they've ever seen, which he has prepared before-hand. But the mistake Rakesh did, is that number of question in each set isn't equal and that might lead to quarrel as every students would probably want to get least number of questions. So he decides to move questions from one set to another so every set contain same no of question. 5 | 6 | Input specification 7 | 8 | The input consists of several blocks of data. Each block starts with the number of sets of question N(1<= N <=10000) followed by N integers (each less than 1000) in separate lines, giving the number of question in each packet. After the last block of data there is the number -1. 9 | 10 | Output specification 11 | 12 | The output should contain one line with the smallest number of moves for each block of data.(Followed by a newline character) One move consists of taking one question from a set and putting it into another one. If it is not possible to have the same number of questions in each set, output the number -1. 13 | 14 | Example 15 | 16 | Sample Input : 17 | 5 18 | 1 19 | 1 20 | 1 21 | 1 22 | 6 23 | 2 24 | 3 25 | 4 26 | -1 27 | Sample Output : 28 | 4 29 | -1 30 | 31 | 32 | Timelimit: 1s 33 | */ 34 | 35 | #include 36 | 37 | int eval(int , int * ); 38 | 39 | int main( int argc , char * argv[] ) { 40 | 41 | int i , j , num , ele[10000] , output[10000] , totalCase = 0; 42 | 43 | do { 44 | scanf("%d" , &num); 45 | if(num == -1) 46 | break; 47 | 48 | totalCase ++; 49 | 50 | for( j = 0 ; j < num ; j++ ) { 51 | scanf( "%d" , &ele[j] ); 52 | } 53 | 54 | output[totalCase - 1] = eval(num , ele); 55 | 56 | 57 | } while (1); 58 | 59 | for( i = 0 ; i < totalCase ; i++ ) { 60 | printf("%d\n",output[i]); 61 | } 62 | return 0; 63 | } 64 | 65 | int eval(int num , int * ele) { 66 | 67 | int i , sum = 0 , average = 0; 68 | float avg = 0.0; 69 | int count = 0 , check = 0; 70 | for(i = 0 ; i < num ; i++) { 71 | sum += ele[i]; 72 | } 73 | avg = (float) (sum / (float) num); 74 | 75 | average ++; 76 | 77 | if(avg - (int) avg == 0) { 78 | average = (int) avg; 79 | for(i = 0 ; i < num ; i++ ) { 80 | int diff = ele[i] - average; 81 | //printf(" Checking for element = %d , average = %d\n" , ele[i] , average); 82 | if(diff > 0) { 83 | check = check + diff; 84 | count += diff; 85 | //printf("Removing from element %d and check = %d\n" , diff , check); 86 | } 87 | else if( diff < 0) { 88 | 89 | check = check + diff; 90 | //count += (-1 * diff); 91 | //printf("moving into element %d and check = %d\n" , diff , check); 92 | } 93 | } 94 | if(check == 0) 95 | return count; 96 | else 97 | return -1; 98 | } 99 | else { 100 | return -1; 101 | } 102 | 103 | 104 | return 0; 105 | } 106 | -------------------------------------------------------------------------------- /APSAssignment/Assignment1/a1q4.c: -------------------------------------------------------------------------------- 1 | /* 2 | Find the sum of all numbers between L and U (both inclusive) such that they are divisible 3 | by a or b. 4 | As the output can be large, 5 | print (output )mod (1000000007) 6 | 0<=LUAB) where L is the 11 | lower bound and U is the upper bound. And A and B are the two numbers. 12 | Output: 13 | Display the sum of all numbers divisible by A or B between L & U. Output each answer on 14 | a saperate line. 15 | Example: 16 | Input: 17 | 2 18 | 0 10 2 5 19 | 10 20 3 5 20 | output: 21 | 35 22 | 75 23 | */ 24 | 25 | #include 26 | #include 27 | #define BIG_NUM 1000000007 28 | 29 | long int eval( long long int , long long int , long long int , long long int ); 30 | 31 | int main( int argc , char * argv[] ) { 32 | long long int l = 0 , u = 0 ; 33 | long long int a = 0 , b = 0 , n = 0; 34 | long long int i; 35 | long int result = 0; 36 | 37 | scanf("%lld" , &n); 38 | 39 | for( i = 0 ; i < n ; i++ ) { 40 | scanf("%lld %lld %lld %lld" , &l , &u , &a , &b); 41 | 42 | result = eval(l , u , a , b); 43 | 44 | printf("%ld\n" , result ); 45 | 46 | } 47 | 48 | return 0; 49 | } 50 | 51 | long int eval(long long int l , long long int u , long long int a , long long int b) { 52 | 53 | long long int _l = l , _u = u; 54 | long long int sum_a = 0 , sum_b = 0 , sum_ab = 0; 55 | long long int c = 0 , _a = a , _b = b , lcm = 1; 56 | long long int sum_u_l , sum_u_b; 57 | if( a > b ) { 58 | c = a; 59 | a = b; 60 | b = c; 61 | } 62 | 63 | if( l % a != 0) 64 | _l = l + (a - (l % a)); 65 | else 66 | _l = l; 67 | 68 | _u = u - (u % a); 69 | 70 | if( a > _u ) 71 | return 0; 72 | 73 | if ( a == 1 ) { 74 | sum_u_l = l * ( l + 1) / 2; 75 | sum_u_b = u * ( u + 1) / 2; 76 | return (sum_u_b - sum_u_l + l) % BIG_NUM; 77 | } 78 | 79 | if( _u >= _l ) 80 | sum_a = ((((_u - _l)/(a) + 1) * ( _l + _u )) / 2); 81 | 82 | 83 | //printf("_l = %ld , _u = %ld , suma = %ld\n" , _l , _u , sum_a); 84 | 85 | if( b % a == 0 || b > _u ) 86 | return sum_a % BIG_NUM; 87 | 88 | if( l % b != 0) 89 | _l = l + b - (l % b); 90 | else 91 | _l = l; 92 | 93 | _u = u - (u % b); 94 | 95 | if( _u >= _l ) 96 | sum_b = ((((_u - _l)/(b) + 1) * ( _l + _u )) / 2); 97 | 98 | //printf("_l = %ld , _u = %ld , sumb = %ld\n" , _l , _u , sum_b); 99 | 100 | while( _a != _b ) { 101 | if ( _a > _b ) 102 | _a = _a - _b; 103 | if( _b > _a ) 104 | _b = _b - _a; 105 | } 106 | 107 | lcm = (a * b) / _a; 108 | 109 | if( l % (lcm) != 0 ) 110 | _l = l + (lcm) - (l % (lcm)); 111 | else 112 | _l = l; 113 | _u = u - (u % (lcm)); 114 | 115 | if( _u >= _l ) 116 | sum_ab = ((((_u - _l)/(lcm) + 1) * ( _l + _u )) / 2); 117 | 118 | //printf("_l = %ld , _u = %ld , sumab = %ld\n" , _l , _u , sum_ab); 119 | 120 | return ((sum_a + sum_b - sum_ab) % BIG_NUM); 121 | } 122 | -------------------------------------------------------------------------------- /APSLab/Lab5/a.c: -------------------------------------------------------------------------------- 1 | /* 2 | A user is typing a sentence using his keyboard. He uses only the keys, 'a' - 'z', H (home) and E(end). Given the sequence of key presses, find the line that is printed on the screen finally. Initially, a cursor is placed at the beginning of empty line and the following actions are performed with each key stroke. 3 | 4 | - when 'a'-'z' is pressed, its printed at current cursor position after moving the rest of the string to right. For eg. If the current cursor position is at t in the string "deter", pressing a 'x' key results in the string "dexter". 5 | 6 | - when 'H' is pressed, cursor moves to the beginning. For eg: pressing 'H' when we have "apple" results in string "apple" 7 | 8 | - when 'E' is pressed, cursor moves to the end of the string. For eg: pressing 'E' when we have "apple" results in string "apple " 9 | 10 | Its just like any normal editors we use. Try it out pressing 'a' - 'z', Home and End keys to get a feel of it :) 11 | 12 | Input: 13 | Only one line, contains a long string of at most 100000 valid characters 14 | 15 | Output: 16 | Print the final string in a single line, followed by a '\n' ( just print a new line, no need to display '\n' ) 17 | 18 | Constraints: 19 | Time Limit : 1s 20 | Sample Case 1: 21 | 22 | Input: 23 | atHc 24 | 25 | Output: 26 | cat\n 27 | 28 | Explanation: 29 | " " → "a " → "at " → "at" → "cat" 30 | 31 | 32 | Sample Case 2: 33 | 34 | Input: 35 | rHsEocHpEkHaEs 36 | 37 | Output: 38 | apsrocks\n 39 | 40 | Explanation: 41 | Of course, whats there to explain... just experience it ! 42 | */ 43 | #include 44 | #include 45 | 46 | typedef struct node 47 | { 48 | char val; 49 | struct node *next; 50 | struct node *prev; 51 | }node; 52 | typedef struct dll 53 | { 54 | struct node *head,*tail; 55 | }dll; 56 | 57 | 58 | void init(dll *l) 59 | { 60 | (l->head)=NULL; 61 | } 62 | 63 | 64 | int main () { 65 | 66 | char ch = 0; 67 | node *head = NULL , *current = NULL , *end = NULL; 68 | int flag_h = 0 ; 69 | int len = 0; 70 | 71 | while ( (ch = getchar()) != '\n' ) { 72 | 73 | if ( ch == 'H' ) { 74 | current = head; 75 | flag_h = 1; 76 | continue; 77 | } 78 | if ( ch == 'E' ) { 79 | current = end; 80 | flag_h = 0; 81 | continue; 82 | } 83 | 84 | node * temp = (node *) malloc ( sizeof(node) ); 85 | temp -> next = NULL; 86 | temp -> val = ch; 87 | 88 | if ( head == NULL ) { 89 | head = temp; 90 | end = temp; 91 | current = temp; 92 | len ++; 93 | flag_h = 0; 94 | continue; 95 | } 96 | 97 | if ( flag_h == 1 ) { 98 | temp -> next = head; 99 | head = temp; 100 | current = head; 101 | //printf("For char %c : flag = %d\n" , temp->val , flag_h); 102 | flag_h = 0; 103 | } 104 | else { 105 | if ( current -> next == NULL ) 106 | end = temp; 107 | temp -> next = current -> next; 108 | current -> next = temp; 109 | current = temp; 110 | 111 | } 112 | len ++; 113 | 114 | } 115 | 116 | node * p = head; 117 | while( len -- ) 118 | { 119 | printf("%c",p->val); 120 | p=p->next; 121 | } 122 | printf("\n"); 123 | 124 | 125 | return 0; 126 | } 127 | -------------------------------------------------------------------------------- /APSAssignment/Assignment2/a2q2.c: -------------------------------------------------------------------------------- 1 | /* 2 | Maximum sum sub-rectangle 3 | 4 | Given a M × M array of positive and negative integers, find the sub-rectangle with the maximum sum. A sub-rectangle is any 5 | contiguous sub-array of size 1 × 1 or greater located within the whole array. The sum of a rectangle is the sum of all the 6 | elements in that rectangle. The input to the program is the value of M followed by the array itself. 7 | Input Specification: 8 | First line contains M ( 1 <= M<= 100 ) 9 | Next M lines contains M elements each, describing the matrix A ( -100 <= A[i][j] <= 100 ) 10 | 11 | 12 | Output Specification: 13 | 14 | The Maximum Sum (Followed by a '\n' which is just for a newline, no need to display) 15 | 16 | 17 | Sample Input: 18 | 19 | 20 | 4 21 | 0 -2 -7 0 22 | 9 2 -6 2 23 | -4 1 -4 1 24 | -1 8 0 -2 25 | Sample Output: 26 | 27 | 15\n 28 | Explanation: 29 | 30 | The SubMatrix (1,0)-(3,1) has the maximum sum of 15. 31 | Timelimit: 1s 32 | */ 33 | #include 34 | #include 35 | #include 36 | 37 | int algo ( int *array , int n ) { 38 | 39 | /* 40 | Case 1 : All numbers are negative 41 | Case 2 : All numbers are positive 42 | Case 3 : There is a mixture of positive and negative 43 | */ 44 | 45 | int i = 0 , flag_p = 1 , flag_n = 1 , sum = 0 , max = INT_MIN; 46 | 47 | for( i = 0 ; i < n ; i++ ) { 48 | 49 | if( array[i] < 0 ) { 50 | flag_p = 0; 51 | } 52 | else if( array[i] >= 0 ) { 53 | flag_n = 0; 54 | } 55 | 56 | sum += array[i]; 57 | if( array[i] > max ) 58 | max = array[i]; 59 | 60 | } 61 | 62 | if( flag_p == 1 ) { 63 | /* All numbers are positive */ 64 | //printf("%d\n" , sum); 65 | return sum; 66 | } 67 | else if( flag_n == 1 ) { 68 | /* All numbers are negative */ 69 | //printf("%d\n" , max); 70 | //sum = min; 71 | return max; 72 | } 73 | else { 74 | /* The numbers are mixed */ 75 | /* sum[i] -> sum till ith element */ 76 | /* array[i] = max( sum[i-1] + array[i] , array[i] ); */ 77 | 78 | sum = 0; 79 | int end = 0 , cur_sum = 0; 80 | 81 | for( end = 0 ; end < n ; end++ ) { 82 | cur_sum += array[end]; 83 | if( cur_sum > sum ) { 84 | sum = cur_sum; 85 | } 86 | if( cur_sum < 0 ) { 87 | cur_sum = 0; 88 | } 89 | } 90 | return sum; 91 | } 92 | 93 | return 0; 94 | } 95 | 96 | int main ( int argc , char * argv[] ) { 97 | 98 | int a , i , j , k; 99 | int **array , *temp ; 100 | int sum = 0 , max = INT_MIN; 101 | 102 | scanf("%d" , &a); 103 | 104 | array = (int **) malloc( a * sizeof(int*) ); 105 | temp = (int *) malloc( a * sizeof(int) ); 106 | 107 | for( i = 0 ; i < a ;i++) { 108 | array[i] = (int *) malloc( a * sizeof(int) ); 109 | } 110 | 111 | for( i = 0 ; i < a ; i++ ) { 112 | for( j = 0 ;j < a ;j++ ) { 113 | scanf("%d" , &array[i][j]); 114 | } 115 | } 116 | 117 | for( i = 0 ; i < a ; i++ ) { 118 | for( k = 0 ; k < a ; k++ ) 119 | temp[k] = 0; 120 | for( j = i ; j < a ; j++ ) { 121 | for ( k = 0 ; k < a ; k++ ) { 122 | temp[k] += array[j][k]; 123 | } 124 | sum = algo(temp , a); 125 | if( sum > max ) 126 | max = sum; 127 | } 128 | } 129 | printf("%d\n" , max); 130 | return 0; 131 | } 132 | -------------------------------------------------------------------------------- /APSAssignment/Assignment2/a2q5.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int _remove ( int * array , int mod , int sum ) { 6 | if( mod == 1 ) { 7 | if( array[1] > 0 ) { 8 | array[1]--; 9 | sum -= 1; 10 | } 11 | else if( array[4] > 0 ) { 12 | array[4]--; 13 | sum -= 4; 14 | } 15 | else if( array[7] > 0 ) { 16 | array[7]--; 17 | sum -= 7; 18 | } 19 | else if( array[2] > 1 ) { 20 | array[2]--; 21 | array[2]--; 22 | sum -= 2; 23 | sum -= 2; 24 | } 25 | else if( array[2] > 0 && array[5] > 0 ) { 26 | array[2]--; 27 | array[5]--; 28 | sum -= 2; 29 | sum -= 5; 30 | } 31 | else if( array[5] > 1 ) { 32 | array[5]--; 33 | sum -= 5; 34 | array[5]--; 35 | sum -= 5; 36 | } 37 | else if( array[2] > 0 && array[8] > 0 ) { 38 | array[2]--; 39 | array[8]--; 40 | sum -= 2; 41 | sum -= 8; 42 | } 43 | else if( array[5] > 0 && array[8] > 0 ) { 44 | array[5]--; 45 | array[8]--; 46 | sum -= 5; 47 | sum -= 8; 48 | } 49 | else if( array[8] > 1 ) { 50 | array[8]--; 51 | sum -= 8; 52 | array[8]--; 53 | sum -= 8; 54 | } 55 | else { 56 | printf("0\n"); 57 | return INT_MIN; 58 | } 59 | } 60 | else { 61 | if( array[2] > 0 ) { 62 | array[2]--; 63 | sum -= 2; 64 | } 65 | else if( array[5] > 0 ) { 66 | array[5]--; 67 | sum -= 5; 68 | } 69 | else if( array[8] > 0 ) { 70 | array[8]--; 71 | sum -= 8; 72 | } 73 | else if( array[1] > 1 ) { 74 | array[1]--; 75 | array[1]--; 76 | sum -= 1; 77 | sum -= 1; 78 | } 79 | else if( array[1] > 0 && array[4] > 0 ) { 80 | array[1]--; 81 | array[4]--; 82 | sum -= 1; 83 | sum -= 4; 84 | } 85 | else if( array[4] > 1 ) { 86 | array[4]--; 87 | sum -= 4; 88 | array[4]--; 89 | sum -= 4; 90 | } 91 | else if( array[1] > 0 && array[7] > 0 ) { 92 | array[1]--; 93 | array[7]--; 94 | sum -= 1; 95 | sum -= 7; 96 | } 97 | else if( array[4] > 0 && array[7] > 0 ) { 98 | array[4]--; 99 | array[7]--; 100 | sum -= 4; 101 | sum -= 7; 102 | } 103 | else if( array[7] > 1 ) { 104 | array[7]--; 105 | sum -= 7; 106 | array[7]--; 107 | sum -= 7; 108 | } 109 | else { 110 | printf("0\n"); 111 | return INT_MIN; 112 | } 113 | } 114 | return sum; 115 | } 116 | 117 | int main ( int argc , char * argv[] ) { 118 | 119 | int *array; 120 | int i = 0 , t , n , j; 121 | long int sum = 0; 122 | int mod = 0 , flag = 0; 123 | 124 | array = (int *) calloc ( 10 , sizeof(int) ); 125 | 126 | scanf("%d" , &t); 127 | 128 | while ( t-- ) { 129 | scanf("%d" , &n); 130 | array[n]++; 131 | sum += n; 132 | if( n == 0 ) 133 | flag = 1; 134 | } 135 | 136 | if( flag == 0 ) { 137 | printf("-1\n"); 138 | return 0; 139 | } 140 | 141 | mod = sum % 3; 142 | 143 | while( mod != 0 ) { 144 | if( mod == 1 ) { 145 | sum = _remove(array , 1 , sum); 146 | } 147 | else { 148 | sum = _remove(array , 2 , sum); 149 | } 150 | if( sum == INT_MIN ) 151 | return 0; 152 | mod = sum % 3; 153 | } 154 | 155 | flag = 0; 156 | for( i = 9 ; i >= 1 ; i-- ) { 157 | if( array[i] != 0 ) 158 | flag = 1; 159 | } 160 | 161 | if( flag == 0 ) { 162 | printf("0\n"); 163 | return 0; 164 | } 165 | 166 | for( i = 9 ; i >= 0 ; i-- ) { 167 | for( j = 0 ; j < array[i] ; j++ ) 168 | printf("%d" , i); 169 | } 170 | printf("\n"); 171 | 172 | return 0; 173 | 174 | } 175 | -------------------------------------------------------------------------------- /APSLab/Lab4/1.c: -------------------------------------------------------------------------------- 1 | /* 2 | Bracket Stabling 3 | 4 | No fancy story for this one. 5 | You would be given a string of curly braces i.e either an opening brace '{' or a closing bracket '}'. 6 | Which may or may not be stable. The definition of a stable string is: 7 | 8 | a. A NULL string is stable. 9 | b. If a string str is stable so would be {str} 10 | c. If two strings are stable than their concatenation would also be stable 11 | i.e. if str1 and str2 is stable then str1str2 is also stable. 12 | 13 | Some examples of Stable strings: {{}}, {}{} and {} 14 | Some examples of Un-Stable strings: {{ , }{ and {{}{ 15 | 16 | You have to tell minimum number of actions needed to make a string stable. 17 | An action is to either replace an opening brace with a closing or vice-versa. 18 | 19 | Input Specification: 20 | 21 | There would be multiple test cases. Each line would be containing one input string. 22 | Each line/string would be a non-empty string 23 | and would be composed of only opening and closing braces '{' and '}' only. 24 | Any string can contain atmost 2000 braces and would be even in length. 25 | Last line would be containg one or more minus sign(-) 26 | your program should not process beyond that. 27 | Output Specification: 28 | 29 | For every input line you have to print the following line : 30 | 31 | n.(space)A 32 | 33 | where 'n' is the test case no (starting from 1) followed by a dot(.) then a single space 34 | and the number of actions needed to make input string stable. 35 | Print a new-line character (\n) after each line. 36 | 37 | 38 | Sample Input: 39 | 40 | 41 | 42 | {{}{ 43 | {{ 44 | {} 45 | ---- 46 | Sample Output: 47 | 48 | 49 | 50 | 1. 1 51 | 2. 1 52 | 3. 0 53 | */ 54 | 55 | #include 56 | char stack[10000]; 57 | int top; 58 | 59 | int readline( char *str ) { 60 | char ch; 61 | int i = 0; 62 | while ( ( ch = getchar() ) != '\n' ) { 63 | *str++ = ch; 64 | i++; 65 | } 66 | *str ='\0'; 67 | return i; 68 | } 69 | void eval( int n , char * array , int l) { 70 | 71 | int ans = 0; 72 | top = -1; 73 | int i = 0; 74 | for( i = 0 ; i < l ; i++ ) { 75 | if( array[i] == '{' ) { 76 | top++; 77 | stack[top] = '{'; 78 | //printf("Pushing : top = %d\n" , top); 79 | /* Top points to the location -> last element */ 80 | } 81 | else if ( array[i] == '}' ) { 82 | if ( top == -1 ) { 83 | top++; 84 | stack[top] = '{'; 85 | ans ++; 86 | //printf("Error case Pushing : top = %d and ans = %d\n" , top, ans); 87 | } 88 | else if ( top != -1 ) { 89 | if( stack[top] == '{' ) { 90 | top--; 91 | //printf("Popping : top = %d\n" , top); 92 | } 93 | else { 94 | top++; 95 | stack[top] = '{'; 96 | //printf("Pushing : top = %d and ans = %d\n" , top, ans); 97 | } 98 | } 99 | } 100 | } 101 | //printf("TOP = %d ans = %d\n" , top , ans); 102 | if ( l == 0 ) 103 | printf("%d. %d\n" , n , 0); 104 | else { 105 | if( top == -1 && ans == 0 ) 106 | printf("%d. %d\n" , n , 0); 107 | else { 108 | //printf("TOP = %d ans = %d\n" , top , ans); 109 | printf("%d. %d\n" , n , ((top+1)/2) + ans); 110 | } 111 | } 112 | } 113 | 114 | int main( int argc , char * argv[] ) { 115 | char array[10000]; 116 | int l = 0 , i = 0;; 117 | while ( 1 ) { 118 | 119 | l = readline(array); 120 | if( array[0] == '-' ) 121 | break; 122 | i++; 123 | eval(i,array,l); 124 | 125 | } 126 | return 0; 127 | } 128 | -------------------------------------------------------------------------------- /APSAssignment/Assignment4/b.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define LEFT 0 7 | #define RIGHT 1 8 | 9 | using namespace std; 10 | 11 | struct node { 12 | int val; 13 | struct node * child[2]; 14 | }; 15 | 16 | struct node * create_node ( int val ) { 17 | struct node * t = new struct node; 18 | t -> val = val; 19 | t -> child[LEFT] = NULL; 20 | t -> child[RIGHT] = NULL; 21 | return t; 22 | } 23 | 24 | void level_order ( struct node * root ) { 25 | 26 | struct node * t; 27 | queue q; 28 | 29 | q.push(root); 30 | 31 | while ( !q.empty() ) { 32 | 33 | t = q.front(); 34 | q.pop(); 35 | 36 | cout << t->val << " "; 37 | 38 | if ( t -> child[LEFT] != NULL ) { 39 | q.push( t -> child[LEFT] ); 40 | } 41 | if ( t -> child[RIGHT] != NULL ) { 42 | q.push( t -> child[RIGHT] ); 43 | } 44 | 45 | } 46 | 47 | cout << endl; 48 | 49 | } 50 | 51 | void inorder ( struct node * root ) { 52 | 53 | struct node * temp; 54 | stack s; 55 | 56 | s.push(root); 57 | 58 | temp = root; 59 | 60 | while ( !s.empty() ) { 61 | 62 | while ( temp -> child[LEFT] != NULL ) { 63 | s.push( temp -> child[LEFT] ); 64 | temp = temp -> child[LEFT]; 65 | } 66 | 67 | temp = s.top(); 68 | s.pop(); 69 | 70 | cout << temp -> val << " "; 71 | 72 | if ( temp -> child[RIGHT] != NULL ) { 73 | s.push ( temp -> child[RIGHT] ); 74 | temp = temp -> child[RIGHT]; 75 | } 76 | 77 | } 78 | 79 | cout << endl; 80 | } 81 | 82 | void postorder ( struct node * root ) { 83 | 84 | struct node * temp; 85 | 86 | stack s; 87 | 88 | temp = root; 89 | 90 | do { 91 | while ( temp != NULL ) { 92 | if ( temp -> child[RIGHT] != NULL ) { 93 | s.push( temp -> child[RIGHT] ); 94 | } 95 | s.push ( temp ); 96 | temp = temp -> child[LEFT]; 97 | } 98 | 99 | temp = s.top(); 100 | s.pop(); 101 | 102 | if ( temp -> child[RIGHT] != NULL && !s.empty() && s.top() == temp -> child[RIGHT] ) { 103 | s.pop(); 104 | s.push ( temp ); 105 | temp = temp -> child[RIGHT]; 106 | } 107 | else { 108 | cout << temp->val << " "; 109 | temp = NULL; 110 | } 111 | 112 | } while ( !s.empty() ); 113 | 114 | cout << endl; 115 | 116 | } 117 | 118 | void insert_tree ( struct node ** root , int val ) { 119 | struct node * temp; 120 | 121 | if ( (*root) == NULL ) { 122 | (*root) = create_node( val ); 123 | return ; 124 | } 125 | 126 | temp = (*root); 127 | 128 | while ( 1 ) { 129 | 130 | struct node * child; 131 | 132 | if ( val < temp->val ) { 133 | child = temp -> child[LEFT]; 134 | if ( child == NULL ) { 135 | temp -> child[LEFT] = create_node( val ); 136 | break; 137 | } 138 | else { 139 | temp = temp -> child[LEFT]; 140 | } 141 | } 142 | else { 143 | child = temp -> child[RIGHT]; 144 | if ( child == NULL ) { 145 | temp -> child[RIGHT] = create_node( val ); 146 | break; 147 | } 148 | else { 149 | temp = temp -> child[RIGHT]; 150 | } 151 | } 152 | } 153 | 154 | } 155 | 156 | int main ( int argc , char * argv[] ) { 157 | 158 | int n , a; 159 | struct node * root = NULL; 160 | 161 | cin >> n; 162 | 163 | vector numbers; 164 | 165 | for ( int i = 0 ; i < n ; i++ ) { 166 | cin >> a; 167 | insert_tree( &root , a ); 168 | } 169 | 170 | inorder ( root ); 171 | postorder ( root ); 172 | level_order ( root ); 173 | 174 | return 0; 175 | } 176 | -------------------------------------------------------------------------------- /APSAssignment/Assignment4/a.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define LEFT 0 7 | #define RIGHT 1 8 | 9 | using namespace std; 10 | 11 | struct node { 12 | int val; 13 | struct node * child[2]; 14 | }; 15 | 16 | struct node * create_node ( int val ) { 17 | struct node * temp = new struct node; 18 | temp -> val = val; 19 | temp -> child[LEFT] = NULL; 20 | temp -> child[RIGHT] = NULL; 21 | 22 | return temp; 23 | } 24 | 25 | int index_preorder = 0; 26 | 27 | int least_common_ancestor( vector , vector , int , int ); 28 | 29 | struct node * construct_tree ( vector inorder , vector preorder , int start , int end ) { 30 | 31 | if ( start > end ) { 32 | return NULL; 33 | } 34 | 35 | struct node * t = create_node ( preorder[index_preorder++] ); 36 | 37 | if ( start == end ) { 38 | return t; 39 | } 40 | 41 | int search_index = distance ( inorder.begin() , find ( inorder.begin() , inorder.end() , t->val )); 42 | 43 | t -> child[LEFT] = construct_tree ( inorder , preorder , start , search_index - 1 ); 44 | t -> child[RIGHT] = construct_tree ( inorder , preorder , search_index + 1 , end ); 45 | 46 | return t; 47 | } 48 | 49 | int least_common_ancestor_r ( struct node * root , int a , int b ) { 50 | 51 | if ( root == NULL ) { 52 | return INT_MIN; 53 | } 54 | 55 | if ( root->val == a || root-> val == b ) { 56 | return root->val; 57 | } 58 | 59 | int left = least_common_ancestor_r ( root->child[LEFT] , a , b ); 60 | int right = least_common_ancestor_r ( root->child[RIGHT] , a , b ); 61 | 62 | if ( left != INT_MIN && right != INT_MIN ) { 63 | return root->val; 64 | } 65 | else { 66 | if ( left != INT_MIN ) 67 | return left; 68 | else 69 | return right; 70 | } 71 | 72 | return 0; 73 | } 74 | 75 | int least_common_ancestor ( vector preorder , vector inorder , int a , int b ) { 76 | 77 | int count = 0; 78 | 79 | int index_a = 0 , index_b = 0; 80 | 81 | for ( unsigned int i = 0 ; i < inorder.size() ; i++ ) { 82 | if ( inorder[i] == a ) { 83 | index_a = i; 84 | count ++; 85 | } 86 | if ( inorder[i] == b ) { 87 | index_b = i; 88 | count ++; 89 | } 90 | if ( count == 2 ) 91 | break; 92 | } 93 | 94 | if ( index_a > index_b ) { 95 | int t = index_a; 96 | index_a = index_b; 97 | index_b = t; 98 | } 99 | 100 | for( unsigned int i = 0 ; i < preorder.size() ; i++ ) { 101 | for ( int j = index_a ; j <= index_b ; j++ ) { 102 | if ( preorder[i] == inorder[j] ) { 103 | return inorder[j]; 104 | } 105 | } 106 | } 107 | 108 | return preorder[0]; 109 | } 110 | 111 | int main( int argc , char * argv[] ) { 112 | 113 | int n , a ; 114 | vector preorder, inorder; 115 | 116 | cin >> n; 117 | 118 | for ( int i = 0 ; i < n ; i++ ) { 119 | cin >> a; 120 | preorder.push_back( a ); 121 | } 122 | 123 | for ( int i = 0 ; i < n ; i++ ) { 124 | cin >> a; 125 | inorder.push_back( a ); 126 | } 127 | 128 | 129 | /*struct node * root = construct_tree ( inorder , preorder , 0 , n - 1 ); 130 | 131 | cin >> n; 132 | 133 | for ( int i = 0 ; i < n ; i++ ) { 134 | int a , b; 135 | cin >> a >> b; 136 | 137 | cout << least_common_ancestor_r( root , a , b ) << endl; 138 | 139 | }*/ 140 | 141 | cin >> n; 142 | for ( int i = 0 ; i < n ; i++ ) { 143 | int a , b; 144 | cin >> a >> b; 145 | cout << least_common_ancestor ( preorder , inorder , a , b ) << endl; 146 | } 147 | 148 | return 0; 149 | } 150 | -------------------------------------------------------------------------------- /APSLab/Lab4/3.c: -------------------------------------------------------------------------------- 1 | /* 2 | Game Of Coins 3 | 4 | Pankaj is very fond of collecting coins. There is a game in which he has to collect coins from different counters. 5 | Each counter contain some number of coins. 6 | Pankaj got happy,thinking that he can collect as many coins as possible,but there seems to be a threshold on the number of coins he can collect. 7 | You have to tell Pankaj from how many consecutive counters he can collect coins such that maximum counter are visited without exceeding limit. 8 | Input Specification: 9 | 10 | First line consist of T number of testcases. 11 | Next line will contain two integers 'C' 'Max'. 12 | C is the number of counters . 13 | Max is the maximum total coins which can be collected. 14 | Third line contains C integers separated by a single space will denote the number of coins found in the C-i-th station. 15 | Output Specification: 16 | 17 | Your output should consist on T pair of numbers denoting the number of coins Pankaj can collect and the number of counters he will pass respectively. 18 | Constraints: 19 | 20 | 1<=C<=100,000 21 | 1<=Max<=10,000,000 22 | For each counter there will be as much 100 coins 23 | 24 | Sample Input: 25 | 26 | 27 | 1 28 | 5 20 29 | 1 4 20 1 2 30 | Sample Output: 31 | 32 | 33 | 5 2\n 34 | Timelimit: 1s 35 | */ 36 | #include 37 | 38 | int main( int argc , char * argv[] ) { 39 | 40 | int t , c , max_coin; 41 | int array[100001]; 42 | long int sum , i , j , cur_sum , cc; 43 | long int cur_cc; 44 | long int max_sum , max_cc; 45 | 46 | scanf("%d" , &t); 47 | 48 | while( t-- ) { 49 | 50 | scanf("%d%d" , &c , &max_coin); 51 | for( i = 0 ; i < c ; i++ ) { 52 | scanf("%d" , &array[i]); 53 | } 54 | 55 | sum = 0; 56 | cc = 0; 57 | i = 0; 58 | cur_sum = 0; 59 | cur_cc = 0; 60 | max_cc = 0; 61 | max_sum = 0; 62 | 63 | for( i = 0 ; i < c ; i++ ) { 64 | 65 | cur_sum += array[i]; 66 | cur_cc++; 67 | //printf("cur_sum = %ld , cur_cc = %ld\n" , cur_sum , cur_cc); 68 | if( cur_sum <= max_coin ) { 69 | sum = cur_sum; 70 | cc = cur_cc; 71 | //printf("sum = %ld , cc = %ld\n" , sum , cc); 72 | if( max_cc < cc ) { 73 | max_sum = sum; 74 | max_cc = cc; 75 | //printf("max_sum = %ld , max_cc = %ld\n" , max_sum , max_cc); 76 | } 77 | else if( max_cc == cc ) { 78 | if( sum >= max_sum ) { 79 | max_sum = sum; 80 | //printf("max_sum = %ld , max_cc = %ld\n" , max_sum , max_cc); 81 | } 82 | } 83 | } 84 | else if ( cur_sum > max_coin ) { 85 | if( array[i] > max_coin ) { 86 | continue; 87 | } 88 | 89 | for( j = cur_cc-1 ; j >= 0 ; j-- ) { 90 | //printf("Index = j = %d\n" , j); 91 | cur_sum -= array[i-j]; 92 | cur_cc --; 93 | //printf("cur_sum = %d and cur_cc = %d\n" , cur_sum , cur_cc); 94 | if( cur_sum <= max_coin ) { 95 | //cur_sum += array[i-j]; 96 | //cur_cc ++; 97 | break; 98 | } 99 | } 100 | 101 | //cur_cc = 1; 102 | //cur_sum = array[i]; 103 | //printf("RESETTING : cursum = %ld , curcc = %ld\n" , cur_sum , cur_cc); 104 | if( max_cc < cur_cc ) { 105 | max_sum = cur_sum; 106 | max_cc = cur_cc; 107 | //printf("max_sum = %ld , max_cc = %ld\n" , max_sum , max_cc); 108 | } 109 | else if ( max_cc == cur_cc ) { 110 | if( cur_sum >= max_sum ) { 111 | max_sum = cur_sum; 112 | //max_cc = 1; 113 | //printf("max_sum = %ld , max_cc = %ld\n" , max_sum , max_cc); 114 | } 115 | } 116 | } 117 | } 118 | 119 | printf("%ld %ld\n" , max_sum , max_cc); 120 | 121 | } 122 | 123 | return 0; 124 | } 125 | -------------------------------------------------------------------------------- /APSAssignment/Assignment5/c.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | struct node { 9 | int node_number; 10 | int cost; 11 | }; 12 | 13 | void create_graph ( int r , int c , list * list_ ) { 14 | int a = r * 8 + c; 15 | 16 | int n , m; 17 | 18 | n = r + 2; m = c + 1; 19 | if ( n >= 0 && n <= 7 && m >= 0 && m <= 7 ) { 20 | struct node * nd = new struct node; 21 | nd->node_number = n*8 + m; 22 | nd->cost = r*n + c*m; 23 | list_[a].push_back(nd); 24 | } 25 | n = r + 2; m = c - 1; 26 | if ( n >= 0 && n <= 7 && m >= 0 && m <= 7 ) { 27 | struct node * nd = new struct node; 28 | nd->node_number = n*8 + m; 29 | nd->cost = r*n + c*m; 30 | list_[a].push_back(nd); 31 | } 32 | n = r - 2; m = c + 1; 33 | if ( n >= 0 && n <= 7 && m >= 0 && m <= 7 ) { 34 | struct node * nd = new struct node; 35 | nd->node_number = n*8 + m; 36 | nd->cost = r*n + c*m; 37 | list_[a].push_back(nd); 38 | } 39 | n = r - 2; m = c - 1; 40 | if ( n >= 0 && n <= 7 && m >= 0 && m <= 7 ) { 41 | struct node * nd = new struct node; 42 | nd->node_number = n*8 + m; 43 | nd->cost = r*n + c*m; 44 | list_[a].push_back(nd); 45 | } 46 | n = r + 1; m = c + 2; 47 | if ( n >= 0 && n <= 7 && m >= 0 && m <= 7 ) { 48 | struct node * nd = new struct node; 49 | nd->node_number = n*8 + m; 50 | nd->cost = r*n + c*m; 51 | list_[a].push_back(nd); 52 | } 53 | n = r + 1; m = c - 2; 54 | if ( n >= 0 && n <= 7 && m >= 0 && m <= 7 ) { 55 | struct node * nd = new struct node; 56 | nd->node_number = n*8 + m; 57 | nd->cost = r*n + c*m; 58 | list_[a].push_back(nd); 59 | } 60 | n = r - 1; m = c + 2; 61 | if ( n >= 0 && n <= 7 && m >= 0 && m <= 7 ) { 62 | struct node * nd = new struct node; 63 | nd->node_number = n*8 + m; 64 | nd->cost = r*n + c*m; 65 | list_[a].push_back(nd); 66 | } 67 | n = r - 1; m = c - 2; 68 | if ( n >= 0 && n <= 7 && m >= 0 && m <= 7 ) { 69 | struct node * nd = new struct node; 70 | nd->node_number = n*8 + m; 71 | nd->cost = r*n + c*m; 72 | list_[a].push_back(nd); 73 | } 74 | 75 | } 76 | 77 | int dijkstra ( int s , int t , list *list_ ) { 78 | 79 | int dist[64]; 80 | int visited[64]; 81 | 82 | for ( int i = 0 ; i < 64 ; i++ ) { 83 | dist[i] = INT_MAX; 84 | visited[i] = 0; 85 | } 86 | 87 | dist[s] = 0; 88 | 89 | while ( s != -1 ) { 90 | 91 | visited[s] = 1; 92 | 93 | for ( list::iterator itr = list_[s].begin() ; itr != list_[s].end() ; itr++ ) { 94 | struct node * v = (*itr); 95 | if ( visited[v->node_number] == 1 ) 96 | continue; 97 | 98 | if ( dist[v->node_number] > dist[s] + v->cost ) { 99 | dist[v->node_number] = dist[s] + v->cost; 100 | } 101 | } 102 | 103 | s = -1; 104 | int min_distance = INT_MAX; 105 | for ( int i = 0 ; i < 64 ; i++ ) { 106 | if ( visited[i] == 0 && dist[i] < min_distance ) { 107 | s = i; 108 | min_distance = dist[i]; 109 | } 110 | } 111 | 112 | } 113 | 114 | return dist[t]; 115 | 116 | } 117 | 118 | int main ( int argc , char * argv[] ) { 119 | 120 | list * list_ = new list[64]; 121 | 122 | for ( int i = 0 ; i < 8 ; i ++ ) { 123 | for ( int j = 0 ; j < 8 ; j++ ) { 124 | create_graph( i , j , list_ ); 125 | } 126 | } 127 | 128 | /*for ( int i = 0 ; i < 64 ; i++ ) { 129 | cout << "Node " << i << " : "; 130 | for ( list::iterator itr = list_[i].begin() ; itr != list_[i].end() ; itr++ ) { 131 | cout << " " << (*itr)->node_number; 132 | } 133 | cout << endl; 134 | }*/ 135 | 136 | int r1,c1,r2,c2; 137 | while ( scanf("%d%d%d%d" , &r1 , &c1 , &r2 , &c2 ) != EOF ) { 138 | cout << dijkstra ( r1 * 8 + c1 , r2 * 8 + c2 , list_ ) << endl; 139 | } 140 | 141 | return 0; 142 | } 143 | -------------------------------------------------------------------------------- /APSLab/Lab6/b.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Magic set 3 | 4 | Given a set of numbers if there is a number which is a prefix of some other number in the set then the set is not a magic set. 5 | eg. {123,234,12} here 12 is the prefix of 123 so it is not a magic set. 6 | Input Specification: 7 | 8 | The first line is the number of test cases, 1 <= t <= 100. 9 | Each test case starts with a number n, the number of numbers, on a separate line, 1 <= n <= 10000. Then follows n lines with one number on each line. Each number has atmost 12 digits. 10 | Output Specification: 11 | 12 | For each test case, output “YES” if the set is a magic set or “NO” otherwise. 13 | Sample Input: 14 | 15 | 16 | 2 17 | 3 18 | 123 19 | 234 20 | 12 21 | 3 22 | 123 23 | 234 24 | 34 25 | Sample Output: 26 | 27 | 28 | NO 29 | YES 30 | Timelimit: 1s 31 | */ 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | 38 | using namespace std; 39 | 40 | struct node { 41 | int val; 42 | struct node * child[10]; 43 | }; 44 | 45 | struct node * create_node ( int val ) { 46 | struct node * temp = new struct node; 47 | for ( int i = 0 ; i < 10 ; i++ ) { 48 | temp -> child[i] = NULL; 49 | } 50 | temp -> val = val; 51 | return temp; 52 | } 53 | 54 | void printlevelorder(struct node *root) { 55 | if (!root) return; 56 | 57 | queue currentLevel, nextLevel; 58 | currentLevel.push(root); 59 | 60 | while (!currentLevel.empty()) { 61 | struct node *currNode = currentLevel.front(); 62 | currentLevel.pop(); 63 | if (currNode) { 64 | cout << currNode->val << " "; 65 | for(int i = 0 ; i < 10; i++ ) { 66 | nextLevel.push(currNode->child[i]); 67 | } 68 | } 69 | if (currentLevel.empty()) { 70 | cout << endl; 71 | swap(currentLevel, nextLevel); 72 | } 73 | } 74 | } 75 | 76 | int insert_trie ( struct node ** root , long long int number ) { 77 | 78 | struct node * temp = *root; 79 | 80 | char number_str[13]; 81 | 82 | sprintf(number_str , "%lld" , number); 83 | 84 | if ( (*root) == NULL ) { 85 | (*root) = create_node(-1); 86 | return -2; 87 | } 88 | 89 | int i = 0; 90 | 91 | int found = 1; 92 | 93 | while ( number_str[i] != '\0' ) { 94 | 95 | //cout <<"Character : " << number_str[i] << endl; 96 | 97 | int number = number_str[i] - '0'; 98 | struct node * temp_node = create_node(number); 99 | 100 | //cout <<" Temp -> data = " << temp->val << endl; 101 | 102 | if ( temp == NULL ) { 103 | //cout << "a" << endl; 104 | temp = temp_node; 105 | } 106 | else if ( temp -> child[number] == NULL ) { 107 | //cout << "b" << endl; 108 | //cout << "Found " << number << " child of parent " << temp->val << " null" << endl; 109 | temp -> child[number] = temp_node; 110 | temp = temp -> child[number]; 111 | found = 0; 112 | } 113 | else if ( temp -> val == number ) { 114 | //cout << "c" << endl; 115 | //cout << "Found " << number << " = that of parent " << temp->val << "" << endl; 116 | i++; 117 | temp = temp -> child[number]; 118 | continue; 119 | } 120 | else { 121 | //cout << "d" << endl; 122 | i++; 123 | temp = temp -> child[number]; 124 | continue; 125 | } 126 | 127 | i++; 128 | 129 | } 130 | 131 | return found; 132 | 133 | } 134 | 135 | int main ( int argc , char * argv[] ) { 136 | 137 | int t; 138 | 139 | cin >> t; 140 | 141 | while ( t-- ) { 142 | 143 | //vector vec; 144 | vector vec; 145 | 146 | int n; 147 | cin >> n; 148 | 149 | struct node * root; 150 | root = NULL; 151 | 152 | int flag = 0; 153 | insert_trie ( &root , -1 ); 154 | 155 | for( int i = 0 ; i < n ; i++ ) { 156 | string number; 157 | //long long int number; 158 | cin >> number; 159 | vec.push_back(number); 160 | } 161 | 162 | sort ( vec.begin() , vec.end() ); 163 | for ( int i = 0 ; i < n-1 ; i++ ) { 164 | if ( !vec[i+1].find(vec[i]) ) { 165 | flag = 1; 166 | break; 167 | } 168 | } 169 | 170 | /*for( int i = 0 ; i < n ; i++ ) { 171 | //cout << "Executing for number = " << number << endl; 172 | int already_present = insert_trie ( &root , vec[n-1-i] ); 173 | //cout << "Root data : " << root->val << endl; 174 | if ( already_present == 1 ) { 175 | flag = 1; 176 | break; 177 | } 178 | }*/ 179 | 180 | if ( flag == 1 ) { 181 | cout << "NO" << endl; 182 | } 183 | else { 184 | cout << "YES" << endl; 185 | } 186 | 187 | //printlevelorder ( root ); 188 | } 189 | return 0; 190 | } 191 | -------------------------------------------------------------------------------- /APSAssignment/Assignment5/Assignment5.txt: -------------------------------------------------------------------------------- 1 | ********************************************************************** 2 | Question 1 3 | ********************************************************************** 4 | 5 | Problem code: CAM5 6 | 7 | help the Prayatna pr team 8 | 9 | 10 | 11 | Well, the annual technical symposium of Department of Computer Technology is around the corner. All that we need, to make it a grand success is Publicity among the peer groups ( ofCourse the sponsors too :P ). We dicided to share the job between the student groups. As per the plan we decided to meet people in person and influence them to attend Prayatna. But to meet them we have to go to various student groups. To do so, we had to cut our classes. But being studious. students refused to cut more classes. Instead of meeting every one in person we decided to meet few people such that the person to whom we pass the news will spread it to all his friends. And those friends will pass it to other friends and so on. Your task is to find the number of people to be met by the organizers to spread the news. 12 | 13 | 14 | 15 | caution: large I/O 16 | 17 | Input 18 | 19 | 20 | 21 | First line of input is 't' - Test cases. Follwed by N, the number of peers in the testcase ( 0 to N-1 ). followed by the no.of friend description 'e'. Following are 'e' descriptions of type 22 | 23 | " a b " denoting 'a' friends with 'b' . if 'a' is friends with 'b' then 'b' is firends with 'a'. 24 | 25 | 26 | 27 | Output 28 | 29 | Output contains t line, the number of people, the organizers have to meet in person for each test case. 30 | 31 | Example 32 | 33 | Input: 34 | 2 35 | 36 | 4 37 | 2 38 | 0 1 39 | 1 2 40 | 41 | 42 | 3 43 | 0 44 | 45 | Output: 46 | 2 47 | 3 48 | Test case Expalained: 49 | 50 | case 1 : 0 is friends with 1; 1 is friends with 2 ; so if we pass the news to 0 & 3 , news will pass it to the entire N peers 51 | case 2 : no one is friends with any one. So we have to meet every one in person. 52 | Constraints: 53 | 54 | t=10 55 | 2 <= N <= 100000 56 | 0 <= e <= N/2 57 | 58 | ********************************************************************** 59 | Question 2 60 | ********************************************************************** 61 | 62 | The Benefactor 63 | 64 | Another chapter of the municipal chronicles of a well known unbelievable lordly major town (if this town is not well known to you, you might want to solve problem CSTREET first) tells us the following story: 65 | Once upon a time the citizens of the unbelievable lordly major town decided to elect a major. At that time this was a very new idea and election campaigns were completely unknown. But of course several citizens wanted to become major and it didn't took long for them to find out, that promising nice things that never will become real tends to be useful in such a situation. One candidate to be elected as a major was Ivo sometimes called the benefactor because of his valuable gifts to the unbelievably lordly major towns citizens. 66 | One day before the election day Ivo the benefactor made a promise to the citizens of the town. In case of his victory in the elections he would ensure that on one of the paved streets of the town street lamps would be erected and that he would pay that with his own money. As thrifty as the citizens of the unbelievable lordly major town were, they elected him and one day after the elections they presented him their decision which street should have street lamps. Of course they chose not only the longest of all streets but renamed several streets so that a very long street in the town existed. 67 | Can you find how long this street was? To be more specific, the situation is as follows. You are presented a list of all paved streets in the unbelievable lordly major town. As you might remember from problem CSTREET in the town the streets are paved in a way that between every two points of interest in the town exactly one paved connection exists. Your task is to find the longest distance that exists between any two places of interest in the city. 68 | 69 | Input 70 | 71 | The first line of input contains the number of testcases t. 72 | The first line of each testcase contains the number of places (2 <=n<=50000) in the town. Each street is given at one line by two places (1<=a,b<=n) and the length of the street (0<=l<20000). 73 | 74 | Output 75 | 76 | For each testcase output one line which contains the maximum length of the longest street in the city. 77 | 78 | Example 79 | 80 | Input: 81 | 1 82 | 6 83 | 1 2 3 84 | 2 3 4 85 | 2 6 2 86 | 6 4 6 87 | 6 5 5 88 | 89 | Output: 90 | 91 | 12 92 | 93 | ********************************************************************** 94 | Question 3 95 | ********************************************************************** 96 | 97 | COSTLY CHESS 98 | 99 | In the country of Rome, Chess is a royal game. For evey move the players had to give some bucks to the Emperor Jurg. The LGMs or Little Green Men, are very good player of chess. But as the chess is a expensive game, thats why it is royal, they asked you to help them find the minimum bucks which they had to pay for moving their Knight from one position to another. Any number of steps can be used to reach the destination. 100 | 101 | Constraints: 102 | 103 | The chess has a dimension of 8X8, and the index of left bottom cell (0, 0). 104 | 105 | Knight move only in a standard way, i.e. 2 row and 1 col or 1 row and 2 col. 106 | 107 | If in a step Knight move from (a, b) to (c, d), then LGM had to pay a*c + b*d bucks to Emperor Jurg. 108 | 109 | 0 ≤ a, b, c, d ≤ 7 110 | 111 | Input 112 | 113 | There are 100-150 test cases. Each test case is composed of four space separeated integers.The first two numbers, a, b, are the starting position of the Knight and the next two, c, d, are the destination of the Knight. Read upto End Of File. 114 | 115 | Output 116 | 117 | For each test case, print the minimum amount of bucks they had to pay in separate line. If its impossible to reach the destination then print -1. 118 | 119 | Example 120 | 121 | Input: 122 | 2 5 5 2 123 | 4 7 3 2 124 | 1 2 3 4 125 | 126 | Output: 127 | 42 128 | 78 129 | 18 130 | 131 | 132 | Explanation for test case #1: 133 | 2 5 5 2 134 | 135 | For moving Knight from (2, 5) to (5, 2) in minimum cost, one of the path is (2, 5) -> (3, 3) ->(5, 2) 136 | Bucks paid: 137 | (2, 5) = 0 138 | (2, 5) -> (3, 3) = 0 + (2*3 + 5*3) = 21 139 | (3, 3) -> (5, 2) = 21 + (3*5 + 3*2) = 42 140 | 141 | 142 | To infinity and beyond... 143 | 144 | ********************************************************************** 145 | Question 4 146 | ********************************************************************** 147 | 148 | Roommate Agreement 149 | Submitted solutions: 3 150 | Leonard was always sickened by how Sheldon considered himself better than him. To decide once and for all who is better among them they decided to ask each other a puzzle. Sheldon pointed out that according to Roommate Agreement Sheldon will ask first. Leonard seeing an opportunity decided that the winner will get to rewrite the Roommate Agreement. 151 | 152 | Sheldon thought for a moment then agreed to the terms thinking that Leonard will never be able to answer right. For Leonard, Sheldon thought of a puzzle which is as follows. He gave Leonard n numbers, which can be both positive and negative. Leonard had to find the number of continuous sequence of numbers such that their sum is zero. 153 | 154 | For example if the sequence is- 5, 2, -2, 5, -5, 9 155 | 156 | There are 3 such sequences 157 | 158 | 2, -2 159 | 160 | 5, -5 161 | 162 | 2, -2, 5, -5 163 | 164 | Since this is a golden opportunity for Leonard to rewrite the Roommate Agreement and get rid of Sheldon's ridiculous clauses, he can't afford to lose. So he turns to you for help. Don't let him down. 165 | 166 | 167 | 168 | Input 169 | 170 | First line contains T - number of test cases 171 | 172 | Second line contains n - the number of elements in a particular test case. 173 | 174 | Next line contain n elements, ai (1<=i<= n) separated by spaces. 175 | 176 | 177 | 178 | Output 179 | 180 | The number of such sequences whose sum if zero. 181 | 182 | 183 | 184 | Constraints 185 | 186 | 1<=t<=5 187 | 188 | 1<=n<=10^6 189 | 190 | -10<= ai <= 10 191 | 192 | 193 | 194 | Example 195 | 196 | Input: 197 | 2 198 | 199 | 4 200 | 201 | 0 1 -1 0 202 | 203 | 6 204 | 205 | 5 2 -2 5 -5 9 206 | 207 | 208 | Output: 209 | 210 | 6 211 | 3 212 | 213 | ********************************************************************** 214 | Question 5 215 | ********************************************************************** 216 | 217 | Problem code: FARIDA 218 | 219 | Once upon time there was a cute princess called Farida living in a castle with her father, mother and uncle. On the way to the castle there lived many monsters. Each one of them had some gold coins. Although they are monsters they will not hurt. Instead they will give you the gold coins, but if and only if you didn't take any coins from the monster directly before the current one. To marry princess Farida you have to pass all the monsters and collect as many coins as possible. Given the number of gold coins each monster has, calculate the maximum number of coins you can collect on your way to the castle. 220 | 221 | Input 222 | 223 | The first line of input contains the number of test cases. Each test case starts with a number N, the number of monsters, 0 <= N <= 10^4. The next line will have N numbers, number of coins each monster has, 0 <= The number of coins with each monster <= 10^9. Monsters described in the order they are encountered on the way to the castle. 224 | 225 | Output 226 | 227 | For each test case print “Case C: X” without quotes. C is the case number, starting with 1. X is the maximum number of coins you can collect. 228 | 229 | Example 230 | 231 | Input: 232 | 2 233 | 5 234 | 1 2 3 4 5 235 | 1 236 | 10 237 | Output: 238 | Case 1: 9 239 | Case 2: 10 240 | 241 | 242 | 243 | ********************************************************************** 244 | Question 6 245 | ********************************************************************** 246 | 247 | Building Bridges 248 | Submitted solutions: 1 249 | The tribe soon discovers that just communication is not enough and wants to meet each other to form a joint force against the terminator. But there is a deep canyon that needs to crossed. Points have been identified on both sides on which bridge ends can be made. But before the construction could be started, a witch Chudael predicted that a bridge can only be built between corresponding end points, i.e. a bridge starting from the ith end point on one side can only end on the ith end point on the other side, where the position of end points is seen in the order in which the points were identified. If not, it would lead to the end of the tribe. The tribe just wants to make as many non-cutting bridges as possible, with the constraint in mind. Bridges "cut" if and only if they have exactly one common point that is not an end point. 250 | 251 | Input 252 | 253 | The first line of the input contains test cases t (1<=t<=50). It is followed by 3*t lines, 3 for each test case. The first line of input for each test case contains the number of end points identified on each side, n (1<=n<=103). The second line contains x-coordinates of end points identified on the first side and similiarly the third line contains the x-coordinates of corresponding end points identified on the other side. The end points are inputted in the order in which they were identified. The x-coordinates can range between -103 to 103. 254 | 255 | Output 256 | 257 | You are required to output a single line for each test case. The line contains a single integer – the maximum number of bridges possible with the constraints explained above. 258 | 259 | Example 260 | 261 | Input: 262 | 3 263 | 4 264 | 265 | 2 5 8 10 266 | 267 | 6 4 1 2 268 | 269 | 3 270 | 271 | 5 3 10 272 | 273 | 6 4 1 274 | 275 | 6 276 | 277 | 1 2 3 4 5 6 278 | 279 | 3 4 5 6 1 2 280 | 281 | Output: 282 | 2 283 | 284 | 2 285 | 286 | 4 287 | 288 | 289 | 290 | Expalanation: For the first test case, two non-overlapping bridges can be formed between the 3rd and 4th end points on each side. 291 | --------------------------------------------------------------------------------