0% found this document useful (0 votes)
2 views14 pages

Additional Solutions Template TPEC

The document contains various C programming code snippets related to searching, sorting, recursion, bit manipulation, greedy algorithms, dynamic programming, and string manipulation. Each section includes incomplete code for tasks such as sorting arrays, finding medians, maximizing XOR, and manipulating strings. The document serves as a template for implementing algorithms and data structures in C.

Uploaded by

vvce22ise0095
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views14 pages

Additional Solutions Template TPEC

The document contains various C programming code snippets related to searching, sorting, recursion, bit manipulation, greedy algorithms, dynamic programming, and string manipulation. Each section includes incomplete code for tasks such as sorting arrays, finding medians, maximizing XOR, and manipulating strings. The document serves as a template for implementing algorithms and data structures in C.

Uploaded by

vvce22ise0095
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

SEARCHING AND SORTING

Closest Numbers
#include<stdio.h>
#include<stdlib.h>

void quickSort(int[], int, int);


int partition(int[], int, int);

int main(){
int i,n;
//input
scanf("%d",&n);
int *a=(int *)malloc(n*sizeof(int));

for(i=0;i<n;i++)
scanf("%d",&a[i]);
//sorting

//……………..Insert Code Here…………….

//finding smallest

//……………..Insert Code Here…………….

//printing all pairs


//……………..Insert Code Here…………….
return 0;
}

void quickSort(int a[], int l, int r)


{
int j;
if( l < r )
{
// divide and conquer
j = partition( a, l, r);
quickSort( a, l, j-1);
quickSort( a, j+1, r);
}
}

int partition(int a[], int l, int r) {


int pivot, i, j, t;
pivot = a[l];
i = l; j = r+1;
while( 1)
{
do ++i; while( a[i] <= pivot && i <= r );
do --j; while( a[j] > pivot );
/*do ++i; while( a[i] >= pivot && i <= r );
do --j; while( a[j] < pivot );*/
if( i >= j ) break;
t = a[i]; a[i] = a[j]; a[j] = t;
}
t = a[l]; a[l] = a[j]; a[j] = t;
return j;
}

Ice Cream Parlor


#include<stdio.h>
int main()
{
int t,c,l,i,j,arr[20000];
scanf("%d",&t);
for( ; t>0 ; t--)
{

//……………..Insert Code Here…………….


}
return 0;
}

Find the Median


RECURSION AND BIT MANIPULATION

Maximizing XOR

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
int maxXor(int l, int r) {

//……………..Insert Code Here…………….


}
int main() {
int res;
int _l;
int _r;
scanf("%d", &_l);
scanf("%d", &_r);
res = maxXor(_l, _r);
printf("%d", res);
return 0;
}
Sum vs XOR
#include <stdio.h>
int main(){
long long int n,m=1;
scanf("%lld",&n);

//……………..Insert Code Here…………….


return 0; }

Flipping Bits
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int t;
unsigned int n;

//……………..Insert Code Here…………….


}
return 0;
}
GREEDY AND DYNAMIC PROGRAMMING

Mark and Toys

#include<stdio.h>
void quicksort(int x[100000],int first,int last){
int pivot,j,temp,i;

if(first<last){
pivot=first;
i=first;
j=last;

while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}

temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);

}}

int main()
{
int n,k,i,avail=0,count=0;
int cost[n];

//……………..Insert Code Here…………….


return 0; }

Greedy Florist

#include <stdio.h>
#include <stdlib.h>

//……………..Insert Code Here…………….

int com(const void *xx, const void *yy)


{
if(*(int *)xx > *(int*)yy) return -1;
return 1;
}

int main()
{

scanf("%d %d\n",&n,&k);
for(i=0;i<n;i++)
scanf("%d",a+i);

qsort(a,n,sizeof(a[0]),com); // sorts in descending order of price

//……………..Insert Code Here…………….


return 0;
}
STRINGS
Camelcase
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
char* s = (char *)malloc(10240 * sizeof(char));

//……………..Insert Code Here…………….


return 0;
}

Mars Exploration
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
char* S = (char *)malloc(10240 * sizeof(char));
scanf("%s",S);
int i;
int count=0;
//……………..Insert Code Here…………….
printf("%d",count);
return 0;
}

Funny String
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int T, N, funny, i, r;
char S[10001];

scanf("%d\n", &T);
while (T--) {
scanf("%s\n", S);
N = strlen(S);

//……………..Insert Code Here…………….


}
return 0;
}
Alternating Characters

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main()
{
int t;
long i=0;
unsigned int count=0;
char * c;
scanf("%d",&t);
c=(char *)malloc(sizeof(char)*(100002));

//……………..Insert Code Here…………….


printf("%u\n",count);
count=0;
}

return 0;
}
Beautiful Binary String

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
int n;
scanf("%d",&n);
char* B = (char *)malloc(10240 * sizeof(char));
scanf("%s",B);
int i=0,count=0;
while(B[i]){

//……………..Insert Code Here…………….


}
printf("%d",count);
return 0;
}
RANGE QUERIES
Fenwick Tree – Add a value
Given a Fenwick Tree (Binary indexed Tree) with the value of at node k
calculated as
tree[k] = sumq (k - p(k) + 1, k)
Where, p(k) = k&-k and denotes the largest power of two that divides k.
Create a function to increases the value at position k by x in the Fenwick
Tree.

#include<stdio.h>
#include<math.h>

void display(int arr[], int n)


{
int i;
for (i=1;i<=n;i++)
{
printf("\t %d ",arr[i]);
}
}

void add(int T[],int n, int k, int x) {

//……………..Insert Code Here…………….


}
int main(){
int n=8, k,x, T[100] = {0, 1, 4, 4, 16, 6, 7, 4, 29};
printf("\n Fenwick Tree");
display(T,n);

//……………..Insert Code Here…………….


return 0;
}

You might also like