KEMBAR78
Accenture Coding Questions - PrepBytes | PDF | String (Computer Science) | C++
0% found this document useful (0 votes)
23 views26 pages

Accenture Coding Questions - PrepBytes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views26 pages

Accenture Coding Questions - PrepBytes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Accenture Coding

Questions

4 83.3K Views

Accenture Coding Questions

In this article, we’ll discuss Accenture Coding questions. Most of Accenture


Coding questions will be of the same pattern which are discussed in this
article.

There are 2-3 Accenture Coding Questions asked in the Exam within a
time limit of 45 minutes. This is the third section of the actual test and
most of our students say that the level of questions asked in the exam is
of medium difficulty.

This third round is based on coding, so you’ve to practice more and more
coding questions using C, C++, Java, and python. The main motive of this
section is to test your coding skills as well as your problem-solving
techniques.

In the exam mainly two problem statements will be given. You’ve to solve
these Accenture coding questions using any of the programming
languages from C, C++, Java, and python.

You can now get one step closer to your dream job and streamline your
placement preparation journey with PrepBytes Placement Programs.

Rules for Accenture Coding Questions Section

Two questions will be asked. You’ve to solve both the questions in 45


minutes.

You must start your code from scratch.

The Coding section is divided into two, first for writing the code and
the other for the output. So you should write the whole program.

Errors are clearly displayed.

If you want to clear this round then you must get one clear output
and one partial output.

Accenture Coding Questions

1. Problem Description:
The Binary number system only uses two digits, 0 and 1 and the
number system can be called binary string. You are required to
implement the following function:
int OperationsBinaryString(char* str);
The function accepts a string str as its argument. The string str
consists of binary digits separated with an alphabet as follows:
– A denotes AND operation
– B denotes OR operation
– C denotes XOR Operation

You are required to calculate the result of the string str, scanning the
string to right taking one operation at a time, and return the same.

Note: No order of priorities of operations is required.


Length of str is odd.
If str is NULL or None (in case of Python), return -1.

Input:
1C0C1C1A0B1

Output:
1

Explanation:
The alphabets in str when expanded becomes “1 XOR 0 XOR 1 XOR 1
AND 0 OR 1”, the result of the expression becomes 1, hence 1 is
returned.

C++ Code:
#include<bits/stdc++.h>
using namespace std;
int OperationsBinaryString(char* str)
{
if(str==NULL)
return -1;
int i=1;
int a=*str-'0';
str++;
while(*str!='\0')
{
char p=*str;str++;
if(p=='A')
a&=(*str -'0');
else if (p=='B')
a|=(*str -'0');
else a^=(*str -'0');
str++;
}
return a;
}
int main()
{
string s;
getline(cin,s);
int len=s.size();
char *str=&s[0];
cout<<OperationsBinaryString(str);
}
Python code:
def OperationsBinaryString(str):
a=int(str[0])
i=1
while i<len(str):
if str[i]=='A':
a&=int(str[i+1])
elif str[i]=='B':
a|=int(str[i+1])
else:
a^=int(str[i+1])
i+=2
return a
str=input()
print(OperationsBinaryString(str))
Java code:
import java.util.*;
class Main
{
public static int operationsBinaryString (String str)
{
if(str==null)
return -1;
int res = str.charAt (0) - '0';
for (int i = 1; i < str.length ();)
{
char prev = str.charAt (i);
i++;
if (prev == 'A')
res = res & (str.charAt (i) - '0');
else if (prev == 'B')
res = res | (str.charAt (i) - '0');
else
res = res ^ (str.charAt (i) - '0');
i++;
}
return res;
}
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
String str =sc.next();
System.out.println (operationsBinaryString (str));
}
}

2. Problem Description:
The function accepts two positive integers ‘r’ and ‘unit’ and a positive
integer array ‘arr’ of size ‘n’ as its argument ‘r’ represents the number
of rats present in an area,
‘unit’ is the amount of food each rat consumes and each ith element
of array ‘arr’ represents the amount of food present in ‘i+1’ house
number, where 0 <= i.

Note:
Return -1 if the array is null.
Return 0 if the total amount of food from all houses is not sufficient
for all the rats.
Computed values lie within the integer range.

Example:
Input:
r: 7
unit: 2
n: 8
arr: 2 8 3 5 7 4 1 2

Output:
4

Explanation:
Total amount of food required for all rats = r * unit
= 7 * 2 = 14.
The amount of food in 1st houses = 2+8+3+5 = 18. Since, the amount
of food in 1st 4 houses is sufficient for all the rats. Thus, output is 4.

C++ Code:
#include<bits/stdc++.h>
using namespace std;
int calculate (int r, int unit, int arr[], int n)
{
if (n == 0)
return -1;
int totalFoodRequired = r * unit;
int foodTillNow = 0;
int house = 0;
for (house = 0; house < n; ++house)
{
foodTillNow += arr[house];
if (foodTillNow >= totalFoodRequired)
{
break;
}
}
if (totalFoodRequired > foodTillNow)
return 0;
return house + 1;
}
int main ()
{
int r;
cin >> r;
int unit;
cin >> unit;
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; ++i)
{
cin >> arr[i];
}
cout << calculate (r, unit, arr, n);
return 0;
}
Python code:
def calculate(r,unit,arr,n):
if n==0:
return -1
totalFoodRequired=r*unit
foodTillNow=0
house=0
for house in range(n):
foodTillNow+=arr[house]
if foodTillNow >= totalFoodRequired:
break
if totalFoodRequired > foodTillNow:
return 0
return house+1
r = int(input())
unit = int(input())
n = int(input())
arr = list(map(int,input().split()))
print(calculate(r,unit,arr,n))
Java code:
import java.util.*;
class Main
{
public static int solve (int r, int unit, int arr[], int n)
{
if (arr == null)
return -1;
int res = r * unit;
int sum = 0;
int count = 0;
for (int i = 0; i < n; i++)
{
sum = sum + arr[i];
count++;
if (sum >= res)
break;
}
if(sum<res)
return 0;
return count;
}
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
int r = sc.nextInt ();
int unit = sc.nextInt ();
int n = sc.nextInt ();
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[i] = sc.nextInt ();
System.out.println (solve (r, unit, arr, n));
}
}
3. Problem Statement:

You are given a function,


int findCount(int arr[], int length, int num, int diff);
The function accepts an integer array ‘arr’, its length and two integer
variables ‘num’ and ‘diff’.
Implement this function to find and return the number of elements of
‘arr’ having an absolute difference of less than or equal to ‘diff’ with
‘num’.

Note: In case there is no element in ‘arr’ whose absolute difference


with ‘num’ is less than or equal to ‘diff’, return -1.

Example:
Input:
arr: 12 3 14 56 77 13
num: 13
diff: 2

Output:
3

Explanation:
Elements of ‘arr’ having absolute difference of less than or equal to
‘diff’ i.e. 2 with ‘num’ i.e. 13 are 12, 13 and 14.
C++ code:
#include<bits/stdc++.h>
using namespace std;
int findCount(int n, int arr[], int num, int diff) {
int count = 0;
for (int i = 0; i < n; ++i)
{
if (abs(arr[i] - num) <= diff)
{
count++;
}
}
return count > 0 ? count : -1;
}
int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
int num; cin >> num;
int diff; cin >> diff;
cout << findCount(n, arr, num, diff);
}
Python code:
def findCount(n, arr, num, diff):
count=0
for i in range(n):
if(abs(arr[i]-num)<=diff):
count+=1
if count:
return count
return 0
n=int(input())
arr=list(map(int,input().split()))
num=int(input())
diff=int(input())
print(findCount(n, arr, num, diff))
Java code:
import java.util.*;
class Main
{
public static int findCount (int arr[], int length, int num, int diff)
{
int count = 0;
for (int i = 0; i < length; i++)
{
if (Math.abs (num - arr[i]) <= diff)
count++;
}
return count>0?count:-1;
}
public static void main (String[]args)
{
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 num = sc.nextInt ();
int diff = sc.nextInt ();
System.out.println (findCount (arr, n, num, diff));
}
}

4. Problem Statement:
Implement the following Function
def ProductSmallestPair(sum, arr)
The function accepts an integer sum and an integer array arr of size
n.
Implement the function to find the pair, (arr[j], arr[k]) where j!=k,such
that arr[j] and arr[k] are the least two elements of array (arr[j] +
arr[k] <= sum) and return the product of element of this pair.

Note:
Return -1 if array is empty or if n < 2
Return 0, if no such pairs found.
All computed values lie within integer range.

Example:
Input
sum:9
Arr:5 2 4 3 9 7 1

Output
2

Explanation:
Pair of least two element is (2, 1) 2 + 1 = 3 < 9, Product of (2, 1) 2*1 = 2.
Thus, output is 2.
C code:
#include<stdio.h>
int productSmallestPair(int *array, int n, int sum)
{
int answer, temp, i, j, check;
if(n<2)
{
answer = -1;
}
else
{
for(i=0; i<n; i++) //sorting of array
{
for(j=i+1; j<n; j++)
{
if(array[i]>array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
check = array[0] + array[1];
if(check<=sum)
{
answer = array[0] * array[1];
}
else
{
answer = 0;
}
}
return answer;
}
int main()
{
int n, sum, result, i;
scanf("%d",&sum);
scanf("%d",&n);
int array[n];
for(i=0; i<n; i++)
{
scanf("%d",&array[i]);
}
result = productSmallestPair(array, n, sum);
printf("%d",result);
return 0;
}
Java code:
import java.util.*;
class Main
{
public static int productSmallestPair (int arr[], int n, int sum)
{
if (n <2)
return -1;
int ans, temp, check;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
check = arr[0] + arr[1];
if (check <= sum)
return arr[0] * arr[1];
else
return 0;
}
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
int sum = sc.nextInt ();
int n = sc.nextInt ();
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[i] = sc.nextInt ();
System.out.println (productSmallestPair (arr, n, sum));
}
}
Python code:
n = int(input())
sum1 = int(input())
arr = list(map(int, input().split()))
if n < 2:
print('-1')
arr = sorted(arr)
for i in range(n-1):
if arr[i] + arr[i+1] < sum1:
print(arr[i] * arr[i+1])
break
else:
print('0')

5. Problem Statement:

N-base notation is a system for writing numbers that uses only n


different symbols. These symbols are the first n symbols from the
given notation list(Including the symbol for o)
Decimal to n base notation are (0:0, 1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8, 9:9,
10:A,11:B and so on upto 35:Z).

Implement the following function


Char* DectoNBase(int n, int num):

The function accept positive integer n and num Implement the


function to calculate the n-base equivalent of num and return the
same as a string

Steps:

Divide the decimal number by n,Treat the division as the integer


division

Write the the remainder (in n-base notation)

Divide the quotient again by n, Treat the division as integer


division

Repeat step 2 and 3 until the quotient is 0

The n-base value is the sequence of the remainders from last to


first

Assumption:
1 < n < = 36

Example
Input
n: 12
num: 718

Output
4BA

Explanation:

num = 718, divisor = 12, quotient=59, remainder=10(A).

num = 59, divisor = 12, quotient=4, remainder=11(B).

num = 4, divisor = 12, quotient=0, remainder=4(A).

C++ code:
#include<bits/stdc++.h>
using namespace std;
string decitoNBase (int n, int num)
{
string res = "";
int quotient = num / n;
vector<int> rem;
rem.push_back(num % n);
while(quotient != 0)
{
rem.push_back(quotient % n);
quotient = quotient / n;
}
for (int i = 0; i < rem.size (); i++)
{
if (rem[i] > 9)
{
res = (char)(rem[i] - 9 + 64) + res;
}
else
res = to_string(rem[i]) + res;
}
return res;
}
int main ()
{
int n, num;
cin >> n>>num;
cout << decitoNBase(n, num);
return 0;
}
Python code:
n = int(input())
num = int(input())
reminder = []
quotient = num // n
reminder.append(num%n)
while quotient != 0:
reminder.append(quotient%n)
quotient = quotient // n
reminder = reminder[::-1]
equivalent = ''
for i in reminder:
if i > 9:
a = i - 9
a = 64 + a
equivalent+=chr(a)
else:
equivalent+=str(i)
print(equivalent)
Java code:
import java.util.*;
class Main
{
public static String dectoNBase (int n, int num)
{
String res = "";
int quotient = num / n;
ArrayList < Integer > rem = new ArrayList < Integer > ();
rem.add (num % n);
while(quotient != 0)
{
rem.add (quotient % n);
quotient = quotient / n;
}
for (int i = 0; i < rem.size (); i++)
{
if (rem.get (i) > 9)
{
res = (char) (rem.get (i) - 9 + 64) + res;
}
else
res = rem.get (i) + res;
}
return res;
}
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
int n = sc.nextInt ();
int num = sc.nextInt ();
System.out.println (dectoNBase (n, num));
}
}

6. Problem Statement:

A carry is a digit that is transferred to left if sum of digits exceeds 9


while adding two numbers from right-to-left one digit at a time.
You are required to implement the following function.
Int NumberOfCarries(int num1 , int num2);

The function accepts two numbers ‘num1’ and ‘num2’ as its


arguments. You are required to calculate and return the total
number of carries generated while adding digits of two numbers
‘num1’ and ‘ num2’.

Assumption: num1, num2>=0

Example:
Input
Num 1: 451
Num 2: 349

Output
2

Explanation:
Adding ‘num 1’ and ‘num 2’ right-to-left results in 2 carries since (
1+9) is 10. 1 is carried and (5+4=1) is 10, again 1 is carried. Hence 2 is
returned.

C code:
#include<stdio.h>
int numberOfCarries(int num1 , int num2)
{
int carry = 0, sum, p, q, count = 0;
while((num1!=0)&&(num2!=0))
{
p = num1 % 10;
q = num2 % 10;
sum = carry + p + q;
if(sum>9)
{
carry = 1;
count++;
}
else
{
carry = 0;
}
num1 = num1/10;
num2 = num2/10;
}
while(num1!=0)
{
p=num1%10;
sum=carry+p;
if(sum>9)
{
carry=1;
count++;
}
else
carry=0;
num1=num1/10;
}
while(num2!=0)
{
q=num2%10;
sum=carry+q;
if(sum>9)
{
carry=1;
count++;
}
else
carry=0;
num2=num2/10;
}
return count;
}
int main()
{
int x, y, a;
scanf("%d",&x);
scanf("%d",&y);
a = numberOfCarries(x, y);
printf("%d",a);
return 0;
}
Java code:
import java.util.*;
class Main
{
public static int numberOfCarries (int num1, int num2)
{
int count = 0;
int temp1 = num1, temp2 = num2;
int rem = 0;
while (temp1 != 0 && temp2 != 0)
{
int d1 = temp1 % 10, d2 = temp2 % 10;
if ((d1 + d2 + rem) > 9)
{
count++;
int sum = d1 + d2 + rem;
sum = sum / 10;
rem = sum;
}
temp1 = temp1 / 10;
temp2 = temp2 / 10;
}
while(temp1!=0)
{
int d1=temp1%10;
if((d1+rem)>9)
{
count++;
int sum=d1+rem;
sum=sum/10;
rem=sum;
}
temp1=temp1/10;
}
while(temp2!=0)
{
int d2=temp2%10;
if((d2+rem)>9)
{
count++;
int sum=d2+rem;
sum=sum/10;
rem=sum;
}
temp2=temp2/10;
}
return count;
}
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
int num1 = sc.nextInt ();
int num2 = sc.nextInt ();
System.out.println (numberOfCarries (num1, num2));
}
}
Python code:
def NumberOfCarries(n1,n2):
count=0
carry = 0
if len(n1) <= len(n2):
l= len(n1)-1
else:
l = len(n2)-1
for i in range(l+1):
temp = int(n1[l-i])+int(n2[l-i])+carry
if len(str(temp))>1:
count+=1
carry = 1
else:
carry = 0
return count+carry
n1=input()
n2=input()
print(NumberOfCarries(n1,n2))

7. Problem Statement:
You are given a function,
void *ReplaceCharacter(Char str[], int n, char ch1, char ch2);

The function accepts a string ‘ str’ of length n and two characters


‘ch1’ and ‘ch2’ as its argument.
Implement the function to modify and return the string ‘ str’ in such a
way that all occurrences of ‘ch1’ in the original string are replaced by
‘ch2’ and all occurrences of ‘ch2’ in the original string are replaced by
‘ch1’.

Assumption: String Contains only lower-case alphabetical letters.

Note:
Return null if the string is null.
If both characters are not present in the string or both of them are
the same , then return the string unchanged.

Example:
Input:
Str: apples
ch1:a
ch2:p

Output:
paales
Explanation:
‘A’ in the original string is replaced with ‘p’ and ‘p’ in the original string
is replaced with ‘a’, thus output is paales.

C code:
#include <stdio.h>
#include <string.h>
void *ReplaceCharacter(char str[], int n, char ch1, char ch2)
{
int i;
for(i=0; i<n ; i++)
{
if(str[i]==ch1)
{
str[i]=ch2;
}
else if(str[i]==ch2)
{
str[i]=ch1;
}
}
printf("%s",str);
}
int main()
{
char a[100];
char b, c;
int len;
scanf("%s",a);
scanf("%s",&b);
scanf("%s",&c);
len = strlen(a);
ReplaceCharacter(a, len, b, c);
return 0;
}
Java code:
import java.util.*;
class Solution
{
public static void replaceChar (String str, char ch1, char ch2)
{
String res = "";
for (int i = 0; i < str.length (); i++)
{
if (str.charAt (i) == ch1)
res = res + ch2;
else if (str.charAt (i) == ch2)
res = res + ch1;
else
res = res + str.charAt (i);
}
System.out.println (res);
}
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
String str = sc.next ();
char ch1 = sc.next ().charAt (0);
char ch2 = sc.next ().charAt (0);
replaceChar (str, ch1, ch2);
}
}
Python code:
def ReplaceCharacter (user_input, str1, str2):
result = ''
if user_input != None:
result = user_input.replace(str1, '*').replace(str2, str1).replace('*', str2)
return result
return 'Null'
user_input = input()
str1, str2 = map(str,input().split())
print(ReplaceCharacter(user_input, str1, str2))

8. Problem Statement:

You are given a function:


Int MaxExponents (int a , int b);
You have to find and return the number between ‘a’ and ‘b’ ( range
inclusive on both ends) which has the maximum exponent of 2.
The algorithm to find the number with maximum exponent of 2
between the given range is

Loop between ‘a’ and ‘b’. Let the looping variable be ‘i’.

Find the exponent (power) of 2 for each ‘i’ and store the number
with maximum exponent of 2 so far in a variable , let say ‘max’.
Set ‘max’ to ‘i’ only if ‘i’ has more exponent of 2 than ‘max’.

Return ‘max’.

Assumption: a < b
Note: If two or more numbers in the range have the same exponents
of 2 , return the small number.
Example
Input:
7
12

Output:
8

Explanation:
Exponents of 2 in:
7-0
8-3
9-0
10-1
11-0
12-2
Hence the maximum exponent if two is of 8.

C++ code:
#include<bits/stdc++.h>
using namespace std;
int count(int n){
int c = 0;
while (n % 2 == 0 && n != 0){
c++;
n = n / 2;
}
return c;
}
int maxExponents(int a, int b){
int max = 0, num = 0, ans;
for (int i = a; i <= b; i++){
int temp = count(i);
if (temp > max){
max = temp;
num = i;
}
}
return num;
}
int main ()
{
int a, b;
cin >> a>>b;
cout<<maxExponents(a, b);
return 0;
}
Java code:
import java.util.*;
class Main
{
public static int count (int i)
{
int count = 0;
while (i % 2 == 0 && i != 0)
{
count += 1;
i = i / 2;
}
return count;
}
public static int maxExponents (int a, int b)
{
int max = 0, num = 0, ans;
for (int i = a; i <= b; i++)
{
int temp = count (i);
if (temp > max)
{
max = temp;
num = i;
}
}
return num;
}
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
int a = sc.nextInt ();
int b = sc.nextInt ();
System.out.println (maxExponents(a, b));
}
}
Python code:
def countExponents(i):
count = 0
while i%2 == 0 and i != 0:
count+=1
i = i//2
return count
def maxExponents(a, b):
maximum, number = 0, a
for i in range(a,b):
temp = countExponents(i)
if temp>maximum:
maximum, number = temp, i
return number
a, b = map(int,input().split())
print(maxExponents(a, b))

9. Problem Statement:
You are required to input the size of the matrix then the elements of
matrix, then you have to divide the main matrix in two sub matrices
(even and odd)
in such a way that element at 0 index will be considered as even and
element at 1st index will be considered as odd and so on.
Then you have sort the even and odd matrices in ascending order
then print the sum of second largest number from both the matrices.
Example:
enter the size of array : 5
enter element at 0 index : 3
enter element at 1 index : 4
enter element at 2 index : 1
enter element at 3 index : 7
enter element at 4 index : 9
Sorted even array : 1 3 9
Sorted odd array : 4 7
7

C code:
#include <stdio.h>
int main ()
{
int arr[100];
int length, i, j, oddlen, evenlen, temp, c, d;
int odd[50], even[50];
printf ("enter the length of array : ");
scanf ("%d", &length);
for (i = 0; i < length; i++)
{
printf ("Enter element at %d index : ", i);
scanf ("%d", &arr[i]);
}
if (length % 2 == 0)
{
oddlen = length / 2;
evenlen = length / 2;
}
else
{
oddlen = length / 2;
evenlen = (length / 2) + 1;
}
for (i = 0; i < length; i++) // seperation of even and odd array
{
if (i % 2 == 0)
{
even[i / 2] = arr[i];
}
else
{
odd[i / 2] = arr[i];
}
}
for(i = 0; i < evenlen - 1; i++) // sorting of even array
{
for (j = i + 1; j < evenlen; j++)
{
temp = 0;
if (even[i] > even[j])
{
temp = even[i];
even[i] = even[j];
even[j] = temp;
}
}
}
for (i = 0; i < oddlen - 1; i++) // sorting of odd array
{
for (j = i + 1; j < oddlen; j++)
{
temp = 0;
if (odd[i] > odd[j])
{
temp = odd[i];
odd[i] = odd[j];
odd[j] = temp;
}
}
}
printf ("\nSorted even array : "); // printing even array
for (i = 0; i < evenlen; i++)
{
printf ("%d ", even[i]);
}
printf ("\n");
printf ("Sorted odd array : "); // printing odd array
for (i = 0; i < oddlen; i++)
{
printf ("%d ", odd[i]);
}
printf ("\n\n%d", even[evenlen - 2] + odd[oddlen-2]); // printing final result
}
C++ code:
#include <iostream>
using namespace std;
int main ()
{
int arr[100];
int length, i, j, oddlen, evenlen, temp, c, d;
int odd[50], even[50];
cout << "enter the length of array : ";
cin >> length;
for (i = 0; i < length; i++)
{
cout << "Enter element at "<<i<<" index : ";
cin >> arr[i];
}
if (length % 2 == 0)
{
oddlen = length / 2;
evenlen = length / 2;
}
else
{
oddlen = length / 2;
evenlen = (length / 2) + 1;
}
for (i = 0; i < length; i++) // seperation of even and odd array
{
if (i % 2 == 0)
{
even[i / 2] = arr[i];
}
else
{
odd[i / 2] = arr[i];
}
}
for (i = 0; i < evenlen - 1; i++) // sorting of even array
{
for (j = i + 1; j < evenlen; j++)
{
temp = 0;
if (even[i] > even[j])
{
temp = even[i];
even[i] = even[j];
even[j] = temp;
}
}
}
for (i = 0; i < oddlen - 1; i++) // sorting of odd array
{
for (j = i + 1; j < oddlen; j++)
{
temp = 0;
if (odd[i] > odd[j])
{
temp = odd[i];
odd[i] = odd[j];
odd[j] = temp;
}
}
}
cout << "\nSorted even array : "; // printing even array
for (i = 0; i < evenlen; i++)
{
cout << even[i]<<" ";
}
cout << "\n";
cout << "Sorted odd array : "; // printing odd array
for (i = 0; i < oddlen; i++)
{
cout << odd[i]<<" ";
}
cout<<endl;
cout << even[evenlen - 2] + odd[oddlen-2]; // printing final result
}
Java code:
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter size of array : ");
int arrsize = sc.nextInt();
int[] main = new int[arrsize];
ArrayList<Integer> even = new<Integer>ArrayList();
ArrayList<Integer> odd = new<Integer>ArrayList();
System.out.println("Enter "+arrsize+" Elements");
for (int i = 0; i < arrsize; i++)
main[i] = sc.nextInt();
for (int i = 0; i < arrsize; i++) {
if(i%2==0)
even.add(main[i]);
else
odd.add(main[i]);
}
Collections.sort(even);
Collections.sort(odd);
System.out.println("Sorted even array ");
for (int e : even)
System.out.print(e+" ");
System.out.println();
System.out.println("sorted odd array ");
for (int e : odd)
System.out.print(e+" ");
System.out.println();
int evensec=even.get(even.size()-2);
int oddsec=odd.get(odd.size()-2);
System.out.println("Second Largest Element in Even List is:"+evensec);
System.out.println("Second Largest Element in Odd List is:"+oddsec);
System.out.println("Sum Of Second Largest Element Of Odd and Even List:"+(evensec+oddse
}
}
Python code:
array = []
evenArr = []
oddArr = []
n = int(input("Enter the size of the array:"))
for i in range(0,n):
number = int(input("Enter Element at {} index:".format(i)))
array.append(number)
if i % 2 == 0:
evenArr.append(array[i])
else:
oddArr.append(array[i])
evenArr = sorted(evenArr)
print("Sorted Even Array:", evenArr[0:len(evenArr)])
oddArr = sorted(oddArr)
print("Sorted Odd Array:", oddArr[0:len(oddArr)])
print(evenArr[1] + oddArr[1])

10. Problem Statement:


You are required to implement the following function:
Int Calculate(int m, int n);
The function accepts 2 positive integers ‘m’ and ‘n’ as its
arguments.You are required to calculate the sum of numbers
divisible both by 3 and 5, between ‘m’ and ‘n’ both inclusive and
return the same.

Note:
0 < m <= n

Example
Input:
m : 12
n : 50

Output
90

Explanation:
The numbers divisible by both 3 and 5, between 12 and 50 both
inclusive are {15, 30, 45} and their sum is 90.
C code:
#include <stdio.h>
int Calculate (int, int);
int main ()
{
int m, n, result;
// Getting Input
printf ("Enter the value of m : ");
scanf ("%d", &m);
printf ("Enter the value of n : ");
scanf ("%d", &n);
result = Calculate (n, m);
// Getting Output
printf ("%d", result);
return 0;
}
int Calculate (int n, int m)
{
// Write your code here
int i, sum = 0;
for (i = m; i <= n; i++)
{
if ((i % 3 == 0) && (i % 5 == 0))
{
sum = sum + i;
}
}
return sum;
}
C++ code:
#include<iostream>
using namespace std;
int Calculate (int, int);
int main ()
{
int m, n, result;
// Getting Input
cout << "Enter the value of m :";
cin >> m;
cout << "Enter the value of n :";
cin >> n;
result = Calculate (n, m);
// Getting Output
cout << result;
return 0;
}
int Calculate (int n, int m)
{
int i, sum = 0;
for (i = m; i <= n; i++)
{
if ((i % 3 == 0) && (i % 5 == 0))
{
sum = sum + i;
}
}
return sum;
}
Java code:
import java.util.Scanner;
public class Main
{
int Calculate (int m, int n)
{
int sum = 0;
for (int i = m; i <= n; i++)
if ((i % 3 == 0) && (i % 5 == 0))
sum = sum + i;
return sum;
}
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
System.out.println ("Enter the value of m and n");
int m = sc.nextInt ();
int n = sc.nextInt ();
Main q = new Main ();
int result = q.Calculate (m, n);
System.out.println (result);
}
}
Python code:
m = int(input("M:"))
n = int(input("N:"))
def calculate(m, n):
sum = 0
for i in range(m,n+1,1):
if i%3 == 0 and i%5 == 0:
sum = sum + i
print(sum)
calculate(m,n)

Practice interview coding questions by taking our placement mock test


from topics like Arrays, Recursion, Sorting, Searching, Linked List, and much
more.

Accenture Coding Questions FAQs(Frequently Asked


Questions)
1. In which programming language we should solve all the Accenture
coding questions?

The students can use any of the programming languages from C,


C++, Java, and Python to solve coding questions.

2. Which Platform does Accenture use to conduct the exam?

Accenture uses the Cocubes platform to conduct all its exams and
assessments.

3. How many rounds are there in the Accenture Selection Process?

There are a total of 3 selection rounds conducted by Accenture to


get the desired dream job.

4. Is it compulsory to clear the Accenture Coding Round?

Yes, it’s compulsory to clear the Accenture Coding Round.

5. Do Accenture Coding questions get repeated each year?

Yes, our students say that they got some repeated questions which
were asked in the previous exams. Mainly the questions from an
inheritance, polymorphism, and constructors in C++. And from C the
questions from the topic scope of variables, pointers, and structures
get repeated.

6. Does Accenture have any sectional cut-off?

Yes the Accenture has sectional as well as subsectional cut-offs. The


candidate who clears both the cut off will be eligible for the next
rounds.

7. What is the difficulty level of Coding questions asked in the exam?

The first question is of medium difficulty level and the second


question is very difficult.

This article tried to discuss the Accenture Coding Questions Hope this blog
helps you understand and solve the problem. To Practice more problems
you can check out Mock Tests at Prepbytes.

You can also refer the following links:

1. Here’s What They are Going To Test You For In Accenture Interview

FOLLOW US QUICK LINKS


LATEST ARTICLES
Interview
Integrated Services Digital Network
Notes
(ISDN)
Mock Tests
CONTACT US VLAN ACL (VACL) in Computer Networks Placement
Inter-VLAN Routing Using a Layer 3 Programs
+91-7969 0021 11
Switch Coding
support@prepbytes.com
Access and Trunk Ports in Computer Courses
Networks About Us
Role-Based Access Control (RBAC) in Blog
Computer Networks

POPULAR ARTICLES

Characteristics of Computer Systems

Difference Between ANN and BNN

Integrated Services Digital Network


(ISDN)

Inter-VLAN Routing Using a Layer 3


Switch

Display Processor in Computer


Graphics

Copyright©2023 Privacy Policy Refund Policy Terms of Use

You might also like