ACCENTURE

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 24

Number Integration and Disintegration

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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 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 the 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+8=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 third 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 = [26][9][7][1] = 26971
Note1: Please note that the number of passes required to process the given number may vary
depending upon the constitution of the mobile number.

Note2: Also note that. 0 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][8][7] = 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

______________________________________________________________________
You are required to implement the following function: Int CountDigitOccurrences(int l, int u , int x):

The function accepts 3 positive integers ‘T’,’u’ and ‘x’ as its arguments. You are required to calculate
the number of occurrences of a digit ‘x’ in the digits of numbers lying in the range between ‘l’ and ’u’
both inclusive, and return the same.

Note:

• l<=u

• 0<=x<=9

Example: Input:

l: 2 u: 13 x: 3

Output: 2

Explanation:

The number of occurrence of digit 3 in the digits of numbers lying in the range [2, 13] both inclusive
is 2, i.e., {3, 13}, hence 2 is returned

Sample input:

l: 1

u: 30 x: 2

Sample output:

12

____________________________________________________________________

Question :

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 eg. if 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

differnce between 7 and 6 is 1

So RSF of 376 = 41

41 is a two digit number , hence RSF of 6928 = 41

Let's see another example

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: inputl will always be >= 100

Note2: Note that while concatenating the differences, we are expected to use the absolute values
(non-negative)

Note3: The input values for all test cases in this program have been designed such that their two-
digit RSF is possible

____________________________________________________________________________

Complete the following function

int makePin(int seed1, int seed2 , int seed3)

100 <= seed1,sedd2,seed3 <= 999

seed1 be ABC

seed2 be MNO

seed3 be PQR
PIN is 4 digit made of

Largest digit of ALL (A,B,C,M,N,O,P,Q,R)

Third digit is largest of (A,M,P)

Second digit is Largest of (B,N,Q)

First digit is Largest of (C, O, R)

Calculate the PIN and return the value

123

345

289

9389

_____________________________________________________________________

Formula

~~~~~~~~

You are given a function,

Int FindSumOfRemainder(int n, int div);

The function take two integers ‘n’ and ’div’ as input implement the function such that it returns the
sum of remainders given by numbers from 1 to ‘n’ (both inclusive) on being divided by ‘div’

Example: Input:

12

Output:

18

Explanation:

The sum of remainders left by numbers from 1 and 12 on being divided by 4 is

1+2+3+0+1+2+3+0+1+2+3+0=18

Sample Input:

15

5
Sample Output:

30

1+2+3+4+0+1+2+3+4+0+1+2+3+4+0

______________________________________________________________________

Breaking an Integer to get Maximum Product

Given an number n, the task is to be broken in such a way that multiplication of its parts is
maximized.

Input : n = 10

Output : 36

exp:

10 = 4 + 3 + 3 and 4 * 3 * 3 = 36

is maximum possible product.

Input : n = 8

Output : 18

8 = 2 + 3 + 3 and 2 * 3 * 3 = 18

is maximum possible product.

_______________________________________________________________________

Arrays

~~~~~~

Problem statement

You are given a function,

int ElementsAndIndices(int arr, int n)

The function takes an integer array 'arr' of size 'n' as its arguments. Implement the function to find
and return the number of array elements which are equal to their index value in array i.e. arr[k] = k,

0 <= k <n. Note:

Indexing starts from 0.

Return -1 if 'arr' is empty or None in case of python


Example: Input:

01 2345678 9

10 1 12 3 5 8 9 7 12 23

Output:

Explanation:

Index Element

0 10

1 1

2 12

3 3

4 5

5 8

6 9

7 7

8 12
9 23

Elements at index {1, 3, 7} are equal to their index values (1, 3, 7} respectively. Since these are 3
elements, thus, output is 3.

Sample Input:

-3 0 1 3 5 7

Sample Output:

________________________________________________________________________

Strings

~~~~~~~

English alphabet contains 26 letters. 5 of them are vowels {a, e , I, o, u} and rest 21 are consonants.

Implement the following function:

int Difficulty(char str[], int len);

The function accepts a string ‘str’ of length ‘len’ as its argument. String ‘str’ contains space separated
words. A word is ‘hard’ if at least one of the 2 conditions hold true:

1. There are more consonants than vowels.

2. There are 3 consecutive consonants.

Otherwise, the word is ‘easy’. Implement the function to find the difficulty quotient of the string and

return the same.

Difficulty quotient = (5*hard) – (2*easy) where, hard = count of hard words in the string and, easy =
count of easy words in the string

Note:

1. Return 0 if string is null. (None, in case of python).

2. ‘str’ contains only lower-case alphabets and spaces.

3. Output lies within integral range. Example:

Input:

str: qiewldoaa life ace by fantasy

H E E H H
Output:

11

Int Difficulty(char str[], int len); Int main()

//Input read from STDIN

int result = Difficulty(str, len);

//Value of result printed to STDOUT

return 0;

Write your code below…

Int Difficulty(char str[], int len)

/* Write your code here*/

_________________________________________________________________________

Problem statement You are given a function, char* ReverseString(char* s);

The function accepts a string 's that contains alphabets

('a' to 'z' and 'A' to 'Z') along with other characters. Reverse this string 's' in such a way that only
alphabets are reversed and other characters are not affected.

Note:

1. Return null if s is null.


2. Null refers to None in case of Python. Example:

Input: a^bk$c Output: c^kb$a Explanation:

Characters '' and '$' are at their original positions while all alphabets got reversed.

Sample Input:

$A5b*ntp^

Sample Output:

$p5t*nbA^

____________________________________________________________________________

PASSWORD : a secret word or phrase that must be used to gain admission to a place.

Passwords provide the first line of defense against unauthorized access to your computer and
personal information. The stronger your password, the more protected your computer will be from
hackers and malicious software. You should maintain strong passwords for all accounts on your
computer. At the same time remembering the passwords also should be an easy task, this problem
depicts one such method.

Example : Mobile Number is 6383236619 and the password is ttetsnosst

Let us understand the process

Step 1: Form a string with the first letter of the digit

6 3 8 3 2 3 6 6 1 9

six three eight three two three six six one nine

stetttsson

Reverse the first half

ttetstsson

Reverse the second half

ttetsnosst

Input : 10 Digit Mobile Number

Output : string of 10 characters in lower case

__________________________________________________________________________________
__

Loose Lines to Tight Lines

A common type of text alignment in print media is "justification", where the spaces between words
and between glyphs or letters are stretched or compressed in order to align both the left and right
ends of consecutive lines of text. When using justification, it is customary to treat the last line of a
paragraph separately by simply left or right aligning it, depending on the language direction. Lines in
which the spaces have been stretched beyond their normal width are called loose lines, while those
whose spaces have been compressed are called tight lines.

Input : String Maximum of 200 Characters

Output : String - with all additional spaces removed

Sample Input: "The word learning is used routinely in discussions about teaching in higher
education"

Output : The word learning is used routinely in discussions about teaching in higher education

_______________________________________________________________________

Eight Queen PUZZLE :

The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard so that
no two queens threaten each other; thus, a solution requires that no two queens share the same
row, column, or diagonal. The eight queens puzzle has 92 distinct solutions. If solutions that differ
only by the symmetry operations of rotation and reflection of the board are counted as one, the
puzzle has 12 solutions.

The task given to us is the representation of the status of 8 Queens placed on a Chess Board. The
Input is an 8 X 8 matrix - made of 0's and 1's, where 0 represent an empty square and 1 represents a
square occupied by a queen. We need to verify if the eight queens are safe

Sample Input and Output:

Input :

00010000

00000010

00100000

00000001

01000000

00001000

10000000

00000100
Output : Valid

Input :

10000000

00010000

01000000

00001000

00100000

00000001

00000100

00100000

Output : Invalid

_________________________________________________________________________________

Prime : Dictionary says the meaning is "Of first importance , main".Primes are central in number
theory because of the fundamental theorem of arithmetic - every natural number greater than 1 is
either a prime itself or can be factorized as a product of primes that is unique up to their order.The
property of being prime is called primality.

Prime Numbers are made by themselves

Composite Numbers are made as product of Prime Numbers

The task of yours is to find if a name is Prime - how do we test a set of characters to be prime, let us
perform a calculation, sum of the ASCII values of the characters of the name, if the sum is Prime -
Name is to be considered as a Prime Name else it is not.

Input : String of maximum 100 characters

Output : Prime Name / NOT a Prime Name

Note : Parameswaran Hariharan is a valid input - do not consider the ASCII value of any embedded
spaces.

__________________________________________________________________________________
___

You have a robot standing on the origin of x-axis. The robot can only follow these type of

instructions, i.e., ‘L’, ‘R’, and a digit.


For every ith instruction, the position ‘p’ of the robot is modified as follows:

1.L: move one unit left (decrease p by 1)

2.R: move one unit right (increase p by 1)

3.A digit j : do same as jth instruction (where j is a digit which is less than i). You are given a function,

Int FindPosition(char* instr);

The function accepts a string ‘instr’ as its argument. Implement the function to find and return the

final position of robot after processing all instructions of ‘instr’.

Assumptions:

1.Initially the position of the robot is 0.

2.Character L and R provided in instructions will be in uppercase.

3.Instructions will contain only the characters L,R, and a digit between 0 to 9. Example:

Input:

Instr: LR1RL2

Output:

Int FindPosition(char* instr);

int main()
{

//Input read from STDIN

int result= FindPosition(instr);

//Value in result printed to STDOUT

return 0;

Write your code below…

int FindPosition(char* instr)

/* Write your code here. */

______________________________________________________________________

Question : 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 —

Stepl - 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-id should be generated as below —Last Letter of the smaller name + Entire word of
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

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

Stepl - 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 smaller name + Entire 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 + Entire 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 alphabets in the user-id. So, user-id = YrAJIV75

Example-2 - If the participant's details are as below -

First_Name = Manoj Last_Name = Kumar PIN = 561327 N = 2

Step1 - Length of First_Name is equal to the Length of Last_Name. Alphabetically, 'Kumar' appears
earlier than 'Manoj' (by comparing alphabetic positions of K and 'M') so the Smaller Name is 'Kumar'
and the Longer Name is 'Manoj'

Step2 - The user-id will be = Last Letter of the smaller name + Entire 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 'Kumar + Entire word in "Manor + 2nd Digit
of PIN from left + 2 "d

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 'Kumar + Entire word in "Manor +
2nd Digit of PIN from left + 2 "d Digit of PIN from right = r + Manoj + 6 + 2 Therefore. user-id =
rManoj62

Step3 - Toggle the alphabets in the user-id. So. user-id = RmANOJ62


Example-3 - If the participant's details are as below -First_Name = Kumud Last_Name = Kumar PIN =
561327 N = 2

Step1 - Length of First_Name is equal to the Length of Last_Name. Alphabetically, 'Kumar' appears
earlier than 'Kumud' (by comparing alphabetic positions of 'Kum a' and 'Kum u') so the Smaller Name
is 'Kumar" and the Longer Name is "Kumud"

Step2 - The user-id will be = Last Letter of the smaller name + Entire 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 "Kumar- + Entire word in "Kumud' + 2nd
Digit of PIN from left + 2 rd Digit of PIN from right = r + Kumud + 6 + 2 Therefore. user-id = rKumud62

Step3 - Toggle the alphabets in the user-id. So. user-id = RkUMUD62

You are part of Joseph's team and he has asked you to write a program (method) to generate the
participant's user-id using the above rules.

_________________________________________________________________________

Patterns

~~~~~~~~

Write a 'C' Program to print the following pattern

3<= N <= 20

N=5

01 02 03 04 05

16 06

15 07

14 08

13 12 11 10 09

__________________________________________________________________________________
________

Maximum subarray sum

Accept array of N integers from the user and find the maximum sum of consecutive elements.

"Input contains two line.

First line of the input contains N value.

Second line contains N number of array elements."

display the maximum sum.

"1<= N <=10000
1<= arr[ele] <= 10^9"

"#include<stdio.h>

#include<limits.h>

int main()

int N;

scanf(""%d"",&N);

int arr[N];

for(int ind=0;ind<N;scanf(""%d"",&arr[ind++]));

int max=INT_MIN,maxsum=0;

for(int ind=0;ind<N;ind++)

maxsum+=arr[ind];

if (max<maxsum)

max=maxsum;

if (maxsum<0)

maxsum=0;

printf(""%d"",max);

return 0;

}"

______________________________________________________________________________

Anagram

Given two strings as a input, Find whether the strings are Anagram are Not a Anagram.

Accept Two strings as a input from the user.

Display Anagram/Not a Anagram.

The strings may contains a characters upto 1000.

"#include<stdio.h>

#include<stdlib.h>
#include<string.h>

int cchar(const void *c , const void *n)

const char *a=c,*b=n;

return (*a)-(*b);

int main()

char str1[1000];

char str2[1000];

scanf(""%s%s"",str1,str2);

qsort(str1,strlen(str1),sizeof(char),cchar);

qsort(str2,strlen(str2),sizeof(char),cchar);

printf(""%s"",(strcmp(str1,str2))?""Not a Anagram"":""Anagram"");

}"

__________________________________________________________________________________
_____

Factorial

Given the integer value as a input. Return the factorial value of a given number as a string.

Accept the integer as a input from the user.

display the factorial of the given number.

1<= N <=100

"#include<stdio.h>

#include<stdlib.h>

#include<string.h>

char* factorial(int n)

char *result=(char*)calloc(10000,sizeof(char));

result[0] = '1';
int size = 1;

for(int i=2;i<=n;i++)

int carry = 0;

for (int ind=0;ind<size;ind++)

int product = (result[ind]-'0') * i + carry;

result[ind] = (product % 10)+'0';

carry = product / 10;

while (carry)

result[size] = (carry % 10)+'0';

carry /= 10;

size++;

return result;

int main()

int num;

scanf(""%d"",&num);

char *result=factorial(num);

for(int i=strlen(result)-1;i>=0;printf(""%c"",result[i--]));

return 0;

}"

__________________________________________________________________________________
_______________________

Maximum occuring character

"Given the group of characters as a input. Your work is to find the character which is having highest
frequency based on occurance.
Note:- if more then one characters having the same maximum frequency display 0 as output;"

Accept the string as a input from the user.

display the character with maximum occurance or display 0.

The string may contains a characters upto 10000.

"#include<stdio.h>

char check(char * str)

int max=0,maxcount=0;

char ref;

for(int ch=0;str[ch];ch++)

int count=1;

for(int ind=ch+1;str[ind];ind++)

if(str[ch]==str[ind])

count++;

if(count>max) max=count,ref=str[ch],maxcount=0;

if(count!=0 && count==max) maxcount++,ref=str[ch];

return (maxcount==1)?ref:'0';

int main()

char str[10000];

scanf(""%[^\n]"",str);

printf(""%c"",check(str));

return 0;
}"

__________________________________________________________________________________
_______________________________

1) Ravi wants to install an elevator at his home for domestic uses. He has everything except
equal weight boxes to balance it on the either side ( two weights) of the elevator .help ravi installing
the elevator with the multiple weight boxes he has by returning the final weight which can be
formed by merging possible unequal weights . the only condition here is the final weight boxes
should be of equal weights . return the maximum possible final weight so that elevator can be more
balanced

Sample input:-

123

Output :-

Explanation :-

Here to balance the weights the only possible way is to merge the weights 1

__________________________________________________________________________________
__________________________________

while playing with the number system , nobita found some interesting numbers and named them
nobita’s numbers . A number is considered to be nobita’s number if all adjacent digits of the number
have an absolute difference of 1.

Write a program to find sum of all of nobita’s numbers within a given range [X,Y]

Read the input from STDIN and print the output to STDOUT .

Do not write arbitrary strings anywhere in the program , as these contribute to the standard output
and test cases will fail

Constraint

10<=X<=Y<=107

Input format

The input contains X and Y separated by single white space

Output format

The output contains the sum of all of nobita’s numbers in the given range

Sample input 1

123 456

Sample output

4119
Explanation

Consider the first number in the range :123

The absolute difference between 1 and 2 is 1 and between 2 and 3 is 1

Thus all adjacent digits of the number have an absolute difference of 1 andhence this is a nobits’s
number

__________________________________________________________________________________
________________________________________

Write a program to take two integers & n as input and find the number of possible sequences of
length n such that each of the next element is greater than or equal to twice of the previous element
but less than or equal to m.

Example 1:

Input:

10

Output: 4

Explanation there should be no elements and value of last element should be at- most m.

The sequences are {1, 2, 4, 8}, {1, 2, 4, 9}, {1,2,4,10}, {1,2,5,10}

Example 2:

Input:

Output: 6

__________________________________________________________________________________
___________________________________________

Product of numbers

You are given a function ,

Int* ProductArray(int* arr,int n);

The function accepts an integer array ‘arr’ of length ‘n’. Implement the function to modify the given
array such that , value at present in the array,in that array have to return products of all integers
except at index.

Assumption:

Array Index starts from 0.


n>1

Each product operation is within the integer range.

Note:

Input and output arrays are of same length.

Example:

Input:

2375

Output:

105 70 30 42

Explanation:

arr[0]=arr[1]*arr[2]*arr[3]=3*7*5=105

__________________________________________________________________________________
__________________________________________

Repeating Digits

Implement the following function

Int CommonDigit(int a, int b, int c);

The function accepts three positive integers ‘a’, ’b’, and ‘c’ as its argument. implement the function
to find the repeating digit in all the three input numbers. if there is no common digit ,then return -1.

Assumption:

All 3 numbers are three digit numbers.

All 3 numbers can have at most 1 digit common.

Examples :

Input:

A:426

B:486

C:652

Output:

Explanation:

6 is common digit in all the three numbers, thus , 6 is the output.

__________________________________________________________________________________
_______________________________________________

You might also like