Posts

Showing posts from December, 2020

bake+eeraf=abeeefkr.. code

  #include   < iostream > #include   < cstring > using   namespace  std; int  main() {      // Complete the program      char  str1[ 100 ];      char  str2[ 100 ];     cin>>str1>>str2;           //cout<<str1<<endl<<str2<<endl;     cout<<strlen(str1)<< " " <<strlen(str2)<<endl;           int  freq[ 256 ]={ 0 };           for ( int  i= 0 ;i<strlen(str1);i++){          int  ascii=str1[i];         freq[ascii]++;     }      for ( int  i= 0 ;i<strlen(str2);i++){       ...

Compress the String

  Write a program to do basic string compression. For a character which is consecutively repeated more than once, replace consecutive duplicate occurrences with the count of repetitions. Exmple: If a string has 'x' repeated 5 times, replace this "xxxxx" with "x5". The string is compressed only when the repeated character count is more than 1. Note : Consecutive count of every character in the input string is less than or equal to 9. Input Format: The first and only line of input contains a string without any leading and trailing spaces. Output Format: The only line of output prints the updated string. Note: You are not required to print anything. It has already been taken care of. Constraints: 0 <= N <= 10^6 Where N is the length of the input string. Time Limit: 1 second Sample Input 1: aaabbccdsa Sample Output 1: a3b2c2dsa Sample Input 2: aaabbcddeeeee Sample Output 2: a3b2cd2e5 #include <iostream> #include <cstring> using namespace std;...

Highest Occuring Character

For a given a string(str), find and return the highest occurring character. Example: Input String: "abcdeapapqarr" Expected Output: 'a' Since 'a' has appeared four times in the string which happens to be the highest frequency character, the answer would be 'a'. If there are two characters in the input string with the same frequency, return the character which comes first. Consider: Assume all the characters in the given string to be in lowercase always. Input Format: The first and only line of input contains a string without any leading and trailing spaces. Output Format: The only line of output prints the updated string. Note: You are not required to print anything explicitly. It has already been taken care of. Constraints: 0 <= N <= 10^6 Where N is the length of the input string. Time Limit: 1 second Sample Input 1: abdefgbabfba Sample Output 1: b Sample Input 2: xy Sample Output 2: x  #include <iostream> #include <cstring> using ...

Remove character

 #include <iostream> #include <cstring> using namespace std; #include "solution.h" For a given a string(str) and a character X, write a function to remove all the occurrences of X from the given string. The input string will remain unchanged if the given character(X) doesn't exist in the input string. Input Format: The first line of input contains a string without any leading and trailing spaces. The second line of input contains a character(X) without any leading and trailing spaces. Output Format: The only line of output prints the updated string. Note: You are not required to print anything explicitly. It has already been taken care of. Constraints: 0 <= N <= 10^6 Where N is the length of the input string. Time Limit: 1 second Sample Input 1: aabccbaa a Sample Output 1: bccb Sample Input 2: xxyyzxx y Sample Output 2: xxzxx #include<cstring> int lenght(char str[]){     int count=0;     for (int i=0;str[i]!='\0';i++){     ...

Reverse Each Word

Aadil has been provided with a sentence in the form of a string as a function parameter. The task is to implement a function so as to print the sentence such that each word in the sentence is reversed. Example: Input Sentence: "Hello, I am Aadil!" The expected output will print, ",olleH I ma !lidaA". Input Format: The first and only line of input contains a string without any leading and trailing spaces. The input string represents the sentence given to Aadil. Output Format: The only line of output prints the sentence(string) such that each word in the sentence is reversed. Constraints: 0 <= N <= 10^6 Where N is the length of the input string. Time Limit: 1 second Sample Input 1: Welcome to Coding Ninjas Sample Output 1: emocleW ot gnidoC sajniN Sample Input 2: Always indent your code Sample Output 2: syawlA tnedni ruoy edoc  #include <iostream> #include <cstring> using namespace std; #include "solution.h" #include<cstring> int le...

Remove Consecutive Duplicates

For a given string(str), remove all the consecutive duplicate characters. Example: Input String: "aaaa" Expected Output: "a" Input String: "aabbbcc" Expected Output: "abc"  Input Format: The first and only line of input contains a string without any leading and trailing spaces. All the characters in the string would be in lower case. Output Format: The only line of output prints the updated string. Note: You are not required to print anything. It has already been taken care of. Constraints: 0 <= N <= 10^6 Where N is the length of the input string. Time Limit: 1 second Sample Input 1: aabccbaa Sample Output 1: abcba Sample Input 2: xxyyzxx Sample Output 2: xyzx  #include <iostream> #include <cstring> using namespace std; #include "solution.h" #include<cstring> void removeConsecutiveDuplicates(char input[]) {     // Write your code here     char cmp=input[0];          int i=0,j=1;     for(int i=0;i...

Check Permutation

  For a given two strings, 'str1' and 'str2', check whether they are a permutation of each other or not. Permutations of each other Two strings are said to be a permutation of each other when either of the string's characters can be rearranged so that it becomes identical to the other one. Example: str1= "sinrtg" str2 = "string" The character of the first string(str1) can be rearranged to form str2 and hence we can say that the given strings are a permutation of each other. Input Format: The first line of input contains a string without any leading and trailing spaces, representing the first string 'str1'. The second line of input contains a string without any leading and trailing spaces, representing the second string 'str2'. Note: All the characters in the input strings would be in lower case. Output Format: The only line of output prints either 'true' or 'false', denoting whether the two strings are a permu...

Largest Row or Column

For a given two-dimensional integer array/list of size (N x M), you need to find out which row or column has the largest sum(sum of all the elements in a row/column) amongst all the rows and columns. Note : If there are more than one rows/columns with maximum sum, consider the row/column that comes first. And if ith row and jth column has the same largest sum, consider the ith row as answer. Input Format : The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. First line of each test case or query contains two integer values, 'N' and 'M', separated by a single space. They represent the 'rows' and 'columns' respectively, for the two-dimensional array/list. Second line onwards, the next 'N' lines or rows represent the ith row values. Each of the ith row constitutes 'M' column values separated by a single space. Output Format : For each test case, If row sum ...

Sum of Two Arrays

  Two random integer arrays/lists have been given as ARR1 and ARR2 of size N and M respectively. Both the arrays/lists contain numbers from 0 to 9(i.e. single digit integer is present at every index). The idea here is to represent each array/list as an integer in itself of digits N and M. You need to find the sum of both the input arrays/list treating them as two integers and put the result in another array/list i.e. output array/list will also contain only single digit at every index. Note: The sizes N and M can be different. Output array/list(of all 0s) has been provided as a function argument. Its size will always be one more than the size of the bigger array/list. Place 0 at the 0th index if there is no carry. No need to print the elements of the output array/list. Input format : The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. First line of each test case or query contains an integer 'N...

Wave Print

  For a given two-dimensional integer array/list of size (N x M), print the array/list in a sine wave order, i.e, print the first column top to bottom, next column bottom to top and so on. Input format : The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. First line of each test case or query contains two integer values, 'N' and 'M', separated by a single space. They represent the 'rows' and 'columns' respectively, for the two-dimensional array/list. Second line onwards, the next 'N' lines or rows represent the ith row values. Each of the ith row constitutes 'M' column values separated by a single space. Output format : For each test case, print the elements of the two-dimensional array/list in the sine wave order in a single line, separated by a single space. Output for every test case will be printed in a seperate line. Constraints : 1 <= t <= ...

Spiral Print

Image
  For a given two-dimensional integer array/list of size (N x M), print it in a spiral form. That is, you need to print in the order followed for every iteration: a. First row(left to right) b. Last column(top to bottom) c. Last row(right to left) d. First column(bottom to top)  Mind that every element will be printed only once. Refer to the Image: Input format : The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. First line of each test case or query contains two integer values, 'N' and 'M', separated by a single space. They represent the 'rows' and 'columns' respectively, for the two-dimensional array/list. Second line onwards, the next 'N' lines or rows represent the ith row values. Each of the ith row constitutes 'M' column values separated by a single space. Output format : For each test case, print the elements of the two-dimensional array/list in...

Print All Substrings

  For a given input string(str), write a function to print all the possible substrings. Substring A substring is a contiguous sequence of characters within a string. Example: "cod" is a substring of "coding". Whereas, "cdng" is not as the characters taken are not contiguous Input Format: The first and only line of input contains a string without any leading and trailing spaces. All the characters in the string would be in lower case. Output Format: Print the total number of substrings possible, where every substring is printed on a single line and hence the total number of output lines will be equal to the total number of substrings. Note: The order in which the substrings are printed, does not matter. Constraints: 0 <= N <= 10^6 Where N is the length of the input string. Time Limit: 1 second Sample Input 1: abc Sample Output 1: a ab abc b bc c  Sample Input 2: co Sample Output 2: c co o #include <iostream> #include <cstring> us...