0% found this document useful (0 votes)
8 views19 pages

Regular Solutions Template - TPEC

The document contains multiple C programming code snippets focusing on various algorithms and data structures, including insertion sort, counting sort, recursion, dynamic programming, and prefix sums. Each section includes a main function that reads input, processes data, and calls specific sorting or calculation functions, though many implementations are incomplete with placeholders for code insertion. Overall, it serves as a guide for implementing and understanding fundamental 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)
8 views19 pages

Regular Solutions Template - TPEC

The document contains multiple C programming code snippets focusing on various algorithms and data structures, including insertion sort, counting sort, recursion, dynamic programming, and prefix sums. Each section includes a main function that reads input, processes data, and calls specific sorting or calculation functions, though many implementations are incomplete with placeholders for code insertion. Overall, it serves as a guide for implementing and understanding fundamental 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/ 19

SEARCHING AND SORTING

Insertion Sort - Part 1

#include <stdio.h>
void print(int ar_size, int* ar) {
int i;
for(i=0; i<ar_size; i++) {
printf("%d ", ar[i]);
}
printf("\n");
}

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

/* Head ends here */


void insertionSort(int ar_size, int * ar) {

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


}

/* Tail starts here */


int main() {

int _ar_size;
scanf("%d", &_ar_size);
int _ar[_ar_size], _ar_i;
for(_ar_i = 0; _ar_i < _ar_size; _ar_i++) {
scanf("%d", &_ar[_ar_i]);
}

insertionSort(_ar_size, _ar);

return 0;
}

Insertion Sort - Part 2


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
/* Head ends here */
void insertionSort(int ar_size, int * ar) {

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


}
/* Tail starts here */
int main() {
int _ar_size;
scanf("%d", &_ar_size);
int _ar[_ar_size], _ar_i;
for(_ar_i = 0; _ar_i < _ar_size; _ar_i++) {
scanf("%d", &_ar[_ar_i]);
}

insertionSort(_ar_size, _ar);

return 0;
}

Correctness and the Loop Invariant


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
/* Head ends here */
#include <stddef.h>
void insertionSort(int ar_size, int * ar) {

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


}
/* Tail starts here */
int main(void) {
int _ar_size;
scanf("%d", &_ar_size);
int _ar[_ar_size], _ar_i;
for(_ar_i = 0; _ar_i < _ar_size; _ar_i++) {
scanf("%d", &_ar[_ar_i]);
}

insertionSort(_ar_size, _ar);

return 0;
}

Running Time of Algorithms


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

/* Head ends here */


void insertionSort(int ar_size, int * ar,int *shifts) {

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


}

/* Tail starts here */


int main() {

int _ar_size,i,j,shifts=0;
scanf("%d", &_ar_size);
int _ar[_ar_size], _ar_i;
for(_ar_i = 0; _ar_i < _ar_size; _ar_i++) {
scanf("%d", &_ar[_ar_i]);
}
for(i=2;i<=_ar_size;i++)
{
insertionSort(i, _ar,&shifts);
}
printf("%d",shifts);
return 0;
}

Counting Sort 1
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

int n,i;
int b[100],a;
//……………..Insert Code Here…………….
return 0;
}
RECURSION AND BIT MANIPULATION
Crossword Puzzle

The Power Sum


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

int the_power_sum(int n, int m,int p){

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


}
int main() {
int n,p;
scanf("%d%d",&n,&p);
printf("%d", the_power_sum(n,1,p));

return 0;
}
Counter Game
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int isPow2(long unsigned int);
unsigned long int largePow(long unsigned int);
int main() {
int t,i,win;
long unsigned int n;

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


return 0;
}
int isPow2(long unsigned int n)
{
//……………..Insert Code Here…………….
}
long unsigned int largePow(long unsigned int n)
{
long unsigned int m;
while(n)
{
m=n;
n=n&(n-1);
}
return m; }
GREEDY AND DYNAMIC PROGRAMMING
The Coin Change Problem

Sherlock and Cost


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

int main() {
int T,N,B,L,R,ML,MR,X,Y,P,Q;
scanf("%d",&T);
for(int i = 0; i < T; i++) {
scanf("%d",&N);
for(int j = 0; j < N; j++) {
scanf("%d",&B);
if(j) {

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


} else {

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


}
L = 1;
R = B;
}
printf("%d\n", (ML > MR ? ML : MR));
}
return 0;
}
Marc's Cakewalk
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
void swap(int *a,int *b)
{
int temp;
temp = *a;
*a = *b;
*b=temp;
}
int partition(int *x,int start,int end)
{
int pivot,pindex,i;
pivot = x[end];
pindex = start;
for(i=start;i<end;i++)
{
if(x[i]>=pivot)
{
swap(&x[i],&x[pindex]);
pindex = pindex + 1;
}
}
swap(&x[pindex],&x[end]);
return pindex;
}
void quicksort(int *x,int start,int end)
{
if(start<end)
{
int i = partition(x,start,end);

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


}
}
int main(){
int n, calories_i, *calories;
int i;
int sum = 0;
scanf("%d",&n);
calories = malloc(sizeof(int) * n);
for(calories_i = 0; calories_i < n; calories_i++)
{
scanf("%d",&calories[calories_i]);
}

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


return 0;
}
STRINGS
Strong Password

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

int minimumNumber(int n, char* password) {

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


}

int main() {
int n;
scanf("%i", &n);
char* password = (char *)malloc(512000 * sizeof(char));
scanf("%s", password);
int answer = minimumNumber(n, password);
printf("%d\n", answer);
return 0;
}
Caesar Cipher

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

int main() {

int n,i,j,k;
char ar[101];
unsigned char x;
scanf("%d",&n);
scanf("%s",ar);
scanf("%d",&k);
for(i=0;i<n;i++)
{

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


}
printf("%s",ar);
return 0;
}
Pangrams

#include <stdio.h>
char s[10000];
int main()
{
gets(s);
int f[300]={0},ans=0,i;
int l=strlen(s);
for(i=0;i<l;i++)
{

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

}
if(ans!=26)
printf("not ");
printf("pangram\n");
return 0;
}
RANGE QUERIES
Prefix Sum Array

Populate a Prefix-Sum Array from an array of integer elements.


Also create a function to calculate the sum of elements between a given
range using the Prefix-Sum Array.

#include<stdio.h>
void display(int arr[], int n)
{
int i;
for (i=0;i<n;i++)
{
printf("\t %d ",arr[i]);
}
}

create_prefix_sum_array(int arr[], int n)


{

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


}

void sum(int arr[], int n, int a, int b){

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


}
int main(){
int n, i, arr[10], a, b;
printf("enter the number of elements\n");
scanf("%d",&n);
printf("enter the array elements\n");
for (i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}

printf("\n Original array");


display(arr, n);

create_prefix_sum_array(arr, n);

printf("\n Prefix sum array");


display(arr,n);

printf("\n enter the range to find the sum");


scanf("%d%d",&a,&b);
sum(arr, n, a, b);
return 0;
}
Fenwick Tree Construction
Populate a Fenwick Tree from an array of integer elements.
Given the value of a node in a Fenwick Tree
tree[k] = sumq (k - p(k) + 1, k)
Where, p(k) = k&-k and denotes the largest power of two that divides k

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

int sum(int arr[], int n, int a, int b){

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


}

void create_fenwick_tree(int T[], int arr[], int n)


{
int a, b, k;

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

void display(int arr[], int n)


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

int main(){
int n, i, arr[100], T[100] , a, b;

// printf("the power of 6 is %u \n", largestPowerOf2(7)) ;


printf("enter the number of elements\n");
scanf("%d",&n);
printf("enter the array elements\n");
for (i=1;i<=n;i++)
{
scanf("%d",&arr[i]);
}
printf("\n Original array");
display(arr, n);
create_fenwick_tree(T, arr, n);
printf("\n Fenwick Tree");
display(T,n);
return 0;
}
Fenwick Tree - Sum of Elements
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 calculate sum of first ‘k’ elements in the Fenwick Tree.

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

int sum(int T[], int k) {

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


}
void display(int arr[], int n)
{
int i;
for (i=1;i<=n;i++)
{
printf("\t %d ",arr[i]);
}
}
int main(){
int n, k, T[100] = {0, 1, 4, 4, 16, 6, 7, 4, 29};
printf("\n Fenwick Tree");
display(T,n);
printf("\n Enter the value for k");
scanf("%d",&k);
sum(T, k);
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy