Accenture
Accenture
Coding 2 Ques
Coding 2 Ques
Before being passed to your function, nums is rotated at an unknown pivot index k (0 <= k < nums.length)
such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-
indexed). For example, [0,1,2,4,4,4,5,6,6,7] might be rotated at pivot index 5 and become
[4,5,6,6,7,0,1,2,4,4].
Given the array nums after the rotation and an integer target, return true if target is in nums, or false if it is
not in nums.
Example 1:
Output: true
Example 2:
Output: false
(LeetCode 81)
Q2. Given an m x n matrix, return all elements of the matrix in spiral order.
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
Constraints:
● m == matrix.length
● n == matrix[i].length
● 1 <= m, n <= 10
● -100 <= matrix[i][j] <= 100
(LeetCode 54)
Adam Number :
Test Case:
Input: 12
Explanation: 12*12=>144
=>21 =>12
Q. Reverse individual words
Test Case 1:
Test Case 1:
other number. (A proper divisor of a number is a positive factor of that number other than the number itself.
Given two Numbers A and B, find whether they are Amicable Numbers or not. Print 1 if they are Amicable else 0.
Example 1:
Input:
A = 220 , B = 284
Output: 1
Explanation:
Proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110. Sum of these is
284. Proper divisors of 284 are 1, 2, 4, 71 and 142 with sum 220.
FizzBuzz
FizzBuzz is a childhood game that iterates over a range of numbers and uses simple logic to decide
whether to say a "Fizz,"
Example:
Input:
n = 15
Output:
["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
Given an array and the task is to print the additive primes in an array. Additive primes: Primes such that the sum
of their digits is also a prime, such a
Input Format :The first line of input consists of integer N The next line contains the N integers
Output Format: The output prints the additive primes in the given array Refer to the sample output for
specifications.
Sample Input
2 4 6 11 12 18 7
Sample Output:
2 11 7
Sample Input
14563
Sample Output:
153
Q3.Given an array nums of distinct integers, return all the possible permutations. You can return the
answer in any order.
Example 1:
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Example 2:
Output: [[0,1],[1,0]]
Constraints:
For example, for arr = [1,2,3], the following are all the permutations of arr: [1,2,3], [1,3,2], [2, 1, 3],
[2, 3, 1], [3,1,2], [3,2,1].
The next permutation of an array of integers is the next lexicographically greater permutation of its
integer. More formally, if all the permutations of the array are sorted in one container according to
their lexicographical order, then the next permutation of that array is the permutation that follows it
in the sorted container. If such arrangement is not possible, the array must be rearranged as the
lowest possible order (i.e., sorted in ascending order).
While the next permutation of arr = [3,2,1] is [1,2,3] because [3,2,1] does not have a lexicographical
larger rearrangement.
The replacement must be in place and use only constant extra memory.
Example 1:
Output: [1,3,2]
Example 2:
Output: [1,2,3]
Constraints:
(LeetCode 31)
Problem Statement :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:
Input:
r: 7
unit: 2
n: 8
arr: 2 8 3 5 7 4 1 2
Output:
4
Problem Statement : The Binary number system only uses
two digits, 0 and 1 and number system can be called binary
string. You are required to implement the following function:
Test Case:
Input:
str: 1C0C1C1A0B1
Output:
1
Problem Statement 3:
You are given a function. int CheckPassword(char str[], int n);
The function accepts string str of size n as an argument.
Implement the function which returns 1 if given string str is
valid password else 0.
str is a valid password if it satisfies the below conditions.
– At least 4 characters
– At least one numeric digit
– At Least one Capital Letter
– Must not have space or slash (/)
– Starting character must not be a number
Assumption:
Input string will not be empty.
Test Case 1:
Input :
aA1_67
Output :
1
Test Case 2:
Input :
a987 abC012
Output :
0
Problem Statement 4:
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’.
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.
Problem Statement : 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.
Input
arr:3 2 1 7 5 4
Output
7
Explanation :
● Second largest among even position elements(1 3 5) is 3
● Second smallest among odd position element is 4
● Thus output is 3+4 = 7
Input
arr:1 8 0 2 3 5 6
Output
8
Problem Statement :Implement the following Function
def ProductSmallestPair(sum, arr)
The function accepts an integers 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
Test Case :
Input
Sum:9
size of Arr = 7
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
Sample Input
sum:4
size of Arr = 6
Arr:9 8 3 -7 3 9
Sample Output
-21
Problem Statement :N-base notation is a system for writing
numbers that uses only n different symbols, This 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:
1. Divide the decimal number by n,Treat the division as the
integer division
2. Write the the remainder (in n-base notation)
3. Divide the quotient again by n, Treat the division as
integer division
4. Repeat step 2 and 3 until the quotient is 0
5. The n-base value is the sequence of the remainders from
last to first
Assumption:
1 < n < = 36
Test Case :
Input
n: 12
num: 718
Output
4BA
Explanation:
num Divisor quotient remainder
718 12 59 10(A)
59 12 4 11(B)
4 12 0 4(4)
Sample Input
n: 21
num: 5678
Sample Output
CI8
Problem Statement : Implement the following functions.a
char* MoveHyphen(char str[],int n);
The function accepts a string “str” of length ‘n’, that contains
alphabets and hyphens (-). Implement the function to move
all hyphens(-) in the string to the front of the given string.
NOTE:- Return null if str is null.
Example :-
● Input:
○ str.Move-Hyphens-to-Front
● Output:
○ —MoveHyphenstoFront
Explanation:-
The string “Move-Hyphens -to-front” has 3 hyphens (-), which
are moved to the front of the string, this output is “—
MoveHyphen”
Sample Input
● Str: String-Compare
Sample Output-
● -StringCompare
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 functions 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
Test Case:
● 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.
Sample Input
Num 1: 23
Num 2: 563
Sample Output
0
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 arguments . Implement the
function to modify and return the string ‘ str’ in such a way
that all occurrences of ‘ch1’ in original string are replaced by
‘ch2’ and all occurrences of ‘ch2’ in original string are
replaced by ‘ch1’.
Assumption: String Contains only lower-case alphabetical
letters.
Note: 1)Return null if string is null
Test Case:
● Input:
○ Str: apples
○ ch1:a
○ ch2:p
● Output:
○ paales
Explanation:
‘A’ in original string is replaced with ‘p’ and ‘p’ in original
string is replaced with ‘a’, thus output is paales.
Test Case:
● Input:
○ Str: apples
○ ch1:a
○ ch2:p
● Output:
○ paales
Explanation:
‘A’ in original string is replaced with ‘p’ and ‘p’ in original
string is replaced with ‘a’, thus output is paales.
Problem Statement:
You are required to implement the following function.
Int OperationChoices(int c, int n, int a , int b )
The function accepts 3 positive integers ‘a’ , ‘b’ and ‘c ‘ as its
arguments. Implement the function to return.
● ( a+ b ) , if c=1
● ( a – b ) , if c=2
● ( a * b ) , if c=3
● (a / b) , if c =4
Assumption : All operations will result in integer output.
Test Case :
● Input
○ c :1
○ a:12
○ b:16
● Output:
○ Since ‘c’=1 , (12+16) is performed which is equal to
28 , hence 28 is returned.
Test Case :
● Input
○ c :1
○ a:12
○ b:16
● Output:
○ Since ‘c’=1 , (12+16) is performed which is equal to
28 , hence 28 is returned.
Sample Input
c:2
a : 16
b : 20
Sample Output
-4
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
1. Loop between ‘a’ and ‘b’. Let the looping variable be ‘i’.
2. Find the exponent (power) of 2 for each ‘i’ and store the
number with maximum exponent of 2 so faqrd in a
variable , let say ‘max’. Set ‘max’ to ‘i’ only if ‘i’ has more
exponent of 2 than ‘max’.
3. Return ‘max’.
Note: If two or more numbers in the range have the same
exponents of 2 , return the small number.
Test Case :
● Input:
○ 7
○ 12
● Output:
○ 8
Explanation:
Exponents of 2 in:
7-0
8-3
9-0
10-1
11-0
12-2
Hence maximum exponent if two is of 8.
Problem Statement: You are required to implement the
following function:
Int Calculate(int m, int n);
The function accepts 2 positive integer ‘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
Test Case :
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.
Sample Input
m : 100
n : 160
Sample Output
510
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