Hello friends how are you , today in this blog i am going to teach, many important programs which is being asked by many companies , If you are preparing to join any company then this type of question will help you definitely.
I am writing this blog in December 2020 and today theses question are asked by a Popular Software company in their coding round question.
Question 1: Nth Character in Decrypted String
Every character in the input string is followed by its frequency . Write a function to decrypt the string the find the nth character of the decrypted string. If no character exists at that position then return -1.
Example :
If the string is "a3b2" then decrypted string will be "aaabb".
String: a2b2c2
Number: 4
Output: b
Explanation: The decrypted string of a2b2c2 will be aabbcc hence the 4th character in decrypted string is b.
Program :
class Dedecr_strptd { public static String returnNthString(String str,int no) { String decr_str=""; char ch[]=str.toCharArray(); for (int i = 0; i < ch.length; i++) { if(Character.isLetter(ch[i])) { decr_str=decr_str+ch[i]; } else { for (int j = 1; j < Character.getNumericValue(ch[i]); j++) { decr_str=decr_str+ch[i-1]; } } } if(decr_str.length()<no) return "-1"; else return decr_str.charAt(no-1)+""; } public static void main(String[] args) { System.out.println(returnNthString("a2b2c3",5)); } } /* ###Output### c */
Example 1
String: abzd
Output: 3
Explanation: abz or abd are the longest subsequences
Example 2
String: bcdabdz
Output: 4
Explanation: bcdz or abdz are the longest subsequences
class LIS { public static void main(String[] args) { String s="abzd"; char arr[]=s.toCharArray(); int max=0,mx,ele; for(int i=0;i<arr.length;i++) { mx=0; ele=arr[i]; for(int j=i;j<arr.length;j++) { if(ele<arr[j]) { ele=arr[j]; mx++; } } if(max<mx) max=mx; } System.out.println("Length of Longest LIS is "+(max+1)); } } /* ###OUTPUT### Length of Longest LIS is 3 */
class Apple { private static int sqrPlotToBuy(int minmumApples) { double appleInSquare = 0; int unit =0; while(minmumApples>appleInSquare){ unit++; appleInSquare +=12*Math.pow(unit,2); } return unit*8; } public static void main(String[] args) { System.out.println(sqrPlotToBuy(1)); System.out.println(sqrPlotToBuy(3)); System.out.println(sqrPlotToBuy(13)); } } /* ###OUTPUT### 8 8 16 */
0 Comments