Hacker rank contest 1&2&3 solutions-Test-18
This hacker rank solutions
Test-18 Solutions
1)
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main() {
int q;
scanf("%i", &q);
for(int a0 = 0; a0 < q; a0++){
char *a = (char *)calloc(26, sizeof(char));
char *s = (char *)malloc(512000 * sizeof(char));
scanf("%s", s);
int result = 0;
for(int i = 0; i < strlen(s); i++) {
if(!a[s[i]-'a']) {
a[s[i]-'a'] = 1;
result++;
if(result == 26)
break;
}
}
printf("%d\n", result);
}
return 0;
}
2)
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main()
{
long long int i,max,freqfreq[100001]={0},j, k,a;
int count=0,count2=0,freq[26]={0};
char* s = (char *)malloc(512000 * sizeof(char));
scanf("%s", s);
a=strlen(s);
if(a>2)
{
for(i=0;i<strlen(s);i++)
{
freq[s[i]-97]++;
}
for(i=0;i<26;i++)
{
freqfreq[freq[i]]++;
}
max=freqfreq[1];
j=1;
for(i=1;i<100001;i++)
{
if(freqfreq[i]>max)
{
max=freqfreq[i];
j=i;
}
if(freqfreq[i]==1)
{
count2++;
k=i;
}
}
for(i=0;i<26;i++)
{
if((count2==1 || count2==2) && abs(j-k)==1)
{
if(freq[i]==0 || freq[i]==j || freq[i]==k)
{
count++;
}
}
else if((count2==1 || count2==2) && abs(j-k)!=1 && k==1)
{
count++;
}
else
{
if(freq[i]==0 || freq[i]==j)
{
count++;
}
}
}
if(count==26)
{
printf("YES");
}
else
{
printf("NO");
}
}
else
{
printf("YES");
}
return 0;
}
Compile In c++
3)
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
string s;
long long n, k;
cin>>n>>k>>s;
// Corner cases
if(n == 1) {
if(k > 0) {
cout<<9;
} else {
cout<<s;
}
return 0;
}
bool possible = true;
bool *changed = new bool[n];
for(long long i = 0; i < n; i++) {
changed[i] = false;
}
// Start by fixing any unmatching indices to make s a palindrome
for(long long i = 0; i < s.size()/2; i++) {
if(s[i] != s[s.size()-i-1]) {
if(k > 0) {
if(s[i] > s[s.size()-i-1]) {
s[s.size()-i-1] = s[i];
changed[s.size()-i-1] = true;
} else {
s[i] = s[s.size()-i-1];
changed[i]= true;
}
k--;
} else {
possible = false;
}
}
}
// Check if we were unable to make s a palindrome
if(!possible) {
cout<<"-1";
return 0;
}
// Now we make s the "optimal" palindrome
// Change indices to 9s as long k is large enough
for(long long i = 0; i < s.size()/2; i++) {
if(k == 0) {
break;
}
if(s[i] != '9') {
if(changed[i] || changed[s.size()-i-1]) {
// Only need k=1 to make both 9's
s[i] = '9';
s[s.size()-i-1] = '9';
k--;
} else {
// Need k = 2
if(k >= 2) {
s[i] = '9';
s[s.size()-i-1] = '9';
k-=2;
}
}
}
}
// If n is odd, we also have to worry about a corner case where we can change the middle digit to a 9
if(n%2 == 1 && k>=1) {
s[n/2] = '9';
}
cout<<s<<"\n";
return 0;
}
Test-17 Solutions
1)#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int t;
scanf("%i",&t);
while(t--)
{
char a[100001],b[100001];
scanf("%s",a);
scanf("%s",b);
int i=0,c[26]={0},f=0;
for(i=0;a[i]!='\0';i++)
c[a[i]-'a']=1;
for(i=0;b[i]!='\0';i++)
if(c[b[i]-'a'])
{
f=1;break;
}
puts(f?"YES":"NO");
}
return 0;
}
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int t;
scanf("%i",&t);
while(t--)
{
char a[100001],b[100001];
scanf("%s",a);
scanf("%s",b);
int i=0,c[26]={0},f=0;
for(i=0;a[i]!='\0';i++)
c[a[i]-'a']=1;
for(i=0;b[i]!='\0';i++)
if(c[b[i]-'a'])
{
f=1;break;
}
puts(f?"YES":"NO");
}
return 0;
}
2)
#include<stdio.h>
int main()
{
int a[26],i,tp;
char c;
for (i=0;i<26;i++)
a[i]=0;
while ((c=getchar())!=EOF&&c!='\n')
{
a[c-97]++;
}
tp=1;
for (i=0;i<26;i++)
{
if (a[i]%2!=0&&tp==1)
tp=0;
else if (a[i]%2!=0&&tp==0)
{
printf("NO");
break;
}
}
if (i==26)
printf("YES");
return 0;
}
#include<stdio.h>
int main()
{
int a[26],i,tp;
char c;
for (i=0;i<26;i++)
a[i]=0;
while ((c=getchar())!=EOF&&c!='\n')
{
a[c-97]++;
}
tp=1;
for (i=0;i<26;i++)
{
if (a[i]%2!=0&&tp==1)
tp=0;
else if (a[i]%2!=0&&tp==0)
{
printf("NO");
break;
}
}
if (i==26)
printf("YES");
return 0;
}
3)
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int count1[100],count2[100];
int main()
{
char a[10009],b[10009];
int i;
scanf("%s%s",a,b);
i=0;
while(a[i])
{
count1[a[i]-97]++;
i++;
}
i=0;
while(b[i])
{
count2[b[i]-97]++;
i++;
}
int ans=0;
for(i=0;i<26;i++)
{
ans=ans+abs(count1[i]-count2[i]);
}
printf("%d\n",ans);
return 0;
}
Test-16 Solutions
1)
#include <math.h>
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <stdbool.h> int getcnt(long long int n) { int cnt=0; while(n) { n/=10; cnt++; } return cnt; } int main(){ int q; scanf("%d",&q); for(int a0 = 0; a0 < q; a0++){ char* s = (char *)malloc(512000 * sizeof(char)); scanf("%s",s); int len=strlen(s); if(len==1) { printf("NO\n"); continue; } int i,j; long long int a,b; int flg=0; char *tmp= (char *)malloc(512000 * sizeof(char)); for(i=1;i<=len/2;i++) { for(j=0;j<len;) { strcpy(tmp,s+j); tmp[i]='\0'; sscanf(tmp,"%lld",&a); if(a==0&&j==0&&i==1) break; //printf("%d\n",a); if(getcnt(a)!=i) break; if(j!=0) if(a!=b+1) break; b=a; j+=i; } if(j>=len) { flg=1; break; } } int flg2=0; if(flg!=1) { for(i=1;i<=len/2;i++) { flg2=0; for(j=0;j<len;) { strcpy(tmp,s+j); if(flg2==0) tmp[i]='\0'; else tmp[i+1]='\0'; // printf("%s\n",tmp); sscanf(tmp,"%lld",&a); if(a==0&&j==0&&i==1) break; if(flg2==0) { if(getcnt(a)!=i) break; } else { if(getcnt(a)!=i+1) break; } if(j!=0) if(a!=b+1) break; b=a; if(flg2==0) j=j+i; else j=j+i+1; if(b==(pow(10,i)-1)) { flg2=1; } } if(j>=len) { flg=1; break; } } } //printf("%d\n",i); if(flg==0) printf("NO\n"); else { strcpy(tmp,s); tmp[i]=0; printf("YES %s\n",tmp); } } return 0; }
2)
#include<stdio.h>
#include<string.h>
#include <math.h> #include <stdlib.h> int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int t; scanf("%d\n", &t); char* str = (char*)calloc(10000, sizeof(char)); for(int i=0;i<t;i++){ scanf("%s", str); int l = strlen(str); int flag=0; for(int i=0,j=l-1;i<l/2;i++,j--){ if( abs(str[i]-str[i+1]) != abs(str[j]-str[j-1]) ){ flag=1; break; } } if(flag) printf("Not Funny\n"); else printf("Funny\n"); } return 0; }
3)#include<stdio.h> int main() { int n,i,j,freq[150][27]={0},count; char str[200]; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%s\n",str); for(j=0;str[j]!='\0';j++) { freq[i][str[j]-97]++; } } count=0; for(i=0;i<26;i++) { for(j=0;j<n;j++) if(freq[j][i]>0) continue; else break; if(j==n) count++; } printf("%d\n",count); return 0; }
Test-15 solutions
LINK: https://www.hackerrank.com/contests/ececoding-test-15/challenges
1)
int main(){
int q;
scanf("%d",&q);
char h[] = "hackerrank";
int i,j=0;
for(int a0 = 0; a0 < q; a0++)
{
j=0;
char* s = (char *)malloc(512000 * sizeof(char));
scanf("%s",s);
for(i=0;i<strlen(s);i++)
{
if(h[j]==s[i])
{
j++;
}
}
if(j==10)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
int main(){
int q;
scanf("%d",&q);
char h[] = "hackerrank";
int i,j=0;
for(int a0 = 0; a0 < q; a0++)
{
j=0;
char* s = (char *)malloc(512000 * sizeof(char));
scanf("%s",s);
for(i=0;i<strlen(s);i++)
{
if(h[j]==s[i])
{
j++;
}
}
if(j==10)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
2)
#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[1000];
while (scanf("%s", &s[strlen(s)]) == 1);
char big[26] = {0};
char small[26] = {0};
for (int i = 0; i < strlen(s); i++) {
if (s[i] >= 'a' && s[i] <= 'z') {
small[s[i] - 'a'] = 1;
}
else if (s[i] >= 'A' && s[i] <= 'Z') {
big[s[i] - 'A'] = 1;
}
}
for (int i = 0; i < 26; i++) {
if (!(big[i] == 1 || small[i] == 1)) {
printf("not pangram");
return 0;
}
}
printf("pangram");
return 0;
}
3)
#include<stdio.h>
#include<string.h>
int main()
{
long long int i,j,k=0,n,m,l,c=0,temp,r=0,q,fl=0;
char s[100000];
long long int t[100000]={0};
scanf("%s",s);
l=strlen(s);
temp=0;
c=1;
scanf("%lld",&n);
for(i=0;i<l;i++)
{
if(s[i]!=temp)
{
temp=s[i];
t[k]=s[i]-96;
k++;
c=1;
}
else
{
c++;
t[k]=(s[i]-96)*c;
k++;
}
r++;
}
for(k=0;k<n;k++)
{
fl=0;
scanf("%lld",&q);
for(i=0;i<r;i++)
{
if(q==t[i])
{
fl=1;
printf("Yes\n");
break;
}
else if(i==r-1 && fl==0)
{
printf("No\n");
}
}
}
}
Test-14 Solution
1)
#include<stdio.h>
#include<string.h>
int main()
{
int n,m,i,j,k;
int pair,know=0,max=0;
char a[500][500];
//char sub[500][500];
scanf("%d%d",&n,&m);
for(i=0;i<n;i++) {
//for(j=0;j<m;j++)
scanf("%s",a[i]);
}
k=0;
for(i=0;i<n-1;i++) {
for(j=i+1;j<n;j++) {
while(k<m) {
if(a[i][k]=='1'|| a[j][k]=='1') { // known subjects
//incr known subs
know++;
}
k++;
}
//max=(know>max)?know:max;
if(know>max) {
pair=1;
max=know;
}
else if(know == max) {
pair++;
}
know=0;
k=0;
//printf("\n[%d---%d]",max,pair);
}
//printf("\n[%d---%d]",max,pair);
//max=pair=0;
}
printf("%d\n%d",max,pair);
return 0;
}
2)
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main(){
int n;
int k;
scanf("%d %d",&n,&k);
int count[k];
for (int i = 0; i < k; i++) {
count[i] = 0;
}
for(int i = 0; i < n; i++){
int a;
scanf("%d",&a);
count[a % k]++;
}
int max = 0;
for (int i = 0; i <= k/2; i++) {
if (i == 0 || i == k - i) {
if (count[i] >= 1) {
max += 1;
}
} else {
max += count[i] > count[k-i] ? count[i] : count[k-i];
}
}
printf("%d\n",max);
return 0;
}
3)
#include<stdio.h>
#include<string.h>
int main()
{
long long int i,j,k=0,n,m,l,c=0,temp,r=0,q,fl=0;
char s[100000];
long long int t[100000]={0};
scanf("%s",s);
l=strlen(s);
temp=0;
c=1;
scanf("%lld",&n);
for(i=0;i<l;i++)
{
if(s[i]!=temp)
{
temp=s[i];
t[k]=s[i]-96;
k++;
c=1;
}
else
{
c++;
t[k]=(s[i]-96)*c;
k++;
}
r++;
}
for(k=0;k<n;k++)
{
fl=0;
scanf("%lld",&q);
for(i=0;i<r;i++)
{
if(q==t[i])
{
fl=1;
printf("Yes\n");
break;
}
else if(i==r-1 && fl==0)
{
printf("No\n");
}
}
}
}
Test-13 Solution
Link: https://www.hackerrank.com/contests/ececoding-test-13
1)
#include<stdio.h> #include<string.h> int main() { long long int n,c=0,m=0,i; char a[101]; scanf("%s",a); scanf("%lld",&n); long long int k=strlen(a); for(i=0;i<k;i++) if(a[i]=='a') c++; long long int b=(n/k)*c; long long int d=n%k; for(i=0;i<d;i++) if(a[i]=='a') m++; long long int sum=b+m; printf("%lld",sum); }
2)
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main(){ int n,a[100],i,count=0,j,ans,max=0; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=0;i<n;i++) { count=0; for(j=i+1;j<n;j++) { if(a[i]==a[j]) { count++; // printf("%d",count); } } if(count>max) { max=count; } } ans=n-max-1; if(ans==n) { ans=0; } printf("%d",ans); return 0; }3)#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { int d1, m1, y1, d2, m2, y2; scanf("%d %d %d %d %d %d", &d1, &m1, &y1, &d2, &m2, &y2); if (y1 > y2) printf("10000"); else if (y1 < y2) printf("0"); else if (m1 > m2) printf("%d", (m1-m2)*500); else if (m1 < m2) printf("0"); else if (d1 > d2) printf("%d", (d1-d2)*15); else printf("0"); return 0; }
Test-12 Solution
LINK: https://www.hackerrank.com/ececoding-test-12
1)
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int min(int a,int b){
if(a>b)
return b;
return a;
}
int main(){
int n;
int k;
scanf("%d %d",&n,&k);
int rq,i;
int cq;
int c[8];
scanf("%d %d",&rq,&cq);
c[0] = n-cq;
c[1] = cq-1;
c[2] = n-rq;
c[3] = rq-1;
c[4] = min(rq,cq)-1;
c[5] = min(n-rq,n-cq);
c[6] = min(n-rq,cq-1);
c[7] = min(rq-1,n-cq);
for(int a0 = 0; a0 < k; a0++){
int ro;
int co;
scanf("%d %d",&ro,&co);
if(ro==rq&&(co-cq)>0){
if(c[0]>(co-cq-1))
c[0] = co-cq-1;
//printf("%d %d\n",a0,0);
}
if(ro==rq&&(co-cq)<0){
if(c[1]>(cq-co-1))
c[1] = cq-co-1;
//// printf("%d %d \n",a0,1);
}
if(co==cq&&(ro-rq)>0){
if(c[2]>(ro-rq-1))
c[2]=(ro-rq-1);
// printf("%d %d\n",a0,2);
}
if(co==cq&&(ro-rq)<0){
if(c[3]>(rq-ro-1))
c[3]=(rq-ro-1);
// printf("%d %d\n",a0,3);
}
if((co-cq)==(ro-rq)&&(ro-rq)<0){
if(c[4]>(rq-ro-1))
c[4]=(rq-ro-1);
// printf("%d %d\n",a0,4);
}
if((co-cq)==(ro-rq)&&(ro-rq)>0){
if(c[5]>(ro-rq-1))
c[5]=(ro-rq-1);
//printf("%d %d\n",a0,5);
}
if((co-cq)==(rq-ro)&&(ro-rq)>0){
if(c[6]>(ro-rq-1))
c[6]=(ro-rq-1);
//printf("%d %d\n",a0,6);
}
if((co-cq)==(rq-ro)&&(ro-rq)<0){
if(c[7]>(rq-ro-1))
c[7]=(rq-ro-1);
// printf("%d %d\n",a0,7);
}
// your code goes here
}
int sum=0;
for(i=0;i<8;i++){
sum = sum + c[i];
//printf("%d ",c[i]);
}
printf("%d",sum);
return 0;
}
2)
#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(512000 * sizeof(char));
scanf("%s",s);
char* t = (char *)malloc(512000 * sizeof(char));
scanf("%s",t);
int k,i=0,l1,l2,del,append,same;
scanf("%d",&k);
l1=strlen(s),l2=strlen(t);
if(strcmp(s,t)==0)
{
if(k%2==0 || k>=2*l1)
printf("Yes");
else
printf("No");
}
else
{
if(k>=2*l2)
printf("Yes");
else
{
while(i<l1 && i<l2)
{
if(s[i]==t[i])
i++;
else
break;
}
same=i;
del=l1-same;
append=l2-same;
if(del+append > k)
printf("No");
else
{
if((del+append)%2==0)
{
if(k%2==0)
printf("Yes");
else
printf("No");
}
else
{
if(k%2==0)
printf("No");
else
printf("Yes");
}
}
}
}
return 0;
}
3)
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int t;
int a,b,sa,sb;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&a,&b);
sa = sqrt(a), sb = sqrt(b);
if(sa*sa == a)sa--;
printf("%d\n",sb - sa);
}
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
Test-11 Solution
LInk: https://www.hackerrank.com/ececoding-test-11
1)
#include<stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a,b,c,count=0;
scanf("%d %d %d",&a,&b,&c);
for(int temp=a;temp<=b;temp++)
{
int p=temp,rev=0;
while(p!=0)
{
rev=rev*10+(p%10);
p=p/10;
}
if(abs(rev-temp)%c==0)
count++;
}
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a,b,c,count=0;
scanf("%d %d %d",&a,&b,&c);
for(int temp=a;temp<=b;temp++)
{
int p=temp,rev=0;
while(p!=0)
{
rev=rev*10+(p%10);
p=p/10;
}
if(abs(rev-temp)%c==0)
count++;
}
printf("%d",count);
return 0;
}
2)
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { int t, n, m, s; scanf("%d", &t); while (t--) scanf("%d%d%d", &n, &m, &s), printf("%d\n", (m+s-2)%n+1); return 0; }
3)
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { long n,k,q,i; long a[100000]; scanf("%ld%ld%ld",&n,&k,&q); long r=k%n; for(i=r;i<n;i++) scanf("%ld",&a[i]); for(i=0;i<r;i++) scanf("%ld",&a[i]); for(i=0;i<q;i++) { scanf("%ld",&k); printf("%ld\n",a[k]); } return 0; }
Test-10 Solutions
LINK: https://www.hackerrank.com/ececoding-test-10
TEST -101)#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; int k; int max=-1,ans=0; scanf("%d %d",&n,&k); int *height = malloc(sizeof(int) * n); for(int height_i = 0; height_i < n; height_i++){ scanf("%d",&height[height_i]); if(height[height_i]>max) max=height[height_i]; } if(max>k) ans=max-k; printf("%d",ans); // your code goes here return 0; } |
2)
#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=26; int *h = malloc(sizeof(int) * n); for(int h_i = 0; h_i < n; h_i++){ scanf("%d",&h[h_i]); } char* word = (char *)malloc(512000 * sizeof(char)); scanf("%s",word); int l=strlen(word); int i; int max=0; for(i=0;i<l;i++) { if(h[(int)word[i]-'a']>max) max=h[(int)word[i]-'a']; } printf("%d",max*l); return 0; }
3)
#include<stdio.h> int main() { int count, i, K, N, T, time; scanf("%d", &T); while (T--) { scanf("%d %d", &N, &K); count = 0; for (i = 0; i < N; i++) { scanf("%d", &time); if (time <= 0) count++; } puts((count < K) ? "YES" : "NO"); } return 0; }
Test-9 solutions
1)
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int m=5,like=0;
int main() {
int n,i;
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
scanf("%d",&n);
for(i=1;i<=n;i++)
{
like=like+(m/2);
m=(m/2)*3;
}
printf("%d",like);
return 0;
}
2)
#include <stdio.h>
int main() {
int n;
scanf("%d",&n);
int p[n+1];
for(int i = 1; i <= n; i++)
scanf("%d",&p[i]);
for(int x = 1; x <= n; x++){
for(int y = 1; y <= n; y++){
if(p[p[y]] == x)
printf("%d\n",y);
}
}
return 0;
}
3)
#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,c_i, k,ENRG=100;
scanf("%d %d",&n,&k);
int *c = malloc(sizeof(int) * n);
for( c_i = 0; c_i < n; c_i++){
scanf("%d",&c[c_i]);
}
c_i = 0;
do
{
c_i += k;
c_i = c_i%n;
if(c[c_i] == 0)
ENRG -= 1;
else
ENRG -= 3;
if(!c_i)
break;
}while(1);
printf("%d\n",ENRG);
return 0;
}
Test-8 Solutions
1)
#include<stdio.h>
int main()
{
int inp[3][3],arr1[3][3]={2,9,4,7,5,3,6,1,8},arr2[3][3]={6,1,8,7,5,3,2,9,4},arr3[3][3]={2,7,6,9,5,1,4,3,8},arr4[3][3]={4,3,8,9,5,1,2,7,6},arr5[3][3]={8,1,6,3,5,7,4,9,2},arr6[3][3]={4,9,2,3,5,7,8,1,6}
,arr7[3][3]={8,3,4,1,5,9,6,7,2},arr8[3][3]={6,7,2,1,5,9,8,3,4},i,j,cost,min=10000;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&inp[i][j]);
cost=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cost+=abs(arr1[i][j]-inp[i][j]);
}
}
if(cost<min)
min=cost;
cost=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cost+=abs(arr2[i][j]-inp[i][j]);
}
}
if(cost<min)
min=cost;
cost=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cost+=abs(arr3[i][j]-inp[i][j]);
}
}
if(cost<min)
min=cost;
cost=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cost+=abs(arr4[i][j]-inp[i][j]);
}
}
if(cost<min)
min=cost;
cost=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cost+=abs(arr5[i][j]-inp[i][j]);
}
}
if(cost<min)
min=cost;
cost=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cost+=abs(arr6[i][j]-inp[i][j]);
}
}
if(cost<min)
min=cost;
cost=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cost+=abs(arr7[i][j]-inp[i][j]);
}
}
if(cost<min)
min=cost;
cost=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cost+=abs(arr8[i][j]-inp[i][j]);
}
}
if(cost<min)
min=cost;
cost=0;
printf("%d\n",min);
return 0;
}
2)
#include <stdio.h>
int main(void) {
// your code goes here
int n;scanf("%d",&n);
int arr[101];
int i,c=0;
for(i=1;i<=100;i++)arr[i]=0;
for(i=0;i<n;i++)
{int x;
scanf("%d",&x);
arr[x]+=1;
}
for(i=1;i<100;i++)
{
int t=(arr[i]+arr[i+1]);
if(t>c)
c=t;
}
printf("%d",c);
return 0;
}
3)
#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);
int *scores = malloc(sizeof(int) * n);
for(int scores_i = 0; scores_i < n; scores_i++){
scanf("%d",&scores[scores_i]);
if(scores_i){
if(scores[scores_i]==scores[scores_i-1]){
scores_i--;
n--;
}
}
}
//printf("%d\n",n);//
int m;
scanf("%d",&m);
int *alice = malloc(sizeof(int) * m);
for(int alice_i = 0; alice_i < m; alice_i++){
scanf("%d",&alice[alice_i]);
}
int rank=n;
for(int j=0; j<m; j++){
while(alice[j]>=scores[rank-1] && rank>0){
rank--;
if(rank==0) break;
}
printf("%d\n",rank+1);
}
return 0;
}
Test-7 Solutions
1)
#include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <stdbool.h> int solve(int n, int p) { int count1=0,count2=0; for(int i=1;i<=n;i++) { if(i==p) break; if(i%2!=0) { count1++; } } for(int j=n;j>0;j--) { if(j==p) break; if(j%2==0) { count2++; } } if(count1<count2) return count1; else return count2; } int main() { int n; scanf("%d", &n); int p; scanf("%d", &p); int result = solve(n, p); printf("%d\n", result); return 0; }
2)
#include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <stdbool.h> int getMoneySpent(int* k, int* d, int s,int n,int m) { // Complete this function int max=0,min=k[0]+d[0]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if((k[i]+d[j]>max)&&(k[i]+d[j]<=s)) max=k[i]+d[j]; } } for(int x=0;x<n;x++) { for(int y=0;y<m;y++) { if((k[x]+d[y])<min) { min=k[x]+d[y]; } } } if(min>s) return -1; else return max; } int main() { int s; int n; int m; scanf("%d %d %d", &s, &n, &m); int *keyboards = malloc(sizeof(int) * n); for(int keyboards_i = 0; keyboards_i < n; keyboards_i++) { scanf("%d",&keyboards[keyboards_i]); } int *drives = malloc(sizeof(int) * m); for(int drives_i = 0; drives_i < m; drives_i++) { scanf("%d",&drives[drives_i]); } int moneySpent = getMoneySpent(keyboards, drives, s,n,m); printf("%d\n", moneySpent); return 0; } 3)
#include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <stdbool.h> char** catAndMouse(int x, int y, int z, int *result_size) { // Complete this function if (abs(x-z)>abs(y-z)) printf("Cat B\n"); else if (abs(x-z)<abs(y-z)) printf("Cat A\n"); else if (abs(x-z)==abs(y-z)) printf("Mouse C\n"); return 0; } int main() { int q; scanf("%i", &q); for(int a0 = 0; a0 < q; a0++) { int x; int y; int z; scanf("%i %i %i", &x, &y, &z); int result_size; char** result = catAndMouse(x, y, z, &result_size); } return 0; }
Test-6 solutions
1)
#include <math.h>
#include <stdio.h>
#include <string.h>
int main()
{
long long int n,k,i,arr[100000],b,sum=0,actual;
scanf("%lld %lld", &n, &k);
for (i=0;i<n;i++)
{
scanf ("%lld",&arr[i]);
}
scanf ("%lld",&b);
for (i=0;i<n;i++)
{
sum=sum+arr[i];
}
actual=(sum-arr[k])/2;
if (b>actual)
printf ("%lld",b-actual);
else if (b=actual)
printf ("Bon Appetit");
return 0;
}
2)
#include<stdio.h>
#include<math.h>
int main()
{
int i,sum=0,n,k,j;
scanf("%d",&n);
int s[n],a[101];
for(i=0;i<n;i++)
{
scanf("%d",&s[i]);
}
for(i=0;i<101;i++)
{
a[i]=0;
for(j=0;j<n;j++)
{
if(s[j]==i)
{
a[i]+=1;
}
}
if(a[i]%2==1)
{
a[i]-=1;
}
sum+=a[i];}
printf("%d",sum/2);
return 0;
}
3)
#include <stdio.h>
#include <stdlib.h>
long countingValleys(long n, char* s)
{
// Complete this function
long i,sealevel=0,nv=0;
for(i=0;i<=n;i++)
{
if(s[i]=='U')
sealevel++;
if(s[i]=='D')
sealevel--;
if(sealevel==0&&s[i]=='U') //you complete a valley only when you end by coming up to sea-level.
++nv;
}
return nv; //no. of valleys
}
int main()
{
long n;
scanf("%ld", &n);
char* s = (char *)malloc(n * sizeof(char));
scanf("%s",s);
int result = countingValleys(n, s);
printf("%d\n", result);
return 0;
}
test-5 solution
1)
#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;
int k;
int count=0;
scanf("%d %d", &n, &k);
int *a = malloc(sizeof(int) * n);
for(int a_i = 0; a_i < n; a_i++){
scanf("%d",&a[a_i]);
}
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if((a[i]+a[j])%k==0)
{
count++;
}
}
}
printf("%d",count);
// write your code here
return 0;
}
2)
#include<stdio.h>
#include<math.h>
int main()
{
int n,i,j,v[i],big;
j=0;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<6;i++)
{
v[i]=0;
}
for(i=0;i<n;i++)
{
if(a[i]==1)
{v[1]++;
}
if(a[i]==2)
{
v[2]++;
}
if(a[i]==3)
{
v[3]++;
}
if(a[i]==4)
{
v[4]++;
}
if(a[i]==5)
{
v[5]++;
}
}
big=v[1];
for(i=2;i<6;i++)
{
if(big<v[i])
{
big=v[i];
j=i;
}
}
printf("%d",j);
return 0;
}
3)
3)
#include <math.h> #include <stdio.h> #include <stdlib.h> #define LEAP(x) ((x%400==0||x%4==0&&x%100!=0)?1:0) void solve(int year) { // Complete this function if(year==1918) printf("26.09.1918"); else if(year<1917&&(year%4==0)||year>1918&&LEAP(year)) printf("12.09.%d",year); else printf("13.09.%d",year); } int main() { int year; scanf("%d", &year); int result_size; solve(year); return 0; }
TEST-4 Solution
1)
#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,t,flag,j,total=0,i;
int m;
scanf("%i %i", &n, &m);
int *a = malloc(sizeof(int) * n);
for (int a_i = 0; a_i < n; a_i++) {
scanf("%i",&a[a_i]);
}
int *b = malloc(sizeof(int) * m);
for (int b_i = 0; b_i < m; b_i++) {
scanf("%i",&b[b_i]);
}
for(t=a[n-1];t<=b[0];t++)
{
flag=0;
for(i=0;i<n;i++)
if(t%a[i] !=0)
{
flag=1;
break;
}
if(flag == 0)
{
for(j=0;j<m;j++)
if(b[j]%t !=0)
{
flag=1;
break;
}
}
if(flag == 0)
total++;
}
printf("%d\n", total);
return 0;
}
2)
#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,result_size=2,max,min;
scanf("%d",&n);
int *s = malloc(sizeof(int) * n);
int result[2];
result[0]=0;
result[1]=0;
for(int s_i = 0; s_i < n; s_i++){
scanf("%d",&s[s_i]);
if(s_i == 0)
max=min=s[s_i];
else
{
if(s[s_i]> max)
{
result[0]++;
max=s[s_i];
}
if(s[s_i]< min)
{
result[1]++;
min=s[s_i];
}
}
}
for(int i = 0; i < result_size; i++) {
if (i) {
printf(" ");
}
printf("%d", result[i]);
}
puts("");
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main() {
int n,result_size=2,max,min;
scanf("%d",&n);
int *s = malloc(sizeof(int) * n);
int result[2];
result[0]=0;
result[1]=0;
for(int s_i = 0; s_i < n; s_i++){
scanf("%d",&s[s_i]);
if(s_i == 0)
max=min=s[s_i];
else
{
if(s[s_i]> max)
{
result[0]++;
max=s[s_i];
}
if(s[s_i]< min)
{
result[1]++;
min=s[s_i];
}
}
}
for(int i = 0; i < result_size; i++) {
if (i) {
printf(" ");
}
printf("%d", result[i]);
}
puts("");
return 0;
}
3)
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int solve(int n, int s_size, int* s, int d, int m){
int i,j,sum,c=0;
for(i=0;i<=n-m;i++)
{
sum=0;
for(j=i;j<m+i;j++)
{
sum=sum+*(s+j);
}
if(sum == d)
c++;
}
return c;
}
int main() {
int n;
scanf("%d", &n);
int *s = malloc(sizeof(int) * n);
for(int s_i = 0; s_i < n; s_i++){
scanf("%d",&s[s_i]);
}
int d;
int m;
scanf("%d %d", &d, &m);
int result = solve(n, n, s, d, m);
printf("%d\n", result);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int solve(int n, int s_size, int* s, int d, int m){
int i,j,sum,c=0;
for(i=0;i<=n-m;i++)
{
sum=0;
for(j=i;j<m+i;j++)
{
sum=sum+*(s+j);
}
if(sum == d)
c++;
}
return c;
}
int main() {
int n;
scanf("%d", &n);
int *s = malloc(sizeof(int) * n);
for(int s_i = 0; s_i < n; s_i++){
scanf("%d",&s[s_i]);
}
int d;
int m;
scanf("%d %d", &d, &m);
int result = solve(n, n, s, d, m);
printf("%d\n", result);
return 0;
}
TEST-1
1)
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { int num1,num2; scanf("%d %d",&num1,&num2); int sum; sum = (num1+num2); printf("%d",sum); return 0; }
2)
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int simpleArraySum(int n, int ar_size, int* ar) {
// Complete this function
int sum=0;
int i;
for(i=0;i<n;i++){
sum=sum+ar[i];
}
return sum;
}
int main() {
int n;
scanf("%i", &n);
int *ar = malloc(sizeof(int) * n);
for(int ar_i = 0; ar_i < n; ar_i++){
scanf("%i",&ar[ar_i]);
}
int result = simpleArraySum(n, n, ar);
printf("%d\n", result);
return 0;
}
3)
#include<stdio.h>
int main()
{
int i,a[3],b[3],c=0,d=0;
for(i=0;i<3;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<3;i++)
{
scanf("%d",&b[i]);
}
for(i=0;i<3;i++)
{
if(a[i]>b[i])
{
c=c+1;
}
else if(a[i]<b[i])
{
d=d+1;
}
else
{
c=c+0;
d=d+0;
}}
printf("%d\t%d",c,d);
return 0;
}
4)
#include <stdio.h>
int main()
{
int n;
scanf("%d",&n);
if (n == 1)
printf("one");
else if (n == 2)
printf("two");
else if (n == 3)
printf("three");
else if (n == 4)
printf("four");
else if (n == 5)
printf("five");
else if (n == 6)
printf("six");
else if (n == 7)
printf("seven");
else if (n == 8)
printf("eight");
else if (n == 9)
printf("nine");
else
printf("Greater than 9 ");
return 0;
}
5)
#include <stdio.h>
int main() {
int i,n1, n2;
const char* words[] = {"one", "two", "three","four","five","six","seven","eight","nine"};
scanf("%d", &n1);
scanf("%d", &n2);
for(i=n1;i<=n2;i++){
if((i <= 9)) {
printf("%s\n", words[i-1]);
}
else if((i > 9) && (i % 2 == 0)){
printf("even\n");
}
else {
printf("odd\n");
}
}
return 0;
}
6)
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
int t;
scanf("%d",&t);
for(int a0 = 0; a0 < t; a0++){
int n;
scanf("%d",&n);
long int counter=0,i,j,k,t3,t5,t15;
t3=(n-1)/3;
t5=(n-1)/5;
t15=(n-1)/15;
i=t3*(6+(t3-1)*3);
j=t5*(10+(t5-1)*5);
k=t15*(30+(t15-1)*15);
counter=(i+j-k)/2;
printf("%ld\n",counter);
}
return 0;
}
7)
#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;
int i=0,sum=0,j,sum2=0;
scanf("%d",&n);
int a[n][n];
for(int a_i = 0; a_i < n; a_i++){
for(int a_j = 0; a_j < n; a_j++){
scanf("%d",&a[a_i][a_j]);
}
}
while(i<n)
{
sum=sum+a[i][i];
i++;
}
j=n-1,i=0;
while(i<n)
{
sum2=sum2+a[i][j];
i++;
j--;
}
printf("%d",abs(sum-sum2));
return 0;
}
8)
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
void staircase(int n) {
// Complete this function
}
int main() {
int i,n,j;
scanf("%d",&n);
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{if(j<=(n-i))
printf(" ");
else
printf("#");
}
printf("\n");
}
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
9)
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
void plusMinus(int arr_size, int* arr) {
// Complete this function
}
int main() {
int n;
float pos=0,neg=0,zer=0;
scanf("%d",&n);
int arr[n];
for(int arr_i = 0; arr_i < n; arr_i++){
scanf("%d",&arr[arr_i]);
if(arr[arr_i]>0)
pos++;
else if(arr[arr_i]<0)
neg++;
else
zer++;
}
printf("%.5f\n%.5f\n%.5f",(float)pos/n,(float)neg/n,(float)zer/n);
return 0;
}
10)
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main() {
long long int a[10],n,i,sum=0;
scanf("%lld",&n);
for(i=0;i<n;i++)
scanf("%lld",&a[i]);
for(i=0;i<n;i++)
sum=sum+a[i];
printf("%lld",sum);
return 0;
}
TEST-2 ( Link) -----HERE (CLICK)
1)
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
int hh, mm, ss ;
char t12[2];
scanf("%d:%d:%d%s", &hh, &mm, &ss, t12) ;
if (strcmp(t12,"PM")==0 && hh!=12) hh += 12 ;
if (strcmp(t12,"AM")==0 && hh==12) hh = 0 ;
printf("%02d:%02d:%02d", hh, mm, ss) ;
return 0;
}
3)
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
for(int i=0;i<100;i++){
scanf("%c",&s[i]);
}
int a=strlen(s);
int i=0;
while(i<a && a!=0){
if(s[i]==s[i+1]){
for(int j=i;j<a;j++){
s[j]=s[j+2];
s[j+1]=s[j+3];
}
i=0;
a=a-2;
}
else{
i++;
}
}
if(a==0){
printf("Empty String");
}
else{
for(int k=0;k<a;k++){
printf("%c",s[k]);
}
}
return 0;
}
5)
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
/* Head ends here */
int lonelyinteger(int a_size, int* a) {
int i,j,n=a_size,temp,flag;
for(i=0 ; i<n ; i++)
{
for(j=0 ; j<n-i-1 ; j++)
{
if(a[j]>a[j+1]) //Swapping Condition is <span class="IL_AD" id="IL_AD9">Checked</span>
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
i=0;
while(i!=n){
if(a[i]!=a[i+1])
{
return a[i];
}
i=i+2;
}
return a[n];
}
/* Tail starts here */
int main() {
int res;
int _a_size, _a_i;
scanf("%d", &_a_size);
int _a[_a_size];
for(_a_i = 0; _a_i < _a_size; _a_i++) {
int _a_item;
scanf("%d", &_a_item);
_a[_a_i] = _a_item;
}
res = lonelyinteger(_a_size, _a);
printf("%d", res);
return 0;
}
klef24.blogspot.in provide this serices of hacker rank solutions for the students of kluniversity. klef24 is one which provide the solution of students of klunoversity.klef24 also provide the link of kluniversity hackerrank solutions.
klef24 also help the solutions for the 1 year students of kluniversity.