1) IDENTIFY POSSIBLE WORDS:
Identify possible words:
Example1:
input1= "Fi_er”
input2=”Fever:filer:Filter: Fixer:fiber:fibre:tailor:offer”
output= "FILER:FIXER FIBER”
2) ALTERNATE ADD SUM
Example1: IfN = 6, and opt=1
Result =6-5+4-3+2-1=3
Example2 If N = 6, and opt=2
Result =6+5-4+3-2+1= 9
The function prototype should be as belowint AddSub(int N, int opt);
3) SIMPLE ENCODED ARRAY
Example 1:Original array = {2, 5, 1, 7, 9, 3} Encoded array = {3, -4, 6, 2, -6, 3} First number in
original array = 2 Sum of all numbers in original array = 27
4) FIND SUMEET SUM (Model-1)
Input1 – 23792 – From these the smallest number that can be formed is 22.
Input2 – 37221 – From these the smallest number that can be formed is 12.
Input3 – 10270 – From these the smallest number that can be formed is 00.
Input4 – 73391 – From these the smallest number that can be formed is 13.
Input5 – 12005 – From these the smallest number that can be formed is 00.
The second part of our task is to find the sum of all these smallest 2-digit numbers and the result
is 47.
Explaining Given Test Case 2:
Input1 – 26674 – From these the smallest number that can be formed is 24.
Input2 – 105 – From these the smallest number that can be formed is 01.
Input3 – 37943 – From these the smallest number that can be formed is 33.
Input4 – 95278 – From these the smallest number that can be formed is 25.
Input5 – 27845 – From these the smallest number that can be formed is 24.
The second part of our task is to find sum of all these smallest 2-digit numbers and the result is
107.
5) FIND SUMEET SUM (Model-2)
Example-1
If the 5 input numbers are 23792,37221,10270,73391 and 12005
The smallest numbers that can be produced using 3 digits from each of these are
223,122,001,133 and 001 respectively, and the sum of these smallest numbers will be 480.
Therefore, the expected result is 480
Example-2
If the 5 input numbers are 26674,105,37943,95278 and 27845,
The smallest numbers that can be produced using 3 digits from each of these are
246,015,334,257 and 245 respectively, and the sum of these smallest numbers will be 1097.
Therefore, the expected result is 1097.
6) FIND SUMEET SUM (Model-3)
Example-1
If the 5 input numbers are 23792,37221,10270,73391 and 12005,
The largest numbers that can be produced using 3 digits from each of these are
973,732,721,973 and 521 respectively and the sum of these largest numbers will be 3920.
Therefore, the expected result is 3920.
Example-2
If the 5 input numbers are 26674,105,37943,95278 and 27845
The largest numbers that can be produced using 3 digits from each of these are
766,510,974,987 and 875 respectively and the sum of these largest numbers will be 4112.
Therefore, the expected result is 4112.
7) FIND SUMEET SUM (Model-4)
Example-1
If the 4 input numbers are 23792,37221,10270 and 73391,
The smallest numbers that can be produced using 2 digits from each of these are 22,12,00
and 13 respectively the sum of these smallest numbers will be 47. Therefore, the expected
result is 47.
Example-2
If the 4 input numbers are 26674,105,37943 and 95278,
The smallest numbers that can be produced using 2 digits from each of these are 24,01,33
and 25 respectively the sum of these smallest numbers will be 83. Therefore, the expected
result is 83.
8) ENCODED TWO STRINGS (Model-1)
Example 1: input1="WIPRO" input 2="TECHNOLOGIES"
Output should be "TECHWOGIES"
Explanation -
The three parts of WIPRO will be "W", "IPR" and "O"
The three parts of TECHNOLOGIES will be "TECH", "NOLO" and "GIES"
So,
Password = First part of word 2 + First part of word 1+ Third part of word 1 + Third part of
word2
=TECH + W + O + GIES
= TECHWOGIES
Example 2: input="MACHINE" input 2="LEARNING"
Output should be "LEMANENG"
Explanation -
The three parts of MACHINE will be "MA", "CHI" and "NE"
The three parts of LEARNING will be "LE", "ARNI", and "NG"
So,
Password = First part of word 2 + First part of word 1+ Third part of word 1 + Third part of
word2
= LE + MA + NE + NG
= LEMANENG
9) ENCODED TWO STRINGS (Model-2)
Example 1: input1="WIPRO" input 2="TECHNOLOGIES"
Output should be "IPRNOLOTECH"
Explanation -
The three parts of WIPRO will be "W", "IPR" and "O"
The three parts of TECHNOLOGIES will be "TECH", "NOLO" and "GIES"
So,
Password = Second part of word 1+ Second part of word 2 + First part of word 2
= IPR + NOLO + TECH
= IPRNOLOTECH
Example 2: input="MACHINE" input 2="LEARNING"
Output should be "CHIARNILE"
Explanation -
The three parts of MACHINE will be "MA", "CHI" and "NE"
The three parts of LEARNING will be "LE", "ARNI", and "NG"
So,
Password = Second part of word 1+ Second part of word 2 + First part of word 2
= LE + MA + NE + NG
= LEMANENG
10) ENCODED TWO STRINGS (Model-3)
Example 1: input1="WIPRO" input 2="TECHNOLOGIES"
Output should be "PGIESWITECH"
Explanation -
The three parts of WIPRO will be "WI", "P" and "RO"
The three parts of TECHNOLOGIES will be "TECH", "NOLO" and "GIES"
So,
Password = Second part of word1+ Third part of word 2 + First part of word1 + First part of
word 2
=P + GIES + WI + TECH
= PGIESWITECH
Example 2: input="MACHINE" input 2="LEARNING"
Output should be "CHIINGMALEA"
Explanation -
The three parts of MACHINE will be "MA", "CHI" and "NE"
The three parts of LEARNING will be "LEA", "RN", and "ING"
So,
Password = Second part of word1+ Third part of word 2 + First part of word1 + First part of
word 2
= CHI + ING + MA + LEA
= CHIINGMALEA
11) ENCODED TWO STRINGS (Model-4)
Example 1: input1="WIPRO" input 2="TECHNOLOGIES"
Output should be "GIESPNOLOWI"
Explanation -
The three parts of WIPRO will be "WI", "P" and "RO"
The three parts of TECHNOLOGIES will be "TECH", "NOLO" and "GIES"
So,
Password = Third part of word2 + Second part of word1 + Second part of word2 + First part of
word1
= GIES + P + NOLO + WI
= GIESPNOLOWI
Example 2: input="MACHINE" input 2="LEARNING"
Output should be "INGCHIRNMA"
Explanation -
The three parts of MACHINE will be "MA", "CHI" and "NE"
The three parts of LEARNING will be "LEA", "RN", and "ING"
So,
Password = Third part of word2 + Second part of word1 + Second part of word2 + First part of
word1
= ING + CHI + RN + MA
= INGCHIRNMA
12) ENCODED TWO STRINGS (Model-5)
Example 1: input1="WIPRO" input 2="TECHNOLOGIES"
Output should be "TECHWIROGIES"
Explanation -
The three parts of WIPRO will be "WI", "P" and "RO"
The three parts of TECHNOLOGIES will be "TECH", "NOLO" and "GIES"
So,
Password = First part of word2 + First part of word1 + Third part of word1 + Third part of
word2
= TECH + WI + RO + GIES
= TECHWIROGIES
Example 2: input="MACHINE" input 2="LEARNING"
Output should be "LEAMANEING"
Explanation -
The three parts of MACHINE will be "MA", "CHI" and "NE"
The three parts of LEARNING will be "LEA", "RN", and "ING"
So,
Password = First part of word2 + First part of word1 + Third part of word1 + Third part of
word2
= LEA + MA + NE + ING
= LEAMANEING
13) FIND KEY (Model-1):
For Example, input1=3521, input2=2452 input3=1352
Key = (5+5+5) + (3+4+3) = 25
Assuming three numbers are passed to given function and complete the function to find and return the
Key
14) FIND KEY (Model-2):
for e.g if input1=3521,input2=2452,input3=1352,then Key=[1][5][2][2]=1522
Assuming that the 3 numbers are passed to the given function.Complete the function to find and
return the key
15) FIND KEY (Model-3):
for e.g if input1=3521,input2=2452,input3=1352,then Key=[3][3][5][1]=3351
Assuming that the 3 numbers are passed to the given function.Complete the function to find and
return the key.
16) TRAVERSE ARRAY AND FIND KEY (Model-1)
Example 1 –
If the array input 1 is 74 -56 15 71 92 23 and input2 is 6
First array element = 74
Here, KEY = 7
NEXT_ADDRESS – 4
4th array element = 92 (NOTE THAT ARRAY ELEMENT ADDRESS STARTS FROM 0, SO 4th
element is 92)
Here, KEY = 9 NEXT ADDRESS = 2
2nd array element 15
Here, KEY = 1, NEXT ADDRESS = 5
5th array element – 23
Here KEY = 2 NEXT ADDRESS = 3
3rd array element 71
Here, KEY 7, NEXT ADDRESS =1
1st array element =-56
Here, KEY = 5 NEXT ADDRESS -STOP (because we have reached a negative number).
FINAL RESULT = Alternatively Add and Subtract the keys = 7+9-1+ 2-7+5= 15
17) TRAVERSE ARRAY AND FIND KEY (Model-2)
Example 1-1 the array input1 is{ 47,-65,51,17,29,32} and input2 is 6
First array element 47
Here, KEY 7,NEXT ADDRESS = 4
4th array element 29 (NOTE THAT ARRAY ELEMENT ADDRESS STARTS FROM 0, So 4th element is 29)
Here, KEY 9 NEXT ADDRESS= 2
2nd array element = 51
Here, KEY 1 NEXT ADDRESS = 5
5th array element = 32
Here, KEY 2 NEXT ADDRESS=3
3rd array element 17
Here KEY 7 NEXT ADDRESS = 1
1st array element = -65
Here, KEY=5 NEXT ADDRESS - STOP (because we have reached a negative number)
FINAL RESULT = Alternately subhead and Add the key 7-9+1-2+7-5=1
Example 2 - If the array s {47,65,51,17,29,-32} and input2 is 6
First array element 47
Here KEY 7 NEXT ADDRESS=4
4th array element is 29
Here, KEY 9 NEXT ADDRESS =2
2nd may element is 51
Here KEY 1 NEXT ADDRESS=5
5th array element - 32
Here, KEY- 2 NEXT_ADDRESS-STOP (because we have reached a negative number)
FINAL RESULT Animatedly Subtract and Add the key =7-9+1-2=-3
Example 3 - the array is 47 65 51 12 29 32 54 and input2 is 7
Here we see that the array does NOT contain any negative number, so the result should be calculated
as the smallest number in the array
FINAL RESULT 12
18) FIND PASSWORD (Model-1)
If input1=12,input2=1313,input3=122,input4=678 and input5=898 , we see that there are THREE
stable numbers i.e 12,1313 and 678 and
TWO unstable numbers i.e 122 and 898
so,the password should be=(Number of Unstable numbers*10)+Number of stable
numbers=(2*10)+3=23
19) FIND PASSWORD (Model-2)
If input1=12,input2=1313,input3=122,input4=678 and input5=898 , we see that there are THREE
stable numbers i.e 12,1313 and 678 and
TWO unstable numbers i.e 122 and 898
so,the password should be=(Number of stable numbers*10)+Number of Unstable
numbers=(3*10)+1=32
20) FIND PASSWORD (Model-3)
If input1=12,input2=1313,input3=122,input4=678 and input5=898 , we see that there are THREE
stable numbers i.e 12,1313 and 678 and
TWO unstable numbers i.e 122 and 898
so,the password should be=Maximum of all stable numbers+Minimum of all Unstable
numbers=1313+122=1435
21) FIND PASSWORD (Model-4)
If input1=12,input2=1313,input3=122,input4=678 and input5=898 , we see that there are THREE
stable numbers 12,1313 and 678 and
TWO unstable numbers 122 and 898
So, the password should be=sum of all stable numbers – sum of all Unstable numbers=983
22) FIND PASSWORD (Model-5)
If input1=12,input2=1313,input3=122,input4=678 and input5=898 , we see that there are THREE
stable numbers 12,1313 and 678 and
TWO unstable numbers 122 and 898
So, the Password should be=Maximum of all stable numbers - Minimum of all Unstable
numbers=1313-122=1191
23) FIND PASSWORD (Model-6)
If input1=12,input2=1313,input3=122,input4=678 and input5=898 , we see that there are THREE
stable numbers 12,1313 and 678 and
TWO unstable numbers 122 and 898
So, the Password should be=Maximum of all Unstable numbers - Minimum of all Unstable
numbers=898-122=776
24) FIND PASSWORD (Model-7)
If input1=12,input2=1313,input3=122,input4=678 and input5=898 , we see that there are THREE
stable numbers 12,1313 and 678 and
TWO unstable numbers 122 and 898
So, the Password should be=sum of all Unstable numbers=898+122=1020
25) FIND PASSWORD (Model-8)
If input1=12,input2=1313,input3=122,input4=678 and input5=898 , we see that there are THREE
stable numbers 12,1313 and 678 and
TWO unstable numbers 122 and 898
So, the Password should be=Maximum of all stable numbers - Minimum of all stable numbers=1313-
12=1301
26) FIND PASSWORD (Model-9)
If input1=12,input2=1313,input3=122,input4=678 and input5=898 , we see that there are THREE
stable numbers 12,1313 and 678 and
TWO unstable numbers 122 and 898
So, the Password should be=Maximum of all stable numbers + Minimum of all stable numbers=1313 +
12=1325
27) PERSONS AND TOKENS (Model-1)
Example 1:
Input1 = 10
Input2 = {“Rajesh", “ Abdul", “Rahul”, “Priya”, “Sanjay", "Nidhi”, “Varun”, “Varsha”, “Basil”, “Asif”}
Input3 = {99, 46, 39, 102, 45, 521, 65, 4, 47, 741}
Expected output "Basil:Abdul: Sanjay"
Explanation: Out of the 10 token numbers, the numbers 45, 46 and 47 are in sequence and their
descending order 47 46 and 45 Hence the expected output is a string containing names separated by a
colon of the 3 persons who hold these tokens. Note that the names are in descending sequence of
their respective taken numbers,
Example 2:
Input1= 7
Input = ["aa", "bb", "cc", "dd", "ee", "gg"]
Inpul 3={9,89,5,0,6,65,4}
Expected output = "ee:cc:gg"
28) PERSONS AND TOKENS (Model-2)
Example 1:
Input1 = 10
Input2 = {“Rajesh","Abdul”, "Rahul”, "Priya","Sanjay", "Nidhi", “Varun" ,”Varsha”, “Basil", “Asif”)
Input3 = {99,46,39,102,45,521,65,4,47,741}
Expected output = "Sanjay:Abdul:Basil"
Explanation: Out of the 10 token numbers, the numbers 45, 46 and 47 are in sequence. Hence the
expected output is a string containing names separated by a colon of the 3 persons who hold these
tokens. Note that the names are in ascending sequence of their respective token numbers
Example 2:
Input1= 17
Input2={“aa","bb",” cc”,” dd”, "ee”, “ff”,”gg")
Input3 = {9,89,5,0,6,65,4}
Expected output ="gg:cc:ee "
Explanation: Out of the 7 token numbers, the numbers 4, 5 and 6 are in sequence Hence the expected
output is a string containing names separated by a colon of the 3 persons who hold these tokens. Note
that the names are in ascending sequence of their respective token numbers
Example 3:
Input1 = 4
Input2 = {"Priya", "Soumya" ,"Sam”, “Vidya”}
Input3 = {9,76,8,23}
Expected output = "NONE"
Explanation: The given 4 token numbers do not contain any sequence of 3 numbers. Hence, the
expected output is a string containing NONE
29) USER ID GENERATION (Model-1)
Example-1 - If the participant's details are as below
First Name = Ray
Last Name =Roy
PIN = 560037
N= 6
Step1 - Length of Last_Name is less than the Length of First_Name, so the Smaler Name is “Roy” and
the Longer Name is “Rajiv”
Step2 - The user id will be = Last Letter of the smaller name +Entre word in the longer name + Digit at
position N in the PIN when traversing the PIN from left to right +Digit at position N in the PIN when
traversing the PIN from right to left
=Last Letter of “Roy”+ Entre word in Rajiv+ 6th Digit of Pin from left + 6th Digit of PIN from right
=y+ Rajiv+7+5
Therefore, user-id=yRajiv75
Step3 -Toggle the alphabet in the user-id. So,user-id = YrAJIV75
30) USER ID GENERATION (Model-2)
Example-1 - If the participant's details are as below -
First Name Rajiv
Last Name = Roy
PIN = 560037
N=6
Step 1 - Length of Last_Name is less than the Length of First_Name so the Smaller Name is “Roy” and
the Longer Name is “Rajiv”
Step 2 - The user-id will be= First Letter of the longer name + Entire word of the smaller name + Digit
at position N in the PIN when traversing the PIN from left to right +Digit at position N in the PIN when
traversing the PIN from right to left
=First Letter of “Rajiv” +Entire word of “Roy” + 6th Digit PIN from left + 6th Digit of PIN from right
=R+Roy + 7+5
Therefore, user-id =RRoy75
Step 3 - Toggle the alphabets in the user-id, user-id= rrOY75
31) USER ID GENERATION (MODEL-3 WITH DIFFERENT TEST CASE)
Example-1 - If the participants details are as below
First Name = Rajiv
Last Name = Roy
PIN = 560037
N=6
Step1 - Length of Last Name is less than the Length of First Name, so the Smaller Name is
"Roy and the Longer Name is "Rajiv“
Step2 - The user-id will be = Last Letter of the longer name + Entire word of the smaller name
+ Digit at position N in the PIN when traversing the PIN from left to right + Digit at position N
in the PIN when traversing the PIN from right to left
= Last Letter of “Rajiv” + Entire word of “Roy” + 6th Digit of pin from left + 6th Digit of pin
from right
= v + Roy + 7 + 5
Therefore, user-id = vRoy75
Step3 -Toggle the alphabets in the user-id = VrOY75
31) ENCODED THREE STRINGS (Model-1)
Input 1: “John”
Input 2: “Johny”
Input 3: “Janardhan”
Output1: FRONT part of input 1 + FRONT part of input 2 + FRONT part of input 3
Output2: MIDDLE part of input1 + MIDDLE part of input2 + MIDDLE part of input3
Output3: END part of the input1 + END part of input2 + END part of input3
For example, for the above example input strings:
Output1 = “J” + “Jo” + “Jan” = “JJoJan”
Output2 = “oh” + “h” + “ard” = “ohhard”
Output3 = “n” + “ny” + “han” = “nnyhan”
Output1 = “JJoJan”
Output2= “ohhard’
Output3 = “NNYHAN”
Help Anand to write a program that would do the above.
32) ENCODED THREE STRINGS (Model-2)
Input 1: “John”
Input 2: “Johny”
Input 3: “Janardhan”
Output1: FRONT part of input 1 + MIDDLE part of input 2 + END part of input 3
Output2: MIDDLE part of input1 + END part of input2 + FRONT part of input3
Output3: END part of the input1 + FRONT part of input2 + MIDDLE part of input3
For example, for the above example input strings:
Output1 = “J” + “h” + “han” = “Jhhan”
Output2 = “oh” + “ny” + “Jan” = “ohnyJan”
Output3= “n” + “Jo” + “ard” += “nJoard”
Output1 = “Jnhan”
Output2= “ohnyJan’
Output3 = “NjOARD”
Help Anand to write a program that would do the above.
33) ENCODED THREE STRINGS (Model-3)
Input 1: “John”
Input 2: “Johny”
Input 3: “Janardhan”
Output1: FRONT part of input 1 + END part of input 2 + MIDDLE part of input 3
Output2: MIDDLE part of input1 + FRONT part of input2 + END part of input3
Output3: END part of the input1 + MIDDLE part of input2 + FRONT part of input3
For example, for the above example input strings:
Output1 = “J” + “nh” + “ard” = “Jnyard”
Output2 = “oh” + “Jo” + “han” = “ohJohan”
Output3= “n” + “h” + “Jan” += “nhJan”
Output1 = “Jnyard”
Output2= “ohJohan’
Output3 = “NHjAN”
Help Anand to write a program that would do the above.
34) ENCODED THREE STRINGS (Model-4)
Input 1: “John”
Input 2: “Johny”
Input 3: “Janardhan”
Output1: FRONT part of input 1 + MIDDLE part of input 2 + END part of input 3
Output2: MIDDLE part of input1 + END part of input2 + FRONT part of input3
Output3: END part of the input1 + FRONT part of input2 + MIDDLE part of input3
For example, for the above example input strings:
Output1 = “J” + “ohn” + “han” = “Johnhan”
Output2 = “oh” + “y” + “Jan” = “ohyJan”
Output3= “n” + “J” + “ard” += “nJard”
Output1 = “Johnhan”
Output2= “ohyJan’
Output3 = “NjARD”
Help Anand to write a program that would do the above.
35) LARGEST POSSIBLE PALINDROME
For e.g the word is "Magma", then the result should be 0
For e.g the word is “Victory” then the result should be -1
i.e "Template” or “template" or “TEMplate” or “TEmPLAte” or “TEMPLATE” should all give the same result whic
h is 3
36) WEIGHT OF HILL PATTERNS
*
*#*
*#*#*
*#*#*#*
*#*#*#*#*
*#*#*#*#*#*
. . .and so on till level N
37) JUMBLED WORDS
Example1:
input1="PROJECT BASED LEARNING"
input2=1
Expected output="POETCJR BSDEA LANNGIRE"
Example2:
input1="PROJECT BASED LEARNING"
input2=2
Expected output="POETRJC BSDAE LANNERIG"
Example3:
input1="WIPRO LIMITED"
input2=1
Expected output="WPORI LMTDEII"
Example4:
input1="WIPRO LIMITED"
input2=2
Expected output="WPOIR LMTDIIE"
38) FIX THE FORMULA
Sample Input/Output-1
Input1= we8+you2-7to/*32
Output=2
Explanation: Here the operators are [+,-,/,*] and the numbers are [8,2,7,3,2]
Thus we would be getting 8+2=>10-7=>3/3=>1*2=>2
Final answer is 2.
Sample Input/Output-2
Input1= i*-t5s-t8h1e4birds
Output=35
Explanation: Here the operators are [+,-,-] and the numbers are [5,8,1,4]
Thus we would be getting 5*8=>40-1=>39-4=>35
Final answer is 35.
39) FORM THE WORD
Input1 = ww:ii:pp:rr:oo
Output= WIPRO
Input1= zx:za:ee
Output=BYE
40) TWO DIGIT REDUCED SUBTRACTED FORM
If input1 = 5271
Expected output = 21
41) MATCHING WORD
Input1: {arc, nep, tis}
Input2: {sit, car, pen}
Input3: 3
Output: 120
Input1: {cnhul, estl, rakeb, ahev}
Input2: {lets, have, lunch, break}
Input3: 4
Output: 2031
42) ROBO Movement (90 degrees, 1 step):
43) ROBO Movement (90 degrees, 2 steps):
44) ROBO Movement (90 degrees, 1 or 2 steps):
45) ROBO Movement (45 degrees, 1 step):
46) ROBO Movement (45 degrees, 2 steps):
47) ROBO Movement (45 degrees, 1or 2 steps):
48) ROBO Movement (90 or 45 degrees, 1 step):
49) ROBO Movement (90 or 45 degrees, 2 step):
50) ROBO Movement (90 or 45 degrees, 1 or 2 steps):
51) ONE DIGIT REDUCED SUBTRACTED FORM
52) PROCESS TWO WORDS
Input1: Today is a nice day
Input2: 41
Output: ince doTday
53) Find Password – Stable Unstable (MODEL-1)
For Example:
If input1=12, input2=1313, input3=122, input4=678, andinput5=898,
stable numbers are 12, 1313 and 678
unstable numbers are 122 and 898
So, the password should be=sum of all stable numbers=2003
54) Find Password – Stable Unstable (MODEL-2)
For Example:
If input1=12, input2=1313, input3=122, input4=678, andinput5=898,
stable numbers are 12, 1313 and 678
unstable numbers are 122 and 898
So, the password should be = (number of stable numbers*10) - number of unstable numbers = (3*10) - 2 = 28
55) Find Password – Stable Unstable (MODEL-3)
For Example:
If input1=12, input2=1313, input3=122, input4=678, andinput5=898,
stable numbers are 12, 1313 and 678
unstable numbers are 122 and 898
So, the password should be = (number of unstable numbers*10) - number of stable numbers = (2*10) - 3 = 17
56) Find Password – Stable Unstable (MODEL-4)
For Example:
If input1=12, input2=1313, input3=122, input4=678, andinput5=898,
stable numbers are 12, 1313 and 678
unstable numbers are 122 and 898
So, the password should be = Maximum of all unstable numbers + Minimum of all unstable numbers = 898+ 122 =
1020
57) Find Password – Stable Unstable (MODEL-5)
Assuming that the five numbers are passed to a function as input1, input2, input3, input4, and input5, complete the f
unction to find and return the password.
For Example:
If input1=12, input2=1313, input3=122, input4=678, andinput5=898,
stable numbers are 12, 1313 and 678
unstable numbers are 122 and 898
So, the password should be = Maximum of all unstable numbers + Minimum of allstable numbers = 898 + 12 = 910
58) Find Password – Stable Unstable (MODEL-6)
Assuming that the five numbers are passed to a function as input1, input2, input3, input4, and input5, complete the f
unction to find and return the password.
For Example:
If input1=12, input2=1313, input3=122, input4=678, andinput5=898,
stable numbers are 12, 1313 and 678
unstable numbers are 122 and 898
So, the password should be = Maximum of all unstable numbers – Minimum of all stable numbers = 898 –12 = 886
59) Nambiar Number 109
For the mobile number 9880127431 the resultant number (Nambiar Number) will be the concatenation of the results
of the 4 passes = (26971) = 26971
For the mobile number 9860857152, the resultant number (Nambiar Number) will be the concatenation of the results
of the 3 passes = [36][8117] = 3687
Example 3
If the given mobile number is 8123454210
The resultant number (Nambiar Number) will be 95970
Example 4
If the given mobile number is 9900114279
The resultant number (Nambiar Number) will be 181149
60) Generate series and find nth element:
Find and return Nth element
Example1- If the three numbers are a=1,b=3,c=6 and N=17
the series will be formed as below-
1,3,6,8,11,13,16,18,21,23,26,28,31,33,36,38,41
17th number in the series 41
61) EATERY JOINT
Example 1
input1= {{0,2},{2,3},{2,2},{5,2}}
input2=4
output :c0-c1-c3:c2
Example 2
input1={{0,1},{2,1},{2,4},{5,3},{6,2},{7,5},{9,4}};
input2=7;
output : c0-c1-c2-c4-c6:c3-c5
Example 3
input1={{0,2},{3,3},{5,3}};
int input2=3;
output : "Served";
62) Find Prime Vowels:
input1=3
output= le2
input1=5
output=oeoe4
63) JOBS:
Given n jobs ids,each job is triggered by another job.A job can be triggered by only one job. A job can trigger multip
le jobs
input1 is an integer number which denotes m i.e count of job ids given to us. n ranges from 3 to 15
if input1 is 5 the job ids are 1,2,3,4 and 5
if input1 is 7 the job ids are 1,2,3,4,5,6 and 7
input2 is a two dimensional integer array:
{{2,1},{3,2}}
Given input1 and input2, write a program to locate the branch which contain the last job id. Return all the job ids pre
sesnt in that branch as a single hypen separated String.
64) SILVER NUMBER PROGRAM
65) SUCCESIVE FACTOR PROGRAM
Example 1- input-12 output-T34
Example 2- input-380 output-T1920
Example 3- input-7 output-TIsPrime
Example 4- input-32 output-FNotPrime
66) Rock Paper & Scissors
Example-1
input1:2
input2:{0,1,1}
input3:{1,1,0}
output: 1
Example-2
input1:3
input2:{0,0,3}
input3:{3,0,0}
output:6
67)Find the number odd string mapping as explained below
Example 1-
input1 123
output 11ODD
Example 2-
input1 111
output 9ODD
Example 3-
input1 7890
output 18EVEN
68)String Task
Input1 tour
output *t*r
69)Math Olympiad
70)Advance constraints
71)ISEmpire
72)String Tally Theorem
Example 1-
Input1 Wipro
Input2 TECHNOLOGY
Output 1
Example 2-
Input1 Talent
Input2 TRANSFORM
Output 3
Example 3-
Input1 TEstCase
Input2 Test
Output 4
73)Identify Possible Words:
74)2nd Word UpperCase:
75)IsPalinStr
76)WeightOfString:
77)MostFrequentDigit
78)UserIdGeneration:
79)FindPasswordStableUnstable:
80)WeightHillPattern:
81)QuadraticRoots:
82)FindNumberOfProblems:
83)Nth Fibonacii:
84)StepNumber:
85)DigiLock:
86)FIND ODD EVEN DIFFERENCE
87) FIND TOTAL DISTANCE
88) FIND MAX DIFFERENCE
89) FIND TOTAL FEET
90) FIND LARGEST SMALLEST DIFFERENCE
91) BILL TAX (C)
92) Sum of letters
i/p Man
o/p 377
93) Number of sum(sum of the largest and smallest element of an array)
i/o 4
i/o {9,5,0,11} o/p 11
input 1: 7
input 2: { 7,2,6,15,54,10,23}
output : 56
94) Second largest(return second largest or return -1)
i/p 3
i/o {2,1,2} o/p 1
95) The cuckoo sequence
i/p 3 o/p 4
96) Find the LCM of two given integers
i/p 2
i/p 3 o/p 6
97) Push zero to end
98) Arrangement(rearrange the number of the array in a such a manner that even and odd numbers are arranged alter
natively in increasing order)
i/p 5
i/p 9,12,23,8,5 o/p 5,8,9,12,23
99) Return an integer representing the number of trailing zeros
i/p 5 o/p 1
100) write the function to decrypt the string and find the Nth character of the decrypt string
i/p a2b2c3
i/p 5 o/p c
101) Return the total number of coins till charity days(Adam's charity)
i/p 5 o/p 55
102) Max contiguous sequence
i/p 5
i/p 1 2 4 -2 3 o/p 4
103) Return the integer array after removal of duplicate values
i/p 11 11 11 13 13 20
i/p 6 o/p 11 13 20
104) Return the length of largest sub array containing equal number of 0s and 1s
i/p 4
i/p 1 1 0 1 o/p 2
105) Return a string with the char of each word in the reverse order
i/p Hello World o/p Olleh Dlrow
106) Reverse of the string
i/p Hello world o/p dlroW olleH
107) Reverse of the string
i/p The sky is blue o/p eulb si yks eht
108) Missing braces program
109) MBA challange(power)
110) Array of duplicate elements
i/p 6
i/p 4 4 7 8 8 9 o/p 4 8
111) Return HCF of given numbers
i/p 3
i/p 2 4 8 o/p 2
112)Sum of unique devisior
i/p 6 o/p 12
113)Playing with number
input1=7;
input2[]= {1,2,3,4,5,6,7};
input3=2;
output:
34567
114) Array sorting(nick checks)
input1:6
input2[]: {3,7,2,5,4,6}
output:
1
115) Fancy Occurance
input1:Welcome to metti
input2:i
output:
Welcome to mett
116) String Within String
input1: abab
input2: ab
output:
yes
117) Palindrome
input1: level
output:
1
118) Max Occuring character
Test cases:
input1 : abcdd
output : d
119) Anagram
input1:build
input : dubli
output : yes
120) Coin Counts
Test cases:
input1 : 2
output : 5
121) Electro Static
Test cases:
input1 : {4,3,5}
input2 : PNP
input3 : 3
output : 600
122) Reverse Array
123)Reverse string word wise
i/p Welcome to mettl o/p mettl to Welcome
124) The smallest possible Two digit numbers
Example1
input1 :7136
output :13
Example2
input1 :9987
output :78
125) Power of First and Last
Example 1:
input1 :21221
output :2^1=2.0
Example 2:
input1 :375
output :3^5=243.0
126) Tic-tac-toe
Example-1
input1 :{1,2,9,5,7,8}
output :O:c2
Example-2
input1 :{5,4,7,3,9,8,1}
output :X:d1
Example-3
input1:{1,8,2,3,7,5,6,4,9}
output:DRAW
127) Find the mobile pin[new]
Example-1
input1: Wipro
input2: Limited
output:17254
Example-2
input1: Dog
input2: Cat
output: 2624
Example-3
input1 :Java
input2 :Program
output :13251
128) Coding Contest (Find number of problems)
Example 1
input1: 3
input2:{"1 0 0","1 1 1","1 0 1"}
output:2
Example 2
input1 :5
input2 :{"1 1 0","1 1 1","0 0 1","0 0 0","1 0 1"}
output:3
129) FindStringCode
Example 1
input1:"World Wide Web"
output:402326
Example 2
input1:"Hello World"
output:2640
130)Array task
input1:{12,1313,122,678,898};
output:23
131) Message controlled Robot movement
1) IDENTIFY POSSIBLE WORDS:
Identify possible words:
Example1:
input1= "Fi_er”
input2=”Fever:filer:Filter: Fixer:fiber:fibre:tailor:offer”
output= "FILER:FIXER FIBER”
Code For _ :
import java.util.regex.*;
int i,j;
input1 = input1.toUpperCase();
input2 = input2.toUpperCase();
String word="", res="";
String[] words = input2.split(":");
for(i=0;i<words.length;i++)
{
word = words[i];
if(input1.length()==word.length())
{
for(j=0;j<input1.length();j++)
{
if(input1.charAt(j)!='_' && input1.charAt(j)!=word.charAt(j))
{
break;
}
}
if(j==input1.length())
{
res=res + word+":";
}
}
}
return res.length()==0?"ERROR-009":res.substring(0,res.length()-1);
CODE FOR _ and ?:
import java.util.regex.*;
input1 = input1.toUpperCase();
input2 = input2.toUpperCase();
String[] words = input2.split(":");
String word="", res="";
for(int i=0;i<input1.length();i++)
{
if(input1.charAt(i)=='_')
word = word + "[A-Z]";
else if(input1.charAt(i)=='?')
word = word + "[A-Z]?";
else
word = word + input1.charAt(i);
}
for(int i=0;i<words.length;i++)
{
if(Pattern.matches(word, words[i]))
res = res + words[i]+":";
}
return res.length()==0?"ERROR-009":res.substring(0,res.length()-1);
-------------------------------------------------------------------------------------
public class Program
{
public static void Main()
{
string input1=Console.ReadLine();
string input2=Console.ReadLine();
input1=input1.ToUpper();
input2=input2.ToUpper();
String word="", res="";
int i,j;
String[] words = input2.Split(':');
for(i=0;i<words.Length;i++)
{
word = words[i];
if(input1.Length==word.Length)
{
for(j=0;j<input1.Length;j++)
{
if((input1[j]!='_')&&(input1[j]!=word[j]))
{
break;
}
}
if(j==input1.Length)
{
res=res + word+":";
}
}
}
Console.WriteLine(res.Length==0?"ERROR-009":res.Substring(0,res.Length-1));
}
}
2) ALTERNATE ADD SUM
Example1: IfN = 6, and opt=1
Result =6-5+4-3+2-1=3
Example2 If N = 6, and opt=2
Result =6+5-4+3-2+1= 9
The function prototype should be as belowint AddSub(int N, int opt);
Code:
int sum=0;
sum+=N;
N=N-1;
if(opt==2)
{
if(N%2==0)
{
while(N>0)
{
if(N%2==0)
sum+=N;
else
sum-=N;
N--;
}
}
else
{
while(N>0)
{
if(N%2==0)
sum-=N;
else
sum+=N;
N--;
}
}
}
else
{
if(N%2==0)
{
while(N>0)
{
if(N%2==0)
sum-=N;
else
sum+=N;
N--;
}
}
else
{
while(N>0)
{
if(N%2==0)
sum+=N;
else
sum-=N;
N--;
}
}
}
return sum;
------------------------------------------------------------------------------------------------------
public class Program
{
public static void Main()
{
int N=Int32.Parse(Console.ReadLine());
int opt=Int32.Parse(Console.ReadLine());
int sum=0;
sum+=N;
N=N-1;
if(opt==2)
{
if(N%2==0)
{
while(N>0)
{
if(N%2==0)
sum+=N;
else
sum-=N;
N--;
}
}
else
{
while(N>0)
{
if(N%2==0)
sum-=N;
else
sum+=N;
N--;
}
}
}
else
{
if(N%2==0)
{
while(N>0)
{
if(N%2==0)
sum-=N;
else
sum+=N;
N--;
}
}
else
{
while(N>0)
{
if(N%2==0)
sum+=N;
else
sum-=N;
N--;
}
}
}
Console.WriteLine(sum);
}
}
3) SIMPLE ENCODED ARRAY
● find the value of the first number of the original array and store it in the member
output1 and
● find the sum of all numbers in the original array and store it in the member output2.
Assumption(s):
● The array elements can be positive and/or negative numbers
Example 1:Original array = {2, 5, 1, 7, 9, 3} Encoded array = {3, -4, 6, 2, -6, 3} First number in
original array = 2 Sum of all numbers in original array = 27
Code:
int[] res = new int[input1.length];
res[res.length - 1] = input1[input1.length - 1];
for (int i = input1.length - 1; i > 0; i--)
{
res[i - 1] = input1[i - 1] - res[i];
}
int sum = 0;
for (int item : res)
sum += item;
return new Result(res[0], sum);
4) FIND SUMEET SUM (Model-1)
Input1 – 23792 – From these the smallest number that can be formed is 22.
Input2 – 37221 – From these the smallest number that can be formed is 12.
Input3 – 10270 – From these the smallest number that can be formed is 00.
Input4 – 73391 – From these the smallest number that can be formed is 13.
Input5 – 12005 – From these the smallest number that can be formed is 00.
The second part of our task is to find the sum of all these smallest 2-digit numbers and the result
is 47.
Explaining Given Test Case 2:
Input1 – 26674 – From these the smallest number that can be formed is 24.
Input2 – 105 – From these the smallest number that can be formed is 01.
Input3 – 37943 – From these the smallest number that can be formed is 33.
Input4 – 95278 – From these the smallest number that can be formed is 25.
Input5 – 27845 – From these the smallest number that can be formed is 24.
The second part of our task is to find sum of all these smallest 2-digit numbers and the result is
107.
Code:
int sum=0;
char ch1[]=(String.valueOf(input1)).toCharArray();
Arrays.sort(ch1);
sum+=Integer.parseInt(String.valueOf(ch1[0])+String.valueOf(ch1[1]));
char ch2[]=(String.valueOf(input2)).toCharArray();
Arrays.sort(ch2);
sum+=Integer.parseInt(String.valueOf(ch2[0])+String.valueOf(ch2[1]));
char ch3[]=(String.valueOf(input3)).toCharArray();
Arrays.sort(ch3);
sum+=Integer.parseInt(String.valueOf(ch3[0])+String.valueOf(ch3[1]));
char ch4[]=(String.valueOf(input4)).toCharArray();
Arrays.sort(ch4);
sum+=Integer.parseInt(String.valueOf(ch4[0])+String.valueOf(ch4[1]));
char ch5[]=(String.valueOf(input5)).toCharArray();
Arrays.sort(ch5);
sum+=Integer.parseInt(String.valueOf(ch5[0])+String.valueOf(ch5[1]));
return sum;
--------------------------------------------------------------------------------------------------------
public class Program
{
public static void Main()
{
int input1=Convert.ToInt32(Console.ReadLine());
int input2=Convert.ToInt32(Console.ReadLine());
int input3=Convert.ToInt32(Console.ReadLine());
string x=input1.ToString();
string y=input2.ToString();
string z=input3.ToString();
char[] ch1=x.ToCharArray();
Array.Sort(ch1);
char[] ch2=y.ToCharArray();
Array.Sort(ch2);
char[] ch3=z.ToCharArray();
Array.Sort(ch3);
int l1=ch1.Length;
int l2=ch2.Length;
int l3=ch3.Length;
string a=Convert.ToString(ch1[l1-1])+Convert.ToString(ch1[l1-2]);
string b=Convert.ToString(ch2[l2-1])+Convert.ToString(ch2[l2-2]);
string c=Convert.ToString(ch3[l3-1])+Convert.ToString(ch3[l3-2]);
int num1=Int32.Parse(a);
Console.WriteLine(num1);
int num2=Int32.Parse(b);
Console.WriteLine(num2);
int num3=Int32.Parse(c);
Console.WriteLine(num3);
int sum=0;
sum=num1+num2+num3;
Console.WriteLine(sum);
}
}
5) FIND SUMEET SUM (Model-2)
FindSumeetSum: Sum of smallest 3- digit numbers from given 5 numbers
Given 5 input numbers, Sumeet has to find the sum of the smallest numbers that can be
produced using 3 digits from each of the above 5 numbers
Example-1
If the 5 input numbers are 23792,37221,10270,73391 and 12005
The smallest numbers that can be produced using 3 digits from each of these are
223,122,001,133 and 001 respectively, and the sum of these smallest numbers will be 480.
Therefore, the expected result is 480
Example-2
If the 5 input numbers are 26674,105,37943,95278 and 27845,
The smallest numbers that can be produced using 3 digits from each of these are
246,015,334,257 and 245 respectively, and the sum of these smallest numbers will be 1097.
Therefore, the expected result is 1097.
Function prototype is as belowInt findSumeetSum(int input1,int input2,int input3,int input4,int input5)
Code:
int sum=0;
char ch1[]=(String.valueOf(input1)).toCharArray();
Arrays.sort(ch1);
sum+=Integer.parseInt(String.valueOf(ch1[0]) + String.valueOf(ch1[1]) + String.valueOf(ch1[2]));
char ch2[]=(String.valueOf(input2)).toCharArray();
Arrays.sort(ch2);
sum+=Integer.parseInt(String.valueOf(ch2[0]) + String.valueOf(ch2[1]) + String.valueOf(ch2[2]));
char ch3[]=(String.valueOf(input3)).toCharArray();
Arrays.sort(ch3);
sum+=Integer.parseInt(String.valueOf(ch3[0]) + String.valueOf(ch3[1]) + String.valueOf(ch3[2]));
char ch4[]=(String.valueOf(input4)).toCharArray();
Arrays.sort(ch4);
sum+=Integer.parseInt(String.valueOf(ch4[0]) + String.valueOf(ch4[1]) + String.valueOf(ch4[2]));
char ch5[]=(String.valueOf(input5)).toCharArray();
Arrays.sort(ch5);
sum+=Integer.parseInt(String.valueOf(ch5[0]) + String.valueOf(ch5[1]) + String.valueOf(ch5[2]));
return sum;
----------------------------------------------------------------------------------------------------------------
public class Program
{
public static void Main()
{
int input1=Convert.ToInt32(Console.ReadLine());
int input2=Convert.ToInt32(Console.ReadLine());
int input3=Convert.ToInt32(Console.ReadLine());
int input4=Convert.ToInt32(Console.ReadLine());
int input5=Convert.ToInt32(Console.ReadLine());
string x=input1.ToString();
string y=input2.ToString();
string z=input3.ToString();
string a=input4.ToString();
string b=input5.ToString();
char[] ch1=x.ToCharArray();
Array.Sort(ch1);
char[] ch2=y.ToCharArray();
Array.Sort(ch2);
char[] ch3=z.ToCharArray();
Array.Sort(ch3);
char[] ch4=a.ToCharArray();
Array.Sort(ch4);
char[] ch5=b.ToCharArray();
Array.Sort(ch5);
string c=Convert.ToString(ch1[0])+Convert.ToString(ch1[1])+Convert.ToString(ch1[2]);
string d=Convert.ToString(ch2[0])+Convert.ToString(ch2[1])+Convert.ToString(ch2[2]);
string e=Convert.ToString(ch3[0])+Convert.ToString(ch3[1])+Convert.ToString(ch3[2]);
string f=Convert.ToString(ch4[0])+Convert.ToString(ch4[1])+Convert.ToString(ch4[2]);
string g=Convert.ToString(ch5[0])+Convert.ToString(ch5[1])+Convert.ToString(ch5[2]);
int num1=Int32.Parse(c);
int num2=Int32.Parse(d);
int num3=Int32.Parse(e);
int num4=Int32.Parse(f);
int num5=Int32.Parse(g);
int sum=0;
sum=num1+num2+num3+num4+num5;
Console.WriteLine(sum);
//26674,105,37943,95278 and 27845
}
}
6) FIND SUMEET SUM (Model-3)
FindSumeetSum: Sum of largest 3-digit numbers from given 5 numbers
Given 5 input numbers, Sumeet has to find the sum of the largest numbers that can be
produced using 3 digits from each of the above 5 numbers
Example-1
If the 5 input numbers are 23792,37221,10270,73391 and 12005,
The largest numbers that can be produced using 3 digits from each of these are
973,732,721,973 and 521 respectively and the sum of these largest numbers will be 3920.
Therefore, the expected result is 3920.
Example-2
If the 5 input numbers are 26674,105,37943,95278 and 27845
The largest numbers that can be produced using 3 digits from each of these are
766,510,974,987 and 875 respectively and the sum of these largest numbers will be 4112.
Therefore, the expected result is 4112.
Int findSumeetSum(int input1,int input2,int input3,int input4,int input5)
Code:
int sum=0,l;
char ch1[]=(String.valueOf(input1)).toCharArray();
Arrays.sort(ch1);
l=ch1.length;
sum+=Integer.parseInt(String.valueOf(ch1[l-1]) + String.valueOf(ch1[l-2]) + String.valueOf(ch1[l-3]));
char ch2[]=(String.valueOf(input2)).toCharArray();
Arrays.sort(ch2);
l=ch2.length;
sum+=Integer.parseInt(String.valueOf(ch2[l-1]) + String.valueOf(ch2[l-2]) + String.valueOf(ch2[l-3]));
char ch3[]=(String.valueOf(input3)).toCharArray();
Arrays.sort(ch3);
l=ch3.length;
sum+=Integer.parseInt(String.valueOf(ch3[l-1]) + String.valueOf(ch3[l-2]) + String.valueOf(ch3[l-3]));
char ch4[]=(String.valueOf(input4)).toCharArray();
Arrays.sort(ch4);
l=ch4.length;
sum+=Integer.parseInt(String.valueOf(ch4[l-1]) + String.valueOf(ch4[l-2]) + String.valueOf(ch4[l-3]));
char ch5[]=(String.valueOf(input5)).toCharArray();
Arrays.sort(ch5);
l=ch5.length;
sum+=Integer.parseInt(String.valueOf(ch5[l-1]) + String.valueOf(ch5[l-2]) + String.valueOf(ch5[l-3]));
return sum;
------------------------------------------------------------------------------------------------------------------
public static void Main()
{
int input1=Convert.ToInt32(Console.ReadLine());
int input2=Convert.ToInt32(Console.ReadLine());
int input3=Convert.ToInt32(Console.ReadLine());
int input4=Convert.ToInt32(Console.ReadLine());
int input5=Convert.ToInt32(Console.ReadLine());
string x=input1.ToString();
int l1=x.Length;
string y=input2.ToString();
int l2=y.Length;
string z=input3.ToString();
int l3=z.Length;
string a=input4.ToString();
int l4=a.Length;
string b=input5.ToString();
int l5=b.Length;
char[] ch1=x.ToCharArray();
Array.Sort(ch1);
char[] ch2=y.ToCharArray();
Array.Sort(ch2);
char[] ch3=z.ToCharArray();
Array.Sort(ch3);
char[] ch4=a.ToCharArray();
Array.Sort(ch4);
char[] ch5=b.ToCharArray();
Array.Sort(ch5);
string c=Convert.ToString(ch1[l1-1])+Convert.ToString(ch1[l1-2])+Convert.ToString(ch1[l1-3]);
string d=Convert.ToString(ch2[l2-1])+Convert.ToString(ch2[l2-2])+Convert.ToString(ch2[l2-3]);
string e=Convert.ToString(ch3[l3-1])+Convert.ToString(ch3[l3-2])+Convert.ToString(ch3[l3-3]);
string f=Convert.ToString(ch4[l4-1])+Convert.ToString(ch4[l4-2])+Convert.ToString(ch4[l4-3]);
string g=Convert.ToString(ch5[l5-1])+Convert.ToString(ch5[l5-2])+Convert.ToString(ch5[l5-3]);
int num1=Int32.Parse(c);
int num2=Int32.Parse(d);
int num3=Int32.Parse(e);
int num4=Int32.Parse(f);
int num5=Int32.Parse(g);
int sum=0;
sum=num1+num2+num3+num4+num5;
Console.WriteLine(sum);
//26674,105,37943,95278 and 27845
//4112
}
}
7) FIND SUMEET SUM (Model-4)
Find SumeetSum: Sum of smallest 2-digit numbers from given 4 numbers
Given 4 input numbers, Sumeet has to find the sum of the smallest numbers that can be
produced using 2 of each of the above 4 numbers
Example-1
If the 4 input numbers are 23792,37221,10270 and 73391,
The smallest numbers that can be produced using 2 digits from each of these are 22,12,00
and 13 respectively the sum of these smallest numbers will be 47. Therefore, the expected
result is 47.
Example-2
If the 4 input numbers are 26674,105,37943 and 95278,
The smallest numbers that can be produced using 2 digits from each of these are 24,01,33
and 25 respectively the sum of these smallest numbers will be 83. Therefore, the expected
result is 83.
Function prototype is as belowInt findSumeetSum(int input1,int input2,int input3,int input4)
Code:
int sum=0;
char ch1[]=(String.valueOf(input1)).toCharArray();
Arrays.sort(ch1);
sum+=Integer.parseInt(String.valueOf(ch1[0])+String.valueOf(ch1[1]));
char ch2[]=(String.valueOf(input2)).toCharArray();
Arrays.sort(ch2);
sum+=Integer.parseInt(String.valueOf(ch2[0])+String.valueOf(ch2[1]));
char ch3[]=(String.valueOf(input3)).toCharArray();
Arrays.sort(ch3);
sum+=Integer.parseInt(String.valueOf(ch3[0])+String.valueOf(ch3[1]));
char ch4[]=(String.valueOf(input4)).toCharArray();
Arrays.sort(ch4);
sum+=Integer.parseInt(String.valueOf(ch4[0])+String.valueOf(ch4[1]));
return sum;
8) ENCODED TWO STRINGS (Model-1)
Let us now look at the below examples
Example 1: input1="WIPRO" input 2="TECHNOLOGIES"
Output should be "TECHWOGIES"
Explanation -
The three parts of WIPRO will be "W", "IPR" and "O"
The three parts of TECHNOLOGIES will be "TECH", "NOLO" and "GIES"
So,
Password = First part of word 2 + First part of word 1+ Third part of word 1 + Third part of
word2
=TECH + W + O + GIES
= TECHWOGIES
Example 2: input="MACHINE" input 2="LEARNING"
Output should be "LEMANENG"
Explanation -
The three parts of MACHINE will be "MA", "CHI" and "NE"
The three parts of LEARNING will be "LE", "ARNI", and "NG"
So,
Password = First part of word 2 + First part of word 1+ Third part of word 1 + Third part of
word2
= LE + MA + NE + NG
= LEMANENG
Code:
String[][] res = new String[2][3];
String[] words = {input1, input2};
String password="";
for(int i=0;i<2;i++)
{
int l = words[i].length();
res[i][0] = words[i].substring(0,l/3);
res[i][1] = words[i].substring(l/3, l-l/3);
res[i][2] = words[i].substring(l-l/3);
}
password = res[1][0] + res[0][0] + res[0][2] + res[1][2];
return password;
------------------------------------------------------------------------------------------
public static void Main()
{
string str1=Console.ReadLine();
string str2=Console.ReadLine();
int l1=str1.Length;
int l2=str2.Length;
string[,] res=new String[2,3];
string[] words={str1,str2};
string password="";
for(int i=0;i<2;i++)
{
int l=words[i].Length;
res[i,0]=words[i].Substring(0,l/3);
res[i,1]=words[i].Substring(l/3,l-l/3);
res[i,2]=words[i].Substring(l-l/3);
}
password=res[1,0]+res[0,0]+res[0,2]+res[1,2];
Console.WriteLine(password);
}
}
9) ENCODED TWO STRINGS (Model-2)
Let us now look at the below examples
Example 1: input1="WIPRO" input 2="TECHNOLOGIES"
Output should be "IPRNOLOTECH"
Explanation -
The three parts of WIPRO will be "W", "IPR" and "O"
The three parts of TECHNOLOGIES will be "TECH", "NOLO" and "GIES"
So,
Password = Second part of word 1+ Second part of word 2 + First part of word 2
= IPR + NOLO + TECH
= IPRNOLOTECH
Example 2: input="MACHINE" input 2="LEARNING"
Output should be "CHIARNILE"
Explanation -
The three parts of MACHINE will be "MA", "CHI" and "NE"
The three parts of LEARNING will be "LE", "ARNI", and "NG"
So,
Password = Second part of word 1+ Second part of word 2 + First part of word 2
= LE + MA + NE + NG
= LEMANENG
Code:
String[][] res = new String[2][3];
String[] words = {input1, input2};
String password="";
for(int i=0;i<2;i++)
{
int l = words[i].length();
res[i][0] = words[i].substring(0,l/3);
res[i][1] = words[i].substring(l/3, l-l/3);
res[i][2] = words[i].substring(l-l/3);
}
password = res[0][1] + res[1][1] + res[1][0];
return password;
-----------------------------------------------------------------------------------------------
public class Program
{
public static void Main()
{
string str1=Console.ReadLine();
string str2=Console.ReadLine();
int l1=str1.Length;
int l2=str2.Length;
string[,] res=new String[2,3];
string[] words={str1,str2};
string password="";
for(int i=0;i<2;i++)
{
int l=words[i].Length;
res[i,0]=words[i].Substring(0,l/3);
res[i,1]=words[i].Substring(l/3,(l-l/3)-l/3);
res[i,2]=words[i].Substring(l-l/3);
}
password=res[0,1]+res[1,1]+res[1,0];
Console.WriteLine(password);
}
}
10) ENCODED TWO STRINGS (Model-3)
You are provided with TWO words. You are expected to split each word into THREE parts
each, and create a password using the below rule –
Password = Second part of word1+ Third part of word 2 + First part of word1 + First
part of word 2
For splitting a given word into three parts the below approach should be used
If word= "ABC" then part1=A, part2=B and part3=C
if word= "ABCD" then part1=A, part2=BC and part3=D
if word= "ABCDE" then part1=AB, part2=C and part3=DE
if word= "ABCDEF" then part1=AB, part2=CD and part3=EF
if word= "ABCDEFG" then part1=AB, part2=CDE and part3=FG
if word = "ABCDEFGH" then part1=ABC, part2=DE and part3=FGH
and so on
i.e.,
1) If the length of the given word can be equally divided into three parts, then each part
gets the same number letters (as seen in above examples of "ABC" and "ABCDEF")
2) If after dividing the length of the given word into three parts, there is one extra character
left, then the extra Character goes to the middle part i.e., part2. (as seen in above
examples of "ABCD" and "ABCDEFG")
3) If after dividing the length of the given word into three parts, there are two extra
characters left, then part1 and part3 get the extra characters (as seen in above
examples of "ABCDE" and "ABCDEFGH")
Let us now look at the below examples
Example 1: input1="WIPRO" input 2="TECHNOLOGIES"
Output should be "PGIESWITECH"
Explanation -
The three parts of WIPRO will be "WI", "P" and "RO"
The three parts of TECHNOLOGIES will be "TECH", "NOLO" and "GIES"
So,
Password = Second part of word1+ Third part of word 2 + First part of word1 + First part of
word 2
=P + GIES + WI + TECH
= PGIESWITECH
Example 2: input="MACHINE" input 2="LEARNING"
Output should be "CHIINGMALEA"
Explanation -
The three parts of MACHINE will be "MA", "CHI" and "NE"
The three parts of LEARNING will be "LEA", "RN", and "ING"
So,
Password = Second part of word1+ Third part of word 2 + First part of word1 + First part of
word 2
= CHI + ING + MA + LEA
= CHIINGMALEA
Code:
String[] words = {input1, input2};
String[][] parts = new String[2][3];
for(int i=0;i<2;i++)
{
int len = words[i].length();
if(len%3==0 || len%3==1)
{
parts[i][0] = words[i].substring(0, len/3);
parts[i][1] = words[i].substring(len/3, len-len/3);
parts[i][2] = words[i].substring(len-len/3);
}
else
{
parts[i][0] = words[i].substring(0, len/3+1);
parts[i][1] = words[i].substring(len/3+1, len-len/3-1);
parts[i][2] = words[i].substring(len-len/3-1);
}
}
return (parts[0][1]+parts[1][2]+parts[0][0]+parts[1][0]);
--------------------------------------------------------------------------------------------------
public static void Main()
{
string str1=Console.ReadLine();
string str2=Console.ReadLine();
int l1=str1.Length;
int l2=str2.Length;
string[,] res=new String[2,3];
string[] words={str1,str2};
string password="";
for(int i=0;i<2;i++)
{
int len = words[i].Length;
if(len%3==0 || len%3==1)
{
res[i,0] = words[i].Substring(0, len/3);
res[i,1] = words[i].Substring(len/3, (len-len/3)-len/3);
res[i,2] = words[i].Substring(len-len/3);
}
else
{
res[i,0] = words[i].Substring(0, len/3+1);
res[i,1] = words[i].Substring(len/3+1, len/3);
res[i,2] = words[i].Substring(len-len/3-1);
}
}
for(int a=0;a<2;a++)
{
for(int b=0;b<3;b++)
{
Console.WriteLine(res[a,b]);
}
}
password=(res[0,1]+res[1,2]+res[0,0]+res[1,0]);
Console.WriteLine(password);
}
}
11) ENCODED TWO STRINGS (Model-4)
You are provided with TWO words. You are expected to split each word into THREE parts
each, and create a password using the below rule –
Password = Third part of word2 + Second part of word1 + Second part of word2 + First
part of word1
For splitting a given word into three parts the below approach should be used
If word= "ABC" then part1=A, part2=B and part3=C
if word= "ABCD" then part1=A, part2=BC and part3=D
if word= "ABCDE" then part1=AB, part2=C and part3=DE
if word= "ABCDEF" then part1=AB, part2=CD and part3=EF
if word= "ABCDEFG" then part1=AB, part2=CDE and part3=FG
if word = "ABCDEFGH" then part1=ABC, part2=DE and part3=FGH
and so on
i.e.,
1) If the length of the given word can be equally divided into three parts, then each part
gets the same number letters (as seen in above examples of "ABC" and "ABCDEF")
2) If after dividing the length of the given word into three parts, there is one extra character
left, then the extra Character goes to the middle part i.e., part2. (as seen in above
examples of "ABCD" and "ABCDEFG")
3) If after dividing the length of the given word into three parts, there are two extra
characters left, then part1 and part3 get the extra characters (as seen in above
examples of "ABCDE" and "ABCDEFGH")
Let us now look at the below examples
Example 1: input1="WIPRO" input 2="TECHNOLOGIES"
Output should be "GIESPNOLOWI"
Explanation -
The three parts of WIPRO will be "WI", "P" and "RO"
The three parts of TECHNOLOGIES will be "TECH", "NOLO" and "GIES"
So,
Password = Third part of word2 + Second part of word1 + Second part of word2 + First part of
word1
= GIES + P + NOLO + WI
= GIESPNOLOWI
Example 2: input="MACHINE" input 2="LEARNING"
Output should be "INGCHIRNMA"
Explanation -
The three parts of MACHINE will be "MA", "CHI" and "NE"
The three parts of LEARNING will be "LEA", "RN", and "ING"
So,
Password = Third part of word2 + Second part of word1 + Second part of word2 + First part of
word1
= ING + CHI + RN + MA
= INGCHIRNMA
Code:
String[] words = {input1, input2};
String[][] parts = new String[2][3];
for(int i=0;i<2;i++)
{
int len = words[i].length();
if(len%3==0 || len%3==1)
{
parts[i][0] = words[i].substring(0, len/3);
parts[i][1] = words[i].substring(len/3, len-len/3);
parts[i][2] = words[i].substring(len-len/3);
}
else
{
parts[i][0] = words[i].substring(0, len/3+1);
parts[i][1] = words[i].substring(len/3+1, len-len/3-1);
parts[i][2] = words[i].substring(len-len/3-1);
}
}
return (parts[1][2]+parts[0][1]+parts[1][1]+parts[0][0]);
------------------------------------------------------------------------------------------------------------
public static void Main()
{
string str1=Console.ReadLine();
string str2=Console.ReadLine();
int l1=str1.Length;
int l2=str2.Length;
string[,] res=new String[2,3];
string[] words={str1,str2};
string password="";
for(int i=0;i<2;i++)
{
int len = words[i].Length;
if(len%3==0 || len%3==1)
{
res[i,0] = words[i].Substring(0, len/3);
res[i,1] = words[i].Substring(len/3, (len-len/3)-len/3);
res[i,2] = words[i].Substring(len-len/3);
}
else
{
res[i,0] = words[i].Substring(0, len/3+1);
res[i,1] = words[i].Substring(len/3+1, len/3);
res[i,2] = words[i].Substring(len-len/3-1);
}
}
for(int a=0;a<2;a++)
{
for(int b=0;b<3;b++)
{
Console.WriteLine(res[a,b]);
}
}
password=(res[1,2]+res[0,1]+res[1,1]+res[0,0]);
Console.WriteLine(password);
}
}
12) ENCODED TWO STRINGS (Model-5)
You are provided with TWO words. You are expected to split each word into THREE parts
each, and create a password using the below rule –
Password = First part of word2 + First part of word1 + Third part of word1 + Third part
of word2
For splitting a given word into three parts the below approach should be used
If word= "ABC" then part1=A, part2=B and part3=C
if word= "ABCD" then part1=A, part2=BC and part3=D
if word= "ABCDE" then part1=AB, part2=C and part3=DE
if word= "ABCDEF" then part1=AB, part2=CD and part3=EF
if word= "ABCDEFG" then part1=AB, part2=CDE and part3=FG
if word = "ABCDEFGH" then part1=ABC, part2=DE and part3=FGH
and so on
i.e.,
1) If the length of the given word can be equally divided into three parts, then each part
gets the same number letters (as seen in above examples of "ABC" and "ABCDEF")
2) If after dividing the length of the given word into three parts, there is one extra character
left, then the extra Character goes to the middle part i.e., part2. (as seen in above
examples of "ABCD" and "ABCDEFG")
3) If after dividing the length of the given word into three parts, there are two extra
characters left, then part1 and part3 get the extra characters (as seen in above
examples of "ABCDE" and "ABCDEFGH")
Let us now look at the below examples
Example 1: input1="WIPRO" input 2="TECHNOLOGIES"
Output should be "TECHWIROGIES"
Explanation -
The three parts of WIPRO will be "WI", "P" and "RO"
The three parts of TECHNOLOGIES will be "TECH", "NOLO" and "GIES"
So,
Password = First part of word2 + First part of word1 + Third part of word1 + Third part of
word2
= TECH + WI + RO + GIES
= TECHWIROGIES
Example 2: input="MACHINE" input 2="LEARNING"
Output should be "LEAMANEING"
Explanation -
The three parts of MACHINE will be "MA", "CHI" and "NE"
The three parts of LEARNING will be "LEA", "RN", and "ING"
So,
Password = First part of word2 + First part of word1 + Third part of word1 + Third part of
word2
= LEA + MA + NE + ING
= LEAMANEING
Code:
String[] words = {input1, input2};
String[][] parts = new String[2][3];
for(int i=0;i<2;i++)
{
int len = words[i].length();
if(len%3==0 || len%3==1)
{
parts[i][0] = words[i].substring(0, len/3);
parts[i][1] = words[i].substring(len/3, len-len/3);
parts[i][2] = words[i].substring(len-len/3);
}
else
{
parts[i][0] = words[i].substring(0, len/3+1);
parts[i][1] = words[i].substring(len/3+1, len-len/3-1);
parts[i][2] = words[i].substring(len-len/3-1);
}
}
return (parts[1][0]+parts[0][0]+parts[0][2]+parts[1][2]);
-------------------------------------------------------------------------------------------
public class Program
{
public static void Main()
{
string str1=Console.ReadLine();
string str2=Console.ReadLine();
int l1=str1.Length;
int l2=str2.Length;
string[,] res=new String[2,3];
string[] words={str1,str2};
string password="";
for(int i=0;i<2;i++)
{
int len = words[i].Length;
if(len%3==0 || len%3==1)
{
res[i,0] = words[i].Substring(0, len/3);
res[i,1] = words[i].Substring(len/3, (len-len/3)-len/3);
res[i,2] = words[i].Substring(len-len/3);
}
else
{
res[i,0] = words[i].Substring(0, len/3+1);
res[i,1] = words[i].Substring(len/3+1, len/3);
res[i,2] = words[i].Substring(len-len/3-1);
}
}
for(int a=0;a<2;a++)
{
for(int b=0;b<3;b++)
{
Console.WriteLine(res[a,b]);
}
}
password=(res[1,0]+res[0,0]+res[0,2]+res[1,2]);
Console.WriteLine(password);
}
}
13) FIND KEY (Model-1):
Find Key:
You are provided with 3 numbers : input1, input2 and input3.
Each of these are 4 digits numbers within >=1000 and <=9999
i.e.,
1000<=input1<=9999
1000<=input2<=9999
1000<=input3<=9999
You are expected to find the key using below formula:
Key = Sum of Largest digits of each number + Sum of Second Largest digits of each
number
For Example, input1=3521, input2=2452 input3=1352
Key = (5+5+5) + (3+4+3) = 25
Assuming three numbers are passed to given function and complete the function to find and return the
Key
ANSWER:
int th1=input1/1000;
int h1=(input1/100)%10;
int t1=(input1/10)%10;
int u1=input1%10;
int []a={h1,t1,u1,th1};
Arrays.sort(a);
int l1=a[3];
int sl1=a[2];
int th2=input2/1000;
int h2=(input2/100)%10;
int t2=(input2/10)%10;
int u2=input2%10;
int []b={h2,t2,u2,th2};
Arrays.sort(b);
int l2=b[3];
int sl2=b[2];
int th3=input3/1000;
int h3=(input3/100)%10;
int t3=(input3/10)%10;
int u3=input3%10;
int []c={h3,t3,u3,th3};
Arrays.sort(c);
int l3=c[3];
int sl3=c[2];
int key=(l1+l2+l3)+(sl1+sl2+sl3);
return key;
----------------------------------------------------------------------------
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
int largest_digit1=0;
int largest_digit2=0;
int largest_digit3=0;
int smallest_digit1=9;
int smallest_digit2=9;
int smallest_digit3=9;
while(a>0){
int max1=a % 10;
int min1=a % 10;
if(largest_digit1< max1){
largest_digit1=max1;
}
if(smallest_digit1>min1){
smallest_digit1=min1;
}
a=a/10;
while(b>0){
int max2=b % 10;
int min2=b%10;
if(largest_digit2< max2){
largest_digit2=max2;
}
if(smallest_digit2 > min2){
smallest_digit2=min2;
}
b=b/10;
}
while(c>0){
int max3=c % 10;
int min3=c%10;
if(largest_digit3< max3){
largest_digit3=max3;
}
if(smallest_digit3 > min3){
smallest_digit3=min3;
}
c=c/10;
}
System.out.print(largest_digit1+smallest_digit1+largest_digit2+smallest_digit2+largest_digit3+smallest_digit3);
}
}
14) FIND KEY (Model-2):
You are provided with 3 numbers input1,input2,input3.
Each of these are four digit numbers within the range >=1000 and <=9999
i.e
1000<=input1<=9999
1000<=input2<=9999
1000<=input3<=9999
you are expected to find the key using the below formula
Key=[smallest digit in the thousands place of all three numbers][LARGEST digit in the hundreds place
of all the three numbers]
[smallest digit in the tens place of all three numbers][LARGEST digit in the units place of all three
numbers]
for e.g if input1=3521,input2=2452,input3=1352,then Key=[1][5][2][2]=1522
Assuming that the 3 numbers are passed to the given function.Complete the function to find and
return the key.
ANSWER:
int th1=input1/1000;
int h1=(input1/100)%10;
int t1=(input1/10)%10;
int u1=input1%10;
int th2=input2/1000;
int h2=(input2/100)%10;
int t2=(input2/10)%10;
int u2=input2%10;
int th3=input3/1000;
int h3=(input3/100)%10;
int t3=(input3/10)%10;
int u3=input3%10;
int mth=Math.min(Math.min(th1,th2),th3);
int mh=Math.max(Math.max(h1,h2),h3);
int mt=Math.min(Math.min(t1,t2),t3);
int mu=Math.max(Math.max(u1,u2),u3);
int key=(mth*1000)+(mh*100)+(mt*10)+mu;
return key;
15) FIND KEY (Model-3):
You are provided with 3 numbers input1,input2,input3.
Each of these are four digit numbers within the range >=1000 and <=9999
i.e
1000<=input1<=9999
1000<=input2<=9999
1000<=input3<=9999
you are expected to find the key using the below formula
Key=[LARGEST digit in the thousands place of all three numbers][smallest digit in the hundreds
place of all the three numbers][LARGEST digit in the tens place of all three numbers][smallest digit
in the units place of all three numbers]
for e.g if input1=3521,input2=2452,input3=1352,then Key=[3][3][5][1]=3351
Assuming that the 3 numbers are passed to the given function.Complete the function to find and
return the key.
ANSWER:
int th1=input1/1000;
int h1=(input1/100)%10;
int t1=(input1/10)%10;
int u1=input1%10;
int th2=input2/1000;
int h2=(input2/100)%10;
int t2=(input2/10)%10;
int u2=input2%10;
int th3=input3/1000;
int h3=(input3/100)%10;
int t3=(input3/10)%10;
int u3=input3%10;
int mth=Math.max(Math.max(th1,th2),th3);
int mh=Math.min(Math.min(h1,h2),h3);
int mt=Math.max(Math.max(t1,t2),t3);
int mu=Math.min(Math.min(u1,u2),u3);
int key=(mth*1000)+(mh*100)+(mt*10)+mu;
return key;
16) TRAVERSE ARRAY AND FIND KEY (Model-1)
Question
Traverse Array and Find Key –
Mohan has received an array of numbers
The numbers in this array are special because each number consists of two parts -a “KEY”
part and a “NEXT ADDRESS” part For example, if the number in the array is 411, the leftmost
digit in the number is “4” is the “KEY part and all the remaining digits in number be “11 form
the “NEXT ADDRESS part.
Mohan’s task is to start from the first array element, pick the “KEY”part go to the “NEXT
ADDRESS array element pick Its “KEY” part, go to the “NEXT ADDRESS array element, pick
its “KEY part, and continue this cycle the encounters a negative number While traversing
through the array in this fashion, we need to perform an alternate addition and subtraction of
the KEYS.
The result of alternate addition and subtraction of all the keys is the expected final result. Note
that we should stop traversing (traveling) through the array when a negative number is
encountered (See Examples 1 and 2 below) Important: If the array does NOT contain any
negative number, the result should be the largest number in the array (See Example 3 below)
Help Mohan by writing the code to find the FINAL Result. Input1 represents the array of
numbers, and input2 represents the number of elements in the array.
Example 1 –
If the array input 1 is 74 -56 15 71 92 23 and input2 is 6
First array element = 74
Here, KEY = 7
NEXT_ADDRESS – 4
4
th array element = 92 (NOTE THAT ARRAY ELEMENT ADDRESS STARTS FROM 0, SO 4th
element is 92)
Here, KEY = 9 NEXT ADDRESS = 2
2
nd array element 15
Here, KEY = 1, NEXT ADDRESS = 5
5
th array element – 23
Here KEY = 2 NEXT ADDRESS = 3
3
rd array element 71
Here, KEY 7, NEXT ADDRESS =1
1
st array element =-56
Here, KEY = 5 NEXT ADDRESS -STOP (because we have reached a negative number).
FINAL RESULT = Alternatively Add and Subtract the keys = 7+9-1+ 2-7+5= 15
Code:
int i, flag=0;
int max = Integer.MIN_VALUE;
for(i=0;i<input2;i++)
{
if(input1[i]<0)
flag=1;
if(input1[i]>max)
max=input1[i];
}
if(flag==0)
return max;
int sign=-1, res=0, key, digits, power;
i=0;
while(i<input2 && flag==1)
{
if(input1[i]<0)
{
flag=0;
input1[i]*=-1;
}
digits = (int)(Math.log10(input1[i])+1);
power = (int)(Math.pow(10, digits-1));
key = (int)input1[i]/power;
if(i==0)
res = key;
else
res = res + key*(sign=sign*-1);
i = input1[i]%power;
}
return res;
17) TRAVERSE ARRAY AND FIND KEY (Model-2)
Mohan has received an array of numbers
The numbers in this array are special because each number consists of two parts -a “KEY” part and a
“NEXT ADDRESS” part For example, if the number in the array is 411, the leftmost digit in the number
le “11” is the “KEY part and all the remaining digits in number be “4 form the “NEXT ADDRESS part.
Mohan’s task is to start from the first array element, pick the “KEY”part go to the “NEXT ADDRESS
array element pick Its “KEY” part, go to the “NEXT ADDRESS array element, pick its “KEY part, and
continue this cycle the encounters a negative number While traversing through the array in this
fashion, ne needs to perform an alternate addition and subtraction of the KEYS.
The result of alternate addition and subtraction of all the keys is the expected final result. Note that
we should stop traversing (traveling) through the array when a negative number is encountered (See
Examples 1 and 2 below) Important: If the array does NOT contain any negative number, the resume
should be the largest number in the array.
Note that we should stop traversing (traveling )through the array when a negative number is
encountered (See Examples 1 and 2 below)
Important: If the array does NOT contain any negative number, the result should be the smallest
number in the array
(See Example 3 below)
Help Mohan by writing the code to find the FINAL Result.
Input represents the array of numbers, and input represents the number of elements in the array
Example 1-1 the array input1 is{ 47,-65,51,17,29,32} and input2 is 6
First array element 47
Here, KEY 7,NEXT ADDRESS = 4
4th array element 29 (NOTE THAT ARRAY ELEMENT ADDRESS STARTS FROM 0, So 4th element is 29)
Here, KEY 9 NEXT ADDRESS= 2
2nd array element = 51
Here, KEY 1 NEXT ADDRESS = 5
5th array element = 32
Here, KEY 2 NEXT ADDRESS=3
3rd array element 17
Here KEY 7 NEXT ADDRESS = 1
1st array element = -65
Here, KEY=5 NEXT ADDRESS - STOP (because we have reached a negative number)
FINAL RESULT = Alternately subhead and Add the key 7-9+1-2+7-5=1
Example 2 - If the array s {47,65,51,17,29,-32} and input2 is 6
First array element 47
Here KEY 7 NEXT ADDRESS=4
4th array element is 29
Here, KEY 9 NEXT ADDRESS =2
2nd may element is 51
Here KEY 1 NEXT ADDRESS=5
5th array element - 32
Here, KEY- 2 NEXT_ADDRESS-STOP (because we have reached a negative number)
FINAL RESULT Animatedly Subtract and Add the key =7-9+1-2=-3
Example 3 - the array is 47 65 51 12 29 32 54 and input2 is 7
Here we see that the array does NOT contain any negative number, so the result should be calculated
as the smallest number in the array
FINAL RESULT 12
ANSWER:
import java.util.*;
public class MyClass {
public int findKey(int input1[],int input2){
int flag=0,k=0,key,add;
int a[]=new int[input2];
for(int i=0;i<input2;i++){
if(input1[i]<0){
flag=1;
break;
}
}
if(flag==0){
Arrays.sort(input1);
return input1[0];
}
else{
key=input1[0]%10;
a[k++]=key;
input1[0]=input1[0]/10;
add=input1[0];
while(input1[add]>0){
key=input1[add]%10;
a[k++]=key;
input1[add]=input1[add]/10;
add=input1[add];
}
a[k++]=-(input1[add]%10);
}
int sum=0;
for(int i=0;i<k;i++){
if(i%2==0)
sum+=a[i];
else
sum-=a[i];
}
return sum;
}
18) FIND PASSWORD (Model-1)
Detective Buckshee junior has been approached by the shantiniketan kids society for help in
finding the password to the games complex.After hearing the scenario,detective Buckshee
junior realises that he will need a programmer's support.He contacts you requests your help.
Please help the detective by writing a function to generate the password.
The scenario is as belowFive numbers are available with the kids.
These numbers are either stable or unstable
A number is stable if each of its digits occur the same number of times, i.e the frequency of each digit
in the number is the same.
For e.g:2277,4004,11,23,583835,1010 are examples of stable numbers.
Similarly,A number is unstable if the frequency of each digit in the number is NOT the same.For
eg:221,4314,101,233,58135,101 are examples of unstable numbers.
The password can be found as belowi.e password=(Number of unstable numbers*10)+Number of stable
numbers
Assuming that the fiVE numbers are passed to a function as input1,input2,input3,input4,input
5.complete the function to find and return the password.
For example:
If input1=12,input2=1313,input3=122,input4=678 and input5=898 , we see that there are THREE
stable numbers i.e 12,1313 and 678 and
TWO unstable numbers i.e 122 and 898
so,the password should be=(Number of Unstable numbers*10)+Number of stable
numbers=(2*10)+3=23
***********************************************************************
int[] num = {input1, input2, input3, input4, input5};
int stable=0, unstable=0, i, j;
for(i=0;i<5;i++)
{
int[] freq = new int[10]; //frequencies of all the digits
int temp=num[i];
int maxf=0;
while(temp!=0)
{
int r = temp%10;
freq[r]++;
temp/=10;
if(freq[r]>maxf)
maxf=freq[r];
}
for(j=0;j<10;j++)
{
if(freq[j]!=0 && freq[j]!=maxf)
break;
}
if(j==10)
stable++;
else
unstable++;
}
return (unstable*10 + stable);
19) FIND PASSWORD (Model-2)
Detective Buckshee junior has been approached by the shantiniketankids society for help in
finding the password to the games complex.After hearing the scenario,detective Buckshee
junior realises that he will need a programmer's support.He contacts you requests your help.
Please help the detective by writing a function to generate the password.
The scenario is as belowFive numbers are available with the kids.
These numbers are either stable or unstable
A numbers is stable if each of its digit occur the same number of times, i.e the frequency of each digit
in the number is the same.
For e.g:2277,4004,11,23,583835,1010 are examples of stable numbers.
Similarily ,A number is unstable if the frequency of each digit in the number is NOT the same.For
eg:221,4314,101,233,58135,101 are examples of unstable numbers.
The password can be found as belowi.e password=(Number of stable numbers*10)+Number of unstable
numbers
Assuming that the fiVE numbers are passed to a function as
input1,input2,input3,input4,input5.complete the function to find and return the password.
For example:
If input1=12,input2=1313,input3=122,input4=678 and input5=898 , we see that there are THREE
stable numbers i.e 12,1313 and 678 and
TWO unstable numbers i.e 122 and 898
so,the password should be=(Number of stable numbers*10)+Number of Unstable
numbers=(3*10)+1=32
ANSWER:
int[] num = {input1, input2, input3, input4, input5};
int stable=0, unstable=0, i, j;
for(i=0;i<5;i++)
{
int[] freq = new int[10]; // frequencies of all the digits
int temp=num[i];
int maxf=0;
while(temp!=0)
{
int r = temp%10;
freq[r]++;
temp/=10;
if(freq[r]>maxf)
maxf=freq[r];
}
for(j=0;j<10;j++)
{
if(freq[j]!=0 && freq[j]!=maxf)
break;
}
if(j==10)
stable++;
else
unstable++;
}
return (stable*10 + unstable);
20) FIND PASSWORD (Model-3)
Detective Buckshee junior has been approached by the shantiniketankids society for help in
finding the password to the games complex.After hearing the scenario,detective Buckshee
junior realises that he will need a programmer's support.He contacts you requests your help.
Please help the detective by writing a function to generate the password.
The scenario is as belowFive numbers are available with the kids.
These numbers are either stable or unstable
A number is stable if each of its digits occur the same number of times, i.e the frequency of each digit
in the number is the same.
For e.g:2277,4004,11,23,583835,1010 are examples of stable numbers.
Similarly ,A number is unstable if the frequency of each digit in the number is NOT the same.For
eg:221,4314,101,233,58135,101 are examples of unstable numbers.
The password can be found as belowi.e Password=Maximum of all stable numbers+Minimum of all
Unstable numbers.
Assuming that the five numbers are passed to a function as input1,input2,input3,input4,input
5.complete the function to find and return the password.
For example:
If input1=12,input2=1313,input3=122,input4=678 and input5=898 , we see that there are THREE
stable numbers i.e 12,1313 and 678 and
TWO unstable numbers i.e 122 and 898
so,the password should be=Maximum of all stable numbers+Minimum of all Unstable
numbers=1313+122=1435
ANSWER:
int input[]=new int[]{input1,input2,input3,input4,input5};
int h[]=new int[10];
String s="";
int st=0,u=0;
int min=Integer.MAX_VALUE;
int max=Integer.MIN_VALUE;
for(int i=0;i<input.length;i++){
for(int j=0;j<10;j++)
h[j]=0;
s=String.valueOf(input[i]);
for(int j=0;j<s.length();j++){
h[Integer.parseInt(s.substring(j,j+1))]++;
}
int c=0,k=-1,flag=0;
for(int j=0;j<10;j++){
if(h[j]!=0 && c==0){
k=h[j];
c++;
}
if(h[j]!=0 && h[j]!=k){
flag=1;
break;
}
}
if(flag==1)
{
if(input[i]<min)
min=input[i];
}
else
{
if(input[i]>max)
max=input[i];
}
}
return max+min;
21) FIND PASSWORD (Model-4)
Detective Buckshee junior has been approached by the shantiniketankids society for help in
finding the password to the games complex. After hearing the scenario, detective Buckshee
junior realizes that he will need a programmer's support. He contacts you requests your help.
Please help the detective by writing a function to generate the password.
The scenario is as belowFive numbers are available with the kids.
These numbers are either stable or unstable
A numbers is stable if each of its digit occur the same number of times, i.e the frequency of each digit
in the number is the same.
For e.g:2277,4004,11,23,583835,1010 are examples of stable numbers.
Similarly, A number is unstable if the frequency of each digit in the number is NOT the same. For
eg:221,4314,101,233,58135,101 are examples of unstable numbers.
The password can be found as belowi.e Password=sum of all stable numbers - sum of all Unstable numbers
Assuming that the five numbers are passed to a function as input1, input2, input3, input4, input5.
Complete the function to find and return the password.
For example:
If input1=12,input2=1313,input3=122,input4=678 and input5=898 , we see that there are THREE
stable numbers 12,1313 and 678 and
TWO unstable numbers 122 and 898
So, the password should be=sum of all stable numbers – sum of all Unstable numbers=983
ANSWER:
int[] num = {input1, input2, input3, input4, input5};
int stable=0, unstable=0, i, j;
for(i=0;i<5;i++)
{
int[] freq = new int[10]; // frequencies of all the digits
int temp=num[i];
int maxf=0;
while(temp!=0)
{
int r = temp%10;
freq[r]++;
temp/=10;
if(freq[r]>maxf)
maxf=freq[r];
}
for(j=0;j<10;j++)
{
if(freq[j]!=0 &&freq[j]!=maxf)
break;
}
if(j==10)
stable=stable+num[i];
else
unstable=unstable+num[i];
}
return (stable - unstable);
22) FIND PASSWORD (Model-5)
Detective Buckshee junior has been approached by the shantiniketankids society for help in
finding the password to the games complex. After hearing the scenario, detective Buckshee
junior realizes that he will need a programmer's support. He contacts you requests your help.
Please help the detective by writing a function to generate the password.
The scenario is as belowFive numbers are available with the kids.
These numbers are either stable or unstable
A numbers is stable if each of its digit occur the same number of times, i.e the frequency of each digit
in the number is the same.
For e.g:2277,4004,11,23,583835,1010 are examples of stable numbers.
Similarly, A number is unstable if the frequency of each digit in the number is NOT the same. For
eg:221,4314,101,233,58135,101 are examples of unstable numbers.
The password can be found as belowi.e Password=Maximum of all stable numbers - Minimum of all
Unstable numbers
Assuming that the five numbers are passed to a function as input1, input2, input3, input4, input5.
Complete the function to find and return the password.
For example:
If input1=12,input2=1313,input3=122,input4=678 and input5=898 , we see that there are THREE
stable numbers 12,1313 and 678 and
TWO unstable numbers 122 and 898
So, the Password should be=Maximum of all stable numbers - Minimum of all Unstable
numbers=1313-122=1191
ANSWER:
int input[]=new int[]{input1,input2,input3,input4,input5};
int h[]=new int[10];
String s="";
int st=0,u=0;
int min=Integer.MAX_VALUE;
int max=Integer.MIN_VALUE;
for(int i=0;i<input.length;i++){
for(int j=0;j<10;j++)
h[j]=0;
s=String.valueOf(input[i]);
for(int j=0;j<s.length();j++){
h[Integer.parseInt(s.substring(j,j+1))]++;
}
int c=0,k=-1,flag=0;
for(int j=0;j<10;j++){
if(h[j]!=0 && c==0){
k=h[j];
c++;
}
if(h[j]!=0 && h[j]!=k){
flag=1;
break;
}
}
if(flag==1)
{
if(input[i]<min)
min=input[i];
}
else
{
if(input[i]>max)
max=input[i];
}
}
return max-min;
23) FIND PASSWORD (Model-6)
Detective Buckshee junior has been approached by the shantiniketankids society for help in
finding the password to the games complex. After hearing the scenario, detective Buckshee
junior realizes that he will need a programmer's support. He contacts you requests your help.
Please help the detective by writing a function to generate the password.
The scenario is as belowFive numbers are available with the kids.
These numbers are either stable or unstable
A numbers is stable if each of its digit occur the same number of times, i.e the frequency of each digit
in the number is the same.
For e.g:2277,4004,11,23,583835,1010 are examples of stable numbers.
Similarly, A number is unstable if the frequency of each digit in the number is NOT the same. For
eg:221,4314,101,233,58135,101 are examples of unstable numbers.
The password can be found as belowi.e Password=Maximum of all Unstable numbers - Minimum of all
Unstable numbers
Assuming that the five numbers are passed to a function as input1, input2, input3, input4, input5.
Complete the function to find and return the password.
For example:
If input1=12,input2=1313,input3=122,input4=678 and input5=898 , we see that there are THREE
stable numbers 12,1313 and 678 and
TWO unstable numbers 122 and 898
So, the Password should be=Maximum of all Unstable numbers - Minimum of all Unstable
numbers=898-122=776
ANSWER:
int input[]=new int[]{input1,input2,input3,input4,input5};
int h[]=new int[10];
String s="";
int l=0;
int res[]=new int[input.length];
for(int i=0;i<input.length;i++){
for(int j=0;j<10;j++)
h[j]=0;
s=String.valueOf(input[i]);
for(int j=0;j<s.length();j++){
h[Integer.parseInt(s.substring(j,j+1))]++;
}
int c=0,k=-1,flag=0;
for(int j=0;j<10;j++){
if(h[j]!=0 && c==0){
k=h[j];
c++;
}
if(h[j]!=0 && h[j]!=k){
flag=1;
break;
}
}
if(flag==1)
res[l++]=input[i];
}
int min=Integer.MAX_VALUE,max=Integer.MIN_VALUE;
for(int i=0;i<l;i++)
{
if(res[i]<min)
min=res[i];
if(res[i]>max)
max=res[i];
}
return max-min;
24) FIND PASSWORD (Model-7)
Detective Buckshee junior has been approached by the shantiniketankids society for help in
finding the password to the games complex. After hearing the scenario, detective Buckshee
junior realizes that he will need a programmer's support. He contacts you requests your help.
Please help the detective by writing a function to generate the password.
The scenario is as belowFive numbers are available with the kids.
These numbers are either stable or unstable
A numbers is stable if each of its digit occur the same number of times, i.e the frequency of each digit
in the number is the same.
For e.g:2277,4004,11,23,583835,1010 are examples of stable numbers.
Similarly, A number is unstable if the frequency of each digit in the number is NOT the same. For
eg:221,4314,101,233,58135,101 are examples of unstable numbers.
The password can be found as belowi.e Password=sum of all Unstable numbers
Assuming that the five numbers are passed to a function as input1, input2, input3, input4, input5.
Complete the function to find and return the password.
For example:
If input1=12,input2=1313,input3=122,input4=678 and input5=898 , we see that there are THREE
stable numbers 12,1313 and 678 and
TWO unstable numbers 122 and 898
So, the Password should be=sum of all Unstable numbers=898+122=1020
ANSWER:
int[] num = {input1, input2, input3, input4, input5};
int stable=0, unstable=0, i, j;
for(i=0;i<5;i++)
{
int[] freq = new int[10]; // frequencies of all the digits
int temp=num[i];
int maxf=0;
while(temp!=0)
{
int r = temp%10;
freq[r]++;
temp/=10;
if(freq[r]>maxf)
maxf=freq[r];
}
for(j=0;j<10;j++)
{
if(freq[j]!=0 &&freq[j]!=maxf)
break;
}
if(j==10)
stable=stable+num[i];
else
unstable=unstable+num[i];
}
return unstable;
25) FIND PASSWORD (Model-8)
Detective Buckshee junior has been approached by the shantiniketankids society for help in
finding the password to the games complex. After hearing the scenario, detective Buckshee
junior realizes that he will need a programmer's support. He contacts you requests your help.
Please help the detective by writing a function to generate the password.
The scenario is as belowFive numbers are available with the kids.
These numbers are either stable or unstable
A numbers is stable if each of its digit occur the same number of times, i.e the frequency of each digit
in the number is the same.
For e.g:2277,4004,11,23,583835,1010 are examples of stable numbers.
Similarly, A number is unstable if the frequency of each digit in the number is NOT the same. For
eg:221,4314,101,233,58135,101 are examples of unstable numbers.
The password can be found as belowi.e Password=Maximum of all stable numbers - Minimum of all stable
numbers
Assuming that the five numbers are passed to a function as input1, input2, input3, input4, input5.
Complete the function to find and return the password.
For example:
If input1=12,input2=1313,input3=122,input4=678 and input5=898 , we see that there are THREE
stable numbers 12,1313 and 678 and
TWO unstable numbers 122 and 898
So, the Password should be=Maximum of all stable numbers - Minimum of all stable numbers=1313-
12=1301
ANSWER:
int input[]=new int[]{input1,input2,input3,input4,input5};
int h[]=new int[10];
String s="";
int l=0;
int res[]=new int[input.length];
for(int i=0;i<input.length;i++){
for(int j=0;j<10;j++)
h[j]=0;
s=String.valueOf(input[i]);
for(int j=0;j<s.length();j++){
h[Integer.parseInt(s.substring(j,j+1))]++;
}
int c=0,k=-1,flag=0;
for(int j=0;j<10;j++){
if(h[j]!=0 && c==0){
k=h[j];
c++;
}
if(h[j]!=0 && h[j]!=k){
flag=1;
break;
}
}
if(flag==0)
res[l++]=input[i];
}
int min=Integer.MAX_VALUE,max=Integer.MIN_VALUE;
for(int i=0;i<l;i++)
{
if(res[i]<min)
min=res[i];
if(res[i]>max)
max=res[i];
}
return max-min;
26) FIND PASSWORD (Model-9)
Detective Buckshee junior has been approached by the shantiniketankids society for help in
finding the password to the games complex. After hearing the scenario, detective Buckshee
junior realizes that he will need a programmer's support. He contacts you requests your help.
Please help the detective by writing a function to generate the password.
The scenario is as belowFive numbers are available with the kids.
These numbers are either stable or unstable
A numbers is stable if each of its digit occur the same number of times, i.e the frequency of each digit
in the number is the same.
For e.g:2277,4004,11,23,583835,1010 are examples of stable numbers.
Similarly, A number is unstable if the frequency of each digit in the number is NOT the same. For
eg:221,4314,101,233,58135,101 are examples of unstable numbers.
The password can be found as belowi.e Password=Maximum of all stable numbers + Minimum of all stable
numbers
Assuming that the five numbers are passed to a function as input1, input2, input3, input4, input5.
Complete the function to find and return the password.
For example:
If input1=12,input2=1313,input3=122,input4=678 and input5=898 , we see that there are THREE
stable numbers 12,1313 and 678 and
TWO unstable numbers 122 and 898
So, the Password should be=Maximum of all stable numbers + Minimum of all stable numbers=1313 +
12=1325
ANSWER:
int input[]=new int[]{input1,input2,input3,input4,input5};
int h[]=new int[10];
String s="";
int l=0;
int res[]=new int[input.length];
for(int i=0;i<input.length;i++){
for(int j=0;j<10;j++)
h[j]=0;
s=String.valueOf(input[i]);
for(int j=0;j<s.length();j++){
h[Integer.parseInt(s.substring(j,j+1))]++;
}
int c=0,k=-1,flag=0;
for(int j=0;j<10;j++){
if(h[j]!=0 && c==0){
k=h[j];
c++;
}
if(h[j]!=0 && h[j]!=k){
flag=1;
break;
}
}
if(flag==0)
res[l++]=input[i];
}
int min=Integer.MAX_VALUE,max=Integer.MIN_VALUE;
for(int i=0;i<l;i++)
{
if(res[i]<min)
min=res[i];
if(res[i]>max)
max=res[i];
}
return max+min;
27) PERSONS AND TOKENS (Model-1)
There are N people sitting in a room
Each of them have been given a token number Only 3 people out of them have their token numbers in
a sequence
You are expected to find the 3 people whose numbers are in sequence in this context, sequence
means numbers which are in continuous order for eg 97, 98,99 are numbers in a sequence However,
3, 5, 7 should not Be considered a continuous sequence.
You are expected to write the logic within a given function find Sequence which takes 3 input
parameters - Input 1= number, representing the number of elements in the array - input2 and input3
(This also represents,N the number of people in the room)
Input 2[] = array of strings representing the names of the people sitting in the room
Input3[] = array of integers representing the token number of the people sitting in the room Note:-
There is a one-to-one mapping between the array input 3 and input 2, ie.
input 3[0] represents the token number of the person input 2[0]
input 3[1] represents the token number of the person input 2[1]
input 3[2] represents the token number of the person input 2[2]
and so on
Expected output: The function find Sequence is expected to return a string containing the names of
the 3 persons whose token numbers are in sequence . Note that the names of the 3 persons should be
in descending order of their token numbers. The names should be separated using a colon. If there is
no sequence available in the given input then return " NONE", find the examples below.
Example 1:
Input1 = 10
Input2 = {“Rajesh", “ Abdul", “Rahul”, “Priya”, “Sanjay", "Nidhi”, “Varun”, “Varsha”, “Basil”, “Asif”}
Input3 = {99, 46, 39, 102, 45, 521, 65, 4, 47, 741}
Expected output "Basil:Abdul: Sanjay"
Explanation: Out of the 10 token numbers, the numbers 45, 46 and 47 are in sequence and their
descending order 47 46 and 45 Hence the expected output is a string containing names separated by a
colon of the 3 persons who hold these tokens. Note that the names are in descending sequence of
their respective taken numbers,
Example 2:
Input1= 7
Input = ["aa", "bb", "cc", "dd", "ee", "gg"]
Inpul 3={9,89,5,0,6,65,4}
Expected output = "ee:cc:gg"
Code:
int i,x=0,y=0,z=0,flag=0;
int[] a=input3.clone();
Arrays.sort(a);
for(i=0;i<input1-2;i++)
{
if(a[i+1]-a[i]==1 && a[i+2]-a[i+1]==1)
{
flag=1;
x=a[i+2];
y=a[i+1];
z=a[i];
break;
}
}
if(flag==0)
return "NONE";
int fi=0,si=0,ti=0;
for(i=0;i<input1;i++)
{
if(x==input3[i])
fi=i;
if(y==input3[i])
si=i;
if(z==input3[i])
ti=i;
}
return input2[fi]+":"+input2[si]+":"+input2[ti];
28) PERSONS AND TOKENS (Model-2)
Input2[] = array of strings representing the names of the people sitting in the room
Input3[] = array of integers representing the token number of the people sitting in the room
Note: There is a one-to-one mapping between the array input3 and input2, i.e.
Input3[0] represents the token number of the person input2[0]
Input3[1] represents the token number of the person input2[2]
Input3[2] represents the token number of the person input 2[2]
……..and so on
Expected output: The function findsequence is expected to return a string containing the names of
the 3 persons whose token numbers are in sequence. Note that the names of the 3 persons should be
in ascending order of their token numbers. The names should be separated using a colon. If there is
no sequence available in the given input then return “NONE”, find the examples below
Example 1:
Input1 = 10
Input2 = {“Rajesh","Abdul”, "Rahul”, "Priya","Sanjay", "Nidhi", “Varun" ,”Varsha”, “Basil", “Asif”)
Input3 = {99,46,39,102,45,521,65,4,47,741}
Expected output = "Sanjay:Abdul:Basil"
Explanation: Out of the 10 token numbers, the numbers 45, 46 and 47 are in sequence. Hence the
expected output is a string containing names separated by a colon of the 3 persons who hold these
tokens. Note that the names are in ascending sequence of their respective token numbers
Example 2:
Input1= 17
Input2={“aa","bb",” cc”,” dd”, "ee”, “ff”,”gg")
Input3 = {9,89,5,0,6,65,4}
Expected output ="gg:cc:ee "
Explanation: Out of the 7 token numbers, the numbers 4, 5 and 6 are in sequence Hence the expected
output is a string containing names separated by a colon of the 3 persons who hold these tokens. Note
that the names are in ascending sequence of their respective token numbers
Example 3:
Input1 = 4
Input2 = {"Priya", "Soumya" ,"Sam”, “Vidya”}
Input3 = {9,76,8,23}
Expected output = "NONE"
Explanation: The given 4 token numbers do not contain any sequence of 3 numbers. Hence, the
expected output is a string containing NONE
Code:
int i,x=0,y=0,z=0,flag=0;
int[] a=input3.clone();
Arrays.sort(a);
for(i=0;i<input1-2;i++)
{
if(a[i+1]-a[i]==1 && a[i+2]-a[i+1]==1)
{
flag=1;
x=a[i];
y=a[i+1];
z=a[i+2];
break;
}
}
if(flag==0)
return "NONE";
int fi=0,si=0,ti=0;
for(i=0;i<input1;i++)
{
if(x==input3[i])
fi=i;
if(y==input3[i])
si=i;
if(z==input3[i])
ti=i;
}
return input2[fi]+":"+input2[si]+":"+input2[ti];
29) USER ID GENERATION (Model-1)
User ID Generation: Joseph's team has been assigned the task of creating user-ids for all participants
of an online gaming competition Joseph has designed a process for generating the user-d using the
participant's First_Name ,Last_Name, PIN code and a number N The process defined by Joseph is as
below –
Step1- Compare the lengths of First_Name and Last_Name of the participant. The one that is shorter
will be called “Smaller Name” and the one that is longer will be called the “longer Name” if both
First_Name and Last_Name are of equal Length ,then the name that appears earlier in alphabetical
order will be called “Smaller Name” and the name that appears later in alphabetical order will be
called the “Longer Name”
Step2 - The user-should be generated as below –
Last Letter of the smaller name + Entre word of the longer name + Digit at position N in the PIN when
traversing PIN from left to right +Digit at position N in the PIN when traversing the PIN from right to
left
Step3 - Toggle the alphabets of the user-id generated in step -2 i.e. upper-case alphabets should
become lower-case and lower-case alphabets should become upper-case.
Let us see a few examples
Example-1 - If the participant's details are as below
First Name = Ray
Last Name =Roy
PIN = 560037
N= 6
Step1 - Length of Last_Name is less than the Length of First_Name, so the Smaler Name is “Roy” and
the Longer Name is “Rajiv”
Step2 - The user id will be = Last Letter of the smaller name +Entre word in the longer name + Digit at
position N in the PIN when traversing the PIN from left to right +Digit at position N in the PIN when
traversing the PIN from right to left
=Last Letter of “Roy”+ Entre word in Rajiv+ 6th Digit of Pin from left + 6th Digit of PIN from right
=y+ Rajiv+7+5
Therefore, user-id=yRajiv75
Step3 -Toggle the alphabet in the user-id. So,user-id = YrAJIV75
ANSWER:
class UserMainCode
{
public String userIdGeneration(String input1,String input2,int input3,int input4){
int s1=input1.length();
int s2=input2.length();
String longer="";
String smaller="";
String output1="";
if(s1==s2)
{
if(input1.compareTo(input2)>0)
{
longer=input1;
smaller=input2;
}
else
{
longer=input2;
smaller=input1;
}
}
if(s1>s2){
longer=input1;
smaller=input2;
}
else if(s1<s2)
{
longer=input2;
smaller=input1;
}
String pin=input3+"";
String output=smaller.charAt(smaller.length()-1)+longer+pin.charAt
(input4-1)+pin.charAt(pin.length()-input4);
for(int i=0;i<output.length();i++)
{
if(Character.isLowerCase(output.charAt(i)))
{
output1+=Character.toUpperCase
(output.charAt(i));
}
else
{
output1+=Character.toLowerCase
(output.charAt(i));
}
}
return output1;
}
}
30) USER ID GENERATION (Model-2)
User ID Generation: Joseph's team has been assigned the task of creating user-ids for all participants
of an online gaming competition Joseph has designed a process for generating the user-d using the
participant's First_Name ,Last_Name, PIN code and a number N The process defined by Joseph is as
below –
Step1- Compare the lengths of First_Name and Last_Name of the participant. The one that is shorter
will be called “Smaller Name” and the one that is longer will be called the “longer Name” if both
First_Name and Last_Name are of equal Length ,then the name that appears earlier in alphabetical
order will be called “Smaller Name” and the name that appears later in alphabetical order will be
called the “Longer Name”
Step2 - The user-should be generated as below –
First Letter of the Longer name + Entire word of the Smaller name + Digit at position N in the PIN when
traversing PIN from left to right +Digit at position N in the PIN when traversing the PIN from right to
left
Step3 - Toggle the alphabets of the user-id generated in step -2 i.e. upper-case alphabets should
become lower-case and lower-case alphabets should become upper-case.
Let us see a few examples.
Example-1 - If the participant's details are as below -
First Name Rajiv
Last Name = Roy
PIN = 560037
N=6
Step 1 - Length of Last_Name is less than the Length of First_Name so the Smaller Name is “Roy” and
the Longer Name is “Rajiv”
Step 2 - The user-id will be= First Letter of the longer name + Entire word of the smaller name + Digit
at position N in the PIN when traversing the PIN from left to right +Digit at position N in the PIN when
traversing the PIN from right to left
=First Letter of “Rajiv” +Entire word of “Roy” + 6th Digit PIN from left + 6th Digit of PIN from right
=R+Roy + 7+5
Therefore, user-id =RRoy75
Step 3 - Toggle the alphabets in the user-id, user-id= rrOY75
ANSWER:
import java.util.*;
public class MyClass {
public String userIdGeneration(String input1,String input2,int input3,int input4)
{
String s="",small="",longer="";
if(input1.length()<input2.length()){
small=input1;
longer=input2;
}
else if(input1.length()>input2.length())
{
small=input2;
longer=input1;
}
else
{
if((input1.compareTo(input2))<0)
{
small=input1;
longer=input2;
}
else
{
small=input2;
longer=input1;
}
}
s+=String.valueOf(longer.charAt(0))+small;
String pin=String.valueOf(input3);
String pinrev=String.valueOf(new StringBuilder(pin).reverse());
s+=String.valueOf(pin.charAt(input4-1))+String.valueOf(pinrev.charAt(input4-1));
String s1="";
for(int i=0;i<s.length()-2;i++){
if(Character.isUpperCase(s.charAt(i)))
s1+=String.valueOf(Character.toLowerCase(s.charAt(i)));
else
s1+=String.valueOf(Character.toUpperCase(s.charAt(i)));
}
s1+=s.substring(s.length()-2,s.length());
return s1;
}
}
31.) USER ID GENERATION (MODEL-3 WITH DIFFERENT TEST CASE)
User ID Generation: Joseph’s team has been assigned the task of creating user-ids for all
participants of an online gaming competition. Joseph has designed a process for generating the
user-id using the participant's First_Name,Last_Name, PIN code and a number N. The process
defined by Joseph is as below -
Step1 - Compare the lengths of First_Name and Last_Name of the participant. The one that is
shorter will be called “Smaller Name” and the one that islonger will be called the “Longer
Name”. If both First_Name and Last_Name are of equal Length, then the name that appears
earlier in alphabetical order will be called “Smaller Name” and the name that appears later in
alphabetical order will be called the “Longer Name”.
Step2-The user-id should be generated as below –
Last Letter of the longer name + Entire word of the smaller name + Digit at position N in the
PIN when traversing the PIN from left to right + Digit at position N in the PIN when traversing
the PIN from right to left
Step 3 - Toggle the alphabets of the user-id generated in step-2 i.e. upper-case alphabets should
become lower-case and lower-case alphabets should become upper-case
Let us see a few examples
Example-1 - If the participants details are as below
First Name = Rajiv
Last Name = Roy
PIN = 560037
N=6
Step1 - Length of Last Name is less than the Length of First Name, so the Smaller Name is
"Roy and the Longer Name is "Rajiv“
Step2 - The user-id will be = Last Letter of the longer name + Entire word of the smaller name
+ Digit at position N in the PIN when traversing the PIN from left to right + Digit at position N
in the PIN when traversing the PIN from right to left
= Last Letter of “Rajiv” + Entire word of “Roy” + 6th Digit of pin from left + 6th Digit of pin
from right
= v + Roy + 7 + 5
Therefore, user-id = vRoy75
Step3 -Toggle the alphabets in the user-id = VrOY75
ANSWER
import java.io.*;
import java.util.*;
// Read only region start
class UserIDGeneration {
public String userIdGeneration(String input1,String input2,int input3,int input4){
// Read only region end
String firstName = input1;
String lastName = input2;
int pin = input3;
int N = input4;
String longerName;
String smallerName;
StringBuilder userId = new StringBuilder();
if (firstName.length() >lastName.length()) {
longerName = firstName;
smallerName = lastName;
} else if (firstName.length() <lastName.length()) {
longerName = lastName;
smallerName = firstName;
}
else
{
if (firstName.compareTo(lastName) <1 ) {
longerName = lastName;
smallerName = firstName;
}
else
{
longerName = firstName;
smallerName = lastName;
}
}
userId.append(longerName.charAt(longerName.length() - 1));
userId.append(smallerName);
for (int i = 0; i<userId.length(); i++)
{
if (Character.isUpperCase(userId.charAt(i)))
userId.setCharAt(i, Character.toLowerCase(userId.charAt(i)));
else
userId.setCharAt(i, Character.toUpperCase(userId.charAt(i)));
}
userId.append(String.valueOf(pin).charAt(N - 1));
userId.append(String.valueOf(pin).charAt(String.valueOf(pin).length() - N));
return userId.toString();
}
}
31) ENCODED THREE STRINGS (Model-1)
Anand was assigned the task of coming up with an encoding mechanism for any given three
strings. He has come up with the following plan.
Step ONE: Given any three strings, break each string into 3 parts each.
For example- if the three strings are below:
Input 1: “John”
Input 2: “Johny”
Input 3: “Janardhan”
“John” should be split into “J”, “oh”, “n,” as the FRONT, MIDDLE and END part, respectively.
“Johny” should be split into “Jo”, “h”, “ny” as the FRONT, MIDDLE and END, respectively.
“Janardhan” should be split into “Jan”, “ard”, “han” as the FRONT, MIDDLE and END part,
respectively.
i.e.
1) If the no. of characters in the string are in multiples of 3, then each split –part will
contain equal no of characters, as seen in the example of “Janadhan”.
2) If the no. of characters in the string are NOT in multiples of 3 ,and if there is one
character more than multiple of 3, then the middle part will get the extra character ,as
seen in the example of “john”.
3) If the no. of characters in the string are Not in multiples of 3 and if there are two
characters more than multiple of 3, then the FRONT and END parts will get one extra
character each, as seen in the example of “Johny”.
Step TWO: Concatenate (join) the FRONT, MIDDLE and END parts of the string as per the
below specified concatenation – rule to from three Output strings.
Output1: FRONT part of input 1 + FRONT part of input 2 + FRONT part of input 3
Output2: MIDDLE part of input1 + MIDDLE part of input2 + MIDDLE part of input3
Output3: END part of the input1 + END part of input2 + END part of input3
For example, for the above example input strings:
Output1 = “J” + “Jo” + “Jan” = “JJoJan”
Output2 = “oh” + “h” + “ard” = “ohhard”
Output3 = “n” + “ny” + “han” = “nnyhan”
Step THREE:
Process the resulting output strings based on the output-processing rule. After the above two
steps, we will now have three output strings. Further processing is required only for the third
output string as per below rule-
“Toggle the case of each character in the string”, i.e., in the third output string, all lower-case
characters should be made upper-case and vice versa.
For example, for the above example strings, output3 is “nnyhan”, so after applying the toggle
rule. Output3 should become “NNYHAN”.
Final Result – The three output strings after applying the above three steps i.e. for the above
example.
Output1 = “JJoJan”
Output2= “ohhard’
Output3 = “NNYHAN”
Help Anand to write a program that would do the above.
Code:
String[][] res = new String[3][3];
String[] words = {input1, input2, input3};
for(int i=0;i<3;i++)
{
int l = words[i].length();
if(l%3==0 || l%3==1)
{
res[i][0] = words[i].substring(0, l/3);
res[i][1] = words[i].substring(l/3, l-l/3);
res[i][2] = words[i].substring(l-l/3);
}
else
{
res[i][0] = words[i].substring(0, l/3+1);
res[i][1] = words[i].substring(l/3+1, l-l/3-1);
res[i][2] = words[i].substring(l-l/3-1);
}
}
//Output1: FRONT part of input 1 + FRONT part of input 2 + FRONT part of input 3
String output1 = res[0][0] + res[1][0] + res[2][0];
//Output2: MIDDLE part of input1 + MIDDLE part of input2 + MIDDLE part of input3
String output2 = res[0][1] + res[1][1] + res[2][1];
//Output3: END part of the input1 + END part of input2 + END part of input3
String output3 = res[0][2] + res[1][2] + res[2][2];
String temp=output3;
output3="";
for(int i=0;i<temp.length();i++)
{
if(Character.isUpperCase(temp.charAt(i)))
output3 = output3 + Character.toLowerCase(temp.charAt(i));
else
output3 = output3 + Character.toUpperCase(temp.charAt(i));
}
return new Result(output1, output2, output3);
32) ENCODED THREE STRINGS (Model-2)
Anand was assigned the task of coming up with an encoding mechanism for any given three
strings. He has come up with the following plan.
Step ONE: Given any three strings, break each string into 3 parts each.
For example- if the three strings are below:
Input 1: “John”
Input 2: “Johny”
Input 3: “Janardhan”
“John” should be split into “J”, “oh”, “n,” as the FRONT, MIDDLE and END part, respectively.
“Johny” should be split into “Jo”, “h”, “ny” as the FRONT, MIDDLE and END, respectively.
“Janardhan” should be split into “Jan”, “ard”, “han” as the FRONT, MIDDLE and END part,
respectively.
i.e.
1) If the no. of characters in the string are in multiples of 3, then each split –part will
contain equal no of characters, as seen in the example of “Janadhan”.
2) If the no. of characters in the string are NOT in multiples of 3 ,and if there is one
character more than multiple of 3, then the middle part will get the extra character ,as
seen in the example of “John”.
3) If the no. of characters in the string are Not in multiples of 3 and if there are two
characters more than multiple of 3, then the FRONT and END parts will get one extra
character each, as seen in the example of “Johny”.
Step TWO: Concatenate (join) the FRONT, MIDDLE and END parts of the string as per the
below specified concatenation – rule to from three Output strings.
Output1: FRONT part of input 1 + MIDDLE part of input 2 + END part of input 3
Output2: MIDDLE part of input1 + END part of input2 + FRONT part of input3
Output3: END part of the input1 + FRONT part of input2 + MIDDLE part of input3
For example, for the above example input strings:
Output1 = “J” + “h” + “han” = “Jhhan”
Output2 = “oh” + “ny” + “Jan” = “ohnyJan”
Output3= “n” + “Jo” + “ard” += “nJoard”
Step THREE:
Process the resulting output strings based on the output-processing rule. After the above two
steps, we will now have three output strings. Further processing is required only for the third
output string as per below rule-
“Toggle the case of each character in the string”, i.e., in the third output string, all lower-case
characters should be made upper-case and vice versa.
For example, for the above example strings, output3 is “nJoard”, so after applying the toggle
rule. Output3 should become “NjOARD”.
Final Result – The three output strings after applying the above three steps i.e. for the above
example.
Output1 = “Jnhan”
Output2= “ohnyJan’
Output3 = “NjOARD”
Help Anand to write a program that would do the above.
Code:
String[][] res = new String[3][3];
String[] words = {input1, input2, input3};
for(int i=0;i<3;i++)
{
int l = words[i].length();
if(l%3==0 || l%3==1)
{
res[i][0] = words[i].substring(0, l/3);
res[i][1] = words[i].substring(l/3, l-l/3);
res[i][2] = words[i].substring(l-l/3);
}
else
{
res[i][0] = words[i].substring(0, l/3+1);
res[i][1] = words[i].substring(l/3+1, l-l/3-1);
res[i][2] = words[i].substring(l-l/3-1);
}
}
//Output1: FRONT part of input 1 + MIDDLE part of input 2 + END part of input 3
String output1 = res[0][0] + res[1][1] + res[2][2];
//Output2: MIDDLE part of input1 + END part of input2 + FRONT part of input3
String output2 = res[0][1] + res[1][2] + res[2][0];
//Output3: END part of the input1 + FRONT part of input2 + MIDDLE part of input3
String output3 = res[0][2] + res[1][0] + res[2][1];
String temp=output3;
output3="";
for(int i=0;i<temp.length();i++)
{
if(Character.isUpperCase(temp.charAt(i)))
output3 = output3 + Character.toLowerCase(temp.charAt(i));
else
output3 = output3 + Character.toUpperCase(temp.charAt(i));
}
return new Result(output1, output2, output3);
33) ENCODED THREE STRINGS (Model-3)
Anand was assigned the task of coming up with an encoding mechanism for any given three
strings. He has come up with the following plan.
Step ONE: Given any three strings, break each string into 3 parts each.
For example- if the three strings are below:
Input 1: “John”
Input 2: “Johny”
Input 3: “Janardhan”
“John” should be split into “J”, “oh”, “n,” as the FRONT, MIDDLE and END part, respectively.
“Johny” should be split into “Jo”, “h”, “ny” as the FRONT, MIDDLE and END, respectively.
“Janardhan” should be split into “Jan”, “ard”, “han” as the FRONT, MIDDLE and END part,
respectively.
i.e.
1) If the no. of characters in the string are in multiples of 3, then each split –part will
contain equal no of characters, as seen in the example of “Janadhan”.
2) If the no. of characters in the string are NOT in multiples of 3 ,and if there is one
character more than multiple of 3, then the middle part will get the extra character ,as
seen in the example of “John”.
3) If the no. of characters in the string are Not in multiples of 3 and if there are two
characters more than multiple of 3, then the FRONT and END parts will get one extra
character each, as seen in the example of “Johny”.
Step TWO: Concatenate (join) the FRONT, MIDDLE and END parts of the string as per the
below specified concatenation – rule to from three Output strings.
Output1: FRONT part of input 1 + END part of input 2 + MIDDLE part of input 3
Output2: MIDDLE part of input1 + FRONT part of input2 + END part of input3
Output3: END part of the input1 + MIDDLE part of input2 + FRONT part of input3
For example, for the above example input strings:
Output1 = “J” + “nh” + “ard” = “Jnyard”
Output2 = “oh” + “Jo” + “han” = “ohJohan”
Output3= “n” + “h” + “Jan” += “nhJan”
Step THREE:
Process the resulting output strings based on the output-processing rule. After the above two
steps, we will now have three output strings. Further processing is required only for the third
output string as per below rule-
“Toggle the case of each character in the string”, i.e., in the third output string, all lower-case
characters should be made upper-case and vice versa.
For example, for the above example strings, output3 is “nhJan”, so after applying the toggle
rule. Output3 should become “NHjAN”.
Final Result – The three output strings after applying the above three steps i.e. for the above
example.
Output1 = “Jnyard”
Output2= “ohJohan’
Output3 = “NHjAN”
Help Anand to write a program that would do the above.
Code:
String[][] res = new String[3][3];
String[] words = {input1, input2, input3};
for(int i=0;i<3;i++)
{
int l = words[i].length();
if(l%3==0 || l%3==1)
{
res[i][0] = words[i].substring(0, l/3);
res[i][1] = words[i].substring(l/3, l-l/3);
res[i][2] = words[i].substring(l-l/3);
}
else
{
res[i][0] = words[i].substring(0, l/3+1);
res[i][1] = words[i].substring(l/3+1, l-l/3-1);
res[i][2] = words[i].substring(l-l/3-1);
}
}
// Output1: FRONT part of input 1 + END part of input 2 + MIDDLE part of input 3
String output1 = res[0][0] + res[1][2] + res[2][1];
// Output2: MIDDLE part of input1 + FRONT part of input2 + END part of input3
String output2 = res[0][1] + res[1][0] + res[2][2];
// Output3: END part of the input1 + MIDDLE part of input2 + FRONT part of input3
String output3 = res[0][2] + res[1][1] + res[2][0];
String temp=output3;
output3="";
for(int i=0;i<temp.length();i++)
{
if(Character.isUpperCase(temp.charAt(i)))
output3 = output3 + Character.toLowerCase(temp.charAt(i));
else
output3 = output3 + Character.toUpperCase(temp.charAt(i));
}
return new Result(output1, output2, output3);
34) ENCODED THREE STRINGS (Model-4)
Anand was assigned the task of coming up with an encoding mechanism for any given three
strings. He has come up with the following plan.
Step ONE: Given any three strings, break each string into 3 parts each.
For example- if the three strings are below:
Input 1: “John”
Input 2: “Johny”
Input 3: “Janardhan”
“John” should be split into “J”, “oh”, “n,” as the FRONT, MIDDLE and END part, respectively.
“Johny” should be split into “J”, “ohn”, “y” as the FRONT, MIDDLE and END, respectively.
“Janardhan” should be split into “Jan”, “ard”, “han” as the FRONT, MIDDLE and END part,
respectively.
i.e.
1) If the length of the given word can be equally divided into three parts, then each part gets
the same number of letters (as seen in above examples of "John" and "Johny")
2) If the length of the given word cannot be equally divided into three parts, then the center
part i.e., part2 gets the extra number of characters (as seen in “Janardhan”)
Step TWO: Concatenate (join) the FRONT, MIDDLE and END parts of the string as per the
below specified concatenation – rule to from three Output strings.
Output1: FRONT part of input 1 + MIDDLE part of input 2 + END part of input 3
Output2: MIDDLE part of input1 + END part of input2 + FRONT part of input3
Output3: END part of the input1 + FRONT part of input2 + MIDDLE part of input3
For example, for the above example input strings:
Output1 = “J” + “ohn” + “han” = “Johnhan”
Output2 = “oh” + “y” + “Jan” = “ohyJan”
Output3= “n” + “J” + “ard” += “nJard”
Step THREE:
Process the resulting output strings based on the output-processing rule. After the above two
steps, we will now have three output string. Further processing is required only for the third
output string as per below rule-
“Toggle the case of each character in the string”, i.e., in the third output string, all lower-case
characters should be made upper-case and vice versa.
For example, for the above example strings, output3 is “nJard”, so after applying the toggle
rule. Output3 should become “NjARD”.
Final Result – The three output strings after applying the above three steps i.e. for the above
example.
Output1 = “Johnhan”
Output2= “ohyJan’
Output3 = “NjARD”
Help Anand to write a program that would do the above.
Code:
String[][] res = new String[3][3];
String[] words = {input1, input2, input3};
for(int i=0;i<3;i++)
{
int l = words[i].length();
res[i][0] = words[i].substring(0, l/3);
res[i][1] = words[i].substring(l/3, l-l/3);
res[i][2] = words[i].substring(l-l/3);
}
//Output1: FRONT part of input 1 + MIDDLE part of input 2 + END part of input 3
String output1 = res[0][0] + res[1][1] + res[2][2];
//Output2: MIDDLE part of input1 + END part of input2 + FRONT part of input3
String output2 = res[0][1] + res[1][2] + res[2][0];
//Output3: END part of the input1 + FRONT part of input2 + MIDDLE part of input3
String output3 = res[0][2] + res[1][0] + res[2][1];
String temp=output3;
output3="";
for(int i=0;i<temp.length();i++)
{
if(Character.isUpperCase(temp.charAt(i)))
output3 = output3 + Character.toLowerCase(temp.charAt(i));
else
output3 = output3 + Character.toUpperCase(temp.charAt(i));
}
return new Result(output1, output2, output3);
35) LARGEST POSSIBLE PALINDROME
Remove characters from a word form largest possible Palindrome:
What is a Palindrome?
Palindrome is a string that spells the same from either directions, for example abba, appa,
amma, malayalam, nayan, deed, level, madam, rotator, reviver stats, tenet…
Mohan was taught about palindromes at school today and he got fascinated by the idea of
palindromes. He started analyzing various words and thought it should be possible to create
palindromes from most words by removing a few characters from the word. Write a method to help
Mohan find the number of characters to be removed from a given word so that the remaining
characters in the word can form palindrome
NOTE: You are not expected to form one or all possible palindromes in the word You are expected to
only find the number of characters that have to be removed from the word so that the remaining
characters can form a palindrome
For example, the given word is Template If ‘m’, ‘p’,’l’ and ‘a’ are removed we are left with “Tete”
which is a good candidate to form a palindrome. In addition, if we let one of the characters,’m’,’p’,’l’
or ‘a’ stay within the word, we can still form valid palindromes. For e.g we remove ‘m’,’l’ and ‘a’ but
not ‘p’, then the set of characters in the word Would be “Tepte” which when re-arranged can form
palindromes Such as "Tepet” or “Etpte” So 3 is the number of characters that have to be removed
from “Template” so that the remaining characters can form he largest possible palindrome
The given function accepts one parameter input1 representing the word mat needs to be analyzedThe
method should return the number of characters to be removed
NOTE 1: if all the characters in the word are already sufficient to form a palindrome, then the number
of characters that have to be removed from the word should be 0.
For e.g the word is "Magma", then the result should be 0
NOTE 2: if all the characters in the word are different and cannot form a palindrome, then the number
of characters that have to be removed from the word should be -1
For e.g the word is “Victory” then the result should be -1
NOTE 3: Ignore the case of the letters white doing the check i.e "Template” or “template" or
“TEMplate” or “TEmPLAte” or “TEMPLATE” should all give the same result which is 3
NOTE 4: You can assume that the given word will be a single word with no spaces and only alphabet
characters.
ANSWER:
public int palindrome(String input1){
input1=input1.toLowerCase();
int h[]=new int[26];
for(int i=0;i<input1.length();i++){
h[(int)input1.charAt(i)-97]++;
}
int c=0;
for(int i=0;i<26;i++){
if(h[i]%2==1)
c++;
}
if(c==0 || c==1)
return 0;
else if(c==input1.length())
return -1;
else
return c-1;
}
36) WEIGHT OF HILL PATTERNS
Given,
the total levels(rows) in a hill pattern (input1),
the weight of the head level(first row) as input2, and
the weight increments of each subsequent row as input3,
you are expected to find the total weight of the hill pattern.
"Total levels" represents the number of rows in the pattern.
"Head level" represents the first row.
Weight of a level represents the value of each star (asterisk) in that row.
Note that the first row will have the weight of the head level,and the weight of each subsequent row
will keep increasing by the specified "weight increment".
The hill patterns will always be of the below format, starting with 1 star at head level and increasing 1
star at each level till level N.From second level(second row) a hash # also gets added to the pattern.
*
*#*
*#*#*
*#*#*#*
*#*#*#*#*
*#*#*#*#*#*
. . .and so on till level N
While the weight of a star * is equal to the weight of the current level(current row),the weight of the
hash # is equal to the weight of the previous level(previous row)
Let us see a couple of examples.
Example1 -
Given,
the total levels(total rows) in the hill pattern = 5 (input1)
the weight of the head level (first row) = 10(input2)
the weight increments of each subsequent level = 2(input3)
Then, The total weight of the hill pattern will be calculated as = 10 + (12+10+12) + (14+12+14+12+14)
+ (16+14+16+14+16+14+16) + (18+16+18+16+18+16+18+16+18) = 10 + 34 + 66 + 106 + 154 = 370
Example2 -
Given,
the total levels in the hill pattern = 4(input1)
the weight of the head level = 1(input2)
the weight increments of each subsequent level = 5(input3)
Then, Total weight of the hill pattern will be = 1 + (6+1+6) + (11+6+11+6+11) +
(16+11+16+11+16+11+16) = 1 + 13 + 45 + 97 = 156
ANSWER:
public static void Hill(int input1,int input2,int input3) {
int hash;
hash=input2;
int sum1=0,sum=0;
for(int i=0;i<input1;i++)
{
for(int j=0;j<=i;j++)
{
sum=sum+input2;
}
input2=input2+input3;
for(int k=0;k<=i&&i!=input2-1;k++)
{
sum1=sum1+hash;
}
hash=hash+input3;
}
return sum;
}
}
37) JUMBLED WORDS
Wipro plans to publish a daily online newsletter on its internal website channelW,and you have been
contacted to contribute to the JUMBLE game on the newsletter's fun page.The JUMBLE game presents
a sentence with each word jumbled,and the readers will be expected to unjumble them.
Your task is to provide the sentence in jumbled format, i.e each word in the sentence jumbled in some
fashion.
Because your task is to provide a jumbled sentence every day,you decide to write a program that can
take any proper sentence as input, and jumble the words in that sentence to produce a jumbled
sentence.
After some thought you decide below two ways of jumbling a word.....
Method-1(forward,backward):In the word,reading left to right(forward),Pick every odd letter starting
from the first letter,and then reading the word from right to left(backward),pick every even letter
starting from the last even letter in the word.
For example,
If the word is "PROJECT"the jumbled word will be "POETCJR"
Similarly,
If the word is "LEARNING" the jumbled word will be "LANNGIRE"
So,If the sentence is "PROJECT BASED LEARNING",the sentence with jumbled words should be
"POETCJR BSDEA LANNGIRE".
Method-2(forward,forward):In the word,reading from left to right(forward),Pick every odd letter
starting from the first letter, and then reading the word again from left to right(forward),pick every
even letter starting from the second letter in the word
For example,
If the word is "PROJECT", the jumbled word will be "POETRJC".
Similarly,
If the word is "LEARNING" the jumbled word will be "LANNERIG"
So,If the sentence is "PROJECT BASED LEARNING",the sentence with jumbled words should be
"POETRJC BSDAE LANNERIG".
The function JumbledWords takes two inputsinput1 which reperesents the string(proper sentence)containing one or
more words that are to be
jumbled.
input2 which represents the type of jumbling method(1 0r 2)
The function is expected to jumble the words in given sentence(input1) based on the jumbling method
specified(input2) and return the result(i.e sentence with each word jumbled).
Example1:
input1="PROJECT BASED LEARNING"
input2=1
Expected output="POETCJR BSDEA LANNGIRE"
Example2:
input1="PROJECT BASED LEARNING"
input2=2
Expected output="POETRJC BSDAE LANNERIG"
Example3:
input1="WIPRO LIMITED"
input2=1
Expected output="WPORI LMTDEII"
Example4:
input1="WIPRO LIMITED"
input2=2
Expected output="WPOIR LMTDIIE"
ANSWER:
public String jumbledWords(String input1,int input2){
String s[]=input1.split(" ");
String s1="",res="",even="",odd="";
for(int i=0;i<s.length;i++){
s1=s[i];
even="";
odd="";
for(int j=0;j<s1.length();j++){
if(j%2==0)
even+=String.valueOf(s1.charAt(j));
else
odd+=String.valueOf(s1.charAt(j));
}
if(input2==1)
res+=even+String.valueOf(new StringBuffer(odd).reverse())+" ";
else
res+=even+odd+" ";
}
return res;
}
38) FIX THE FORMULA
Kely has been tricked by her brother to answer a question with a number. She is perplexed. Here is the
question “Fo+23the3*like2+” it took time for her to understand. Now she wants to automate it with a
program so that any time her brother comes with such tricky String she could answer with lesser
efforts.
Here is what we have to do, separate the math operators and the digits.
Like in the above String you can see the operators (+,*,+) and digits (2,3,3,2).
Rest all characters are ignored
No arrange the digits and operators in the order of the appearance to get the correct result.
2+3*3+2 to be solved as
(2+3) = 5
Then, (5*3)=15
Then (15+2) = 17
So for the given String Fo+23the3*like2+final answer is 17
Help kely by writing a program to solve the above given problem.
Prototype: Public int fix TheFormula(String inpt1)
Assumptions:
1. Numbers present in the String are always considered as single digits(0-9)
2. Only operators used in the String are(+,-,*,/)
3. Always we will have length +1 numbers to operators (int the above example 3 operators and 4
numbers)
Sample Input/Output-1
Input1= we8+you2-7to/*32
Output=2
Explanation: Here the operators are [+,-,/,*] and the numbers are [8,2,7,3,2]
Thus we would be getting 8+2=>10-7=>3/3=>1*2=>2
Final answer is 2.
Sample Input/Output-2
Input1= i*-t5s-t8h1e4birds
Output=35
Explanation: Here the operators are [+,-,-] and the numbers are [5,8,1,4]
Thus we would be getting 5*8=>40-1=>39-4=>35
Final answer is 35.
ANSWER:
public int fixTheFormula(String input1){
int d[]=new int[input1.length()];
char c[]=new char[input1.length()];
int k1=0,k2=0;
for(int i=0;i<input1.length();i++){
if(!Character.isLetter(input1.charAt(i))){
if(Character.isDigit(input1.charAt(i))){
d[k1++]=Integer.parseInt(String.valueOf(input1.charAt(i)));
}
else{
c[k2++]=input1.charAt(i);
}
}
}
int res=d[0],k=1;
for(int i=0;i<k2;i++){
if(c[i]=='+'){
res+=d[k];
}
else if(c[i]=='-'){
res-=d[k];
}
else if(c[i]=='*'){
res*=d[k];
}
else{
res/=d[k];
}
k++;
}
return res;
}
39) FORM THE WORD
Given a string input1, which contains many numbers of words separated by : and each word contains
exactly two lower case alphabets, generate and output based upon the below 2 cases.
Note:
1. All the characters in input1 are lower case alphabets
2. Input1 will always contain more than one word separated by :
3. Output should be returned in UpperCase
Case 1:
Check whether the two alphabets are same
If yes then take one alphabet from it and add it to output
Example-1
Input1 = ww:ii:pp:rr:oo
Output= WIPRO
Explanation
Word1 is ww, both are same hence take w
Word2 is ii, both are same hence take i
Word3 is pp, both are same hence take p
Word4 is rr, both are same hence take r
Word5 is oo, both are same hence take o
Hence the output is WIPRO
Case 2:
If the two alphabets are not same, then find the position value of them and find maximum value –
minimum value.
Take the alphabet which comes at this (maximum value – minimum value) position in the alphabet
series.
Example-2
Input1= zx:za:ee
Output=BYE
Explanation
Word1 is zx, both are not same alphabets
Position value of z is 26
Position value of x is 24
Max-min will be 26-24 = 2
Alphabet which comes in 2nd position is b
Word2 is za, both are not same alphabets
Position value of z is 26
Position value of a is 1
Max-min will be 26-1 = 25
Alphabet which comes in 25th position is y
Word3 is ee, both are same hence take e
Hence the output is BYE
ANSWER:
import java.util.*;
public class MyClass {
public String formTheWord(String input1){
String s[]=input1.split(":");
String s1="",res="";
int x;
for(int i=0;i<s.length;i++){
s1=s[i];
if(((int)s1.charAt(0))-((int)s1.charAt(1))==0){
res+=String.valueOf(s1.charAt(0));
}
else if(((int)s1.charAt(0))-((int)s1.charAt(1))>0){
x=((int)s1.charAt(0))-((int)s1.charAt(1));
res+=String.valueOf((char)(96+x));
}
else{
x=((int)s1.charAt(1))-((int)s1.charAt(0));
res+=String.valueOf((char)(96+x));
}
}
return res.toUpperCase();
}
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
String s=sc.next();
System.out.println(new MyClass().formTheWord(s));
}
}
40) TWO DIGIT REDUCED SUBTRACTED FORM
Given a number, you are expected to find its two-digit “Reduced Subtracted Form(RSF)”
The “Reduced Subtracted Form(RSF)” of a number can be found by concatenating the difference
between its adjacent digits
To find the two-digit “Reduced Subtracted Form(RSF)”,we need to continue this process till the
resultant RSF is not a two digit number.
For egif the input number is 6928, its RSF can be found by concatenating the difference between (6
and 9), (9 and 2) and (2 and 8) as shown below –
Difference between 6 and 9 is 3
Difference between 9 and 2 is 7
Difference between 2 and 8 is 6
So, the “Reduced Subtracted Form(RSF)” of 6928 = 376
The resultant RSF (376) is not a two-digit number, so we must continue finding its “Reduced
Subtracted Form(RSF)”
Difference between 3 and 7 is 4
Difference between 7 and 6 is 1
So, the “Reduced Subtracted Form(RSF)” of 376 = 41
The resultant RSF (41) is two-digit number, so we have reached the “two-digit Reduced Subtracted
Form”.
Therefore, the two-digit RSF of 6928 = 41
Let’s see another example
If input1 = 5271
Expected output = 21
Explanation:
RSF of 5271 = (5-2)(2-7)(7-1)=356
RSF of 356=(3-5)(5-6)=21
Note1: input1 will be always be >=100
Note2:Note that while concatenating the differences, we are expected to use the absolute values(nonnegative)
Note3: The input values for all test cases in this program have been designed such that their two-digits
RSF will definitely result in a two-digit number
ANSWER:
Two digit reduced subtracted form
while(input1>=100)
{
int x=input1,l=0;
while(input1>0)
{
input1=input1/10;
l++;
}
int a[]=new int[l];
int i=l-1;
while(x>0)
{
a[i]=x%10;
x=x/10;
i--;
}
for(int j=0;j<l-1;j++)
{
input1=input1*10+Math.abs(a[j]-a[j+1]);
}
}
return input1;
41) MATCHING WORD
Given 2 String array s1 & s2 which contains x elements each. S1 contains jumbled words and
s2 contains corresponding correct words but in same or different order.
Find the matching word from S2 with the jumbled word from S1. Store the index of S2 in the
string S. Repeat the same for all the elements of S1 and concatenate the index values to the
string S.
Prototype: String findMatchingWord(string[] input1, string[] input2, int input3)
Input:2 string array S1, S2 &1 integer representing total elements in array S1 or S2.
S1- Array of jumbled words
S2- Array of correct words with the same or different order.
Output: string S which holds concatenated index values of S2.
Note: Elements in both the arrays shall be in lower case
Example 1:
Input1: {arc, nep, tis}
Input2: {sit,car,pen}
Input3: 3
Output: 120
Explanation:
The jumbled word “arc” frominput1 match with correct word “car” from input2. Index of
“car” is 1. Similarly “nep” matches with “pen”, index of “pen” is 2 and “tis” matches with
“sit”, whose index is 0. By concatenating all the index values we get the output as 120.
Example 2:
Input1: {cnhul, estl, rakeb, ahev}
Input2: {lets, have, lunch, break}
Input3: 4
Output: 2031
Answer:
String res="";
for(int i=0;i<input3;i++)
{
char t1[] = input1[i].toCharArray();
char t2[] = input2[i].toCharArray();
//sorting
Arrays.sort(t1);
Arrays.sort(t2);
input1[i]= new String(t1);
input2[i]= new String(t2);
}
for(int i=0;i<input3;i++)
{
for(int j=0;j<input3;j++)
{
if(input1[i].equals(input2[j]))
{
res=res+j;
break;
}
}
}
return Integer.parseInt(res);
-------------------------------------------------------------------------------------------------
import java.util.Arrays;
public class Main
{
public static void main(String[] args) {
String []s1={"arc", "nep", "tis"};
String []s2={"sit", "car", "pen"};
int x=s1.length;
int c=0,j=0,k=0,count=0;
char []c1={};
char []c2={};
for(int i=0;i<x;i++){
c1=s1[i].toCharArray();
Arrays.sort(c1);
for( j=0;j<x;j++){
c2=s2[j].toCharArray();
Arrays.sort(c2);
for( k=0;k<s2[j].length();k++){
if(c1.length==c2.length){
if(c1[k]==c2[k])
c++;
}
}
if(c==k){
count++;
System.out.print(j);
}
c=0;
}
}
}
}
----------------------------------------------------------------------------------------------------------
import java.util.Arrays;
import java.util.HashMap;
public class Main
{
public static void findMatchingWord(String[] i1,String[] i2,int i3){
for(int i=0;i<i3;i++){
char[] chars=i1[i].toCharArray();
Arrays.sort(chars);
i1[i]=new String(chars);
}
for(int i=0;i<i3;i++){
char[] chars=i2[i].toCharArray();
Arrays.sort(chars);
i2[i]=new String(chars);
}
HashMap<String, Integer>
pos=new HashMap<String, Integer>();
for(int i=0;i<i3;i++){
pos.put(i2[i],i);
}
String ans="";
for(int i=0;i<i3;i++){
ans=ans+ pos.get(i1[i]);
}
System.out.print(ans);
}
public static void main(String[] args){
String[] i1={"arc", "nep", "tis"};
String[] i2={"sit", "car", "pen"};
int i3=3;
String ans="";
findMatchingWord(i1,i2,i3);
42) ROBO Movement (90 degrees, 1 step):
String[] robo = input3.split("-");
int x = Integer.parseInt(robo[0]);
int y = Integer.parseInt(robo[1]);
String d = robo[2];
for(int i=0;i<input4.length();i+=2)
{
String c = input4.charAt(i)+"";
if(c.equals("R"))
{
if(d.equals("N"))
d="E";
else if(d.equals("E"))
d="S";
else if(d.equals("S"))
d="W";
else if(d.equals("W"))
d="N";
}
else if(c.equals("L"))
{
if(d.equals("N"))
d="W";
else if(d.equals("E"))
d="N";
else if(d.equals("S"))
d="E";
else if(d.equals("W"))
d="S";
}
else
{
if(d.equals("N") && y+1<=input2)
y=y+1;
else if(d.equals("E") && x+1<=input1)
x+=1;
else if(d.equals("S") && y-1>=0)
y-=1;
else if(d.equals("W") && x-1>=0)
x-=1;
else
return x+"-"+y+"-"+d+"-ER";
}
}
return x+"-"+y+"-"+d;
43) ROBO Movement (90 degrees, 2 steps):
String[] robo = input3.split("-");
int x = Integer.parseInt(robo[0]);
int y = Integer.parseInt(robo[1]);
String d = robo[2];
for(int i=0;i<input4.length();i+=2)
{
String c = input4.charAt(i)+"";
if(c.equals("R"))
{
if(d.equals("N"))
d="E";
else if(d.equals("E"))
d="S";
else if(d.equals("S"))
d="W";
else if(d.equals("W"))
d="N";
}
else if(c.equals("L"))
{
if(d.equals("N"))
d="W";
else if(d.equals("E"))
d="N";
else if(d.equals("S"))
d="E";
else if(d.equals("W"))
d="S";
}
else
{
if(d.equals("N") && y+2<=input2)
y+=2;
else if(d.equals("E") && x+2<=input1)
x+=2;
else if(d.equals("S") && y-2>=0)
y-=2;
else if(d.equals("W") && x-2>=0)
x-=2;
else
return x+"-"+y+"-"+d+"-ER";
}
}
return x+"-"+y+"-"+d;
44) ROBO Movement (90 degrees, 1 or 2 steps):
String[] robo = input3.split("-");
int x = Integer.parseInt(robo[0]);
int y = Integer.parseInt(robo[1]);
String d = robo[2];
for(int i=0;i<input4.length();i+=2)
{
String c = input4.charAt(i)+"";
if(c.equals("R"))
{
if(d.equals("N"))
d="E";
else if(d.equals("E"))
d="S";
else if(d.equals("S"))
d="W";
else if(d.equals("W"))
d="N";
}
else if(c.equals("L"))
{
if(d.equals("N"))
d="W";
else if(d.equals("E"))
d="N";
else if(d.equals("S"))
d="E";
else if(d.equals("W"))
d="S";
}
else
{
int m= c.equals(“m”)?1:2;
if(d.equals("N") && y+m<=input2)
y+=m;
else if(d.equals("E") && x+m<=input1)
x+=m;
else if(d.equals("S") && y-m>=0)
y-=m;
else if(d.equals("W") && x-m>=0)
x-=m;
else
return x+"-"+y+"-"+d+"-ER";
}
}
return x+"-"+y+"-"+d;
45) ROBO Movement (45 degrees, 1 step):
String[] robo = input3.split("-");
int x = Integer.parseInt(robo[0]);
int y = Integer.parseInt(robo[1]);
String d = robo[2];
for(int i=0;i<input4.length();i+=2)
{
String c = input4.charAt(i)+"";
if(c.equals("r"))
{
if(d.equals("N"))
d="NE";
else if(d.equals("E"))
d="SE";
else if(d.equals("S"))
d="SW";
else if(d.equals("W"))
d="NW";
else if(d.equals("NE"))
d="E";
else if(d.equals("SE"))
d="S";
else if(d.equals("SW"))
d="W";
else if(d.equals("NW"))
d="N";
}
else if(c.equals("l"))
{
if(d.equals("N"))
d="NW";
else if(d.equals("E"))
d="NE";
else if(d.equals("S"))
d="SE";
else if(d.equals("W"))
d="SW";
else if(d.equals("NE"))
d="N";
else if(d.equals("SE"))
d="E";
else if(d.equals("SW"))
d="S";
else if(d.equals("NW"))
d="W";
}
else
{
if(d.equals("N") && y+1<=input2)
y+=1;
else if(d.equals("E") && x+1<=input1)
x+=1;
else if(d.equals("S") && y-1>=0)
y-=1;
else if(d.equals("W") && x-1>=0)
x-=1;
else if(d.equals("NE") && x+1<=input1 && y+1<=input2)
{
x+=1;
y+=1;
}
else if(d.equals("SE") && x+1<=input1 && y-1>=0)
{
x+=1;
y-=1;
}
else if(d.equals("SW") && x-1>=0 && y-1>=0)
{
x-=1;
y-=1;
}
else if(d.equals("NW") && x-1>=0 && y+1<=input2)
{
x-=1;
y+=1;
}
else
return x+"-"+y+"-"+d+"-ER";
}
}
return x+"-"+y+"-"+d;
46) ROBO Movement (45 degrees, 2 steps):
String[] robo = input3.split("-");
int x = Integer.parseInt(robo[0]);
int y = Integer.parseInt(robo[1]);
String d = robo[2];
for(int i=0;i<input4.length();i+=2)
{
String c = input4.charAt(i)+"";
else if(c.equals("r"))
{
if(d.equals("N"))
d="NE";
else if(d.equals("E"))
d="SE";
else if(d.equals("S"))
d="SW";
else if(d.equals("W"))
d="NW";
else if(d.equals("NE"))
d="E";
else if(d.equals("SE"))
d="S";
else if(d.equals("SW"))
d="W";
else if(d.equals("NW"))
d="N";
}
else if(c.equals("l"))
{
if(d.equals("N"))
d="NW";
else if(d.equals("E"))
d="NE";
else if(d.equals("S"))
d="SE";
else if(d.equals("W"))
d="SW";
else if(d.equals("NE"))
d="N";
else if(d.equals("SE"))
d="E";
else if(d.equals("SW"))
d="S";
else if(d.equals("NW"))
d="W";
}
else
{
if(d.equals("N") && y+2<=input2)
y+=2;
else if(d.equals("E") && x+2<=input1)
x+=2;
else if(d.equals("S") && y-2>=0)
y-=2;
else if(d.equals("W") && x-2>=0)
x-=2;
else if(d.equals("NE") && x+2<=input1 && y+2<=input2)
{
x+=2;
y+=2;
}
else if(d.equals("SE") && x+2<=input1 && y-2>=0)
{
x+=2;
y-=2;
}
else if(d.equals("SW") && x-2>=0 && y-2>=0)
{
x-=2;
y-=2;
}
else if(d.equals("NW") && x-2>=0 && y+2<=input2)
{
x-=2;
y+=2;
}
else
return x+"-"+y+"-"+d+"-ER";
}
}
return x+"-"+y+"-"+d;
47) ROBO Movement (45 degrees, 1or 2 steps):
{
String[] robo = input3.split("-");
int x = Integer.parseInt(robo[0]);
int y = Integer.parseInt(robo[1]);
String d = robo[2];
for(int i=0;i<input4.length();i+=2)
{
String c = input4.charAt(i)+"";
else if(c.equals("r"))
{
if(d.equals("N"))
d="NE";
else if(d.equals("E"))
d="SE";
else if(d.equals("S"))
d="SW";
else if(d.equals("W"))
d="NW";
else if(d.equals("NE"))
d="E";
else if(d.equals("SE"))
d="S";
else if(d.equals("SW"))
d="W";
else if(d.equals("NW"))
d="N";
}
else if(c.equals("l"))
{
if(d.equals("N"))
d="NW";
else if(d.equals("E"))
d="NE";
else if(d.equals("S"))
d="SE";
else if(d.equals("W"))
d="SW";
else if(d.equals("NE"))
d="N";
else if(d.equals("SE"))
d="E";
else if(d.equals("SW"))
d="S";
else if(d.equals("NW"))
d="W";
}
else
{
int m = c.equals(“m”)?1:2;
if(d.equals("N") && y+m<=input2)
y=y+m;
else if(d.equals("E") && x+m<=input1)
x+=m;
else if(d.equals("S") && y-m>=0)
y-=m;
else if(d.equals("W") && x-m>=0)
x-=m;
else if(d.equals("NE") && x+m<=input1 && y+m<=input2)
{
x+=m;
y+=m;
}
else if(d.equals("SE") && x+m<=input1 && y-m>=0)
{
x+=m;
y-=m;
}
else if(d.equals("SW") && x-m>=0 && y-m>=0)
{
x-=m;
y-=m;
}
else if(d.equals("NW") && x-m>=0 && y+m<=input2)
{
x-=m;
y+=m;
}
else
return x+"-"+y+"-"+d+"-ER";
}
}
return x+"-"+y+"-"+d;
48) ROBO Movement (90 or 45 degrees, 1 step):
String[] robo = input3.split("-");
int x = Integer.parseInt(robo[0]);
int y = Integer.parseInt(robo[1]);
String d = robo[2];
for(int i=0;i<input4.length();i+=2)
{
String c = input4.charAt(i)+"";
if(c.equals("R"))
{
if(d.equals("N"))
d="E";
else if(d.equals("E"))
d="S";
else if(d.equals("S"))
d="W";
else if(d.equals("W"))
d="N";
else if(d.equals("NE"))
d="SE";
else if(d.equals("SE"))
d="SW";
else if(d.equals("SW"))
d="NW";
else if(d.equals("NW"))
d="NE";
}
else if(c.equals("r"))
{
if(d.equals("N"))
d="NE";
else if(d.equals("E"))
d="SE";
else if(d.equals("S"))
d="SW";
else if(d.equals("W"))
d="NW";
else if(d.equals("NE"))
d="E";
else if(d.equals("SE"))
d="S";
else if(d.equals("SW"))
d="W";
else if(d.equals("NW"))
d="N";
}
else if(c.equals("L"))
{
if(d.equals("N"))
d="W";
else if(d.equals("E"))
d="N";
else if(d.equals("S"))
d="E";
else if(d.equals("W"))
d="S";
else if(d.equals("NE"))
d="NW";
else if(d.equals("SE"))
d="NE";
else if(d.equals("SW"))
d="SE";
else if(d.equals("NW"))
d="SW";
}
else if(c.equals("l"))
{
if(d.equals("N"))
d="NW";
else if(d.equals("E"))
d="NE";
else if(d.equals("S"))
d="SE";
else if(d.equals("W"))
d="SW";
else if(d.equals("NE"))
d="N";
else if(d.equals("SE"))
d="E";
else if(d.equals("SW"))
d="S";
else if(d.equals("NW"))
d="W";
}
else
{
int m = 1;
if(d.equals("N") && y+m<=input2)
y=y+m;
else if(d.equals("E") && x+m<=input1)
x+=m;
else if(d.equals("S") && y-m>=0)
y-=m;
else if(d.equals("W") && x-m>=0)
x-=m;
else if(d.equals("NE") && x+m<=input1 && y+m<=input2)
{
x+=m;
y+=m;
}
else if(d.equals("SE") && x+m<=input1 && y-m>=0)
{
x+=m;
y-=m;
}
else if(d.equals("SW") && x-m>=0 && y-m>=0)
{
x-=m;
y-=m;
}
else if(d.equals("NW") && x-m>=0 && y+m<=input2)
{
x-=m;
y+=m;
}
else
return x+"-"+y+"-"+d+"-ER";
}
}
return x+"-"+y+"-"+d;
49) ROBO Movement (90 or 45 degrees, 2 step):
String[] robo = input3.split("-");
int x = Integer.parseInt(robo[0]);
int y = Integer.parseInt(robo[1]);
String d = robo[2];
for(int i=0;i<input4.length();i+=2)
{
String c = input4.charAt(i)+"";
if(c.equals("R"))
{
if(d.equals("N"))
d="E";
else if(d.equals("E"))
d="S";
else if(d.equals("S"))
d="W";
else if(d.equals("W"))
d="N";
else if(d.equals("NE"))
d="SE";
else if(d.equals("SE"))
d="SW";
else if(d.equals("SW"))
d="NW";
else if(d.equals("NW"))
d="NE";
}
else if(c.equals("r"))
{
if(d.equals("N"))
d="NE";
else if(d.equals("E"))
d="SE";
else if(d.equals("S"))
d="SW";
else if(d.equals("W"))
d="NW";
else if(d.equals("NE"))
d="E";
else if(d.equals("SE"))
d="S";
else if(d.equals("SW"))
d="W";
else if(d.equals("NW"))
d="N";
}
else if(c.equals("L"))
{
if(d.equals("N"))
d="W";
else if(d.equals("E"))
d="N";
else if(d.equals("S"))
d="E";
else if(d.equals("W"))
d="S";
else if(d.equals("NE"))
d="NW";
else if(d.equals("SE"))
d="NE";
else if(d.equals("SW"))
d="SE";
else if(d.equals("NW"))
d="SW";
}
else if(c.equals("l"))
{
if(d.equals("N"))
d="NW";
else if(d.equals("E"))
d="NE";
else if(d.equals("S"))
d="SE";
else if(d.equals("W"))
d="SW";
else if(d.equals("NE"))
d="N";
else if(d.equals("SE"))
d="E";
else if(d.equals("SW"))
d="S";
else if(d.equals("NW"))
d="W";
}
else
{
int m = 2; // 2 steps
if(d.equals("N") && y+m<=input2)
y=y+m;
else if(d.equals("E") && x+m<=input1)
x+=m;
else if(d.equals("S") && y-m>=0)
y-=m;
else if(d.equals("W") && x-m>=0)
x-=m;
else if(d.equals("NE") && x+m<=input1 && y+m<=input2)
{
x+=m;
y+=m;
}
else if(d.equals("SE") && x+m<=input1 && y-m>=0)
{
x+=m;
y-=m;
}
else if(d.equals("SW") && x-m>=0 && y-m>=0)
{
x-=m;
y-=m;
}
else if(d.equals("NW") && x-m>=0 && y+m<=input2)
{
x-=m;
y+=m;
}
else
return x+"-"+y+"-"+d+"-ER";
}
}
return x+"-"+y+"-"+d;
50) ROBO Movement (90 or 45 degrees, 1 or 2 steps):
String[] robo = input3.split("-");
int x = Integer.parseInt(robo[0]);
int y = Integer.parseInt(robo[1]);
String d = robo[2];
for(int i=0;i<input4.length();i+=2)
{
String c = input4.charAt(i)+"";
if(c.equals("R"))
{
if(d.equals("N"))
d="E";
else if(d.equals("E"))
d="S";
else if(d.equals("S"))
d="W";
else if(d.equals("W"))
d="N";
else if(d.equals("NE"))
d="SE";
else if(d.equals("SE"))
d="SW";
else if(d.equals("SW"))
d="NW";
else if(d.equals("NW"))
d="NE";
}
else if(c.equals("r"))
{
if(d.equals("N"))
d="NE";
else if(d.equals("E"))
d="SE";
else if(d.equals("S"))
d="SW";
else if(d.equals("W"))
d="NW";
else if(d.equals("NE"))
d="E";
else if(d.equals("SE"))
d="S";
else if(d.equals("SW"))
d="W";
else if(d.equals("NW"))
d="N";
}
else if(c.equals("L"))
{
if(d.equals("N"))
d="W";
else if(d.equals("E"))
d="N";
else if(d.equals("S"))
d="E";
else if(d.equals("W"))
d="S";
else if(d.equals("NE"))
d="NW";
else if(d.equals("SE"))
d="NE";
else if(d.equals("SW"))
d="SE";
else if(d.equals("NW"))
d="SW";
}
else if(c.equals("l"))
{
if(d.equals("N"))
d="NW";
else if(d.equals("E"))
d="NE";
else if(d.equals("S"))
d="SE";
else if(d.equals("W"))
d="SW";
else if(d.equals("NE"))
d="N";
else if(d.equals("SE"))
d="E";
else if(d.equals("SW"))
d="S";
else if(d.equals("NW"))
d="W";
}
else
{
int m = c.equals("m")?1:2;
if(d.equals("N") && y+m<=input2)
y=y+m;
else if(d.equals("E") && x+m<=input1)
x+=m;
else if(d.equals("S") && y-m>=0)
y-=m;
else if(d.equals("W") && x-m>=0)
x-=m;
else if(d.equals("NE") && x+m<=input1 && y+m<=input2)
{
x+=m;
y+=m;
}
else if(d.equals("SE") && x+m<=input1 && y-m>=0)
{
x+=m;
y-=m;
}
else if(d.equals("SW") && x-m>=0 && y-m>=0)
{
x-=m;
y-=m;
}
else if(d.equals("NW") && x-m>=0 && y+m<=input2)
{
x-=m;
y+=m;
}
else
return x+"-"+y+"-"+d+"-ER";
}
}
return x+"-"+y+"-"+d;
51) ONE DIGIT REDUCED SUBTRACTED FORM
while(input1>=10)
{
int x=input1,l=0;
while(input1>0)
{
input1=input1/10;
l++;
}
int a[]=new int[l];
int i=l-1;
while(x>0)
{
a[i]=x%10;
x=x/10;
i--;
}
for(int j=0;j<l-1;j++)
{
input1=input1*10+Math.abs(a[j]-a[j+1]);
}
}
return input1;
52) PROCESS TWO WORDS
Input1: Today is a nice day
Input2: 41
Output: ince doTday
Code:
String i1=input1;
int i2=input2;
int second=i2%10;
i2=i2/10;
int first=i2;
String arr[]=i1.split(" ");
String first_w=arr[first-1];
String second_w=arr[second-1];
String first_w1=first_w.substring(first_w.length()/2,first_w.length());
String first_w2;
if(first_w.length()%2==0)
{
first_w2=first_w.substring(0,first_w.length()/2);
}
else
{
first_w2=first_w.substring(0,(first_w.length()/2)+1);
}
StringBuilder i1_w=new StringBuilder();
i1_w.append(first_w2);
i1_w=i1_w.reverse();
String sw1=second_w.substring(second_w.length()/2,second_w.length());
String sw2;
if(second_w.length()%2==0)
{
sw2=second_w.substring(0,second_w.length()/2);
}
else
{
sw2=second_w.substring(0,(second_w.length()/2)+1);
}
StringBuilder iw2=new StringBuilder();
iw2.append(sw2);
iw2=iw2.reverse();
String ans=i1_w+first_w1+" "+iw2+sw1;
return ans;
53) Find Password – Stable Unstable (MODEL-1)
Detective Buckshee junior has been approached by the shantiniketan kids’ society for help
in finding the password to the games complex. After hearing the scenario, detective
Buckshee junior realizes that he will need a programmer’s support. He contacts you and
requests your help. Please help the detective by writing a function to generate the password.
The scenario is as below:
Five numbers are available with the kids.
These numbers are either stable or unstable.
A number is STABLE if each of its digit occur the same number of times. i.e.,
thefrequency of each digit in the number is the same. For e.g. 2277, 4004, 11, 23,583835,
1010 are examples of stable numbers.
Similarly, A number is UNSTABLE if the frequency of each digit in the number isNOT
the same, for e.g., 221, 4314, 101, 233, 58135, 101 are examples ofunstable numbers.
The password can be found as below:
i.e. Password=sum of all stable numbers.
Assuming that the five numbers are passed to a function as input1, input2, input3, input4,
and input5, complete the function to find and return the password.
For Example:
If input1=12, input2=1313, input3=122, input4=678, andinput5=898,
stable numbers are 12, 1313 and 678
unstable numbers are 122 and 898
So, the password should be=sum of all stable numbers=2003
Code:
int[] num = {input1, input2, input3, input4, input5};
int stable=0, i, j;
for(i=0;i<5;i++)
{
int[] freq = new int[10]; // frequencies of all the digits
int temp=num[i];
int maxf=0;
while(temp!=0)
{
int r = temp%10;
freq[r]++;
temp/=10;
if(freq[r]>maxf)
maxf=freq[r];
}
for(j=0;j<10;j++)
{
if(freq[j]!=0 &&freq[j]!=maxf)
break;
}
if(j==10)
stable+=num[i];
}
return stable;
54) Find Password – Stable Unstable (MODEL-2)
Detective Buckshee junior has been approached by the shantiniketan kids’ society for help
in finding the password to the games complex. After hearing the scenario, detective
Buckshee junior realizes that he will need a programmer’s support. He contacts you and
requests your help. Please help the detective by writing a function to generate the password.
The scenario is as below:
Five numbers are available with the kids.
These numbers are either stable or unstable.
A number is STABLE if each of its digit occur the same number of times. i.e.,
thefrequency of each digit in the number is the same. For e.g. 2277, 4004, 11, 23,583835,
1010 are examples of stable numbers.
Similarly, A number is UNSTABLE if the frequency of each digit in the number isNOT
the same, for e.g., 221, 4314, 101, 233, 58135, 101 are examples ofunstable numbers.
The password can be found as below:
i.e. Password= (number of stable numbers*10) - number of
unstable numbers.
Assuming that the five numbers are passed to a function as input1, input2, input3, input4,
and input5, complete the function to find and return the password.
For Example:
If input1=12, input2=1313, input3=122, input4=678, andinput5=898,
stable numbers are 12, 1313 and 678
unstable numbers are 122 and 898
So, the password should be = (number of stable numbers*10) - number of unstable
numbers = (3*10) - 2 = 28
Code:
int[] num = {input1, input2, input3, input4, input5};
int stable=0, unstable=0, i, j;
for(i=0;i<5;i++)
{
int[] freq = new int[10]; // frequencies of all the digits
int temp=num[i];
int maxf=0;
while(temp!=0)
{
int r = temp%10;
freq[r]++;
temp/=10;
if(freq[r]>maxf)
maxf=freq[r];
}
for(j=0;j<10;j++)
{
if(freq[j]!=0 &&freq[j]!=maxf)
break;
}
if(j==10)
stable++;
else
unstable++;
}
return (stable*10 - unstable);
55) Find Password – Stable Unstable (MODEL-3)
Detective Buckshee junior has been approached by the shantiniketan kids’ society for help
in finding the password to the games complex. After hearing the scenario, detective
Buckshee junior realizes that he will need a programmer’s support. He contacts you and
requests your help. Please help the detective by writing a function to generate the password.
The scenario is as below:
Five numbers are available with the kids.
These numbers are either stable or unstable.
A number is STABLE if each of its digit occur the same number of times. i.e.,
thefrequency of each digit in the number is the same. For e.g. 2277, 4004, 11, 23,583835,
1010 are examples of stable numbers.
Similarly, A number is UNSTABLE if the frequency of each digit in the number isNOT
the same, for e.g., 221, 4314, 101, 233, 58135, 101 are examples ofunstable numbers.
The password can be found as below:
i.e. Password= (number of unstable numbers*10) - number of
stable numbers.
Assuming that the five numbers are passed to a function as input1, input2, input3, input4,
and input5, complete the function to find and return the password.
For Example:
If input1=12, input2=1313, input3=122, input4=678, andinput5=898,
stable numbers are 12, 1313 and 678
unstable numbers are 122 and 898
So, the password should be = (number of unstable numbers*10) - number of stable
numbers = (2*10) - 3 = 17
Code:
int[] num = {input1, input2, input3, input4, input5};
int stable=0, unstable=0, i, j;
for(i=0;i<5;i++)
{
int[] freq = new int[10]; // frequencies of all the digits
int temp=num[i];
int maxf=0;
while(temp!=0)
{
int r = temp%10;
freq[r]++;
temp/=10;
if(freq[r]>maxf)
maxf=freq[r];
}
for(j=0;j<10;j++)
{
if(freq[j]!=0 &&freq[j]!=maxf)
break;
}
if(j==10)
stable++;
else
unstable++;
}
return (unstable*10 - stable);
56) Find Password – Stable Unstable (MODEL-4)
Detective Buckshee junior has been approached by the shantiniketan kids’ society for help
in finding the password to the games complex. After hearing the scenario, detective
Buckshee junior realizes that he will need a programmer’s support. He contacts you and
requests your help. Please help the detective by writing a function to generate the password.
The scenario is as below:
Five numbers are available with the kids.
These numbers are either stable or unstable.
A number is STABLE if each of its digit occur the same number of times. i.e.,
thefrequency of each digit in the number is the same. For e.g. 2277, 4004, 11, 23,583835,
1010 are examples of stable numbers.
Similarly, A number is UNSTABLE if the frequency of each digit in the number isNOT
the same, for e.g., 221, 4314, 101, 233, 58135, 101 are examples ofunstable numbers.
The password can be found as below:
i.e. Password= Maximum of all unstable numbers + Minimum of
all unstable numbers.
Assuming that the five numbers are passed to a function as input1, input2, input3, input4,
and input5, complete the function to find and return the password.
For Example:
If input1=12, input2=1313, input3=122, input4=678, andinput5=898,
stable numbers are 12, 1313 and 678
unstable numbers are 122 and 898
So, the password should be = Maximum of all unstable numbers + Minimum of all
unstable numbers = 898+ 122 = 1020
Code:
int[] num = {input1, input2, input3, input4, input5};
int max_unstable=Integer.MIN_VALUE, min_unstable=Integer.MAX_VALUE, i, j;
for(i=0;i<5;i++)
{
int[] freq = new int[10];
int temp=num[i];
int maxf=0;
while(temp!=0)
{
int r = temp%10;
freq[r]++;
temp/=10;
if(freq[r]>maxf)
maxf=freq[r];
}
for(j=0;j<10;j++)
{
if(freq[j]!=0 && freq[j]!=maxf)
break;
}
if(j!=10)
{
if(num[i]>max_unstable)
max_unstable=num[i];
if(num[i]<min_unstable)
min_unstable=num[i];
}
}
if(max_unstable==Integer.MIN_VALUE)
max_unstable=0;
if(min_unstable==Integer.MAX_VALUE)
min_unstable=0;
return (max_unstable+min_unstable);
57) Find Password – Stable Unstable (MODEL-5)
Detective Buckshee junior has been approached by the shantiniketan kids’ society for help
in finding the password to the games complex. After hearing the scenario, detective
Buckshee junior realizes that he will need a programmer’s support. He contacts you and
requests your help. Please help the detective by writing a function to generate the password.
The scenario is as below:
Five numbers are available with the kids.
These numbers are either stable or unstable.
A number is STABLE if each of its digit occur the same number of times. i.e.,
thefrequency of each digit in the number is the same. For e.g. 2277, 4004, 11, 23,583835,
1010 are examples of stable numbers.
Similarly, A number is UNSTABLE if the frequency of each digit in the number isNOT
the same, for e.g., 221, 4314, 101, 233, 58135, 101 are examples ofunstable numbers.
The password can be found as below:
i.e. Password= Maximum of all unstable numbers + Minimum of
all stable numbers.
Assuming that the five numbers are passed to a function as input1, input2, input3, input4,
and input5, complete the function to find and return the password.
For Example:
If input1=12, input2=1313, input3=122, input4=678, andinput5=898,
stable numbers are 12, 1313 and 678
unstable numbers are 122 and 898
So, the password should be = Maximum of all unstable numbers + Minimum of
allstable numbers = 898 + 12 = 910
Code:
int[] num = {input1, input2, input3, input4, input5};
int max_unstable=Integer.MIN_VALUE, min_stable=Integer.MAX_VALUE, i, j;
for(i=0;i<5;i++)
{
int[] freq = new int[10]; // frequencies of all the digits
int temp=num[i];
int maxf=0;
while(temp!=0)
{
int r = temp%10;
freq[r]++;
temp/=10;
if(freq[r]>maxf)
maxf=freq[r];
}
for(j=0;j<10;j++)
{
if(freq[j]!=0 && freq[j]!=maxf)
break;
}
if(j==10&&num[i]<min_stable)
min_stable=num[i];
else if(j!=10 && num[i]>max_unstable)
max_unstable=num[i];
}
if(max_unstable==Integer.MIN_VALUE)
max_unstable=0;
if(min_stable==Integer.MAX_VALUE)
min_stable=0;
return (max_unstable + min_stable);
58) Find Password – Stable Unstable (MODEL-6)
Detective Buckshee junior has been approached by the shantiniketan kids’ society for help
in finding the password to the games complex. After hearing the scenario, detective
Buckshee junior realizes that he will need a programmer’s support. He contacts you and
requests your help. Please help the detective by writing a function to generate the password.
The scenario is as below:
Five numbers are available with the kids.
These numbers are either stable or unstable.
A number is STABLE if each of its digit occur the same number of times. i.e.,
thefrequency of each digit in the number is the same. For e.g. 2277, 4004, 11, 23,583835,
1010 are examples of stable numbers.
Similarly, A number is UNSTABLE if the frequency of each digit in the number isNOT
the same, for e.g., 221, 4314, 101, 233, 58135, 101 are examples ofunstable numbers.
The password can be found as below:
i.e. Password= Maximum of all unstable numbers – Minimum of
all stable numbers.
Assuming that the five numbers are passed to a function as input1, input2, input3, input4,
and input5, complete the function to find and return the password.
For Example:
If input1=12, input2=1313, input3=122, input4=678, andinput5=898,
stable numbers are 12, 1313 and 678
unstable numbers are 122 and 898
So, the password should be = Maximum of all unstable numbers – Minimum of all
stable numbers = 898 –12 = 886
Code:
int[] num = {input1, input2, input3, input4, input5};
int max_unstable=Integer.MIN_VALUE, min_stable=Integer.MAX_VALUE, i, j;
for(i=0;i<5;i++)
{
int[] freq = new int[10];
int temp=num[i];
int maxf=0;
while(temp!=0)
{
int r = temp%10;
freq[r]++;
temp/=10;
if(freq[r]>maxf)
maxf=freq[r];
}
for(j=0;j<10;j++)
{
if(freq[j]!=0 &&freq[j]!=maxf)
break;
}
if(j==10&&num[i]<min_stable)
min_stable=num[i];
else if(j!=10 && num[i]>max_unstable)
max_unstable=num[i];
}
if(max_unstable==Integer.MIN_VALUE)
max_unstable=0;
if(min_stable==Integer.MAX_VALUE)
min_stable=0;
return (max_unstable - min_stable);
59) Nambiar Number
The "Nambiar Number" Generator: M. Nambiar has devised a mechanism to process any
given mobile number and thus generate a new resultant number. He calls this mechanism as
the "Nambiar Number Generator and the resultant number is referred to as the "Nambiar
Number. The mechanism is as follows In the given mobile number, starting with the first digit,
keep on adding all subsequent digits till the state (even or odd) of the sum of the digits is
opposite to the state (odd or even) of the first digit Continue this from the subsequent digit till
th Last digit of the mobile number is reached Concatenating the sums thus generated results
in the Nambiar Number.
The below examples will help to illustrate this
Please also look at the bottom of this problem description for the expected function
prototype.
Example 1
If the given mobile number is 9880127431
The first pass should start with the first digit
First digit is 9 which is odd So we will keep adding subsequent digits till the sum becomes
even
9+2=17 (17 is odd so continue adding the digits) 9+8+8
25 (25 is odd so continue adding the digits) 9+8+8+0 = 25 (25 is odd, so continue adding the
digits) 9+8+8+0+1 = 26 (26 is even which is opposite to the state of the first digit 9)
So Stop first pass here and remember that the result at the end of first pass =26
Now we enter the second pass
The second pass should start after the digit where we stopped the first pass, In the first pass
we have added the digits 9,8,8,0 and 1
So, the first digit for second pass will be 2, which is even
Now, we will keep adding subsequent digits till the sum becomes odd. 2+7=9 (9 is odd, which
is opposite to the state of the first digit 2)
So, Stop second pass here and remember that the result at the end of second pass =9
Now we enter the third pass.
In the first pass we have added the digits 9,8,8,0 and 1, and the resultant sum was 26 In the
second pass we have added the digits 2 and 7 and the resultant sum was 9
The third pass should start after the digit where we stopped the second pass So, the first digit
for the pass will be 4 which is even
Now, we will keep adding subsequent digits till the sum becomes odd
4+3=7 (7 is odd, which is opposite to the state of the first digit 4) So Stop third pass here and
remember that the result at the end of third pass =7
Now we enter the fourth pass
In the first pass we have added the digits 9,8 8,0 and 1, and the resultant sum was 26 In the
second pass we have added the digits 2 and 7 and the resultant sum was 9
In the third pass we have added the digits 4 and 3, and the resultant sum was 7 The fourth
pass should start after the digit where we stopped the third pass
So the first digit for fourth pass will be 1 which is odd Now, we will keep adding subsequent
digits till the sum becomes even
However, we realize that this digit 1 is the last digit of the mobile number and there are no
further digits So Stop fourth pass here and remember that the result at the end of fourth
pass =1
For the mobile number 9880127431 the resultant number (Nambiar Number) will be the
concatenation of the results of the 4 passes = (26971) = 26971
Note: Please note that the number of passes required to process the given number may vary
depending upon the constitution of the mobile number Note: Also note that. O should be
considered as an even number
Example 2
If the given mobile number is 9860857152
First digit 9 is odd
First pass results in 9+8+6+0+8+5=36
Second pass results in 7+1=8 Third pass results in 5+2=7
Note that the third pass stops at 7 (even though we do not meet a change of state) because
we have reached the end of the mobile number.
For the mobile number 9860857152, the resultant number (Nambiar Number) will be the
concatenation of the results of the 3 passes = [36][8117] = 3687
Example 3
If the given mobile number is 8123454210
The resultant number (Nambiar Number) will be 95970
Example 4
If the given mobile number is 9900114279
The resultant number (Nambiar Number) will be 181149
Code:
public int nnGenerator(String input1)
{
String mobileNo = input1;
StringBuilder numbiarNo = new StringBuilder();
for (int i = 0; i < mobileNo.length(); i++)
{
int firstDigit = Integer.parseInt(String.valueOf(mobileNo.charAt(i)));
int firstDigitEvenOrOdd = firstDigit % 2 == 0 ? 0 : 1;
int sum = firstDigit;
int j = i + 1;
if (j == mobileNo.length())
{
numbiarNo.append(firstDigit);
break;
}
while (true)
{
sum += Integer.parseInt(String.valueOf(mobileNo.charAt(j++)));
if (sum % 2 != firstDigitEvenOrOdd || j >= mobileNo.length())
{
numbiarNo.append(sum);
i = j - 1;
break;
}
}
}
return Integer.parseInt(numbiarNo.toString());
}
-----------------------------------------------------------------------------------------------------
import java.util.Arrays;
public class Main
{
public int nnGenerator(String input1){
String s=input1;
int len=s.length();
int a[]=new int[len];
for(int i=0 ;i <len ;i++)
{ a[i]=(s.charAt(i)-'0');
}
System.out.println(Arrays.toString(a));
int i=0;
String temp="";
int k=a[i];
int evenflag,oddflag;
if(k%2==0)
{
evenflag=1;
oddflag=0;
}
else
{
evenflag=0;
oddflag=1;
}
while(i<len)
{
if(i==len-1)
{
System.out.print(k);
temp+=k;
break;
}
if((k%2!=0)&&(oddflag==1))
{
k+=a[i+1];
i++;
}
else if((k%2==0)&&(evenflag==1))
{
k+=a[i+1];
i++;
}
else
{
System.out.print(k+" ");
temp+=k;
i=i+1;
k=a[i];
if(k%2==0)
{
evenflag=1;
oddflag=0;
}
else
{
evenflag=0;
oddflag=1;
}
return Integer.parseInt(temp);
}
}
}
}
60) Generate series and find nth element:
//1,3,6,8,11,13,16,18,21,23,,26,28,31,33,36,38,41
int gap1 = (input2 - input1);
int gap2 = (input3 - input2);
int output = input1;
for (int i = 1; i < input4; i++) {
if (i % 2 == 1)
output += gap1;
else
output += gap2;
}
return output;
61)EATERY JOINT
Example 1
input1= {{0,2},{2,3},{2,2},{5,2}}
input2=4
output :c0-c1-c3:c2
Example2
input1={{0,1},{2,1},{2,4},{5,3},{6,2},{7,5},{9,4}};
input2=7;
output : c0-c1-c2-c4-c6:c3-c5
Example3
input1={{0,2},{3,3},{5,3}};
int input2=3;
output : "Served";
CODE:
int time=input1[0][1];
String se="c0-";
int count=1;
String le="";
for(int i=1;i<input2;i++)
{
if(time<=input1[i][0])
{
time=time+input1[i][1];
se=se+"c"+i+"-";
count++;
}
else
{
le=le+"c"+i+"-";
}
}
String ans="";
if(count==input2)
{
ans="served";
}
else
{
se=se.substring(0,se.length()-1);
if(le.length()>0)
le=le.substring(0,le.length()-1);
ans=se+":"+le;
}
return ans;
---------------------------------------------------------------------------------------------------------
public class Main
{
public static void main(String[] args) {
int [][] input1={{0,2},{3,3},{5,3}};
int input2=3;
System.out.println(eateryJoint(input1,input2));
}
static String eateryJoint(int [][] input1,int input2){
String a="",b="";
int t=0,c=0;
for(int i=0;i<input2;i++){
if(input1[i][0] >=t && input1[i][0]<30){
a=a+"c"+Integer.toString(i)+"-";
t=t+input1[i][1];
c=c+1;
}
else{
b=b+"c"+Integer.toString(i)+"-";
}
}
if(c==input2){
return "Served";
}
else{
a=a.substring(0,a.length()-1);
b=b.substring(0,b.length()-1);
return a+":"+b;
}
}
}
62) Find Prime Vowels:
CODE:
public class Main{
static String word[] = {"zero", "one", "two", "three","four", "five", "six", "seven",
"eight","nine"};
static String printWordsWithoutIfSwitch(int n)
{
int digits[] = new int[10];
String s="",w="";
int dc = 0;
do
{
digits[dc] = n % 10;
n = n/10;
dc++;
} while (n != 0);
for (int i = dc - 1; i >= 0; i--)
{
s+=word[digits[i]] + " ";
}
for(int i = 0; i < s.length(); i++){
if(s.charAt(i)== 'a' || s.charAt(i)== 'e' || s.charAt(i)== 'i' || s.charAt(i)== 'o' || s.charAt(i)==
'u'){
if(Character.isLetter(s.charAt(i))){
w += s.charAt(i);
}
}
}
w=w+w.length();
return w;
}
public static int NthPrime(int input1){
int num=1,count=0,i;
while(count<input1)
{
num=num+1;
for(i=2;i<=num;i++)
{
if(num%i==0)
break;
}
if(i==num)
count++;
}
return num;
}
}
63) JOBS:
CODE:
int i;
String res = "";
int[] trigger = new int[input1+1];
for(i=0;i<input2.length;i++)
{
trigger[input2[i][0]] = input2[i][1];
}
i=input1;
while(i!=0)
{
res = "-" + i + res;
i = trigger[i];
}
return res.substring(1);
-----------------------------------------------------------------------------------------------------------
int search=input1;
int l=input2.length;
String res="";
res=res+search+"-";
boolean b=true;
while(b)
{
for(int i=0;i<l;i++)
{
if(input2[i][0]==search)
{
res=res+input2[i][1]+"-";
search=input2[i][1];
if(search==1)
{
res=res.substring(0,res.length()-1);
b=false;
}
}
}
}
StringBuffer sb=new StringBuffer(res);
String ans=new String(sb.reverse());
return ans;
64) SILVER NUMBER PROGRAM
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int input1=sc.nextInt();
int input2=sc.nextInt();
int input3=sc.nextInt();
int b=sumofsilver(input1,input2,input3);
System.out.println(b);
}
public static int sumofsilver(int input1,int input2,int input3){
char[]x=(String.valueOf(input1)+String.valueOf(input2)+String.valueOf(input3)).toCharArray();
int a=0,j;
for(j=0;j<x.length;j++)
if(x[j]=='7' || x[j]=='8' || x[j]=='9')
a++;
else if(x[j]=='1' || x[j]=='2' || x[j]=='3')
a=a+3;
else if(x[j]!='0')
a=a+2;
return a;
}
}
65) SUCCESIVE FACTOR PROGRAM
Example 1- input-12 output-T34
Example 2- input-380 output-T1920
Example 3- input-7 output-TIsPrime
Example 4- input-32 output-FNotPrime
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int input1=sc.nextInt();
String b=findsuccesivefactor(input1);
System.out.println(b);
}
public static String findsuccesivefactor(int input1){
int x=(int)(Math.sqrt(input1));
int f=0;
if(x*(x+1)==input1){
return "T"+x+""+(x+1);
}
else
for(int i=2;i<input1/2;i++){
if(input1 % i==0){
f=1;
break;
}
}
if(f==0 && input1>1){
return "TIsPrime";
}
else{
return "FNotPrime";
}
}
}
66) Rock Paper & Scissors
Example-1
input1:2
input2:{0,1,1}
input3:{1,1,0}
output: 1
Example-2
input1:3
input2:{0,0,3}
input3:{3,0,0}
output:6
67)Find the number odd string mapping as explained below
Example 1-
input1 123
output 11ODD
Example 2-
input1 111
output 9ODD
Example 3-
input1 7890
output 18EVEN
public class Main
{
public static void main(String[] args) {
int input=111;
String str="";
Object obj=input;
String number=obj.toString();
int len=number.length();
for(int i=0;i<len;i++){
switch(number.substring(i,i+1)){
case "0":
str += "zero";
break;
case "1":
str += "one";
break;
case "2":
str += "two";
break;
case "3":
str += "three";
break;
case "4":
str += "four";
break;
case "5":
str += "five";
break;
case "6":
str += "six";
break;
case "7":
str += "seven";
break;
case "8":
str += "eight";
break;
case "9":
str += "nine";
break;
}
}
int len_of_string=str.length();
if(len_of_string % 2==0){
System.out.println(len_of_string +"EVEN");
}
else
System.out.println(len_of_string +"ODD");
}
}
---------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.next();
char []c=s.toCharArray();
int l=c.length;
String str="";
String full="";
for(int i=0;i<l;i++){
if(c[i]=='0')
str ="zero";
else if(c[i]=='1')
str ="one";
else if(c[i]=='2')
str ="two";
else if(c[i]=='3')
str ="three";
else if(c[i]=='4')
str ="four";
else if(c[i]=='5')
str ="five";
else if(c[i]=='6')
str ="six";
else if(c[i]=='7')
str ="seven";
else if(c[i]=='8')
str ="eight";
else if(c[i]=='9')
str ="nine";
full=full + str;
}
int l1=full.length();
if(l1 % 2==0){
System.out.println(l1 +"EVEN");
}
else
System.out.println(l1 +"ODD");
}
}
68)String Task
Input1 tour
output *t*r
code
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
String b = ModifyString(a);
System.out.println(b);
}
public static String ModifyString(String input1){
String s = input1.replaceAll("[aeiouyAEIOUY]","");
s = s.toLowerCase();
StringBuffer sol = new StringBuffer();
for(int i =0;i<s.length();i++){
sol.append("*");
sol.append(s.charAt(i));
}
String result = sol.toString();
return result;
}
69)Math Olympiad
String[] splitArray1 = input1.split(" ");
int[] array1 = new int[splitArray1.length];
for (int i = 0; i < splitArray1.length; i++) {
array1[i] = Integer.parseInt(splitArray1[i]);
}
int a=array1[0];
int b=array1[1];
String[] splitArray = input2.split(" ");
int[] array = new int[splitArray.length];
int count=0;
for (int i = 0; i < splitArray.length; i++) {
array[i] = Integer.parseInt(splitArray[i]);
}
for (int i = 0; i < array.length; i++) {
if (array[i]>=array[b-1])
count++;
}
return count;
70)Advance constraints
static int findAdvancedConstrains(String input1,String input2){
int len=Integer.parseInt(Character.toString(input1.charAt(0)));
int k=Integer.parseInt(Character.toString(input1.charAt(2)));
int count=0;
String []str=input2.split(" ");
int []str2=new int[str.length];
System.out.println(str2[k]);
for(int i=0;i<str.length;i++){
str2[i]=Integer.parseInt(Str[i]);
}
int kelement=str2[k];
for(int i=0;i<str2.length;i++){
if(str2[i]>=kelement){
count++;
}
}
71)ISEmirp
public static int countEmirp(int[] input1, int input2) {
int count = 0;
int res;
for(int i = 0; i < input1.length; i++) {
res = reverse(input1[i]);
if(res != input1[i]) {
if(isPrime(res) == true)
count++;
}
}
return count;
}
public static int reverse(int x)
{
int rev = 0;
while (x > 0)
{
rev = (rev * 10) + x % 10;
x = x / 10;
}
return rev;
public static boolean isPrime(int n)
{
// Corner case
if (n <= 1)
return false;
// Check from 2 to n-1
for (int i = 2; i < n; i++)
if (n % i == 0)
return false;
return true;
}
72)String Tally Theorem
Example 1-
Input1 Wipro
Input2 TECHNOLOGY
Output 1
Example 2-
Input1 Talent
Input2 TRANSFORM
Output 3
Example 3-
Input1 TEstCase
Input2 Test
Output 4
code
import java.util.Arrays;
public class Main
{
static String removeDuplicate(char str1[])
{
int index = 0;
for (int i = 0; i < str1.length; i++)
{
int j;
for (j = 0; j < i; j++)
{
if (str1[i] == str1[j])
{
break;
}
}
if (j == i)
{
str1[index++] = str1[i];
}
}
return String.valueOf(Arrays.copyOf(str1, index));
}
static void count(String str1, String str2)
{
int c = 0, j = 0;
for (int i = 0; i < str1.length(); i++)
{
if (str2. indexOf(str1.charAt(i)) >= 0)
{
c += 1;
}
}
System.out.println(c);
}
public static void main(String[] args) {
String str3 = "TEstCase";
String str4 = "Test";
String str5=str3.toUpperCase();
String str6=str4.toUpperCase();
char str1[]=str5.toCharArray();
char str2[]=str6.toCharArray();
String str7=String.valueOf(removeDuplicate(str1));
String str8=String.valueOf(removeDuplicate(str2));
removeDuplicate(str2);
count(str7, str8);
}
}
-----------------------------------------------------------------------------
static void count(String str1, String str2)
{
int c = 0, j = 0;
for (int i = 0; i < str1.length(); i++)
{
if (str2. indexOf(str1.charAt(i)) >= 0)
{
c += 1;
}
}
System.out.println("No. of matching characters are: " + c);
}
public static void main (String[] args)
{
String str3 = "TEstCase";
String str4 = "Test";
String str1=str3.toUpperCase();
String str2=str4.toUpperCase();
count(str1, str2);
}
}
73)Identify Possible Words:
https://tests.mettl.com/authenticateKey/13486c16
public class IdentifyPossibleWords {
static String PossibleWords(String input1,String input2)
{
int i,j;
input1 = input1.toUpperCase();
input2 = input2.toUpperCase();
String word="", res="";
String[] words = input2.split(":");
for(i=0;i<words.length;i++)
{
word = words[i];
if(input1.length()==word.length())
{
for(j=0;j<input1.length();j++)
{
if(input1.charAt(j)!='_' && input1.charAt(j)!=word.charAt(j))
{
break;
}
}
if(j==input1.length())
{
res=res + word+":";
}
}
}
return res.length()==0?"ERROR-009":res.substring(0,res.length()-1);
}
public static void main(String[] args) {
String fin = PossibleWords("Fi_er","Fixer:Filer:fiber:fibre:tailor:offer");
System.out.println(fin);
}
}
74)2nd Word UpperCase:
https://tests.mettl.com/authenticateKey/4a72723f
public class Second_upper {
public static String Upper(String s)
{
String str = s;
String ch[] = str.split(" ");
//int len =str.length();
return ch[1].toUpperCase();
}
public static void main(String[] args) {
String res =Upper("Hello welcome guys");
System.out.println(res);
}
}
75)IsPalinStr
https://tests.mettl.com/authenticateKey/ffe8042
//Write a program to accept a String from the command prompt and display whether the string is a palindrome or not
.
class Pali2{
public static void main(String[] args) {
String s = "fad";
int len = s.length();
String rev = "";
for (int i = len-1; i>=0; i--) {
rev=rev+s.charAt(i);
}
System.out.println(rev);
if(s.equals(rev))
{
System.out.println("Palindrome");
}
else
{
System.out.println("Not a Palindrome");
}
}
}
76)WeightOfString:
https://tests.mettl.com/authenticateKey/387952fc
import java.io.*;
import java.util.*;
class WeightOfString
{
public static int Weight(String input1,int input2)
{
String str=input1.toUpperCase();
int sum=0;
for(int i=0;i<input1.length();i++)
{
if(input2==0)
{
if(str.charAt(i)=='A' || str.charAt(i)=='E' ||str.charAt(i)=='I' || str.charAt(i)=='O' || str.charAt(i)=='U' || !Character.i
sLetter(str.charAt(i)))
{
continue;
}
else
{
int a=str.charAt(i)-64;
sum+=a;
}
}
else
{
if(!Character.isLetter(str.charAt(i)))
continue;
else
{
int a=str.charAt(i)-64;
sum+=a;
}
}
}
return sum;
}
public static void main(String[] args) {
int res =Weight("Hello",0);
System.out.println(res);
}
}
77)MostFrequentDigit
https://tests.mettl.com/authenticateKey/916310b8
public class MostRepDigit
{
public static void main(String[] args) {
int a=222, b = 312, c = 300,d = 100; //233233
String st = String.valueOf(a)+String.valueOf(b)+String.valueOf(c)+String.valueOf(d);
System.out.println(st);
int len = st.length();
int[] freq =new int[10];//0 to 9
for(int i = 0;i<len;i++)
{
int digit = st.charAt(i) - '0';
freq[digit]++;
}
/*for(int i=0;i<10;i++)
{
System.out.println(freq[i]);
}*/
int max = freq[0];
for(int i=0;i<10;i++)
{
if(max<freq[i])
{
max = freq[i];
}
}
System.out.println("Number of repeatitons of a number: "+max);
int collect=0;
for(int i=0;i<10;i++)
{
if(freq[i]==max)
{
collect = i;
System.out.println("Most Frequent Value: "+collect);
}
}
}
-----------------------------------------------------------------------------------------------------
class Frequncy{
public static void main(String args[]){
int w=222,v=312,c=300,d=100;
int b[]={w,v,c,d};
int a[]=new int[50];
int y[]=new int[10];
int k=0;
for(int i=0;i<b.length;i++)
while(b[i]>0){
int x=b[i]%10;
a[k]=x;
k++;
b[i]=b[i]/10;
}
for(int i=0;i<k;i++){
for(int j=0;j<10;j++){
if(a[i]==j)
y[j]++;
}
}
int max=y[0];
for(int i=1;i<y.length;i++)
{
if(max<y[i])
max=y[i];
}
for(int i=0;i<y.length;i++)
{
if(max==y[i])
System.out.println(i);
}
}
}
78)UserIdGeneration:
https://tests.mettl.com/authenticateKey/592740f3
class PasswordSmallerLargerNumber {
public static String passw(String input1,String input2,int input3,int input4)
{
String s1 = input1;
String s2 =input2;
int m = input3;
int N= input4;
String longer = "",smaller="";
if(s1.length()>s2.length())
{
longer =s1;
smaller =s2;
}
else
{
smaller = s1;
longer = s2;
}
if(s1.length()==s2.length())
{
if(s1.compareTo(s2)>0)
{
longer = s1;
smaller =s2;
}
else
{
smaller =s1;
longer = s2;
}
}
String num = String.valueOf(m);
String result ="";
String r = smaller.charAt(smaller.length()-1)+longer+num.charAt(N-1)+num.charAt(num.length()-N);
for(int i=0;i<r.length();i++)
{
if(Character.isLowerCase(r.charAt(i)))
{
result = result+Character.toUpperCase(r.charAt(i));
}
else
{
result = result+Character.toLowerCase(r.charAt(i));
}
}
return result;
}
public static void main(String[] args) {
String res = passw("Hanu","Ram",5647,2);
System.out.println(res);
}
}
79)FindPasswordStableUnstable:
https://tests.mettl.com/authenticateKey/5106dfd
class PasswordStableUnstable {
public static int password(int input1,int input2,int input3,int input4, int input5)
{
int[] arr = {input1,input2,input3,input4,input5};
int stable=0,unstable=0;
for(int i =0;i<arr.length;i++)
{
int[] freq = new int[10];
int arr1 = arr[i];
int max=0;
while(arr1>0)
{
int e =arr1%10;
freq[e]++;
arr1=arr1/10;
if(freq[e]>max) // 0 0 2 0 0 0 0 2 //max=2
{
max = freq[e];
}
}
int l;
for(l=0;l<10;l++)
{
if(freq[l]!=0 && freq[l]!=max)
{
break;
}
}
if(l==10)
{
stable+=arr[i];
}
else
{
unstable+=arr[i];
}
}
return (stable - unstable);
}
public static void main(String[] args) {
int pass = password(455,257,3300,110,303);
System.out.println(pass);
}
}
80)WeightHillPattern:
https://tests.mettl.com/authenticateKey/d612c0e6
public class HillsPattern {
public static int totalHillWeight(int input1,int input2,int input3)
{
int sum=0;
for(int i = 0;i<input1;i++)
{
for(int j=0;j<=i;j++)
{
sum = sum+input2;
}
input2 = input2+input3;
}
return sum;
}
public static void main(String[] args) {
int t = totalHillWeight(4,1,5);
System.out.println(t);
}
}
81)QuadraticRoots:
import java.util.Scanner;
public class QuadraticEquationExample1
{
public static void main(String[] Strings)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the value of a: ");
double a = input.nextDouble();
System.out.print("Enter the value of b: ");
double b = input.nextDouble();
System.out.print("Enter the value of c: ");
double c = input.nextDouble();
double d= b * b - 4.0 * a * c;
if (d> 0.0)
{
double r1 = (-b + Math.pow(d, 0.5)) / (2.0 * a);
double r2 = (-b - Math.pow(d, 0.5)) / (2.0 * a);
System.out.println("The roots are " + r1 + " and " + r2);
}
else if (d == 0.0)
{
double r1 = -b / (2.0 * a);
System.out.println("The root is " + r1);
}
else
{
System.out.println("Roots are not real.");
}
}
}
82)FindNumberOfProblems:
public class Main
{
public static void main(String[] args) {
int count1=0;
int input1=5;
String input2[]={"1 1 0","0 0 1","0 0 0","1 0 1"};
for(String s :input2){
int k=0;
for(int i=0;i<s.length();++i){
if(s.charAt(i)=='1')
k++;
}
if(k>=2)
count1++;
}
System.out.println(count1);
}
}
83)Nth Fibonacii:
public class Demo{
static int fibo(int num){
int first = 0, second = 1, temp;
if (num == 0)
return first;
if (num == 1)
return second;
for (int i = 2; i <= num; i++){
temp = first + second;
first = second;
second = temp;
}
return second;
}
public static void main(String args[]){
int num = 7;
System.out.print("The 7th fibonacci number is : ");
System.out.println(fibo(num));
}
}
84)StepNumber:
public class StepNumber {
static String Step(int input1)
{
String str = String.valueOf(input1);
int len = str.length();
int[] arr = new int[len];
int rem =0;
String out = "";
if(len == 1)
{
out = str+"-false";
}
else
{
int i=len;
while(input1!=0)
{
rem = input1%10;
arr[i-1] = rem;
input1 = input1/10;
i--;
}
int k =1;
int count=0;
for(int j=0;j<len-1;j++)
{
if(arr[j]-arr[k]==1 || arr[j]-arr[k]==-1) //0 1 //1 2 //2 3
{
count++;
}
k++;
}
if(count ==len-1)
{
out = str+"-true";
}
else
{
out = str+"-false";
}
}
return out;
}
public static void main(String[] args) {
String res = Step(707); //9849
System.out.println(res);
}
}
85)DigiLock:
public class DigiLock {
public int Lock(int in1,int in2,int in3)
{
int a[] = {in1,in2,in3};
int temp = 0, x = 0, y = 0, z = 0, k = 0, l = 0, m = 0, max = 0, key = 0;
int u[] = new int[3];
int t[] = new int[3];
int h[] = new int[3];
for(int i=0;i<a.length;i++)
{
u[i] = a[i]%10;
temp = a[i]/10;
t[i] = temp%10;
h[i] = a[i]/100;
}
x = Math.min(u[0], Math.min(u[1], u[2]));
y = Math.min(t[0], Math.min(t[1], t[2]));
z = Math.min(h[0], Math.min(h[1], h[2]));
k = Math.max(u[0], Math.max(u[1], u[2]));
l = Math.max(t[0], Math.max(t[1], t[2]));
m = Math.max(h[0], Math.max(h[1], h[2]));
max = Math.max(k, Math.max(l, m));
key = (1000*max) + (100*z) + (10*y) +x;
return key;
}
public static void main(String[] args) {
DigiLock sol = new DigiLock();
int fin = sol.Lock(143, 540, 034);
System.out.println(fin);
}
86)FIND ODD EVEN DIFFERENCE
import java.util.Scanner;
class Main
{
public static int findOddEvenDifference(int n, int[] arr) {
int odd = 0, even = 0;
for(int i = 0; i < n; i++)
{
if(arr[i] % 2 == 0)
{
even = even + arr[i];
}
else
{
odd = odd + arr[i];
}
}
return odd - even;
}
public static void main(String[] args)
{
int n;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
int[] array = new int[n];
for(int i = 0; i < n; i++)
{
array[i] = sc.nextInt();
}
int result = findOddEvenDifference(n, array);
System.out.print(result);
}
}
87) FIND TOTAL DISTANCE
import java.util.Scanner;
class main
{
public static int findTotalDistance(int n, int[] arr)
{
int difference, sum = 0;
for(int i = 0; i < n - 1; i++)
{
difference = Math.abs(arr[i] - arr[i + 1]);
sum = sum + difference;
}
return sum;
}
public static void main(String[] args)
{
int n;
Scanner STDIN_SCANNER = new Scanner(System.in);
n = STDIN_SCANNER.nextInt();
int[] array = new int[n];
for(int i = 0; i < n; i++)
{
array[i] = STDIN_SCANNER.nextInt();
}
int result = findTotalDistance(n, array);
System.out.print(result);
}
}
88) FIND MAX DIFFERENCE
import java.util.Scanner;
class main
{
public static int findTotalDistance(int n, int[] arr)
{
int difference, max = 0;
for(int i = 0; i < n - 1; i++)
{
difference = arr[i] - arr[i + 1];
if(difference>max)
{
max = difference;
}
}
return max;
}
public static void main(String[] args)
{
int n;
Scanner STDIN_SCANNER = new Scanner(System.in);
n = STDIN_SCANNER.nextInt();
int[] array = new int[n];
for(int i = 0; i < n; i++)
{
array[i] = STDIN_SCANNER.nextInt();
}
int result = findTotalDistance(n, array);
System.out.print(result);
}
}
89) FIND TOTAL FEET
import java.util.Scanner;
class main
{
public static int findTotalFeet(int n, int[] arr)
{
int feet, total = 0;
for(int i=0; i<n; i++)
{
feet = arr[i] / 12;
total = total + feet;
}
return total;
}
public static void main(String[] args)
{
int n;
Scanner STDIN_SCANNER = new Scanner(System.in);
n = STDIN_SCANNER.nextInt();
int[] array = new int[n];
for(int i = 0; i < n; i++)
{
array[i] = STDIN_SCANNER.nextInt();
}
int result = findTotalFeet(n, array);
System.out.print(result);
}
}
90) FIND LARGEST SMALLEST DIFFERENCE
import java.util.Scanner;
class main
{
public static int findLargeSmallDifference(int n, int[] array)
{
int small = array[0], large = 0;
for(int i=0; i<n; i++)
{
if(array[i]>large)
{
large = array[i];
}
if(array[i]<small)
{
small = array[i];
}
}
return large-small;
}
public static void main(String[] args)
{
int n;
Scanner STDIN_SCANNER = new Scanner(System.in);
n = STDIN_SCANNER.nextInt();
int[] array = new int[n];
for(int i = 0; i < n; i++)
{
array[i] = STDIN_SCANNER.nextInt();
}
int result = findLargeSmallDifference(n, array);
System.out.print(result);
}
}
91) BILL TAX (C)
#include <stdio.h>
int main()
{
int i = 0, sum = 0, total_bill = 0, tax_amount = 0, n, m
[100];
scanf("%d", &n);
for (i = 0; i < n; i++)
{
scanf("%d", &m[i]);
}
for (i = 0; i < n; i++)
{
sum = sum + m[i];
}
if (sum > 1000)
{
tax_amount = (sum - 1000) / 10;
}
total_bill = total_bill + tax_amount;
printf("%d", total_bill - 400);
}
93) Number of sum(sum of the largest and smallest element of an array)
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int n = sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++)
a[i] = sc.nextInt();
int max=a[0], min=a[0];
for(int i=1;i<n;i++) {
if(a[i] > max)
max = a[i];
if(a[i] < min)
min = a[i];
}
int sum = max+min;
System.out.println(sum);
}
}
--------------------------------------------------------------------------------------
private static int sum(int input1, int[] input2)
{
int max=input2[0];
int min=input2[0];
for(int i=1;i<input2.length;i++)
{
if(max<input2[i])
max=input2[i];
if(min>input2[i])
min=input2[i];
}
return max+min;
}
94) Second largest(return second largest or return -1)
phython
l=int(input())
arr=list(map(int, input().split()))
arr=list(set(arr))
length_list=len(arr)
arr.sort()
print(arr[length_list-2])
------------------------------------------------------------------------
java
import java.util.*;
class GFG{
static void print2largest(int arr[],int arr_size)
{
int i, first, second;
if (arr_size < 2)
{
System.out.printf(" Invalid Input ");
return;
}
Arrays.sort(arr);
for (i = arr_size - 2; i >= 0; i--)
{
if (arr[i] != arr[arr_size - 1])
{
System.out.printf("element is %d\n", arr[i]);
return;
}
}
}
public static void main(String[] args)
{
int arr[] = {2,1,2};
int n = arr.length;
print2largest(arr, n);
}
}
95) The Cuckoo Sequent (C++)
#include <iostream>
using namespace std;
long long int helper(int n)
{
if(n==1) return 0;
if(n==2) return 1;
long long int cuckoo[n+1]={0};
cuckoo[1]=0;
cuckoo[2]=1;
for(int i=3;i<=n;i++)
{
cuckoo[i]=1*cuckoo[i-1]+2*cuckoo[i-2]+3*1;
}
return cuckoo[n];
}
int main(){
int n;
cin>>n;
cout << helper(n);
return 0;
}
96) Find the LCM of two given integers
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner s=new Scanner(System.in);
int a=s.nextInt();
int b=s.nextInt();
System.out.println( lcm(a, b));
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
}
97) Push zero to end
import java.util.Scanner;
public class Main
{
static void pushZerosToEnd(int arr[], int n)
{
int count = 0;
for (int i = 0; i < n; i++)
if (arr[i] != 0)
arr[count++] = arr[i];
while (count < n)
arr[count++] = 0;
}
public static void main (String[] args)
{
int arr[] = {1,0,2,5,0,6,0,0,9};
int n = arr.length;
pushZerosToEnd(arr, n);
for (int i=0; i<n; i++)
System.out.print(arr[i]+" ");
}
}
98) Arrangement(rearrange the number of the array in a such a manner that even and odd numbers are arranged alter
natively in increasing order)
import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
int n;
Scanner s = new Scanner(System.in);
n = s.nextInt();
int arr[]= new int[n];
for (int i = 0;i<n ; i++)
{
arr[i] = s.nextInt();
}
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
static void AlternateRearrange(int arr[], int n)
{
Arrays.sort(arr) ;
Vector v1 = new Vector();
Vector v2 = new Vector();
for (int i = 0; i < n; i++)
if (arr[i] % 2 == 0)
v1.add(arr[i]);
else
v2.add(arr[i]);
int index = 0, i = 0, j = 0;
boolean flag = false;
if (arr[0] % 2 == 0)
flag = true;
{
if (flag == true)
{
arr[index] = (int)v1.get(i);
i += 1 ;
index += 1 ;
flag = !flag;
}
{
arr[index] = (int)v2.get(j) ;
j += 1 ;
index += 1 ;
flag = !flag;
}
}
}
}
99) Return an integer representing the number of trailing zeros
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
System.out.println(findTrailingZeros(n));
}
static int findTrailingZeros(int n)
{
if (n < 0)
return -1;
int count = 0;
for (int i = 5; n / i >= 1; i *= 5)
count += n / i;
return count;
}
}
100) write the function to decrypt the string and find the nth character of the decrypt string
import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
int k = sc.nextInt();
System.out.println(encodedChar(str, k));
}
static char encodedChar(String str,int k)
{
String expand = "";
String temp = "";
int freq = 0;
for (int i=0; i < str.length() ; )
{
temp = "";
freq = 0;
while (i < str.length() && str.charAt(i)>='a'
&& str.charAt(i)<='z')
{
temp += str.charAt(i);
i++;
}
while (i < str.length() && str.charAt(i)>='1'
&& str.charAt(i)<='9')
{
freq = freq*10 + str.charAt(i) - '0';
i++;
}
for (int j=1; j<=freq; j++)
expand += temp;
}
if (freq==0)
expand += temp;
return expand.charAt(k-1);
}
}
101) Return the total number of coins till charity days(Adam's charity)
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = 0;
for(int i=1;i<=n;i++)
sum += Math.pow(i,2);
System.out.println(sum);
}
}
102) Max contiguous sequence
import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int a[]= new int[n];
for (int i = 0;i<n ; i++)
{
a[i] = s.nextInt();
}
System.out.println( maxSubArraySum(a));
}
static int maxSubArraySum(int a[])
{
int size = a.length;
int max_so_far = Integer.MIN_VALUE, max_ending_here = 0;
for (int i = 0; i < size; i++)
{
max_ending_here = max_ending_here + a[i];
if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
if (max_ending_here < 0)
max_ending_here = 0;
}
return max_so_far;
}
}
103) Return the integer array after removal of duplicate values
class Main
{
static int removeDuplicates(int arr[], int n)
{
if (n == 0 || n == 1)
return n;
int j = 0;
for (int i = 0; i < n-1; i++)
if (arr[i] != arr[i+1])
arr[j++] = arr[i];
arr[j++] = arr[n-1];
return j;
}
public static void main (String[] args)
{
int arr[] = {11,11,11,13,13,20};
int n = arr.length;
n = removeDuplicates(arr, n);
// Print updated array
for (int i=0; i<n; i++)
System.out.print(arr[i]+" ");
}
}
104) Return the length of largest sub array containing equal number of 0s and 1s
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
int ans = max_length(arr,n);
System.out.println(ans);
}
public static int max_length(int[] arr,int n){
Map<Integer, Integer> m = new HashMap<>();
int mx = 0, cnt = 0;
for(int i = 0;i < arr.length;i++) {
cnt+=(arr[i] == 1)?1:-1;
if(cnt == 0)
mx = Math.max(mx, i+1);
if(m.containsKey(cnt)) {
mx = Math.max(mx, i - m.get(cnt));
} else
m.put(cnt, i);
}
return mx;
}
}
105) Return a string with the char of each word in the reverse order
import java.io.*;
import java.util.*;
class GFG {
static void reverseWords(String str)
{
Stack<Character> st=new Stack<Character>();
for (int i = 0; i < str.length(); ++i) {
if (str.charAt(i) != ' ')
st.push(str.charAt(i));
else {
while (st.empty() == false) {
System.out.print(st.pop());
}
System.out.print(" ");
}
}
while (st.empty() == false) {
System.out.print(st.pop());
}
}
public static void main(String[] args)
{
String str = "Hello World";
reverseWords(str);
}
}
106) Reverse of the string
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
String name ="Hello World";
int leng=name.length();
String rev="";
for(int i=leng-1;i>=0;i--){
rev=rev+name.charAt(i);
}
System.out.println(rev);
}
}
107) Reverse of the string
class Codechef
{
public static void main(String[] arg)
{
Codechef rs=new Codechef();
Scanner sc=new Scanner(System.in);
System.out.print("Enter a string: ");
String str=sc.nextLine();
System.out.println("Reverse of a String is : "+rs.reversestr(str)); //called method
}
//reverse string method
static String reversestr(String s)
{
String r="";
for(int i=s.length();i>0;--i) //execute until condition i>0 becomes false
{
r=r+(s.charAt(i-1));
}
return r;
}
}
108)Missing braces program
class Codechef
{
static String areBracketsBalanced(String expr){
Deque<Character> stack =new ArrayDeque<Character>();
for(int i=0;i<expr.length();i++){
char x=expr.charAt(i);
if(x=='{'||x=='['||x=='('){
stack.push(x);
continue;
}
if(stack.isEmpty())
return "error";
char check;
switch (x){
case ')':
check =stack.pop();
if(check=='('|| check=='[')
return "error";
break;
case '}':
check =stack.pop();
if(check=='{'|| check=='[')
return "error";
break;
case ']':
check =stack.pop();
if(check=='('|| check=='[')
return "error";
break;
}
}
if(stack.isEmpty())
return "correct";
else return "werror";
}
}
109) MBA Challange
import java.util.Scanner;
public class PowerOfNumberExample1
{
static int power(int base, int exponent)
{
int power = 1;
for (int i = 1; i <= exponent; i++)
power = power * base;
return power;
}
public static void main(String args[])
{
int base, exponent;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the base: ");
base=sc.nextInt();
System.out.print("Enter the exponent: ");
exponent=sc.nextInt();
int pow=power(base, exponent);
System.out.println(base +" to the power " +exponent + " is: "+pow);
}
}
110) Array of duplicate elements
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner s=new Scanner(System.in);
int N=s.nextInt();
int [] a = new int [] {1, 2, 3, 4, 2, 7, 8, 8, 3};
for(int i = 0; i < a.length; i++) {
for(int j = i + 1; j < a.length; j++) {
if(a[i] == a[j])
System.out.print(a[j]+ " "); }}
}
}
111) Return HCF of given numbers
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
int arr[] = { 2,4,8 };
int n = arr.length;
System.out.println(findGCD(arr, n));
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int findGCD(int arr[], int n)
{
int result = 0;
for (int element: arr){
result = gcd(result, element);
if(result == 1)
{
return 1;
}
}
return result;
}
112) Sum of unique devisior
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner input=new Scanner(System.in);
int number=input.nextInt();
int j = 0;
for(int i = 1; i <= number; i++) {
if (number % i == 0) {
j = j + i;
}
}
System.out.println(j);
}
}
113)Playing with number
static int[] shiftArray(int input1, int[] input2, int input3)
{
int ar[]=new int[input2.length];
int j=0;
for(int i=input3;i<input2.length;i++)
{
ar[j++]=input2[i];
}
for(int i=0; i<input3;i++)
{
ar[j++]=input2[i];
}
return ar;
}
114) Array sort
private static int getCount(int input1, int[] input2)
{
int ar[]=new int[input2.length];
for(int i =0;i<input2.length;i++)
{
ar[i]=input2[i];
}
Arrays.sort(ar);
int num=ar[0];
for(int i=0;i<input2.length;i++)
{
if(ar[i]!=num)
return 0;
num++;
}
return 1;
}
115) Fancy Occurance
private static String fancyOcc(String input1, String input2)
{
String s="";
char com=input2.charAt(0);
for(int i= 0; i<input1.length();i++)
{
char ch=input1.charAt(i);
if(ch!=com)
s=s+ch;
}
return s;
}
116) String Within String
private static String isPermutation(String input1,String input2)
{
input1=input1.toLowerCase();
input2=input2.toLowerCase();
int arr1[] = new int[26];
int arr2[] = new int[26];
for(int i=0; i<input1.length();i++)
{
char ch = input1.charAt(i);
arr1[ch-97]++;
}
for(int i=0;i<input2.length();i++)
{
char ch1 = input2.charAt(i);
arr2[ch1-97]++;
if(arr1[ch1-97]<arr2[ch1-97])
return "no";
}
return "yes";
}
117) Palindrome
private static int isPalindrome(String input1)
{
int j = input1.length();
for(int i=0;i<s.length()/2;i++)
{
char ch= input1.charAt(i);
if(ch!= input1.charAt(j-1))
return 0;
j--;
}
return 1;
}
118) Max occurring character
private static String maxOccurance(String input1)
{
char[] c=new char[26];
String s="";
for(int i=0;i<input1.length();i++)
{
char ch= input1.charAt(i);
c[ch-97]++;
}
int max=0;
for(int i=1;i<26;i++)
{
if(c[max]<c[i])
max=i;
}
for(int i=0;i<26;i++)
{
if(c[i]==c[max]&&i!=max)
return "0";
}
return s+(char)(max+97);
}
119) Anagram
private static String isAnagarm (String input1,String input2)
{
int n1 = input1.length();
int n2 = input2.length();
if (n1 != n2)
return "no";
input1=input1.toUpperCase();
input2=input2.toUpperCase();
int a[]= new int[26];
int a2[]= new int[26];
for(int i=0;i<n1;i++)
{
char ch= input1.charAt(i);
a[ch-65]++;
}
for(int i=0;i<n2;i++)
{
char ch= input2.charAt(i);
a2[ch-65]++;
}
for (int i = 0; i < 26; i++)
if (a[i] != a2[i])
return "no";
return "yes";
}
120) Coin Counts
Test cases:
input1 : 2
output : 5
private static int getcount(int input1)
{
int coin=0;
while(input1!=0)
{
coin=coin+(input1*input1);
input1--;
}
return coin;
}
121) Electro Static
Test cases:
input1 : {4,3,5}
input2 : PNP
input3 : 3
output : 600
private static int electroStatic(int[] input1, String input2, int input3)
{
int sum=0;
for(int i = 0;i<input3;i++)
{
char ch = input2.charAt(i);
switch(ch)
{
case 'P':
sum=sum+input1[i];
break;
case 'N':
sum=sum-input1[i];
break;
}
}
return sum*100;
}
122) Reverse Array
public static int[] reverseArray(int [] input1,int input2) {
int [] ars=new int[input2];
int k=0;
for(int i=input2-1;i>=0;i--)
{
ars[k]=input1[i];
k++;
}
return ars;
}
123) Reverse string word wise
import java.util.regex.Pattern;
public class Exp {
static String reverseWords(String str)
{
Pattern pattern = Pattern.compile("\\s");
String[] temp = pattern.split(str);
String result = "";
for (int i = 0; i < temp.length; i++) {
if (i == temp.length - 1)
result = temp[i] + result;
else
result = " " + temp[i] + result;
}
return result;
}
public static void main(String[] args)
{
String s1 = "Welcome to mettl";
System.out.println(reverseWords(s1));
}
}
124) The smallest possible Two digit numbers
Example1
input1 :7136
output :13
Example2
input1 :9987
output :78
import java.util.Arrays;
public class Main
{
public static void main(String[] args) {
int input1=9987;
int n=input1;
int count=0;
int rem=0;
while(input1 !=0){
count++;
input1=input1/10;
}
int[] a=new int[count];
for(int i=0;i<a.length;i++){
rem=n%10;
a[i]=rem;
n=n/10;
}
Arrays.sort(a);
System.out.println(a[0]*10+a[1]);
}
}
----------------------------------------------------------------------------------------------------------------
code-
import java.util.Scanner;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int input1=sc.nextInt();
int rem;
int smallest=Integer.MAX_VALUE;
int sec_smallest=Integer.MAX_VALUE;
while(input1>0){
rem=input1 % 10;
if(smallest >= rem){
sec_smallest=smallest;
smallest = rem;
}
else if(rem <= sec_smallest){
sec_smallest=rem;
}
input1=input1/10;
}
System.out.println(smallest+""+sec_smallest);
}
}
125) Power of First and Last
Example 1:
input1 :21221
output :2^1=2.0
Example 2:
input1 :375
output :3^5=243.0
public class Main
{
public static void main(String[] args) {
int number =375;
int firstDigit = 0;
int lastDigit = 0;
lastDigit = number%10;
while(number!=0) {
firstDigit = number%10;
number /= 10;
}
double power=Math.pow(firstDigit,lastDigit);
System.out.println(power);
}
}
126) Tic-tac-toe
Example-1
input1 :{1,2,9,5,7,8}
output :O:c2
Example-2
input1 :{5,4,7,3,9,8,1}
output :X:d1
Example-3
input1:{1,8,2,3,7,5,6,4,9}
output:DRAW
public class Main
{
public static void main(String[] args) {
int[] input1={1,8,2,3,7,5,6,4,9};
System.out.println(ticTacToe(input1));
}
static String ticTacToe(int[] input1){
int size = (input1.length)/2;
int x[];
if(input1.length%2!=0){
x = new int[size+1];
}else{
x = new int[size];
}
int o[] = new int[size];
for(int i=0, j=0; j<x.length; i+=2, j++){
x[j] = input1[i];
}
for(int i=1, j=0; j<size; i+=2, j++){
o[j] = input1[i];
}
StringBuilder xstr = new StringBuilder();
StringBuilder ostr = new StringBuilder();
for (int j : x) {
xstr.append(String.valueOf(j));
}
for (int j : o) {
ostr.append(String.valueOf(j));
}
if(xstr.toString().contains("1") && xstr.toString().contains("4") &&
xstr.toString().contains("7")){
return "X:c1";
}else if(xstr.toString().contains("2") && xstr.toString().contains("5") &&
xstr.toString().contains("8")){
return "X:c2";
}else if(xstr.toString().contains("3") && xstr.toString().contains("6") &&
xstr.toString().contains("9")){
return "X:c3";
}else if(xstr.toString().contains("1") && xstr.toString().contains("2") &&
xstr.toString().contains("3")){
return "X:r1";
}else if(xstr.toString().contains("4") && xstr.toString().contains("5") &&
xstr.toString().contains("6")){
return "X:r2";
}else if(xstr.toString().contains("7") && xstr.toString().contains("8") &&
xstr.toString().contains("9")){
return "X:r3";
}else if(xstr.toString().contains("1") && xstr.toString().contains("5") &&
xstr.toString().contains("9")){
return "X:d1";
}else if(xstr.toString().contains("3") && xstr.toString().contains("5") &&
xstr.toString().contains("7")){
return "X:d2";
}
else if(ostr.toString().contains("1") && ostr.toString().contains("4") &&
ostr.toString().contains("7")){
return "O:c1";
}else if(ostr.toString().contains("2") && ostr.toString().contains("5") &&
ostr.toString().contains("8")){
return "O:c2";
}else if(ostr.toString().contains("3") && ostr.toString().contains("6") &&
ostr.toString().contains("9")){
return "O:c3";
}else if(ostr.toString().contains("1") && ostr.toString().contains("2") &&
ostr.toString().contains("3")){
return "O:r1";
}else if(ostr.toString().contains("4") && ostr.toString().contains("5") &&
ostr.toString().contains("6")){
return "O:r2";
}else if(ostr.toString().contains("7") && ostr.toString().contains("8") &&
ostr.toString().contains("9")){
return "O:r3";
}else if(ostr.toString().contains("1") && ostr.toString().contains("5") &&
ostr.toString().contains("9")){
return "O:d1";
}else if(ostr.toString().contains("3") && ostr.toString().contains("5") &&
ostr.toString().contains("7")){
return "O:d2";
}else{
return "DRAW";
}
}
}
127) Find the mobile pin[new]
Example-1
input1: Wipro
input2: Limited
output:17254
Example-2
input1: Dog
input2: Cat
output: 2624
Example-3
input1 :Java
input2 :Program
output :13251
public class Main
{
public static void main(String[] args) {
String input1="Java";
String input2="Program";
System.out.println(findMobilePin(input1,input2));
}
static int findMobilePin(String input1,String input2)
{
String a,b;
a=input1;
b=input2;
int m[]=new int[26];
int n[]=new int[26];
int ml=0,nl=0;
a=a.toLowerCase();
b=b.toLowerCase();
for(int i=0;i<a.length();i++)
{
m[a.charAt(i)-'a']++;
}
for(int i=0;i<b.length();i++)
{
n[b.charAt(i)-'a']++;
}
int un=0;
for(int i=0;i<26;i++)
{
if(m[i]>0&&n[i]>0)
{
m[i]=0;
n[i]=0;
un++;
}
if(m[i]>1)
{
m[i]=0;
}
if(n[i]>1)
{
n[i]=0;
}
}
for(int i=0;i<26;i++)
{
if(m[i]>0)
ml=ml+(m[i]*(i+1));
if(n[i]>0)
nl=nl+(n[i]*(i+1));
}
int res;
res=Integer.valueOf(Integer.toString(un)+Integer.toString(ml)+Integer.toString(nl));
return res;
}
}
128) Coding Contest (Find number of problems)
Example 1
input1: 3
input2:{"1 0 0","1 1 1","1 0 1"}
output:2
Example 2
input1 :5
input2 :{"1 1 0","1 1 1","0 0 1","0 0 0","1 0 1"}
output:3
public class Main
{
public static void main(String[] args) {
String[] input2 = {"1 0 0","1 1 1","1 0 1"};
int input1 = 3;
System.out.println(FindNoOfProblems(input1, input2 ));
}
static int FindNoOfProblems(int input1, String[] input2)
{
int result = 0;
for(int i=0; i<input2.length; i++){
int count = 0;
for(int j=0 ; j<input2[i].length(); j++){
if(input2[i].charAt(j) == '1'){
count++;
}
}
if(count > 1)
result++;
}
return result;
}
129) FindStringCode
Example 1
input1:"World Wide Web"
output:402326
Example 2
input1:"Hello World"
output:2640
public class Main
{
public static void main(String[] args) {
String string="World Wide Web";
String words[]=string.split(" ");
int len=words.length;
String str="";
int sum=0;
for(int i=0;i<len;i++){
String word_on_hold=words[i].toLowerCase();
int l=word_on_hold.length();
char ctr_array[]=word_on_hold.toCharArray();
if(l%2==0){
for(int j=0;j<l/2;j++){
int diff=(int)(ctr_array[j])-(int)(ctr_array[(l-1)-j]);
sum += Math.abs(diff);
}
}
else{
for(int j=0;j<l/2;j++){
int diff=(int)(ctr_array[j])-(int)(ctr_array[(l-1)-j]);
sum += Math.abs(diff);
}
sum += (int)(ctr_array[l/2])-96;
}
str += String.valueOf(sum);
sum=0;
}
System.out.println(str);
}
}
-----------------------------------------------------------------------------------------------------------------------
public class Main
{
public static void main(String[] args) {
String input1="Hello World";
System.out.println(findStringCode(input1));
}
static int findStringCode(String input1){
StringBuilder sb=new StringBuilder();
String input=input1.toLowerCase();
String []arr=input.split(" ");
int sum=0;
int temp=0;
String x=" ";
for(String s:arr){
if(s.length()%2==0){
int j=s.length()-1;
for(int i=0;i<s.length()/2;i++){
int a=((int)s.charAt(i))-96;
int b=((int)s.charAt(j))-96;
int diff=a-b;
sum=sum+diff;
j--;
}
sb.append(sum);
sum=0;
}
else{
int j=s.length()-1;
for(int i=0;i<s.length()/2;i++){
int a=((int)s.charAt(i))-96;
int b=((int)s.charAt(j))-96;
int diff=a-b;
sum=sum+diff;
j--;
temp=i;
}
sum=sum+((int)s.charAt(temp+1))-96;
sb.append(sum);
sum=0;
}
}
String z=sb.toString();
return Integer.parseInt(z);
}
}
130)Array task
input1:{12,1313,122,678,898};
output:23
import java.util.Arrays;
public class Main
{
public static void main(String[] args) {
int[] a={12,1313,122,678,898};
int count=1,z=0,y=0;
for(int i=0;i<5;i++)
{
String b=""+a[i];//12
String[] c=b.split("",-2);
String str="";
Arrays.sort(c);
count=1;
for(int j=0;j<c.length-1;j++)
{
if(!c[j].equals(c[j+1]))
count++;
}
if(c.length%count==0)
z++;
else
y++;
}
System.out.println(z*10+y);
}
}
131) Message controlled Robot movement
public String moveRobot(int input1,int input2,String input3,String input4){
String path[]=input3.split("-");
int x=Integer.parseInt(path[0]);
int y=Integer.parseInt(path[1]);
String pos=path[2];
String arr[]=input4.split(" ");
int f=0;
for(String s:arr)
{ if(s.equals("R"))
{ if(pos.equals("N"))
pos="E";
else if(pos.equals("E"))
pos="S";
else if(pos.equals("S"))
pos="W";
else pos="N";
}
else if(s.equals("L"))
{
if(pos.equals("N"))
pos="W";
else if(pos.equals("E"))
pos="N";
else if(pos.equals("S"))
pos="E";
else pos="S";
}
else if(f!=1)
{
if(pos.equals("N"))
{
if(input2>y)
y=y+1;
else
f=1;
}
else if(pos.equals("S"))
{
if(y>0)
y=y-1;
else
f=1;
}
else if(pos.equals("E"))
{
if(input1>x)
x=x+1;
else f=1;
}
else {
if(x>0)
x=x-1;
else f=1;
}
}
}
if(f!=1)
return String.valueOf(x)+"-"+String.valueOf(y)+"-"+String.valueOf(pos);
else return String.valueOf(x)+"-"+String.valueOf(y)+"-"+String.valueOf(pos)+"-"+"ER";
}
}