Posts

Geek For Geeks - Convert to Roman No

Image
  Given an integer n, your task is to complete the function   convertToRoman   which prints the corresponding roman number of n. Various symbols and their values are given below. I 1 V 5 X 10 L 50 C 100 D 500 M 1000   Example 1: Input: n = 5 Output: V   Example 2: Input: n = 3 Output: III   Your Task: Complete the function  convertToRoman()  which takes an integer N as input parameter and returns the equivalent roman.    Expected Time Complexity:  O(log 10 N) Expected Auxiliary Space:  O(log 10 N * 10)   Constraints: 1<= n <=3999 Solution: Java

Geek for geeks | Reverse words in a given string

Image
  Given a String S, reverse the string without reversing its individual words. Words are separated by dots. Example 1: Input: S = i.like.this.program.very.much Output: much.very.program.this.like.i Explanation: After reversing the whole string(not individual words), the input string becomes much.very.program.this.like.i Example 2: Input: S = pqr.mno Output: mno.pqr Explanation: After reversing the whole string , the input string becomes mno.pqr Your Task: You dont need to read input or print anything. Complete the function  reverseWords()  which takes string S as input parameter and returns a string containing the words in reversed order. Each word in the returning string should also be separated by '.'  Expected Time Complexity:  O(|S|) Expected Auxiliary Space:  O(|S|) Constraints: 1 <= |S| <= 2000 Solution: Java

Hackerrank | Super Reduced Strings

  Reduce a string of lowercase characters in range   ascii[‘a’..’z’] by doing a series of operations. In each operation, select a pair of adjacent letters that match, and delete them. Delete as many characters as possible using this method and return the resulting string. If the final string is empty, return  Empty String Example . aab  shortens to  b  in one operation: remove the adjacent  a  characters. Remove the two 'b' characters leaving 'aa'. Remove the two 'a' characters to leave ''. Return 'Empty String'. Function Description Complete the  superReducedString  function in the editor below. superReducedString has the following parameter(s): string s:  a string to reduce Returns string:  the reduced string or  Empty String Input Format A single string,  . Constraints Sample Input 0 aaabccddd Sample Output 0 abd Explanation 0 Perform the following sequence of operations to get the final string: aaabccddd → abccd...

Geek For Geeks | Trailing Zeroes in a factorial of a number | Java

For an integer  N  find the number of trailing zeroes in  N!. Example 1: Input: N = 5 Output: 1 Explanation: 5! = 120 so the number of trailing zero is 1. Expected Time Complexity:  O(logN) Expected Auxiliary Space:  O(1) Constraints: 1 <= N <= 10 9   public class  TrailingZeroes { public static void main (String[] args) { int n = 384 ; int count = 0 ; for ( int i = 5 ; n/i >= 1 ; i *= 5 ) { count += n/i ; } System. out .println(count) ; } }

HackerRank | Minimum Distances

  We define the distance between two array values as the number of indices between the two values. Given   , find the minimum distance between any pair of equal elements in the array. If no such value exists, print   . For example, if  , there are two matching pairs of values:  . The indices of the  's are   and  , so their distance is  . The indices of the  's are   and  , so their distance is  . Function Description Complete the  minimumDistances  function in the editor below. It should return the minimum distance between any two matching elements. minimumDistances has the following parameter(s): a : an array of integers Input Format The first line contains an integer  , the size of array  . The second line contains   space-separated integers  . Constraints Output Format Print a single integer denoting the minimum   in  . If no such value exists, print  . S...