Crack Challenge

Same Element - Two Arrays
The program must accept two integer arrays of same size N as the input. The program must print the elements if the first element in the first array is equal to the Nth element in the second array, then the second element in the first array is equal to the N-1th element in the second array and so on. If there is no element is equal then the program must print -1 as the output.

Boundary Condition(s):
2 <= N <= 100
1 <= Array Element Value <= 100000

Input Format:
The first line contains the value of N.
The second line contains N integers separated by space(s).
The third line contains N integers separated by space(s).

Output Format:
The first line contains either the integers in the array separated by space(s) or -1.

Example Input/Output 1:
Input:
5
4 12 55 98 53
43 98 34 33 4

Output:
4 98

Explanation:
The first element in the first array is 4 and the fifth element in the second array is 4 both are same
The second element in the first array is 12 and the fourth element in the second array is 33 both are different
The third element in the first array is 55 and the third element in the second array is 34 both are different
The fourth element in the first array is 98 and the second element in the second array is 98 both are same
The fifth element in the first array is 53 and the first element in the second array is 43 both are different
Hence the output is 4 98

Example Input/Output 2:
Input:
10
56 6 3 59 53 40 28 7 23 57
53 30 18 33 4 13 56 55 9 12

Output:
-1
 #include<stdio.h> #include <stdlib.h>   int main() {     int n,i,j,t=0;     scanf("%d",&n);     int a[n],b[n];     for(i=0;i<n;i++)         scanf("%d",&a[i]);     for(i=0;i<n;i++)         scanf("%d",&b[i]);          for(i=0;i<n;i++)         for(j=i;j<=i;j++){             if(a[i]==b[n-i-1]){              printf("%d ",a[i]);              t++;             }         }     if(t==0)        printf("-1");     }                                                                                                                            RunCode
Sum of Unique Elements in the Array
The program must accept a positive integer array of size N as the input. The program
must print the sum of unique elements in the array as the output.

Boundary Condition(s):
3 <= N <= 50
1 <= Array Element Value <= 99

Input Format:
The first line contains the value of N.
The sceond line contains N integers separated by space(s).

Output Format:
The first line contains the sum of unique elements in the array.

Example Input/Output 1:
Input:
5
1 2 3 4 2

Output:
8

Explanation:
The unique elements in the array are 1 3 4.
The sum of unique elements in the array is 8.
Hence the output is 8

Example Input/Output 2:
Input:
8
2 3 4 5 2 3 4 5

Output:
0
#include<stdio.h>
#include <stdlib.h>
int main()
{
    int n,i,j,flag,sum=0,cnt;
    scanf("%d",&n);
    int a[n];
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    
    for(i=0;i<n;i++)
    {
        flag=0;cnt=0;
        for(j=i+1;j<n;j++)
        {
            if(a[i]==a[j])
            {
                flag=1;
                cnt++;
                a[j]=0;
            }
        }
        if(flag==0 && cnt==0)
            sum+=a[i];
    }
    printf("%d",sum);
}                                                                                                                       RunCode
String Modification
The program must accept a string S as the input. The program must remove all the vowels and insert a character "." before each consonant in the given string S. Then the program must print the modified string as the output.
Note: All the alphabets in S are only in lowercase.
Boundary Condition(s): 3 <= Length of S <=100
Input Format: The first line contains the string S.
Output Format: The first line contains the modified string.
Example Input/Output 1: Input: hello
Output: .h.l.l
Example Input/Output 2: Input: skillrack
Output: .s.k.l.l.r.c.k
#include <stdio.h>
#include<string.h>
int main()
{
    char str[101],st[101];
    scanf("%s",str);
    int i,cnt=0,l=strlen(str);
    for(i=0;i<l;i++)
        if(str[i]!='
Code in C --> RunCode 
#include <stdio.h>
#include<string.h>
int main()
{
    char str[101],st[101];
    scanf("%s",str);
    int i,cnt=0,l=strlen(str);
    for(i=0;i<l;i++)
        if(str[i]!='a' && str[i]!='e' && str[i]!='i' && str[i]!='o' &&str[i]!='u')
        {
            st[cnt++]='.';
            st[cnt++]=str[i];
        }
    printf("%s",st);
    return 0;
}                                                                                                                           RunCode  
Flip Count - Matrix
The program must accept a matrix of size NxN as the input. The
matrix contains only 0's and 1's. The program must transpose 
the matrix then the program must print the flip count required
to make the transpose matrix and original matrix equal.
Boundary condition(s):
1 <= N <= 50

Input Format:
The first line contains the integer value of N.
The next N lines contain N integers separated by space(s).
Output Format:
The first line contains the flip count required to make
the original matrix and the transposed matrix equal.

Example Input/Output 1:
Input:
3
0 0 1  
1 1 1  
1 0 0 
Output:
4

Explanation:
Orginal matrix
0 0 1
1 1 1
1 0 0
Transpose matrix
0 1 1
0 1 0
1 1 0
The number of flips required is 4.
Hence the output is 4

Example Input/Output 2:
Input:
4
1 0 1 1
0 0 0 1
0 1 0 1
1 1 0 0
Output:
6Code in C -->                                                       Run Code
#include<stdio.h>
#include <stdlib.h>

int main()
{
  int n,i,j,cnt=0;
  scanf("%d",&n);
  int mat[n][n],m[n][n];
  for(i=0;i<n;i++)
  {
      for(j=0;j<n;j++)
      {
          scanf("%d",&mat[i][j]);
      }
  }
  for(i=0;i<n;i++)
  {
      for(j=0;j<n;j++)
      {
          if(mat[i][j]!=mat[j][i])
          {
              cnt++;
          }
      }
  }
  printf("%d",cnt);         
}                                                                                                                                   Run Code
Matrix - Upper Left to Lower Right
The program must accept the upper left triangle 
elements of an integer matrix size of NxN as 
the input. The program must fill the lower right
triangle of the matrix with the inverted upper 
left triangle elements. Finally, the program 
must print the modified matrix as the output.
Boundary Condition(s):
2 <= N <= 100
Input Format:
The first line contains the value of N.
The next N lines contain the upper left triangle 
elements separated by space(s).
Output Format:
The first N line contain N elements of the
modified matrix separated by space(s).

Example Input/Output 1:
Input:
4
3 7 3 7
3 2 8
4 8
9

Output
3 7 3 7
3 2 8 3
4 8 2 7
9 4 3 3
Explanation:
The upper left triangle elements are
3 7 3 7
3 2 8
4 8
9
The inverted upper left trianlge (lower right triangle) elements are
* * * 7
* * 8 3
* 8 2 7
9 4 3 3
Now the elements in the matrix are
3 7 3 7
3 2 8 3
4 8 2 7
9 4 3 3

Example Input/Output 2:
Input:
5
37 11 37 18 37
62 29 32 58
62 91 75
80 11
53
Output:
37 11 37 18 37
62 29 32 58 18
62 91 75 32 37
80 11 91 29 11
53 80 62 62 37Code in C -->                                                     RunCode   
#include<stdio.h>
#include <stdlib.h>

int main()
{
    int n,i,j;
    scanf("%d",&n);
    int m[n][n],mat[n][n];
    for(i=0;i<n;i++)
    {
        for(j=0;j<n-i;j++)
        {
            scanf("%d",&m[i][j]);
            mat[i][j]=m[i][j];
        }
    }
    for(i=0;i<n;i++)
    {
        int t=i;
        for(int p=i;p<i+1;p++)
            for(int k=0;k<n-i;k++)
            printf("%d ",m[p][k]);
        for(j=0;j<i;j++)
        {
            printf("%d ",mat[--t][n-i-1]);
        }
        printf("\n");
    }

}                                                                          RunCode                           
Crack Challenge 17.10.18
String Sorted in Descending Order
The program must accept N string values as the input. The program must print the N string values sorted lexicographically in descending order as the output.
Note: All the alphabets are lowercase in each string.
Boundary Condition(s): 1 <= N <= 50 1 <= Length of each string <= 1000
Input Format: The first line contains the integer N. The next N lines contain a string in each line.
Output Format: The first N lines contain the string values sorted lexicographically in descending order.
Example Input/Output 1: Input: 4 project elephant tiger time
Output: time tiger project elephant
Example Input/Output 2: Input: 5 orange watermelon  pineapple lemon strawberry
Output: watermelon strawberry pineapple orange lemon
Code in C --> Run Code
#include<stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
    int n;
    scanf("%d",&n);
    char str[1001][1001],temp[1001][1001];
    int i,j;
    for(i=0;i<n;i++)
    scanf("%s",str[i]);
    for(i=0;i<n;i++)
        for(j=i+1;j<n;j++){
            if(strcmp(str[i],str[j])>0){
                strcpy(temp[i],str[i]);
                strcpy(str[i],str[j]);
                strcpy(str[j],temp[i]);
            }
        }
    for(i=n-1;i>=0;i--)
    printf("%s\n",str[i]);
}                                                                              Run Code                                                       
Crack Challenge 16.10.18
Contiguous Integers or Not
The program must accept an integer array of size N as the input. The program must print YES if all the integers in the array can be used to form a contiguous set of integers (duplicates allowed) as the output. Else the program must print NO as the output. 
Boundary Condition(s):
2 <= N <= 100
1 <= Array Element Value <= 100000
Input Format:
The first line contains the value of N.
The second line contains N integers separated by space(s).
Output Format:
The first line contains either YES or NO. 

Example Input/Output 1:
Input:
8
5 2 3 6 4 4 6 6
Output:
YES

Explanation:
The integers 5, 2, 3, 6, 4, 4, 6 and 6 are used to form a contiguous set of integers 2, 3, 4, 4, 5, 6, 6 and 6
Hence the output YES is printed.

Example Input/Output 2:
Input:
5
10 14 12 13 13
 
Output:
NO
Code in C --> Run Code
#include<stdio.h>
#include <stdlib.h>

int main()
{
   int n;
   scanf("%d",&n);
   int a[n];
   for(int i=0;i<n;i++)
   scanf("%d ",&a[i]);
   for(int i=0;i<n;i++)
   {
       for(int j=i+1;j<n;j++)
       {
           if(a[i]>a[j])
           {
               int min=a[i];
               a[i]=a[j];
               a[j]=min;   
           }
       }
   }
   int cnt=1;
   for(int i=0;i<n;i++)
   {
       if((a[i]==a[i+1]) || (a[i]+1==a[i+1]))
       cnt++;
       
   }
   if(n<1)
   printf("NO");
   else if(cnt==n)
   printf("YES");
   else
   printf("NO");
}                                                                                 Run Code
Crack Challenge 15.10.18
X Lines Integers Pattern
The program must accept two integers N and X as the input. The program must print the desired pattern as shown in the Example Input/Output sections.
Boundary Condition(s):
1 <= N <= 100
1 <= X <= 10
Example Input/Output 1:
Input:
8 5
Output:
8 
16 17 
24 25 26 27 
32 33 34 35 36 37 38 39 
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 

Example Input/Output 2:
Input:
9 3
Output:
9 
18 19 
27 28 29 30 
36 37 38 39 40 41 42 43  
Code in C --> Run Code
#include<stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    int n,x;
    scanf("%d %d",&n,&x);
    for(int i=0;i<x;i++)
    {
        int t=n*(i+1);
        for(int j=0;j<pow(2,i);j++)
        {
            printf("%d ",t++);
        }
        printf("\n");
    }
}
Crack Challenge 14.10.18
Remove Alphabet
The program must accept two alphabets CH1 and CH2 as the input. The program must print the output based on the following conditions.
- If CH1 is either 'U' or 'u' then print all the uppercase alphabets except CH2.
- If CH1 is either 'L' or 'l' then print all the lowercase alphabets except CH2.
- For any other values of CH1 then print INVALID.

Example Input/Output 1:
Input:
U v
Output:
A B C D E F G H I J K L M N O P Q R S T U W X Y Z

Example Input/Output 2:
Input:
L C
Output:
a b d e f g h i j k l m n o p q r s t u v w x y z
Code in C --> Run Code  
#include<stdio.h>
#include <stdlib.h>

int main()
{
   char ch1,ch2,c;
   scanf("%c %c",&ch1,&ch2);
   int i;
   if(ch1=='U' ||ch1=='u')
   {
       c='A';
       for(i=0;i<26;i++)
       {
            if(c!=ch2-32 && c!=ch2)
            {
               printf("%c ",c++);
            }
            else
            c++;
        }
   }
   else if(ch1=='L' || ch1=='l')
   {
      c='a';
      for(i=0;i<26;i++)
      {
          if(c!=ch2+32 && c!=ch2)
          {
             printf("%c ",c++);
          }
          else
          c++;
      }
   }
   else
   printf("INVALID");

}
 Crack Challenge 13.10.18
 Structure Laptop with Minimum Cost
The program must accept an integer X followed by the brand name or the product number of N laptops with their costs. The program must print either the brand name or the product number of a laptop which has the minimum cost among N laptops. If X is 1 then X is followed by the brand name. If X is 2 then X is followed by the product number. Fill in the missing lines of code so that the program runs successfully.
Note: If two or more laptops have the same cost then print the information of first occurring laptop.
Input Format:
The first line contains the value of N.
The next N lines contain an integer, a string (brand name) or an integer (product number) and an integer (cost) separated by space(s).
Output Format:
The first line contains either a string (brand name) or an integer (product number) of the laptop which has the minimum cost among N laptops.
Example Input/Output 1:
Input:
4
1 Sony 70000
2 89793456 30000
2 65765767 60000
1 HP 45000
Output:
89793456

Example Input/Output 2:
Input:
3
1 Samsung 50000
2 76576777 45000
1 Acer 20000
Output:
AcerCode in C -->                                   Run Code
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
struct laptops
{
    int X;
    int cost;
    union name
    {
        char brandName[101];
        int productNumber;
    }nameorNum;
};
int main()
{
    int N,X,min=INT_MAX,minIndex=-1;
    scanf("%d",&N);
    struct laptops laptop[N];
    for(int index=0;index<N;index++)
    {
        scanf("%d",&X);
        if(X==1)
        {
            laptop[index].X=1;
            scanf("%s%d",laptop[index].nameorNum.brandName,&laptop[index].cost);
        }
        if(X==2)
        {
            laptop[index].X=2;
            scanf("%d %d",&laptop[index].nameorNum.productNumber,&laptop[index].cost);
        }
    }
    int n;
    for(int index=0;index<N;index++)
    {
        if(laptop[index].cost<min)
        {
            min=laptop[index].cost;
            n=index;
        }
    }
    if(laptop[n].X==1)
    printf("%s",laptop[n].nameorNum.brandName);
    else
    printf("%d",laptop[n].nameorNum.productNumber);
    return 0;
}

Crack Test 12.10.18
Horizontal and Vertical Zig - Zag
The program must accept an integer N as the input. The program must print the desired pattern as shown in the Example Input/Output section.
Boundary Condition(s):
1 <= N <= 100
Input Format:
The first line contains the value of N.
Output Format:
The first N lines contain the desired pattern as shown in the Example Input/Output section.
Example Input/Output 1:
Input:
5
Output:
1 2 3 4 5
2 9 8 7 6
3 8 10 11 12
4 7 11 14 13
5 6 12 13 15

Example Input/Output 2:
Input:
8
Output:
1 2 3 4 5 6 7 8
2 15 14 13 12 11 10 9
3 14 16 17 18 19 20 21
4 13 17 26 25 24 23 22
5 12 18 25 27 28 29 30
6 11 19 24 28 33 32 31
7 10 20 23 29 32 34 35
8 9 21 22 30 31 35 36Code in C -->                                                       Run Code 
#include<stdio.h>
#include <stdlib.h>

int main()
{
  int n,a=1;
  scanf("%d",&n);
  int i,j,k=1;
  int arr[n][n];
  for(i=0;i<n;i++)
  {
      if(i%2==0)
      {
          for(j=0;j<n;j++)
          {
              if(i<=j)
              {
                  arr[j][i]=a;
                  a+=1;
              }
          }
      }
      else
      {
          for(j=n-1;j>0;j--)
          {
              if(i<=j)
              {
                  arr[j][i]=a;
                  a++;
              }
          }
      }
  }
  int ar[n][n];
  for(i=0;i<n;i++)
    {
        for(j=0;j<i;j++)
        {
            printf("%d ",arr[i][j]);
        }
         ar[i][i]=arr[i][i];
         for(int p=0;p<i;p++)
         printf("");
        for(j=0;j<n-i;j++)
        {
            if(i%2==0)
            printf("%d ",ar[i][i]++);
            else
            printf("%d ",ar[i][i]--);
        }
        printf("\n");
    }
  return 0;
}
Crack Challenge 12.10.18
First Half and Second Half - String
The program must accept a string S as the input. The program must print the alphabets of the string S which are in the first half of the alphabets (a to m) and then the program must print the alphabets in the string S which are in the second half of the alphabet (n to z) as the output.
Note: The alphabets are only in lower case.
Boundary Condition(s):
1 <= Length of S <= 100

Example Input/Output 1:
Input:
electronics
Output:
elecictrons

Explantion:
The first half of the alphabets are e l e c i c
The second half of the alphabets are t r o n s
Hence the output is elecictrons

Example Input/Output 2:
Input:
keyboard
Output:
kebadyorCode in C  -->                                                  Run CodeCode in CPP  -->                                                    Run Code
//Code in C
//Code in C
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
   char a[100];
   scanf("%s",a);
   int l=strlen(a);
   for(int i=0;i<l;i++)
       if(a[i]>96 && a[i]<110)
       printf("%c",a[i]);
   for(int i=0;i<l;i++)
       if(a[i]>=110 && a[i]<123)
       printf("%c",a[i]);
}
=======================================
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
  char a[100],b[100],c[100];
  scanf("%s",a);
  int k=0,j=0,l=strlen(a);
  for(int i=0;i<l;i++)
  {
      if(a[i]>96 && a[i]<110)
      b[j++]=a[i];
  }
  for(int i=0;i<l;i++)
  {
      if(a[i]>109 && a[i]<123)
      c[k++]=a[i];
  }
  for(int i=0;i<j;i++)
  if(b[i]!='\0')
  printf("%c",b[i]);
  for(int i=0;i<k;i++)
  if(c[i]!='\0')
  printf("%c",c[i]);
  return 0;
}
//Code in Cpp
#include <string.h>
#include <iostream>
using namespace std;

int main() {
 string a;
 cin>>a;
 int k=0,j=0,l=a.length();
   for(int i=0;i<l;i++)
      if(a[i]>96 && a[i]<110)
      cout<<a[i];
  for(int i=0;i<l;i++)
      if(a[i]>109 && a[i]<123)
      cout<<a[i];
  return 0;
}
=================================================
#include <string.h>
#include <iostream>
using namespace std;

int main() {
 string a,b,c;
 cin>>a;
 int k=0,j=0,l=a.length();
   for(int i=0;i<l;i++)
  {
      if(a[i]>96 && a[i]<110)
      b[j++]=a[i];
  }
  for(int i=0;i<l;i++)
  {
      if(a[i]>109 && a[i]<123)
      c[k++]=a[i];
  }
  for(int i=0;i<j;i++)
  if(b[i]!='\0')
  cout<<b[i];
  for(int i=0;i<k;i++)
  if(c[i]!='\0')
  cout<<c[i];
  return 0;
} 
Crack Test 11.10.18
Two String Reverse Interlace Pattern
Two string values of equal length are passed as the input to the program. The program must print the desired pattern as shown in the Example Input/Output sections.
Boundary Condition(s):
1 <= Length of each string <= 1000

Example Input/Output 1:
Input:
min max
Output:
mxianm

Example Input/Output 2:
Input:
good real
Output:
gloaoedr
//Code in C
#include<stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
  char s1[1000],s2[1000];
  scanf("%s %s",s1,s2);
  int l=strlen(s1);
  for(int i=0;i<2*l;i++)
  {
        printf("%c",s1[i]);
        printf("%c",s2[l-1-i]);
  }
 }
//code in cpp
#include<string.h> 
#include <iostream>
using namespace std;
int main() {
   string s1,s2;
 cin>>s1;
 cin>>s2;
 int l=s1.length();
 for(int i=0;i<l;i++)
 {
     cout<<s1[i]<<s2[l-1-i];
 }
 return 0;
}
Crack Challenge 11.10.18
Vowels and Consonants
The program must a string S as the input. The program must print all the vowels in the string followed by all the consonants in the string as the output.
Boundary Condition(s):
1 <= Length of S <= 200

Example Input/Output 1:
Input:
elephant
Output:
eealphnt

Explanation:
Here the vowels are "eea" and the consonants are "lphnt"
Hence the output is "eealphnt".

Example Input/Output 2:
Input:
HETEROGENEOUS 
Output:
EEOEEOUHTRGNS
//Code in C
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
   char str[200];
   scanf("%s",str);
   for(int i=0;i<strlen(str);i++)
   {
       if(str[i]=='a'||str[i]=='A'||str[i]=='e'||str[i]=='E'||str[i]=='i'||str[i]=='I'||str[i]=='o'||str[i]=='O'||str[i]=='u'||str[i]=='U')
       printf("%c",str[i]);
   }
   for(int i=0;i<strlen(str);i++)
   {
       if(str[i]!='a'&&str[i]!='A'&&str[i]!='e'&&str[i]!='E'&&str[i]!='i'&&str[i]!='I'&&str[i]!='o'&&str[i]!='O'&&str[i]!='u'&&str[i]!='U')
       printf("%c",str[i]);
   }
   return 0;
}
================================================
#include<stdio.h>
#include <stdlib.h>
int main()
{
 char str[200];
 scanf("%s",str);
 int len=strlen(str);
 for(int i=0;i<len;i++)
 {
     if((str[i]==65||str[i]==97)||(str[i]==69||str[i]==101)||(str[i]==73||str[i]==105)||(str[i]==79||str[i]==111)||(str[i]==85||str[i]==117))
     {
         printf("%c",str[i]);
         str[i]='*';
     }
 }
 for(int i=0;i<len;i++)
 {
    if(str[i]!='*')
    printf("%c",str[i]);
 }
}
//Code in cpp
#include <iostream>
#include <string.h>
 
using namespace std;

int main()
{
    char s[200];
    cin>>s;
    int i;
    for(i=0;i<strlen(s);i++)
    {
        if(s[i]=='a'||s[i]=='e'||s[i]=='o'||s[i]=='u'||s[i]=='i'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U')
        cout<<s[i];
    }
    for(i=0;i<strlen(s);i++)
    {
        char c=tolower(s[i]);
        if(c!='a'&&c!='e'&&c!='i'&&c!='o'&&c!='u')
        cout<<s[i];
    }
    return 0;
}
//Code in Python3 
s=input()
for letter in s:
    if letter in'aeiouAEIOU':
        print(letter,end='')
for letter in s:
    if letter in'bcdfghjklmnpqrsvwxyzBCDFGHJKLMNPQRSTVWXYZt':
        print(letter,end='')
//Code in JAVA
import java.util.*;
public class Hello {
    public static void main(String[] args) {
  Scanner s=new Scanner(System.in);
  String str=s.next();
  String s1="",s2="";
  char[] c=str.toCharArray();
  for(int i=0;i<str.length();i++)
  {
      if(c[i]=='a'||c[i]=='e'||c[i]=='i'||c[i]=='o'||c[i]=='u'||c[i]=='A'||c[i]=='E'||c[i]=='I'||c[i]=='O'||c[i]=='U')
      s1+=c[i];
      else
      s2+=c[i];
  }
  System.out.println(s1+""+s2);
 }
}
Crack Challenge 10.10.18
Find and Replace
The program must accept three string values S1, S2 and S3 as the input. The program must find all occurrences of the string S1 in the string S3 and replace all the occurrences of the string S1 by the string S2. Finally, the program must print the modified string S3 as the output.
Note: String values are only in lower case Hint: Use strcmp() in-built function
Boundary Condition(s): 1 <= Length of S1, S2 <= 20 1 <= Length of S3 <= 1000
Example Input/Output 1: Input: tiger lion the tiger is a wild animal the tiger is known as the king of the jungle
Output: the lion is a wild animal the lion is known as the king of the jungle
Example Input/Output 2: Input: abcd xyz bcd abc abcd cde abcde abcd asdf bcde
Output: bcd abc xyz cde abcde xyz asdf bcde
#include<stdio.h>
#include <stdlib.h>
int main()
{
    char a[1001],b[1001],c[1001];
    scanf("%s %s",a,b);
    while(scanf("%s",c)>0)
    {
        if(strcmp(c,a)==0)
         printf("%s ",b);
        else
         printf("%s ",c);
    }
}
#Python
a,b=map(str,input().split())
s=list(map(str,input().split()))
for i in s:
    if i==a:
        print(b,end=" ")
    else:
        print(i,end=" ")
======================================       
p,q=input().split();r=input().split()
for i in range(len(r)):
    if r[i]==p:
        r[i]=q 
print(*r)
======================================       
 n=input().split(" ")
s=input().split(" ")
a=0
for i in s:
    if i==n[0]:
        s[a]=n[1]
    a=a+1
print(*s)
=======================================
x,y=input().split()
z=input().split()
a=list(z)
for i in range(0,len(a)):
    if(a[i]==x):
       a[i]=y
    elif(a[i]==y):
        a[i]=x

for j in a:
    
    print(j)
//Code in c++
#include <iostream>
#include<string.h>
using namespace std;
int main(int argc, char** argv)
{
char a[20],b[20],c[20];
cin>>a>>b;
while(cin>>c)
{
    if(strcmp(c,a)==0)
    cout<<b<<" ";
    else
    cout<<c<<" ";
}
}
//java
import java.io.*;
class GFG {
    public static void main(String[] args) {
       Scanner sc=new Scanner(System.in);
  String s1=sc.nextLine();
  String s2=sc.nextLine();
  String[] s3=s1.split(" ");
  String[] s4=s2.split(" ");
  for(int i=0;i<s4.length;i++){
      if(s3[0].equals(s4[i])){
          System.out.println(s3[1]+" ");}
      else{
          System.out.println(s4[i]+" ");}}}} 
Crack Challenge 09.10.18
N Equal Strings
The program must accept a string S and an integer N as the input. The program must print N equal parts of the string S if the string S can be divided into N equal parts. Else the program must print -1 as the output.
Boundary Condition(s):
2 <= Length of S <= 1000
2 <= N <= Length of S
Example Input/Output 1:
Input:
whiteblackgreen 3
Output:
white black green
Explanation:
Divide the string whiteblackgreen into 3 equal parts as white black green
Hence the output is white black green
Example Input/Output 2:
Input:
pencilrubber 5
Output:
-1 

#include <stdio.h>
#include<stdlib>
int main()
{
    int n,cnt;
    scanf("%d",&n);
    int i,j,in=0,t=0,p=0;
    int a[n],b[n],c[n],d[n];
    for(i=0;i<n;i++)
    scanf("%d ",&a[i]);
    for(i=0;i<n;i++)
    {
        if(a[i]!=-1)
        {
           cnt=1;
           for(j=i+1;j<n;j++)
           {
             if(a[i]==a[j])
             {
                cnt++;
                a[j]=-1;
             }
            }
            b[in]=a[i];
            c[in]=cnt;
         in++;
        }
    }
    for(i=0;i<in;i++)
    {
        if(c[i]%2!=0)
        {
          d[t++]=b[i];
          p++;
        }
    }
    if(p==0)
      printf("-1");
    else
     {
        for(i=0;i<t;i++)
        printf("%d ",d[i]); }

Crack Challenge 08.10.18
Jumbled PROGRAM- 001
The program given below performs a certain logic but the lines are jumbled. Reorder the lines so that the program runs successfully.
#include <stdio.h>

int N, max, ctr, curr;
int main()
scanf("%d %d", &N, &max);
{
    if(curr > max)
    {
        max = curr;
    }
}
for(ctr = 2; ctr <= N; ctr++)
{
}
printf("%d", max);
scanf("%d", &curr);

#include<stdio.h>
int main()
{
    int n,max,ctr,curr;
    scanf("%d %d",&n,&max);
    for(ctr=2;ctr<=n;ctr++)
    {
        scanf("%d",&curr);
        if(curr>max)
        {
            max=curr;
        }
    }
    printf("%d",max);
}
Crack Challenge 07.10.2018
Escape Speed
The program must accept three floating point values as G (gravitational constant), M(mass) and r(radius) of a planet. The program must calculate and print the escape speed of the object with precision up to 3 decimal places.

Formula: 
Example Input/Output 1:
Input:
1.567 2.4783 3.4671
Output:
1.497

Example Input/Output 2:
Input:
1.9038 2.7920 4.3937
Output:
1.555

#include <stdlib.h>
#include<math.h>
#include <stdio.h>
int main()
{
    float g,m,r,t;
   scanf("%f %f %f",&g,&m,&r);printf("%.3f %f %f",g,m,r);
   printf("\n");
   t=sqrt((2*g*m)/r);
   printf("%.3f",t);
}
Crack Challenge 06.10.2018
Adjacent Even Digits
The program must accept an integer N as the input. The program must print the digits surrounded by even digits on both sides. The first and last digits have only one digit adjacent digit to them. So consider only the single adjacent digit for them. If there is no digit surrounded by even digits, then the program must print -1 as the output.
Boundary Condition(s):
11 <= N <= 999999999
Input Format:
The first line contains the value N.
Output Format:
The first line contains either the digits having even adjacent digits or -1.

Example Input/Output 1:
Input:
14689025
Output:
1695

Explanation:
The adjacent digit of 1 is 4 (even number). So 1 is printed.
The adjacent digits of 4 are 1 and 6 (only 6 is even). So 4 is not printed.
The adjacent digits of 6 are 4 and 8 (both are even numbers). So 6 is printed.
The adjacent digits of 8 are 6 and 9 (only 6 is even). So 8 is not printed.
The adjacent digits of 9 are 8 and 0 (both are even numbers). So 9 is printed.
The adjacent digits of 0 are 9 and 2 (only 2 is even). So 0 is not printed.
The adjacent digits of 2 are 0 and 5 (only 0 is even). So 2 is not printed.
The adjacent digit of 5 is 2 (even number). So 5 is printed.
Hence the output is 1695.

Example Input/Output 2:
Input:
1357935
Output:
-1

#include<stdio.h>
#include <stdlib.h>
int main()
{
 int n;
 scanf("%d",&n);
 int p=n;
 int a[20],b[20],i,r,c=0,d=0;
 while(p>0)
 {
     if(p%2==0)
     d++;
     p/=10;
 }
 if(d==0)
 printf("-1");
 while(n>0)
 {
     r=n%10;
     n=n/10;
     a[c++]=r;
  }
  int k=0;
 for(i=0;i<c;i++)
 {
     if(i==0&&(a[i+1]%2==0))
     b[k++]=a[i];
     else if(i==c-1&&(a[i-1]%2==0))
     b[k++]=a[i];
     else
     {
         if((a[i-1]%2==0)&&(a[i+1]%2==0))
         b[k++]=a[i];
     }
 }
 for(i=k-1;i>=0;i--)
 printf("%d",b[i]);
 return 0; }
Crack Challenge 05.10.18
Integers Occurring Odd Number of Times
The program must accept an integer array of size N as the input. The program must print the integers that are occurring odd number of times in the array as the output. If there is no integer occurring odd number of times, then the program must print -1 as the output.
Boundary Condition(s):
3 <= N <= 100
1 <= Each integer < 100

Example Input/Output 1:
Input:
5

#include <stdio.h>
#include<stdlib>
int main()
{
    int n,cnt;
    scanf("%d",&n);
    int i,j,in=0,t=0,p=0;
    int a[n],b[n],c[n],d[n];
    for(i=0;i<n;i++)
    scanf("%d ",&a[i]);
    for(i=0;i<n;i++)
    {
        if(a[i]!=-1)
        {
           cnt=1;
           for(j=i+1;j<n;j++)
           {
             if(a[i]==a[j])
             {
                cnt++;
                a[j]=-1;
             }
            }
            b[in]=a[i];
            c[in]=cnt;
         in++;
        }
    }
    for(i=0;i<in;i++)
    {
        if(c[i]%2!=0)
        {
          d[t++]=b[i];
          p++;
        }
    }
    if(p==0)
      printf("-1");
    else
     {
        for(i=0;i<t;i++)
        printf("%d ",d[i]); }

Crack Challenge 04.10.2018 
Even Integers in Descending Order
The program must accept a positive integer array of size N as the input. The program must print the even integers in the array in descending order as the output.
Note: Atleast one integer is even in the array.
Boundary Condition(s):
5 <= N <= 50
Example Input/output 1:
Input:
5
45 67 587 48 398
Output:
398 48
Example Input/Output 2:
Input:
6
82 374 785 983 2714 9098
Output: 
9098 2714 374 82
#include<stdio.h>
#include <stdlib.h>
int main()
{
   int n;
   scanf("%d",&n);
   int i,j,t=0;
   int ar[n],a[n];
   for(i=0;i<n;i++)
   scanf("%d ",&ar[i]);
   int m=ar[0];
   for(i=0;i<n;i++)
   {
       for(j=i+1;j<n;j++)
       {
           if(ar[i]<ar[j])
           {
                   m=ar[i];
                   ar[i]=ar[j];
                   ar[j]=m;
           }
       }
   }
   for(i=0;i<n;i++)
   { 
       if(ar[i]%2==0)
       {
            a[t++]=ar[i];
       }
   }
   for(i=0;i<t;i++)
   printf("%d ",a[i]);
}
Crack Challenge 03.10.2018
Longest Word
The program must accept a string S as the input. The program print the longest word from the string S as the output. If two or more longest words are of same length then consider the first one.
Boundary Condition(s):
1 <= Length of S <= 1000
Example Input/Output 1:
Input:
Good Morning
Output:
Morning
Explanation:
The length of the word "Good" is 4 and the length of the "Morning" is 7
Hence Morning is printed as the output.
Example Input/Output 2:
Input:
good time
Output:
good

#include<stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
 char s1[1000],s2[1000],s3[1000];
 int i,j=0,l=0;
  scanf("%[^\n]s",s1);
 for(i=0;i<=strlen(s1);i++)
  {
      if(s1[i]!=32 && s1[i]!='\0')
        s2[j++]=s1[i];
     else
     {
         s2[j]='\0';
         if(strlen(s2)>l)
         {
          strcpy(s3,s2);
          l=strlen(s2);
         }
         j=0;
     }
 }
 printf("%s",s3);
 return 0;
}

No comments: