Additional Solutions TPEC
Additional Solutions TPEC
Additional Solutions TPEC
Closest Numbers
#include<stdio.h>
#include<stdlib.h>
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
quickSort(a,0,n-1);
//finding smallest
int min=a[1]-a[0];
for(i=2;i<n;i++)
if(a[i]-a[i-1]<min) min=a[i]-a[i-1];
Maximizing XOR
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
int maxXor(int l, int r) {
int max = 0,i,j;
for(i=l;i<r;i++)
for(j=i+1;j<=r;j++)
max = max<(i^j)?i^j:max;
return max;
}
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);
while(n>0){
if(n%2==0)m*=2;
n/=2;
}
printf("%lld\n",m);
return 0; }
Flipping Bits
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int t;
unsigned int n;
scanf("%d", &t);
while(t-- > 0) {
scanf("%u", &n);
printf("%u\n", ~n);
}
return 0;
}
GREEDY AND DYNAMIC PROGRAMMING
#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];
scanf("%d",&n);
scanf("%d",&k);
for(i=0;i<n;i++)
scanf("%d",&cost[i]);
quicksort(cost,0,n-1);
while(avail<=k)
{
avail+=cost[count];
count++;
}
printf("%d\n",count-1);
return 0; }
Greedy Florist
#include <stdio.h>
#include <stdlib.h>
int a[200],i,j,k,l,sum,n;
int main()
{
scanf("%d %d\n",&n,&k);
for(i=0;i<n;i++)
scanf("%d",a+i);
int main(){
char* s = (char *)malloc(10240 * sizeof(char));
scanf("%s",s);
int count=0,i;
for(i=0;i<strlen(s);i++)
{
if(s[i]>=65 && s[i]<=90)
{
count++;
}
}
printf("%d\n",count+1);
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;
for(i=0;S[i]!='\0';i+=3){
if(S[i]!='S'){
count++;
}
if(S[i+1]!='O'){
count++;
}
if(S[i+2]!='S'){
count++;
}
}
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);
funny = 1;
for(i=1, r=N-2; i<N; i++, r--) {
if (fabs(S[i]-S[i-1]) != fabs(S[r]-S[r+1])) {
funny = 0;
break;
}
}
if (funny)
{
printf("funny");
}
else
printf("not funny");
}
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));
while(t--)
{
scanf("%s",c);
for(i=0; c[i] != ‘\0’; i++)
{
if(c[i]==c[i+1])
{
count++;
}
}
printf("%u\n",count);
count=0;
}
return 0;
}
#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]){
if(B[i]=='0'&&B[i+1]=='1'&&B[i+2]=='0'){
B[i+2]='1';
count++;
}
i++;
}
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>