(maintained by Lakshmi Sarvani Videla, Former Assistant Professor, KLEF and now Computer Science Lecturer, SRR and CVR Government Degree College, Vijayawada
Friday, 31 May 2019
Wednesday, 29 May 2019
Replace Mirror characters in a given string : C program
Given a string and a number N, we need to mirror the characters from N-th position
up to the length of the string in the alphabetical order.
In mirror operation,change ‘a’ to ‘z’, ‘b’ to ‘y’, and so on.
Using the fact that the distance from 'z' to that character and
distance from 'a' to mirror of the character are same.
up to the length of the string in the alphabetical order.
In mirror operation,change ‘a’ to ‘z’, ‘b’ to ‘y’, and so on.
Using the fact that the distance from 'z' to that character and
distance from 'a' to mirror of the character are same.
Program:
#include <stdio.h>
main()
{
char s1[100];
scanf("%[^\n]",s1);
int i,n;
scanf("%d",&n);
for(i=n-1;s1[i]!='\0';i++)
s1[i]='z'-s1[i]+'a';
printf("%s",s1);
}
Output:
section
2
svxgrlm
WAP to Find Highest Frequency Character in a String
#include <stdio.h>
main()
{
char s1[100];
scanf("%[^\n]",s1);
int i,h[26] ={0};
for(i=0;s1[i] != '\0';i++)
h[s1[i] -97 ]++;
int max=0,index;
for(i=0;i<26;i++)
if(h[i]>max)
{
index=i;
max=h[i];
}
printf("%c occured %d times",index+97,max);
}
Monday, 27 May 2019
Array Rotations Questions in C, Arrays: Left Rotation Hacker Rank Solution in C, Circular Array Rotation Hacker Rank Solution in C
Link to Question:
https://www.hackerrank.com/challenges/array-left-rotation/problem
https://www.hackerrank.com/challenges/ctci-array-left-rotation/problem
Program:
#include <stdio.h>
main()
{
int n,i,d;
scanf("%d%d",&n,&d);
int a[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=d;i<d+n;i++)
printf("%d ",a[i%n]); // we start printing from last index
}
==============================================================
Circular Array Rotation
Link to Question
https://www.hackerrank.com/challenges/circular-array-rotation/problem
Program
#include <stdio.h>
int main()
{
int n,i,k,q,j=0;
scanf("%d%d%d",&n,&k,&q);
int a[n],b[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
//after every n rotations, the rotated array becomes original array
int r=k%n;
for(int i=0;i<n;i++)
b[(i+r)%n]=a[i];
while(q--)
{
int id;
scanf("%d",&id);
printf("%d\n",b[id]);
}
}
You can write the above program without using array b
by using
printf("%d\n",a[(n-(k%n)+m)%n]);
===============================================================================by using
printf("%d\n",a[(n-(k%n)+m)%n]);
output:
5
1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4
1 2 3 4 5
#include <stdio.h>
main()
{
int n,i,j;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=1;i<n+1;i++)
{
for(j=i;j<i+n;j++)
printf("%d ",a[j%n]); // we start printing from first index
printf("\n");
}
}
=======================================================================
output:
5
1 2 3 4 5
5 1 2 3 4
4 5 1 2 3
3 4 5 1 2
2 3 4 5 1
1 2 3 4 5
#include <stdio.h>
main()
{
int n,i,j;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=n-1;i>=0;i--)
{
for(j=i;j<i+n;j++)
printf("%d ",a[j%n]); // we start printing from last index
printf("\n");
}
}
Sunday, 19 May 2019
candies hacker rank solution in c
Question:
https://www.hackerrank.com/challenges/candies/problem
#include <stdio.h>
int main()
{
long int n;
scanf("%ld",&n);
long int r[n],candies[n],i,sum=0;
candies[0]=1;
scanf("%ld",&r[i]);
for(i=1;i<n;i++)
{
scanf("%ld",&r[i]);
if(r[i]> r[i-1])
candies[i] = candies[i-1]+1;
else
candies[i] = 1;
}
sum=candies[n-1];
for(i=n-2;i>=0;i--)
{
if(r[i]>r[i+1] && candies[i] <= candies[i+1])
candies[i] = candies[i+1]+1;
sum=sum+candies[i];
}
printf("%ld",sum);
}
Subscribe to:
Posts (Atom)