Skip to content

Commit b46c3f0

Browse files
committed
Initial Files
1 parent 2a58a2b commit b46c3f0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+2647
-0
lines changed

PRACTICE/10_carry.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Question:10
3+
(Asked in Accenture Offcampus 1 Aug 2021, Slot 2)
4+
5+
Problem Statement
6+
7+
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
8+
9+
You are required to implement the following function.
10+
11+
Int NumberOfCarries(int num1 , int num2);
12+
13+
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’.
14+
15+
Assumption: num1, num2>=0
16+
17+
Example:
18+
19+
Input
20+
Num 1: 451
21+
Num 2: 349
22+
Output
23+
2
24+
Explanation:
25+
26+
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.
27+
28+
Sample Input
29+
30+
Num 1: 23
31+
32+
Num 2: 563
33+
34+
Sample Output
35+
36+
0
37+
*/
38+
39+
#include <bits/stdc++.h>
40+
using namespace std;
41+
42+
int numberOfCarries(int num1, int num2){
43+
44+
int greater, count = 0, rem1 = 0, rem2 = 0, carry = 0;
45+
46+
(num1 > num2 ? greater = num1 : greater = num2);
47+
48+
while(greater){
49+
rem1 = num1 % 10;
50+
rem2 = num2 % 10;
51+
52+
if(rem1 + rem2 + carry > 9) count++;
53+
54+
carry = (rem1+rem2)/10;
55+
56+
greater /= 10;
57+
num1 /= 10;
58+
num2 /= 10;
59+
}
60+
61+
return count;
62+
}
63+
64+
int main(){
65+
int num1, num2;
66+
cin >> num1 >> num2;
67+
cout << numberOfCarries(num1, num2);
68+
}

PRACTICE/11_replace.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Question:11
3+
(Asked in Accenture Offcampus 1 Aug 2021, Slot 3)
4+
5+
Problem Statement
6+
7+
You are given a function,
8+
9+
Void *ReplaceCharacter(Char str[], int n, char ch1, char ch2);
10+
11+
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’.
12+
13+
Assumption: String Contains only lower-case alphabetical letters.
14+
15+
Note:
16+
17+
Return null if string is null.
18+
If both characters are not present in string or both of them are same , then return the string unchanged.
19+
Example:
20+
21+
Input:
22+
Str: apples
23+
ch1:a
24+
ch2:p
25+
Output:
26+
paales
27+
Explanation:
28+
29+
‘A’ in original string is replaced with ‘p’ and ‘p’ in original string is replaced with ‘a’, thus output is paales.
30+
*/
31+
32+
#include <bits/stdc++.h>
33+
using namespace std;
34+
35+
string replaceCharacter(string s, int n, char c1, char c2){
36+
if(n == 0) return 0;
37+
38+
for(int i = 0; i < n; i++){
39+
if(s[i] == c1) s[i] = c2;
40+
else if(s[i] == c2) s[i] = c1;
41+
}
42+
43+
return s;
44+
}
45+
46+
int main(){
47+
string s;
48+
char c1, c2;
49+
cin >> s >> c1 >> c2;
50+
int n = s.size();
51+
cout << replaceCharacter(s, n, c1, c2);
52+
}

PRACTICE/12operations.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Question:12
3+
(Asked in Accenture Offcampus 2 Aug 2021, Slot 1)
4+
5+
Problem Statement
6+
7+
You are required to implement the following function.
8+
9+
Int OperationChoices(int c, int n, int a , int b )
10+
11+
The function accepts 3 positive integers ‘a’ , ‘b’ and ‘c ‘ as its arguments. Implement the function to return.
12+
13+
( a+ b ) , if c=1
14+
( a – b ) , if c=2
15+
( a * b ) , if c=3
16+
(a / b) , if c =4
17+
Assumption : All operations will result in integer output.
18+
19+
Example:
20+
21+
Input
22+
c :1
23+
a:12
24+
b:16
25+
Output:
26+
Since ‘c’=1 , (12+16) is performed which is equal to 28 , hence 28 is returned.
27+
Sample Input
28+
29+
c : 2
30+
31+
a : 16
32+
33+
b : 20
34+
35+
Sample Output
36+
37+
-4
38+
*/
39+
40+
#include <bits/stdc++.h>
41+
using namespace std;
42+
43+
int operationChoices(int c, int a, int b){
44+
if(c == 1) return a+b;
45+
if(c == 2) return a-b;
46+
if(c == 3) return a*b;
47+
if(c == 4) return a/b;
48+
49+
return 0;
50+
}
51+
52+
int main(){
53+
int a, b, c;
54+
cin >> c >> a >> b;
55+
cout << operationChoices(c, a, b);
56+
}

PRACTICE/13_exponent.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
Question:13
3+
(Asked in Accenture Offcampus 2 Aug 2021, Slot 2)
4+
5+
Problem Statement
6+
7+
You are given a function,
8+
9+
Int MaxExponents (int a , int b);
10+
11+
You have to find and return the number between ‘a’ and ‘b’ ( range inclusive on both ends) which has the maximum exponent of 2.
12+
13+
The algorithm to find the number with maximum exponent of 2 between the given range is
14+
15+
Loop between ‘a’ and ‘b’. Let the looping variable be ‘i’.
16+
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’.
17+
Return ‘max’.
18+
Assumption: a <b
19+
20+
Note: If two or more numbers in the range have the same exponents of 2 , return the small number.
21+
22+
Example
23+
24+
Input:
25+
7
26+
12
27+
Output:
28+
8
29+
Explanation:
30+
31+
Exponents of 2 in:
32+
33+
7-0
34+
35+
8-3
36+
37+
9-0
38+
39+
10-1
40+
41+
11-0
42+
43+
12-2
44+
45+
Hence maximum exponent if two is of 8.
46+
*/
47+
48+
#include <bits/stdc++.h>
49+
using namespace std;
50+
51+
int maxExponents(int a, int b){
52+
int ans = 0, max = 0, currenti;
53+
54+
for(int i = a; i <= b; i++){
55+
int count = 1;
56+
int ci = i;
57+
if(ci%2==0){
58+
while(ci%2==0){
59+
count++;
60+
ci = ci/2;
61+
}
62+
if(count>max){
63+
max = count;
64+
ans = i;
65+
66+
}
67+
68+
}
69+
70+
}
71+
return ans;
72+
73+
return ans;
74+
}
75+
76+
int main(){
77+
int a, b;
78+
cin >> a >> b;
79+
cout << maxExponents(a, b);
80+
}

PRACTICE/14_calculate.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
3+
Question : 14
4+
(Asked in Accenture Offcampus 2 Aug 2021, Slot 3)
5+
6+
Problem Statement
7+
8+
You are required to implement the following function:
9+
10+
Int Calculate(int m, int n);
11+
12+
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.
13+
Note
14+
0 < m <= n
15+
16+
Example
17+
18+
Input:
19+
20+
m : 12
21+
22+
n : 50
23+
24+
Output
25+
26+
90
27+
28+
Explanation:
29+
The numbers divisible by both 3 and 5, between 12 and 50 both inclusive are {15, 30, 45} and their sum is 90.
30+
Sample Input
31+
m : 100
32+
n : 160
33+
Sample Output
34+
510
35+
36+
*/
37+
38+
#include <bits/stdc++.h>
39+
using namespace std;
40+
41+
int calculate (int m, int n){
42+
43+
int sum = 0;
44+
45+
for(int i = m; i <= n;){
46+
if(i%3 == 0 && i % 5 == 0){
47+
sum += i;
48+
i += 15;
49+
}
50+
else i++;
51+
}
52+
53+
return sum;
54+
}
55+
56+
int main(){
57+
int m, n;
58+
cin >> m >> n;
59+
60+
cout << calculate(m, n);
61+
}

PRACTICE/15_largest.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
3+
Question 15
4+
Problem Statement
5+
6+
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
7+
8+
Example
9+
10+
enter the size of array : 5
11+
enter element at 0 index : 3
12+
enter element at 1 index : 4
13+
enter element at 2 index : 1
14+
enter element at 3 index : 7
15+
enter element at 4 index : 9
16+
Sorted even array : 1 3 9
17+
Sorted odd array : 4 7
18+
19+
7
20+
21+
*/
22+
23+
#include <bits/stdc++.h>
24+
using namespace std;
25+
26+
int largest(int n, int arr[]){
27+
vector<int> even;
28+
vector<int> odd;
29+
30+
for(int i = 0; i < n; i++){
31+
if(i&1) odd.push_back(arr[i]);
32+
else even.push_back(arr[i]);
33+
}
34+
35+
sort(odd.begin(), odd.end(), greater<int>());
36+
sort(even.begin(), even.end(), greater<int>());
37+
38+
return(odd[1]+even[1]);
39+
}
40+
41+
int main(){
42+
int n;
43+
cin >> n;
44+
int arr[n];
45+
for(int i =0; i < n; i++) cin >> arr[i];
46+
47+
cout << largest(n, arr) << endl;
48+
}

0 commit comments

Comments
 (0)