Example1:
Input:
3
malayalam
ere
level
Output:
15
Example2:
Input:
4
a
b
cd
ab
Output:
5
#include<stdio.h>int main() { int n; scanf("%d",&n); int fre1[26]={0}; for(int i=0;i<n;i++) { char str[100]; scanf("%s",str); for(int j=0;str[j]!='\0';j++) { fre1[str[j]-'a']++; } } int value=0,flag=0; for(int i=0;i<26;i++) { if(fre1[i]%2) flag=1; if(fre1[i]>=2) value=value+(2*(fre1[i]/2)); } printf("%d",flag?value+1:value);}Example1:
Input:
[{}]
Output:
Valid
Example2:
Input:
{[[])]
Output:
Invalid
#include<stdio.h>int main() { char str[1001]; int len; scanf("%s%n",str,&len); int flag=0; if(len%2) { printf("Invalid"); return 0; } else { for(int i=0;i<len/2;i++) { if(str[i]=='{'&&str[len-i-1]!='}') flag=1; else if(str[i]=='('&&str[len-i-1]!=')') flag=1; else if(str[i]=='['&&str[len-i-1]!=']') flag=1; } printf(flag?"Invalid":"Valid"); } }#include<stdio.h>#include<stdlib.h>int main() { char str[100],tempstr[100]; int len; scanf("%s%n",str,&len); int i=0,sindex=0; while(i<len) { if(isalpha(str[i])&&isdigit(str[i+1])) { int j=i+1,num=0; while(isdigit(str[j])) { num=num*10+(str[j]-'0'); j++; } for(int index=0;index<num;index++) { tempstr[sindex++]=str[i]; } i=j; } else { tempstr[sindex++]=str[i]; i++; } } tempstr[sindex]='\0'; printf("%s",tempstr);}Inputs1:
X X X T
. . . .
O O . .
. . . .
Output1:
X won
Inputs2:
X O X T
X X O O
O X O X
X X O O
Output2:
draw
Inputs3:
X O X .
O X . .
. . . .
. . . .
Output3:
Inputs4:
O O X X
O X X X
O X . T
O . . O
Output4:
Inputs5:
X X X O
. . O .
. O . .
T . . .
Output5:
Inputs6:
O X X X
X O . .
. . O .
. . . O
Output6:
O won
#include<stdio.h>int main() { char mat[4][4]; int flagx=0,flago=0,t=0; for(int row=0;row<4;row++) { int countx=0,counto=0,countt=0; for(int col=0;col<4;col++) { char str[100]; scanf("%s",str); mat[row][col]=str[0]; if(mat[row][col]=='X') countx++; else if(mat[row][col]=='O') counto++; else if(mat[row][col]=='T') { countt++; t=1; } } if(countx==4||(countx==3&&countt==1)) { printf("X is won"); return 0; } else if(counto==4||(counto==3&&countt==1)) { printf("O is won"); return 0; } } for(int col=0;col<4;col++) { int countx=0,counto=0,countt=0; for(int row=0;row<4;row++) { if(mat[row][col]=='X') countx++; else if(mat[row][col]=='O') counto++; else if(mat[row][col]=='T') { countt++; t=1; } } if(countx==4||(countx==3&&countt==1)) { printf("X is won"); return 0; } else if(counto==4||(counto==3&&countt==1)) { printf("O is won"); return 0; } } int i=0; int countx=0,counto=0,countt=0; while(i<4) { if(mat[i][i]=='X') countx++; else if(mat[i][i]=='O') counto++; else if(mat[i][i]=='T') { countt++; t=1; } i++; } if(countx==4||(countx==3&&countt==1)) { printf("X is won"); return 0; } else if(counto==4||(counto==3&&countt==1)) { printf("O is won"); return 0; } i=0; int j=3,countO=0,countX=0,countT=0; while(i<=3) { if(mat[i][j]=='X') countX++; else if(mat[i][j]=='O') countO++; else if(mat[i][j]=='T') { countT++; t=1; } i++; j--; } if(countX==4||(countX==3&&countT==1)) { printf("X is won"); return 0; } else if(countO==4||(countO==3&&countT==1)) { printf("O is won"); return 0; } if(!t) { printf("Game is incomplete"); return 0; } printf("Draw"); }Example:
Input:
mayday
daybreak
Output:
day
Example:
Input:
kgear
gearr
Output:
gear
#include<stdio.h>#include<string.h>int main() { char str1[1001],str2[1001]; scanf("%s %s",str1,str2); int len=strlen(str1); int i=0,j=0; while(i<len) { if(str1[i]==str2[j]) { i++; j++; } else { i++; j=0; } } str2[j]='\0'; printf("%s",str2);}Two soccer teams A and B play a series of matches over a period of time. In a match, the winning team gets 3 points. If the match ends in a tie (draw) with both teams scoring same goals, then both the teams get one point each. The losing team does not get any point. The program must accept the goals scored by both team A and B in certain number of matches and print the cumulative scores of team A and B separated by a space.
Example Input/Output 1:
Input:
3 5 1 3 2 0
Output:
7 1
Explanation: Team A drew the first match and hence both team A and B got one point each. Team A won both matches two and three and hence got additional 6 points. So the final score of team A is 7 and team B is 1.
#include<stdio.h>int main() { int arr[1001],index=-1; while(scanf("%d",&arr[++index])>0); int start=0,end=index/2,team1=0,team2=0; while(start<index/2) { if(arr[start]>arr[end]) team1+=3; else if(arr[start]==arr[end]) { team1+=1; team2+=1; } else if(arr[start]<arr[end]) team2+=3; start++; end++; } printf("%d %d",team1,team2);}A string S is passed as the input. The program must print the number of articles in S. The string S passed as the input NEED NOT be correct grammatically.
Example Input/Output 1:
Input:
I went to a movie yesterday along with an old man.
Output:
2
Example Input/Output 2:
Input:
In 1885 it was banished from the shelves of the Concord Public Library, an act that attracted a lot of publicity and discussion in the press. It is still frequently in the news, as various schools and school systems across the country either ban it from or restore it to their classrooms. The texts and illustrations below attempt to capture both the novel's achievement and some aspects of its controversiality.
Output:
8
Example Input/Output 3:
Input:
Count the articles which are a, an and the.
Output:
4
#include<stdio.h>#include <stdlib.h>int isarticle(char str[]){ int len=strlen(str); if(len==1&&tolower(str[0])=='a') return 1; else if(len==2&&tolower(str[0])=='a'&&str[1]=='n') return 1; else if(len==3&&tolower(str[0])=='t'&&str[1]=='h'&&str[2]=='e') return 1; else return 0;}int main(){char str[100];int count=0;while(scanf("%s",str)>0){ int len=strlen(str); if(str[len-1]=='.'||str[len-1]==',') str[len-1]=NULL; if(isarticle(str)) count++;}printf("%d",count);}To encrypt messages Jil will first decide on the number of columns C to use. Then Jil will pad the message with letters chosen randomly so that they form a rectangular matrix. Finally Jil will write down the message navigating the rows from left to right and then from right to left. The program must accept the encrypted message M as input and then extract and print the original message (along with any additional padding letters) from the encrypted one based on the value of C.
Sample Input/Output:
Example 1:
Input:
midinadiazne
3
Output:
madeinindiaz
Explanation:
m i d
a n i
d i a
e n z
Here z is the padding letter. The navigating across the rows mid (left to right) ina (right to left) and so on we come up with the encrypted message midinadiazne.
Example 2:
Input:
loaesfbnaiordilertenrdhdw
5
Output:
lionroaredandthebirdsflew
Explanation:
l o a e s
i a n b f
o r d i l
n e t r e
r d h d w
Here there are no padding letters. The navigating across the rows left to right and then from right to left we get loaesfbnaiordilertenrdhdw
#include<stdio.h>#include <stdlib.h>int main(){char str[1001],msge[100][100];int n,index=-1;scanf("%s\n%d",str,&n);for(int i=0;i<strlen(str)/n;i++){ for(int j=0;j<n;j++) { if(i%2==0) msge[i][j]=str[++index]; else msge[i][j]=str[index--]; } index=index+n;}for(int col=0;col<n;col++){ for(int row=0;row<strlen(str)/n;row++) printf("%c",msge[row][col]);}}An array of N integers (both positive and negative) is given as the input to the program.
The program must print the maximum sum of the subarray in the given array as the output.
Boundary Condition(s): 1 <= N <= 1000
Input Format:
The first line contains N.
The second line contains N integers separated by space.
Output Format:
The first line contains the maximum subarray sum.
Example Input/Output 1:
Input:
7 2 1 -6 4 -2 8 -2
Output:
10
Explanation:
The subarray 4 -2 8 has the maximum sum 10.
Example Input/Output 2:
Input:
10 73 64 18 -18 44 -4 -17 -28 -59 54
Output:
181
#include <stdio.h>int main() {int N;scanf("%d",&N);int num,sum=0,max=0;for(int index=0;index<N;index++){scanf("%d",&num);sum=sum+num;if(sum<0){sum=0;}else if(sum>max){max=sum;}}printf("%d",max);}Example Input/Output 1:
Input:
i a z
Output:
i h g f e d c b a
a b c d e f g h i j k l m n o p q r s t u v w x y z
i j k l m n o p q r s t u v w x y z
Example Input/Output 2:
Input:
a j p
Output:
a b c d e f g h i j
j k l m n o p
a b c d e f g h i j k l m n o p
#include<stdio.h>#include <stdlib.h>void print(char start,char end){ if(start=='z'&&end=='z') { printf("z\n"); return; } if(start=='z') { printf("z "); start='a'; } if(start<end) { while(start<=end) printf("%c ",start++); } else { while(start>=end) printf("%c ",start--); } printf("\n");}int main(){char ch1,ch2,ch3;scanf("%c %c %c",&ch1,&ch2,&ch3);print(ch1,ch2);print(ch2,ch3);print(ch1,ch3);}#include<stdio.h>#include<string.h>int main() { char a[100]; int fresmall[26]={0}; int frecap[26]={0}; scanf("%s",a); for(int i=0;i<strlen(a);i++) { if(a[i]>='a'&&a[i]<='z') { fresmall[a[i]-'a']++; } else if(a[i]>='A'&&a[i]<='Z') { frecap[a[i]-'A']++; } } printf("FREQUENCY OF SMALL LETTER IN A GIVEN WORD:\n"); for(int i=0;i<26;i++) { if(fresmall[i]>0) { printf("%c->%d\n",i+'a',fresmall[i]); } } printf("\nFREQUENCY OF CAPTIAL LETTER IN A GIVENWORD:\n"); for(int i=0;i<26;i++) { if(frecap[i]>0) { printf("%c->%d\n",i+'A',frecap[i]); } } printf("\nFREQUENCY IN THE ORDER OF GIVEN INPUT:\n"); for(int i=0;i<strlen(a);i++) { for(int j=0;j<26;j++) { if(a[i]==j+'a'&&fresmall[j]!=0) { printf("%c->%d\n",a[i],fresmall[j]); fresmall[j]=0; } else if(a[i]==j+'A'&&frecap[j]!=0) { printf("%c->%d\n",a[i],frecap[j]); frecap[j]=0; } } } }#include<stdio.h>int main() { char a[100]; int fre[26]={0}; int total=0; scanf("%[^\n]",a); for(int i=0;i<strlen(a);i++) { if(a[i]>='a'&&a[i]<='z') { total=total+!fre[a[i]-'a']; fre[a[i]-'a']=1; } } if(total==26) { printf("Given is pangram"); } else { printf("not a pangram"); } }Example:
Input
aaabc
Output
abaca
#include<stdio.h>int main() { char str[100]; int len,k=0,flag=0; scanf("%s%n",str,&len); while(k<len) { for(int i=0;i<len;i++) { if(i==len-1) { continue; } else if(str[i]==str[i+1]) { flag=1; break; } } if(flag==0) { printf("%s",str); return 0; } else { for(int i=1;i<len;i++) { if(i==len-1) { continue; } else if(str[i-1]==str[i]&&str[i]!=str[i+1]) { char temp=str[i]; str[i]=str[i+1]; str[i+1]=temp; } } } flag=0; k++; } printf("Not possible");}Example:
Input
mddam
Output
0 //if a string has odd length it can have one odd character
Example:
Input
aabbccasd
Output
2
#include<stdio.h>#include<string.h>int main() { char str[100]; int fre[26]={0},len,count=0; scanf("%s%n",str,&len); for(int i=0;i<strlen(str);i++) { if(str[i]>='a'&&str[i]<='z') { fre[str[i]-'a']++; } } for(int i=0;i<26;i++) { if(fre[i]!=0) { count=count+(fre[i]%2); } } if(len%2==1)//for odd sequence { printf("%d",count-1); } else//for even sequence { printf("%d",count); }}#include<stdio.h>#include<string.h>int arr[9],count=0;void check(int num){ if(!arr[num]) { printf("%d",num); arr[num]=1; count=1; }}int main() { char str[100]; scanf("%s",str); for(int index=strlen(str)-1;index>0;index--) { if(str[index]==str[index-1]) check((str[index]-'0')); } if(!count) printf("-1");}#include<stdio.h>#include <stdlib.h>#include<string.h>int main(){char str[100],max[100],len=0;while(scanf("%s",str)>0){ int slen=strlen(str); if(slen>len) { len=slen; strcpy(max,str); }}printf("%s",max);}Example:
Input
skillrack
race
Output:
skillk
#include<stdio.h>#include <stdlib.h>#include<string.h>int main(){char a[1001],b[100];scanf("%s%s",a,b);char temp[100];int k=0;for(int i=0;i<strlen(b);i++){ for(int j=0;j<strlen(a);j++) { if(b[i]==a[j]) { a[j]='0'; } }}for(int i=0;i<strlen(a);i++){ if(a[i]!='0') temp[k++]=a[i];}temp[k]='\0';printf("%s",temp);}#include<stdio.h>int main() { int n; scanf("%d\n",&n); int num[n],wei[n]; for(int i=0;i<n;i++) { int weigth=0; scanf("%d\n",&num[i]); if(num[i]%4==0&&num[i]%6==0) { weigth=weigth+4; } if(num[i]%2==0) { weigth=weigth+3; } for(int j=2;j<=num[i]/2;j++) { if(num[i]==(j*j)) { weigth=weigth+5; } } wei[i]=weigth; } for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { if(wei[i]>wei[j]) { int temp=wei[i]; wei[i]=wei[j]; wei[j]=temp; temp=num[i]; num[i]=num[j]; num[j]=temp; } else if(wei[i]==wei[j]) { if(num[i]>num[j]) { int temp=wei[i]; wei[i]=wei[j]; wei[j]=temp; temp=num[i]; num[i]=num[j]; num[j]=temp; } } } } for(int i=n-1;i>=0;i--) { printf("%d\n",num[i]); }}#include<stdio.h>#include<string.h>int main() { char a[1001]; char check[100]; int size,k=0; scanf("%s\n%s\n%d",a,check,&size); char matrix[size][size]; for(int i=0;i<size;i++) { if(a[k]=='\0') { break; } for(int j=0;j<size;j++) { matrix[i][j]=a[k]; k++; } } for(int i=0;i<size;i++) { int flag1=0,start1=0; for(int j=0;j<size;j++) { if(matrix[i][j]==check[flag1]) { if(flag1==0) { start1=j; } flag1++; } } if(flag1==strlen(check)) { printf("%d.%d %d.%d",i,start1,i,start1+(strlen(check)-1)); return 0; } } for(int i=0;i<size;i++) { int flag2=0,start2=0,colum; for(int j=0;j<size;j++) { if(matrix[j][i]==check[flag2]) { if(flag2==0) { start2=j; colum=i; } flag2++; } } if(flag2==strlen(check)) { printf("%d.%d %d.%d",start2,colum,start2+(strlen(check)-1),colum); return 0; } } }Example:
Input:
(1,3) (3,1) (1,2) (1,2)
Output:
(1,3) (3,1)
#include<stdio.h>#include <stdlib.h>int main(){int x,y,a[101],i=0;while(scanf("(%d,%d) ",&x,&y)>0){ a[i++]=x; a[i++]=y;}for(int j=1;j<i;j++){ for(int k=j+1;k<i;k++) { if(j==i-1||k==i-1) { continue; } else if(a[j-1]==a[k+1]&&a[j]==a[k]) { printf("(%d,%d) (%d,%d)",a[j-1],a[j],a[k],a[k+1]); printf("\n"); break; } }}}import java.util.*;class Check{ public int x,y; Check(int x,int y) { this.x=x; this.y=y; }}public class Binary{ Set<String> set1=new LinkedHashSet<String>(); public void find(Check c) { String p="{"+c.x+","+c.y+"}"; set1.add(p); String rev="{"+c.y+","+c.x+"}"; if(set1.contains(rev)) { System.out.println(rev+"|"+p); System.out.println(); } } public static void main (String[] args) { Scanner sc=new Scanner(System.in); Binary b=new Binary(); while(sc.hasNext()) { int x=sc.nextInt(); int y=sc.nextInt(); Check c=new Check(x,y); b.find(c); } }} import java.util.*;class Check{ public int x,y; Check(int x,int y) { this.x=x; this.y=y; }}public class Binary{ static Set<String> set1=new LinkedHashSet<String>(); static int count=0; public static void find(Check []c) { for(Check m:c) { String p="{"+m.x+","+m.y+"}"; set1.add(p); String rev="{"+m.y+","+m.x+"}"; if(set1.contains(rev)) { System.out.println(p+" | "+rev); System.out.println(); count=1; } } } public static void main (String[] a) { Scanner sc=new Scanner(System.in); Binary b=new Binary(); System.out.println("Enter the number of pairs"); int num=sc.nextInt(); Check [] c=new Check[num]; if(num<=1) { System.out.println("Invalid Input"); System.exit(0); } System.out.println("Symmetric Pairs:"); for(int i=0;i<num;i++) { int x=sc.nextInt(); int y=sc.nextInt(); c[i]=new Check(x,y); } find(c); if(count==0) { System.out.println("No Symmetric pairs found"); } }}A string S and N words are given as the input to the program. The program must print Yes if each word among the N words can be formed using the alphabets in the string S. Else the program must print No as the output.
Boundary Condition(s): 1 <= Length of each string <= 1000 1 <= N <= 100
Input Format: The first line contains string S. The second line contains N. The next N lines contain N string values one in each line.
Output Format: The first line contains Yes or No based on the given conditions.
Example Input/Output 1:
Input:
telephonegrammary
3
telegram
memory
elegant
Output:
Yes
Example Input/Output 2:
Input:
amsterdamacademy
2
amy
racer
Output:
No Explanation: racer has two r which are not available in the string S.
#include <stdio.h>#include<string.h> int main(){ int N,strfre[26]={0}; char str[1001]; scanf("%s\n",str); scanf("%d\n",&N); for(int i=0;i<strlen(str);i++) { strfre[str[i]-'a']++; } for(int i=0;i<N;i++) { char temp[100]; int tempfre[26]={0}; scanf("%s\n",temp); for(int k=0;k<strlen(temp);k++) { tempfre[temp[k]-'a']++; } for(int d=0;d<26;d++) { if(tempfre[d]!=0&&tempfre[d]>strfre[d]) { printf("NO"); return 0; } } } printf("YES"); }