Hello friends, I hope you all are doing great. In this post today we will learn to solve the 3 coding questions which is being asked by the Accenture company recently. If you are preparing for selection in any company then this post will help you definitely.
Lets Start
1.Difference of Sum
Implement the following Function
def differenceofSum(n. m)
The function accepts two integers n, m as arguments Find the sum of all numbers in range from 1 to m(both inclusive) that are not divisible by n. Return difference between sum of integers not divisible by n with sum of numbers divisible by n.
Assumption:
- n>0 and m>0
- Sum lies between integral range
Example
Input
n:4
m:20
Output
90
Explanation
- Sum of numbers divisible by 4 are 4 + 8 + 12 + 16 + 20 = 60
- Sum of numbers not divisible by 4 are 1 +2 + 3 + 5 + 6 + 7 + 9 + 10 + 11 + 13 + 14 + 15 + 17 + 18 + 19 = 150
- Difference 150 – 60 = 90
Sample Input
n:3
m:10
Sample Output
19
class DifferenceOfSum { static int differenceofSum(int n,int m) { int sum1=0,sum2=0; for (int i = 1; i <=m; i++) { if(i%n==0) sum1=sum1+i; else sum2=sum2+i; } return Math.abs(sum1-sum2); } public static void main(String[] args) { System.out.println(differenceofSum(4, 20)); } } /* OUTPUT 90 */
2.Replace Frequent Character in String
You are required to implement the following function:
String FrequentCharacterReplaced(String str, char x);
The function accepts a string 'str' and a character 'x' as its arguments. You are required to find the most frequent character in string 'str' and replace it with character 'x' across the string, and return the same.
Note:
If the frequency of two characters are the same, we have to consider the character with lower ascii value.
Example:
Input:
str: bbadbbababb
Output:
ttadttatatt
Explanation:
The most frequent character in string 'str' is 'b', replacing 'b' with 't' will form string 'ttadttatatt', hence 'ttadttatatt' is returned.
class FrequentCharacter { static String FrequentCharacterReplaced(String str, char x) { char c=' '; int count1=0,count2; char[] ch=str.toCharArray(); for (int i = 0; i < str.length(); i++) { count2=0; for (int j = 0; j < str.length(); j++) { if(ch[i]==ch[j]) count2++; } if(count2 > count1) { count1=count2; c=ch[i]; } } return str.replace(c, x); } public static void main(String[] args) { ; System.out.println(FrequentCharacterReplaced("bbadbbababb",'t')); } } /* OUTPUT ttadttatatt */
3.Find the maximum element and its index in an array
Problem statement:
You are given a function, void MaxInArray(int arr[], int length); The function accepts an integer array 'arr' of size 'length' as its argument. Implement the function to find the maximum element of the array and print the maximum element and its index to the standard output
(STDOUT). The maximum element and its index should be printed in separate lines.
Note:
- Array index starts with 0
- Maximum element and its index should be separated by a line in the output
- Assume there is only 1 maximum element in the array
- Print exactly what is asked, do not print any additional greeting messages
Example:
Input:
23 45 82 27 66 12 78 13 71 86
Output:
86
9
Explanation:
86 is the maximum element of the array at index 9.
class ArrayMaximum { static void MaxInArray(int arr[], int length) { int index=-1; int max=arr[0]; for (int i = 0; i < length; i++) { if(max<=arr[i]) { max=arr[i]; index=i; } } System.out.println(max+"\n"+index); } public static void main(String[] args) { int[] a={23,45,82,27,66,12,78,13,71,86}; MaxInArray(a,10) ; } } /* OUTPUT 86 9 */
Request:-If you found this post helpful then let me know by your comment and share it with your friend.
0 Comments