0% found this document useful (0 votes)
2 views143 pages

Class Questions Problem Solving

The document contains various coding problems and their solutions, primarily focusing on algorithms related to arrays, strings, and greedy methods. It includes functions for finding minimum subarrays, merging intervals, job scheduling, and calculating maximum profits, among others. Each problem is linked to its respective online coding platform for further reference.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views143 pages

Class Questions Problem Solving

The document contains various coding problems and their solutions, primarily focusing on algorithms related to arrays, strings, and greedy methods. It includes functions for finding minimum subarrays, merging intervals, job scheduling, and calculating maximum profits, among others. Each problem is linked to its respective online coding platform for further reference.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

HWI

Minimum Size [Link]


Subarray Sum
Code int minSubArrayLen(int target, vector<int> &nums) {
int prefixsum = 0, result = INT_MAX, start = 0;
for(int end = 0; end < [Link](); end++) {
prefixsum += nums[end];
while(prefixsum >= target) {
result = min(result, end - start + 1);
prefixsum -= nums[start++];
}
}
if(result == INT_MAX) return 0;
else return result;
}
Minimum [Link]
Window
Substring
Code string minWindow(string s, string t) {
unordered_map<char, int> mp;
for(auto i : t) mp[i]++;
int start = 0, count = 0;
int st = -1;
int len = INT_MAX;
for(int end = 0; end < [Link](); end++) {
char ch = s[end];
if([Link](ch) != [Link]()) {
if(mp[ch] > 0) count++;
mp[ch]--;
}
while(count == [Link]()) {
if(len > end - start + 1) {
len = min(len, end - start + 1);
st = start;
}
char ch = s[start];
if([Link](ch) != [Link]()) {
mp[ch]++;
if(mp[ch] > 0) count--;
}
start++;
}
}
if(st == -1) return "";
else {
string temp = [Link](st, len);
return temp;
}
}
Subarray [Link]
Product Less
Than K
Code int numSubarrayProductLessThanK(vector<int> &nums, int
k) {
if(k == 0 || k == 1) return 0;
int start = 0, product = 1, count = 0;
for(int end = 0; end < [Link](); end++) {
product *= nums[end];
while(product >= k) {
product = product / nums[start];
start++;
}
count += end - start + 1;
}
return count;
}
Number of [Link]
Substrings three-characters/
Containing All
Three
Characters
Code int numberOfSubstrings(string s) {
int start = 0, count = 0, result = 0;
int n = [Link]();
unordered_map<char, int> mp = {{'a', 1}, {'b', 1},
{'c', 1}};
for(int end = 0; end < [Link](); end++) {
char temp = s[end];
if([Link](temp) != [Link]()) {
if(mp[temp] > 0) count++;
mp[temp]--;
}
while(count == 3) {`
result += (n - end);
mp[s[start]]++;
if(mp[s[start]] > 0) count--;
start++;
}
}
return result;
}
Subarrays [Link]
with K
Different
Integers
Code #include <bits/stdc++.h>
using namespace std;

int fun(vector<int> &nums, int k){


int start = 0, count = 0;
unordered_map<int, int> mp;

for(int end = 0; end < [Link](); end++){


int ch = nums[end];
mp[ch]++;

while([Link]() > k){


mp[nums[start]]--;
if(mp[nums[start]] == 0) [Link](nums[start]);
start++;
}

count += end - start + 1;


}

return count;
}

int subarraysWithKDistinct(vector<int> &nums, int k){


int count1 = fun(nums, k);
int count2 = fun(nums, k - 1);
return count1 - count2;
}

Greedy
Minimum [Link]
Platforms 1587115620/1
Problem
code
Merge [Link]
intervals
Code vector<vector<int>> merge(vector<vector<int>>
&intervals) {
if([Link]() == 0) return {};
sort([Link](), [Link]());
vector<vector<int>> result;
int start = intervals[0][0];
int end = intervals[0][1];
for(int i = 1; i < [Link](); i++){
if(intervals[i][0] <= end){
end = max(end, intervals[i][1]);
} else {
result.push_back({start, end});
start = intervals[i][0];
end = intervals[i][1];
}
}
result.push_back({start, end});
return result;
}
Non- [Link]
overlapping
Intervals
int eraseOverlapIntervals(vector<vector<int>>
&intervals) {
int n = [Link]();
sort([Link](), [Link](),
[](vector<int> &a, vector<int> &b) {
return a[1] < b[1];
});
int count = 1;
int lastFinish = intervals[0][1];
for (int i = 1; i < n; i++) {
if (intervals[i][0] >= lastFinish)
count++;
else
continue;
lastFinish = intervals[i][1];
}
return n - count;
}
Insert Interval [Link]

Maximum [Link]
Meetings in in-one-room/1?
One Room itm_source=geeksforgeeks&itm_medium=article&itm_campaign
=practice_card

#include <bits/stdc++.h>
using namespace std;

vector<int> maxMeetings(vector<int> &start, vector<int>


&finish) {
int n = [Link]();
vector<tuple<int, int, int>> meetings;
for (int i = 0; i < n; i++) {
meetings.push_back({finish[i], start[i], i + 1});
}
sort([Link](), [Link]());
vector<int> result;
int lastFinish = -1;
for (auto [f, s, idx]: meetings) {
if (s > lastFinish) {
result.push_back(idx);
lastFinish = f;
}
}
return result;
}
Partition [Link]
Labels description/

vector<int> partitionLabels(string s) {
int start = 0, end = 0;
unordered_map<char, int> mp;
for (int i = 0; i < [Link](); i++)
mp[s[i]] = i;
vector<int> result;
for (int i = 0; i < [Link](); i++) {
end = max(mp[s[i]], end);
if (end == i) {
result.push_back(end - start + 1);
start = end + 1;
}
}
return result;
}
Fractional [Link]
Knapsack knapsack-1587115620/1

double fractionalKnapsack(vector<int>& val, vector<int>&


wt, int capacity) {
int n = [Link]();
vector<vector<double>> temp(n, vector<double>(3));
for (int i = 0; i < n; i++) {
temp[i][0] = (double)val[i] / wt[i];
temp[i][1] = val[i];
temp[i][2] = wt[i];
}
sort([Link](), [Link](),
[](vector<double> &a, vector<double> &b) {
return a[0] > b[0];
});
double profit = 0;
for (int i = 0; i < n; i++) {
if (capacity >= temp[i][2]) {
profit += temp[i][1];
capacity -= temp[i][2];
}
else {
profit += ((double)capacity / temp[i][2]) *
temp[i][1];
break;
}
}
return profit;
}
Buy Maximum [Link]
Stocks if i stocks-if-i-stocks-can-be-bought-on-i-th-day/1
stocks can be
bought on ith
day
int buyMaximumProducts(int n, int k, int price[]) {
vector<pair<int, int>> stocks;

for (int i = 0; i < n; i++) {


stocks.push_back({price[i], i + 1});
}

sort([Link](), [Link]());

int totalStocks = 0;

for (int i = 0; i < n; i++) {


int cost = stocks[i].first;
int maxCanBuy = stocks[i].second;

int canBuy = min(maxCanBuy, k / cost);

totalStocks += canBuy;
k -= canBuy * cost;

if (k == 0) break;
}

return totalStocks;
}
Job [Link]
Sequencing 1587115620/1
Problem
struct Job {
int id, dead, profit;
};

vector<int> jobScheduling(Job arr[], int n) {


sort(arr, arr + n, [](Job &a, Job &b) {
return [Link] > [Link];
});

int maxDeadline = 0;
for (int i = 0; i < n; i++)
maxDeadline = max(maxDeadline, arr[i].dead);

vector<int> slot(maxDeadline + 1, -1);

int countJobs = 0, totalProfit = 0;

for (int i = 0; i < n; i++) {


for (int j = arr[i].dead; j > 0; j--) {
if (slot[j] == -1) {
slot[j] = arr[i].id;
countJobs++;
totalProfit += arr[i].profit;
break;
}
}
}

return {countJobs, totalProfit};


}

Recursion

Ispalindrome
bool fun(string &s, int left, int right){
if(s[left] != s[right]) return false;
if(left >= right) return true;
return fun(s, left+1, right-1);
}

bool isPalindrome(string S) {
return fun(S, 0, [Link]()-1);
}

Subsets - 1
vector<vector<int>> result;
void fun(vector<int> &nums, int i, vector<int> temp){
if(i == [Link]()){
result.push_back(temp);
return;
}
fun(nums, i+1, temp);
temp.push_back(nums[i]);
fun(nums, i+1, temp);
temp.pop_back();
}
vector<vector<int>> subsetsWithDup(vector<int>& nums)
{
vector<int> temp;
fun(nums, 0, temp);
sort([Link](), [Link]());
return result;
}

Subsets - II
vector<vector<int>> result;
void fun(int i, vector<int> &arr, vector<int> &ans) {
if (i == [Link]()) {
result.push_back(ans);
return;
}
ans.push_back(arr[i]);
fun(i + 1, arr, ans);
ans.pop_back();
int j = i + 1;
while (j < [Link]() && arr[i] == arr[j])
j++;
fun(j, arr, ans);
}

Binary Strings
vector<string> result;

void fun(int i, int n, string temp) {


if (i == n) {
result.push_back(temp);
return;
}

fun(i + 1, n, temp + '1');


temp.pop_back();

if (temp[[Link]() - 1] == '1') {
fun(i + 1, n, temp + '0');
temp.pop_back();
}
}

vector<string> validStrings(int N) {
fun(1, N, "0");
fun(1, N, "1");
return result;
}

Combinations
Prefix Sum HWI
Longest
subarray with
sum divisible
by k
Code int func(int N, int K, vector<int>& nums) {
unordered_map<int, int> mp;
int len = 0;
int presum = 0;

for(int i = 0; i < N; i++) {


presum += nums[i];
int temp = presum % K;

if(temp == 0)
len = max(len, i + 1);
else {
if([Link](temp) != [Link]())
len = max(len, i - mp[temp]);
}

mp[temp] = i;
}

if(len == 0)
return -1;
else
return len;
}
Longest
subarray with
equal 0 and 1
Code int func(int N, vector<int>& nums) {
int len = 0;
vector<int> temp(N);
for(int i = 0; i < N; i++) {
if(nums[i] == 0)
temp[i] = -1;
else
temp[i] = 1;
}
unordered_map<int, int> mp;
int presum = 0;
for(int i = 0; i < N; i++) {
presum += temp[i];
if(presum == 0)
len = max(len, i + 1);
else {
if([Link](presum) != [Link]()) {
len = max(len, i - mp[presum]);
} else {
mp[presum] = i;
}
}
}
if(len == 0)
return -1;
else
return len;
}
Count
subarrays with
equal 0 and 1
int func(int N, vector<int>& nums) {
int count = 0;
vector<int> temp(N);
for(int i = 0; i < N; i++) {
if(nums[i] == 0)
temp[i] = -1;
else
temp[i] = 1;
}
unordered_map<int, int> mp;
int presum = 0;
for(int i = 0; i < N; i++) {
presum += temp[i];
if(presum == 0) {
count++;
}
if([Link](presum) != [Link]()) {
count += mp[presum];
}
mp[presum]++;
}
return count;
}

#include <bits/stdc++.h>
using namespace std;
int func(int N, vector<int>& L, vector<int>& R) {
int maxVal = *max_element([Link](), [Link]());
int newsize = maxVal + 2;
vector<int> freq(newsize, 0);
for(int i = 0; i < N; i++) {
freq[L[i]]++;
freq[R[i] + 1]--;
}
int prefix = freq[0];
for(int i = 1; i < newsize; i++) {
prefix += freq[i];
freq[i] = prefix;
}
return max_element([Link](), [Link]()) -
[Link]();
}
Priority Queue + Greedy
Minimum
Cost of
Ropes
Code - priority_queue<int, vector<int>, greater<int>> pq;
for (int x : ropes) [Link](x);
int cost = 0;
while ([Link]() > 1) {
int first = [Link]();
[Link]();
int second = [Link]();
[Link]();
int sum = first + second;
cost += sum;
[Link](sum);
}
return cost;
}
Maximum
number of
events that
can be
attended
Code
Tab 1
Test Questions

Title Given two non-empty strings, S1 and S2, containing digits, alphabets, and
special characters. Generate a digit code (C) by extracting the digits from S1
and S2, such that C has a digit from the first string followed by a digit from the
second string. If you run out of digits in S1 or S2, append the remaining digits
from the other string at the end of C. See sample test case for better
understanding.
Input Two lines containing S1 and S2.
Format
Output A single string in a single line.
Format
Constraint 1 <= len(S1), len(S2) <= 100
Sample a4b1$c3d4x89
Input 1 b2c9*t7
Sample 421937489
Output 1
Explanatio S1 = "a4b1$c3d4x89", and S2 = "b2c9*t7". First digits from S1 and S2 are "4"
n1 and "2" respectively. C is 42. Second digits from S1 and S2 are "1" and "9"
respectively. C becomes 4219. Third digits from S1 and S2 are "3" and "7"
respectively. C becomes 421937. S2 has run out of digits. Append all the
remaining digits from S1 to C to get 42193789. Hence the answer.
Test Case a4b1$c3d4x89
Input 1 b2c9*t7
Test Case 421937489
Output 1
Test Case &61)9B26gdjsonc))0
Input 2 bc
Test Case 619260
Output 2
Test Case 0bm8
Input 3 ncjdns0esncbcdy1bhdheu2847%^&*(3
Test Case 008128473
Output 3
Test Case 0g0n0d0g0g
Input 4 1*1(1H1&
Test Case 010101010
Output 4
Test Case a5b1$c3d4x8-23
Input 5 b2c9*t7
Test Case 5219374823
Output 5
Test Case b1n2m3
Input 6 b5j6k7
Test Case 152637
Output 6
Test Case u7ty45hfjhfha2jfdh3jdfh
Input 7 x3jhf6jfdjhf4djfhjf36&
Test Case 7346542336
Output 7
Test Case gjkj49jkj393jkjh
Input 8 djd7nf9f
Test Case 4799393
Output 8
Test Case fj12
Input 9 jf45kjfkj8fj
Test Case 14258
Output 9
Test Case fj12f
Input 10 jf45kjfkj8fj
Test Case
Output 10
Test Case gjkfdj39fj9df
Input 11 5
Test Case 3599
Output 11

Statement Consider the following series of numbers:

2 3 6 5 7 35 11 13 143 17 19 323 …

The series begins with the first two prime numbers (2 and 3) followed by their
product (6). The next pair of consecutive prime numbers (5 and 7) is then
printed followed by their product (35) and so on.

Print first N terms of this series.


Input A single line containing the value of N.
Format
Output N space separated numbers.
Format
Constraint 1 < N <= 1000
Sample 7
Input 1
Sample 2 3 6 5 7 35 11
Output 1
Explanatio The first two prime numbers are 2, and 3, and their product is 6. Therefore 2, 3,
n and 6 form the first 3 terms of the series. The next two prime numbers are 5,
and 7, and their product is 35. Hence 5, 7, and 35 forms the next 3 terms of the
series. The next prime number is 11. This will be the 7th and last term of the
series. Hence the answer.
Test Case 7
Input 1
Test Case 2 3 6 5 7 35 11
Output 1
Test Case 5
Input 2
Test Case 23657
Output 2
Test Case 28
Input 3
Test Case 2 3 6 5 7 35 11 13 143 17 19 323 23 29 667 31 37 1147 41 43 1763 47 53 2491
Output 3 59 61 3599 67
Test Case 20
Input 4
Test Case 2 3 6 5 7 35 11 13 143 17 19 323 23 29 667 31 37 1147 41 43
Output 4
Test Case 15
Input 5
Test Case 2 3 6 5 7 35 11 13 143 17 19 323 23 29 667
Output 5
Test Case 250
Input 6

TCS NQT | ACCENTURE | CAPGEMINI | WIPRO Questions

Title
Single Digit Sum
Given a number N and a natural number K > 0. Concatenate N, K times. Then
repeatedly add all the digits of the resultant number, until the result has only a
single digit.
Input First line of Input contains the number N and the second line contains K (number of
Format times N has to be concatenated).
Output Return single digit sum.
Format
Constraint 1 <= N <= 10^4
1 <= K <= 10
Sample 9875
Input 1 4
Sample 8
Output 1
Explanatio N = 9875 and K = 4. After the concatenation, the resultant number is,
n 9875987598759875. Adding all the digits we get, 116 ( = 5 + 7 + 8 + 9 + 5 + 7 + 8 +
9 + 5 + 7 + 8 + 9 + 5 + 7 + 8 + 9). Adding all the digits of 116 we get, 8 (= 1 + 1 + 6).
Test Case 315
Input 1 4
Test Case 9
Output 2
Test Case 9
Input 2 2
Test Case 9
Output 2
Test Case 15
Input 3 1
Test Case 6
Output 3
Test Case 987
Input 4 8
Test Case 3
Output 4
Test Case 5
Input 5 1
Test Case 5
Output 5

Title
All Armstrongs
An Armstrong number is the one whose sum of the nth power of its digits is equal
to the number itself. Your task is to print all the Armstrong numbers in the range
[lower, upper]. If no Armstrong Number is found between the given range, print -1.
Input Two integer values each in a new line representing lower and upper.
Format
Output Space separated armstrong numbers in range or -1.
Format
Constraint 1 < numbers < 10 ^ 5
Sample 100
Input 1 999
Sample 153 370 371 407
Output 1
Test Case 1
Input 1 100
Test Case 123456789
Output 1
Test Case 500
Input 2 1000
Test Case -1
Output 2
Test Case 1000
Input 3 10000
Test Case 1634 8208 9474
Output 3
Test Case 100
Input 4 200
Test Case 153
Output 4
Test Case 200
Input 5 300
Test Case -1
Output 5

Jumping Numbers
Statement Given a positive number X. Find all jumping numbers smaller than or equal to X. A
number is called a jumping number if all adjacent digits in it differ by only 1. All
single digit numbers are considered as jumping numbers.
Input Single line of input containing the value of X.
Format
Output Output all the jumping numbers in sorted order.
Format
Constraint 1 <= X <= 10^4
Sample 50
Input 1
Sample 0 1 2 3 4 5 6 7 8 9 10 12 21 23 32 34 43 45
Output 1
Explanatio 0 to 9 are all single digit numbers. They all are considered jumping numbers. The
n difference between any two subsequent digits of other numbers is 1. For example,
in 12, 1 and 2 differ by 1, and in 32, 3 and 2 differ by 1. Therefore they are
considered jumping numbers. Whereas, in 24, 2 and 4 differ by 2, hence they are
not considered jumping numbers.
Test Case 5
Input 1
Test Case 012345
Output 1
Test Case 15
Input 2
Test Case 0 1 2 3 4 5 6 7 8 9 10 12
Output 2
Test Case 200
Input 3
Test Case 0 1 2 3 4 5 6 7 8 9 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98 101 121
Output 3 123
Test Case 2000
Input 4
Test Case 0 1 2 3 4 5 6 7 8 9 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98 101 121
Output 4 123 210 212 232 234 321 323 343 345 432 434 454 456 543 545 565 567 654 656
676 678 765 767 787 789 876 878 898 987 989 1010 1012 1210 1212 1232 1234
Test Case 5000
Input 5
Test Case 0 1 2 3 4 5 6 7 8 9 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98 101 121
Output 5 123 210 212 232 234 321 323 343 345 432 434 454 456 543 545 565 567 654 656
676 678 765 767 787 789 876 878 898 987 989 1010 1012 1210 1212 1232 1234
2101 2121 2123 2321 2323 2343 2345 3210 3212 3232 3234 3432 3434 3454
3456 4321 4323 4343 4345 4543 4545 4565 4567

Sum Merge
You are given two sorted arrays of size N each. Merge the given sorted arrays in
sorted order and print the sum of two middle elements after merging the arrays.
Input Three lines of input. The first line contains integer N. The next two lines contain
Format array elements in space separated format
Output Print the sum of the middle elements of the array after merging in sorted order.
Format
Constraint Both arrays are of equal size.
1 < N < 100
1 <= arr[i] <= 1000
Sample 5
Input 1 10 20 30 35 40
25 32 45 50 55
Sample 67
Output 1
Explanatio The sorted merged array is, 10, 20, 25, 30, 32, 35, 40, 45, 50, 55. The sum of the
n two middle elements (marked as bold) is 67
Test Case 5
Input 1 12345
6 7 8 9 10
Test Case 11
Output 1
Test Case 3
Input 2 10 20 30
15 25 35
Test Case 45
Output 2
Test Case 6
Input 3 1 2 3 10 11 12
456789
Test Case 13
Output 3
Test Case 5
Input 4 21 22 24 26 28
10 20 30 40 50
Test Case 50
Output 4
Test Case 2
Input 5 11
11
Test Case 2
Output 5
Test Case 5
Input 6 5 15 25 35 45
10 20 30 40 50
Test Case 55
Output 6

Class Position
Statement The marks of N students of a particular class are given as space separated values.
Find out the position of each student in the class. Position 1 is assigned to a student
with maximum marks and so on. If two students get the same marks then both will
be assigned the same position and the next position will be skipped. See sample
case for further explanation.
Input Two lines of input. The first line contains integer N. Next line contain array
Format elements in space separated format
Output Array of positions in space separated format.
Format
Constraints 1 <= N <= 60
0 <= arr[i] <= 100
Sample 5
Input 1 10 56 87 40 90
Sample 53241
Output 1
Explanatio The position of the first student (with 10 marks) is 5th. The position of the second
n student (with 56 marks) is 3rd. Similarly, the position of the last student (with 90
marks) is first. Therefore, the output is 5 3 2 4 1.
Sample 6
Input 2 80 56 97 80 90 90
Sample 461422
Output 2
Explanatio The position of the first student (with 80 marks) is 4th. The position of the second
n student (with 56 marks) is 6th. Positions of the last two students(90 marks each) is
2nd. Therefore, both of them are assigned 2nd position and the 3rd position is
skipped. The output is, 4 6 1 4 2 2.
Test Case 5
Input 1 100 65 80 40 90
Test Case 14352
Output 1
Test Case 6
Input 2 80 80 97 80 90 90
Test Case 441422
Output 2
Test Case 6
Input 3 100 100 100 90 95 80
Test Case 111546
Output 3
Test Case 4
Input 4 50 40 30 20
Test Case 1234
Output 4
Test Case 6
Input 5 555555
Test Case 111111
Output 5
Test Case 4
Input 6 80 70 80 60

Test Case 1314


Output 6

Move All Zeros


Given an integer array of size N. Your task is to modify the original array such that
all the zeros move towards the end of the array while maintaining the relative
order of the non-zero elements in the array. Print the modified array. Do not use an
extra array.
Input Two lines of input. The first line contains integer N. Next line contain array
Format elements in space separated format
Output Single line output. The modified array having all zeros at the right end and
Format elements are space separated.
Constraint 1 <= N <= 10^3
-10^3 <= arr[i] <= 10^3
Sample 5
Input 1 0 1 0 3 12
Sample 1 3 12 0 0
Output 1
Explanatio The original array has 2 zeros. They are moved to the end of the array. The non
n zero elements have retained their relative positioning. The modified array is 1 3 12
00
Test Case 8
Input 1 1 2 0 -4 3 0 5 0
Test Case 1 2 -4 3 5 0 0 0
Output 1
Test Case 7
Input 2 1200036
Test Case 1236000
Output 2
Test Case 9
Input 3 1 0 8 -6 9 7 3 0 -8
Test Case 1 8 -6 9 7 3 -8 0 0
Output 3
Test Case 7
Input 4 1 5 10 8 6 9 3
Test Case 1 5 10 8 6 9 3
Output 4

Height Checker
Statement N students are standing in a line in random order. But they are expected to be
standing in non decreasing order. You are given an integer array whose ith value
represents the height of the student standing at the ith position. Return the count
of the students who are not standing in their expected positions.
Input Two lines of input. The first line contains integer N. Next line contain array
Format elements in space separated format
Output Single integer.
Format
Constraint 1 <= N <= 100
1 <= heights[i] <= 100
Sample 6
Input 1 114213
Sample 3
Output 1
Explanatio Students standing at the 2nd, 4th and 5th indexes are not standing at their
n expected positions. They should have been standing at the 5th, 2nd and 4th index
positions. Therefore the output is 3.
Test Case 5
Input 1 51234
Test Case 5
Output 1
Test Case 5
Input 2 12345
Test Case 0
Output 2
Test Case 5
Input 3 42315
Test Case 2
Output 3
Test Case 8
Input 4 213546789
Test Case 4
Output 4
Test Case 6
Input 5 546789
Test Case 2
Output 5
Test Case 5
Input 6 13425
Test Case 3
Output 6

Rearrange Array
Statement Given an array of integers. Your task is to print the array in the following order -
smallest number, largest number, 2nd smallest number, 2nd largest number, 3rd
smallest number, 3rd largest number and so on.
Input Two lines of input. The first line contains integer N. Next line contain array
Format elements in space separated format
Output Single line of space separated integers.
Format
Constraint 1 <= N <= 10^3
1 <= arr[i] <= 10^3
Sample 4
Input 1 1584
Sample 1845
Output 1
Explanatio Smallest is 1. Largest is 8. Second smallest is 4. And the second largest is 5.
n Therefore the output is 1 8 4 5.
Test Case 9
Input 1 581429376
Test Case 192837465
Output 1
Test Case 4
Input 2 4321
Test Case 1423
Output 2
Test Case 1
Input 3 20
Test Case 20
Output 3
Test Case 4
Input 4 1234
Test Case 1423
Output 4
Test Case 5
Input 5 13425
Test Case 15243
Output 5
Majority Element
Statement Given an array of size n. Print the majority element. The majority element is the
element that appears more than ⌊n/2⌋ times. You may assume that the array is
non-empty. If there is no majority element print "No Majority Element" without
quotes.
Input Two lines of input. The first line contains integer N. Next line contain array
Format elements in space separated format
Output Return majority element if any otherwise print No Majority Element.
Format
Constraint 1 <= N <= 10^3
-10^3 <= arr[i] <= 10^3
Sample 7
Input 1 2211122
Sample 2
Output 1
Explanatio 2 is appearing than 4 times, which is greater than 3 (= ⌊7/2⌋).
n
Sample 8
Input 2 33424424
Sample No Majority Element
Output 2
Explanatio No element is appearing more than 4 (= ⌊8/2⌋) times.
n
Test Case 9
Input 1 334244244
Test Case 4
Output 1
Test Case 20
Input 2 43788128382286888888
Test Case 8
Output 2
Test Case 7
Input 3 2 22 2 3 3 1 1
Test Case No Majority Element
Output 3
Test Case 5
Input 4 -1 -1 2 3 -1
Test Case -1
Output 4
Test Case 6
Input 5 113343
Test Case No Majority Element
Output 5
Nani House
Statement A kid, having N toys, is going to his Nani house for summer vacation. He wants to
carry some toys. But he can lift upto 5 kg (5000 grams) in weight. Find out the
maximum number of toys he can go with. You will be given an array of integers,
whose ith element represents the weight of the ith toy.
Input Two lines of input. The first line contains integer N. Next line contain array
Format elements in space separated format
Output Single integer.
Format
Constraint 1 <= N <= 50
1 <= weight[i] <= 9999
Sample 7
Input 1 200 1800 1200 1500 500 800 4000
Sample 5
Output 1
Explanatio The only possible largest combination of toys, which has the least total weight
n consists of 1st, 3rd, 4th, 5th and 6th toys. The weight of this combination is 4200.
Therefore the output is 5.
Test Case 8
Input 1 2000 3000 1500 1400 800 1200 600 2500
Test Case 4
Output 1
Test Case 11
Input 2 100 1010 1200 800 700 1500 600 2500 3000 8000 900
Test Case 6
Output 2
Test Case 10
Input 3 100 200 300 400 1200 18000 1800 2000 3000 10000
Test Case 6
Output 3
Test Case 6
Input 4 200 500 100 800 500 100
Test Case 6
Output 4
Test Case 7
Input 5 100 200 1700 1800 600 9000 8900
Test Case 5
Output 5

Password Matching
11 As part of developing a new password management system, your task is to validate
whether the new password is correct or not. You are given two words, S and R, and
T integers.
Positive integers represent right rotation by the given value and negative integers
represent left rotation by the given value.

Apply these T operations on S and compare the result with R. If they are the same
"Password Accepted" (without quotes) else print "Try Again" (without quotes).
Input Three lines of input. First line contains two space separated words (S and R). The
Format second line contains an integer (T). And the third line contains T space separated
integers values.
Output "Password Accepted" (without quotes) or "Try Again" (without quotes).
Format
Constraint 1 < S, R < 10^3
1 <= T <= 100
-50 <= arr[i] <= 50
Sample Hello Hello
Input 1 3
1 3 -2
Sample Try Again
Output 1
Explanatio S is Hello. Right rotation by 1 position gives oHell. Again right rotation by 3
n positions gives elloH. Left rotation by 2 times gives loHel. This is not equal to Hello.
Hence the output is Try Again.
Test Case eat&treat& &treat&eat
Input 1 3
7 4 -11
Test Case Try Again
Output 1
Test Case helloworld& world&hello
Input 2 7
-2 6 4 -8 2 1 3
Test Case Password Accepted
Output 2
Test Case doneisbetter isdonebetter
Input 3 4
-10 -5 25 -2
Test Case Try Again
Output 3
Test Case doneisbetter isdonebetter
Input 4 3
12 -24 36
Test Case Try Again
Output 4
Test Case Hello Hello
Input 5 1
55
Move Sections
12 You are given a string S of length greater than 5. You have to create a new resultant
string such that every fourth and sixth character present in the string is moved to
the end of the string in the order as they appear in the original string. Print the
newly created string.
Input Single line containing string S.
Format
Output Single line containing string.
Format
Constraint 5 <= len(S) <= 10^6
Sample quickfoxjumps
Input
Sample quikojumscfxp
Output
Explanatio c, f, x and p are fourth and sixth characters in S, which are moved to the end.
n
Test Case ohelloohelloohelloX
Input 1
Test Case oheloellohelXloholo
Output 2
Test Case SRmByYFmNBZsveKRqoGaHHQMHBSnNzJHEIUzjtASDscAdAaBnKpinvmVTPTqfD
Input 2 QimonOgytXoJkJQrYitMSOhZVDpwModBakxjkQO
Test Case SRmyFNBZveKqGHHQHBSNJEIUjtADcdAanKpnmTPTfDQmngytoJkQYtMShZVpMd
Output 2 BaxjkOBYmsRoaMnzHzSsABivVqioOXJriODwokQ
Test Case DoctorWho
Input 3
Test Case DocoWotrh
Output 3
Test Case October052020
Input 4
Test Case Octbr5200oe02
Output 4

Remove with Patience


13 Given two strings S and R. Replace all the instances of R from S with a new string
formed by concatenating the character just before R by as many times as the length
of R.
Input Space separated strings (S and R).
Format
Output Print the resultant string.
Format
Constraint len(R) < len(S) <= 10^3
Sample Hello ll
Input 1
Sample Heeeo
Output 1
Explanatio ll is to be removed from Hello. And replaced with ee, because the character 'e'
n comes just before ll and the length of ll is 2. Hence the output is Heeeo.
Test Case Helleh leh
Input 1
Test Case Hellll
Output 1
Test Case AdoggodA oggo
Input 2
Test Case AddddddA
Output2
Test Case HelloLL Hello
Input 3
Test Case LL
Output 3
Test Case AdoggodA Adog
Input 4

14
Remove From Selected
Statement Given two strings S1 and S2, remove those characters from the first string which
are present in the second string. Both the strings contain only lowercase
characters. Print S1 after removal of characters.
Input Two strings S1 and S2 are given in two lines.
Format
Output A single string.
Format
Constraint 0 <= length(S1), length(S2) <= 10^3
Sample hello
Input 1 ihedfghj
Sample llo
Output 1
Explanatio Characters 'h' and 'e' from S1 are present in S2. Remaining string after removing
n these characters from S1 is 'llo'.
Test Case parul
Input 1 rahul
Test Case p
Output 2
Test Case abhishek
Input 2 abhigyan
Test Case sek
Output 2
Test Case priyanshu
Input 3 priyanshi
Test Case u
Output 3
Test Case rishu
Input 4 qltr
Test Case ishu
Output 4

15
Reverse Letters Only
Statement You are given a string S consisting of alphabets, digits and special characters.
Reverse only alphabets(uppercase or lowercase). Except alphabets all other
characters will remain in the same position.
Input Single line containing the input string, S.
Format
Output Single line of output containing a string.
Format
Constraints len(S) <= 10^6.
Sample abcd-e
Input 1
Sample edcb-a
Output 1
Explanatio - is the only special character. In the output string, only - has retained its original
n position and the remaining string has been reversed.
Test Case a-bC-dEf-ghIj
Input 1
Test Case j-Ih-gfE-dCba
Output 1
Test Case Test1ng-Leet=code-Q!
Input 2
Test Case Qedo1ct-eeLg=ntse-T!
Output 2
Test Case --!@#
Input 3
Test Case --!@#
Output 3
Test Case abcd--!#
Input 4
Test Case dcba--!#
Output 4
Test Case abcde
Input 5
Test Case edcba
Output 5
Palindrome and Reverse
16 Split the given string (S) in two parts from the midpoint. Reverse each part and join
them to form a new string (T). If T is palindrome, print True otherwise print False.
Note: If len(S) is odd, add the middle character to the right substring.
Input Single line of input containing S.
Format
Output "True" or "False" (without quotes).
Format
Constraint 2 <= len(S) <= 10^6
Sample Hello
Input 1
Sample False
Output 1
Explanatio Splitting "Hello" in two parts we get "He" and "llo". Reversing and adding them we
n get, "eHllo", which is not a palindrome, hence False is printed.
Test Case alula
Input 1
Test Case False
Output 2
Test Case A dog, a plan, a canal: pagoda
Input 2
Test Case False
Output 2
Test Case ABCCBAD
Input 3
Test Case True
Output 3
Test Case A dog god A
Input 4
Test Case False
Output 4
Test Case helleh
Input 5
Test Case True
Output 5

17
Not Vowel
Statement From a given list of N words, print those words which do not contain any vowel. If
all the words have vowels in them, print -1 instead.
Input Two lines of input. First line contains N and the second line contains N space
Format separated words.
Output Space separated words or -1
Format
Constraint 1 < N <= 10^3
Sample 3
Input 1 Kite Google BMW
Sample BMW
Output 1
Test Case 3
Input 1 Preet Jeet Crypt
Test Case Crypt
Output 2
Test Case 3
Input 2 Japan Russia China
Test Case -1
Output 2
Test Case 2
Input 3 Jjpr qytr
Test Case Jjpr qytr
Output 3
Test Case 3
Input 4 Bbllr Jpwqeggdds
Test Case Bbllr
Output 4

Abbreviation
18 A string abbreviation is considered valid if the abbreviated string has at least 3
characters and a maximum of 5 characters. In addition, the abbreviated string
should contain alphabets in the same order as they appear in the original string.
Your task is to write a function which checks if the abbreviation is correct and
returns a boolean response.
Input Single line of input containing two space separated strings.
Format
Output "True" or "False" (without quotes).
Format
Sample Boulevard Blvd
Input 1
Sample True
Output 1
Explanatio The abbreviation is Blvd. All of its characters are part of Boulevard, the original
n string. They appear in the same order in the original string and the length of
abbreviation is greater than 3 and less than 5. Hence it is a valid abbreviation.
Test Case department dmpt
Input 1
Test Case False
Output 1
Test Case minimum min
Input 2
Test Case True
Output 2
Test Case approximately aplty
Input 3
Test Case False
Output 3
Test Case Boulevard BBlvd
Input 4
Test Case False
Output 4

Compress Recurrences
Statement You are given a string and a compression value (an integer, n). Count the frequency
of consecutive occurrences of each character in the string. If the frequency is
greater than or equal to the compression value, print the character followed by its
frequency. Otherwise print the characters as they appear in the original string.
Input Two lines of input. The first line contains the value of the string and the second line
Format contains the value of n.
Output Print the new alphanumeric string.
Format
Constraint 5 <= len(s1) <= 10^3
Sample aaabbsdgfdhaaaaaa
Input 1 3
Sample a3bbsdgfdha6
Output 1
Explanatio 'a' appears 3 times (equal to the compression value) hence it is printed as a3. 'B'
n appears two times (less than the compression value) hence it is printed as 'bb'.
Similar logic is to be applied for other characters too. Hence the final output is
'a3bbsdgfdha6'.
Test Case aaaaabb
Input 1 3
Test Case a5bb
Output 1
Test Case etNGeetNGeetNGe
Input 2 1
Test Case e1t1N1G1e2t1N1G1e2t1N1G1e1
Output 2
Test Case ohelloohelloohello
Input 3 3
Test Case ohelloohelloohello
Output 3
Test Case ohelloohelloohello
Input 4 1
Test Case o1h1e1l2o2h1e1l2o2h1e1l2o1
Output 4

The Last Word


20 Given a string S consisting of alphabets and some special characters (' ', '*', '#' and
'&'). Return the length of the last word in the string. If the last word does not exist,
print 0. A word is defined as the longest substring consisting of non-special
characters.
Input A single line of input containing S.
Format
Output An integer.
Format
Constraint 1 < len(S) <= 10^3
Sample Hello World
Input 1
Sample 5
Output 1
Explanatio Last word in 'Hello World' is World, whose length is 5.
n
Sample Word Hello*&#
Input 2
Sample 5
Output 2
Explanatio Last word in 'Word Hello*&#' is Hello, whose length is 5.
n
Test Case hello &# world
Input 1
Test Case
Output 1 5
Test Case [[A[[B[[a[[b [[a[[b
Input 2
Test Case 6
Output 2
Test Case RgS*IHPxF#zMWdit
Input 3
Test Case 6
Output 3
Test Case *#*&
Input 4
Test Case 0
Output 4
Test Case Hello Learnys#
Input 5
Test Case 7
Output 5
Test Case Infosys *&#
Input 5
Test Case 7
Output 5
PPT Questions

9
String Validation

Given a string S, determine if it contains both the characters '&' and '#' and the
length of the string is even. If both conditions are satisfied, print 'YES';
otherwise, print 'NO’.
Input Single line input string
Format
Output "YES" or “NO”.
Format
Sample abc&def#
Input 1
Sample YES
Output 1
Explanation String contains “&” and “#” and length of string is even 8
Sample hello&world
Input 2
Sample NO
Output 2
Explanation String contains not contain “#”
Test Case &#
Input 1
Test Case YES
Output 1

10
Adding first-2 and last element-2 of list
Given N integers. Find the sum of the first 2 elements and last 2 elements in
separate lines. You can assume that N >= 4.
Input First line contains N. The next line contains the N space separated integers.
Format
Output Two integer in two different line.
Format
Sample 18
Input 1 2 3 4 5 6 7 8 9 10 12 13 15 21 22 45 90 100
Sample 3
Output 190
Test Case 4
Input 1 1111
Test Case 2
Output 2 2
Test Case 5
Input 2 11222
Test Case 2
Output 2 4
Test Case 5
Input 3 -1 -4 0 0 0
Test Case -5
Output 3 0
Test Case 4
Input 4 10 20 -34 10
Test Case 30
Output 4 -24

11
Count and Compare Odds and Evens
Given a list of N positive integers. You need to find out the count of odd and even
integers. If the count of odd integers are greater than the count of even integers,
print "Odd" (without quotes). If the count of even integers are greater than the
count of odd integers, print "Even" (without quotes). Print "Tie" (without quotes)
if count of odd and even integers are equal.
Input Two lines of input. First line contains an integer N, represting the total number of
Format integers. Second line of input contains N space separated integers.
Output Single string in single line.
Format
Sample 5
Input 1 10 24 3 37 38
Sample Even
Output 1
Test Case 8
Input 1 12345678
Test Case Tie
Output 1
Test Case 7
Input 2 1 3 5 7 9 11 13
Test Case Odd
Output 2
Test Case 4
Input 3 10 2 8 16
Test Case Even
Output 3

12
Group Salary Summation
You are given salaries of N groups of employees (each group has M employees) in
the format specified below. The count of employees in each group may not be the
same. Print the sum of salaries of each group of employees.
Input First line contains the value of N, followed by N pairs of lines. The first line of each
Format pair contains the value of M and the second line of each pair contains space
separated M integers, denoting the salary of each employees.
Output N lines, each having single integer.
Format
Sample 3
Input 1` 4
12000 10000 5000 8000
2
3000 2000
3
7000 9000 5000
Sample 35000
Output 1 5000
21000
Test Case 5
Input 1 2
5000 8000
1
50000
3
7000 9000 5000
2
12000 10000
4
23000 1200 0 3400
Test Case 13000
Output 1 50000
21000
22000
27600
Test Case 2
Input 2 4
1200 1000 50000 18000
6
323251
Test Case 70200
Output 2 16
Test Case 3
Input 3 4
12000 10000 5000 8000
2
3000 2000
3
7000 9000 5000

Test Case 35000


Output 3 5000
21000
13
Group-wise Marks Analysis
You are given marks of N groups of students in the format specified below. The
strength of each group may not be the same. Print the maximum, minimum, total,
and average marks (rounded upto one place of decimal) of each group of
students.

The built-in function, round(number, count), returns a floating point number that
is a rounded version of the specified number, with the specified number of
decimals (count). You can use this function in this problem.
Input Format First line contains the value of N, followed by N pairs of lines. The first line of
each pair contains the value of M and the second line of each pair contains space
separated M integers, denoting the marks of each student.
Output N lines, each having M space separated values.
Format
Sample Input 3
1 3
12 14 16
4
11 13 18 19
6
12 14 16 18 20 13
Sample 16 12 42 14.0
Output 1 19 11 61 15.2
20 12 93 15.5
Test Case 3
Input 1 4
10 18 30 34
5
11 10 18 10 20
5
12 22 19 18 22
Test Case 34 10 92 23.0
Output 1 20 10 69 13.8
22 12 93 18.6
Test Case 4
Input 2 1
10
2
55 67
5
12 16 17 20 21
6
21 12 33 34 56 78
Test Case 10 10 10 10.0
Output 2 67 55 122 61.0
21 12 86 17.2
78 12 234 39.0
Test Case 2
Input 3 5
50 45 60 55 70
3
90 95 100
Test Case 70 45 280 56.0
Output 3 100 90 285 95.0
Test Case 2
Input 4 4
30 30 30 30
2
99 99
Test Case 30 30 120 30.0
Output 4 99 99 198 99.0

14
Marks Sum of Top 5 and Last 5
You are given marks of some students. Print the sum of the marks of the top 5
students, and the marks of last 5 students in decreasing order.
Input Format The first line contains the value of N and the next line has N space separated
integers.
Output First line containing an integer value and second line contains 5 integer
Format numbers.
Sample Input 8
1 50 30 60 55 75 85 65 45
Sample 340
Output 1 60 55 50 45 30
Test Case 17
Input 1 0 12 0 14 1 14 1 16 45 23 10 0 25 27 16 18 21
Test Case 141
Output 1 11000
Test Case 12
Input 2 98 34 1 23 56 0 12 7 10 23 45 78
Test Case 311
Output 2 12 10 7 1 0
15.
Swap K-Blocks
Given an array of N integers and a positive integer K. Swap every K element with
the next K elements. N is a multiple of 2K. Use two nested loops.
Input First line of input contains the two integers denoting the value of N and K. The
Format second line contains N space separated integers.
Output N space separated integers in a single line.
Format
Sample 12 3
Input 1 2 3 4 5 6 7 8 9 10 11 12
Sample 4 5 6 1 2 3 10 11 12 7 8 9
Output
Test Case 10 5
Input 1 1 2 3 4 10 11 12 67 15 56
Test Case 11 12 67 15 56 1 2 3 4 10
Output 1
Test Case 84
Input 2 -1 2 -3 5 7 8 9 0
Test Case 7 8 9 0 -1 2 -3 5
Output 2

16
Remove Elements

Given an integer array having N elements. Find the number of occurrences of 1,


and remove the element at the index, which is equal to the number of
occurrences of 1. Print the modified array. Given that, count of 1 < N.
Input First line contains the value of N. The next line contains N space-separated
Format integers.
Output N - 1 space-separated integers.
Format
Sample 9
Input 241618415
Sample 24118415
Output
Sample 5
Input 2 23546
Sample 3546
Output 2
Test case 6
input 1 111112
Test case 11111
output 1
Test case 8
input 2 12181411
Test case 1218111
output 2

17
Split The Array

Given an integer array having N elements. Find the first locations of the
minimum and maximum elements. Split the array into three parts from these
locations. Built another array by concatenating the second part, followed by the
first part, followed by the third part. Print the elements of this array in a single
line. It can be assumed that the minimum and maximum elements will not be the
same.
Input First line contains the value of N. The next line contains N space-separated
Format integers.
Output N space-separated integers.
Format
Sample 8
Input 24135976
Sample 13524976
Output
Sample 10
Input 2 2135917964
Sample 1352917964
Output 2
Sample 11
Input 3 42935817964
Sample 93584217964
Output 3

18
Compare Arrays
Given two arrays (A and B) having lengths N and M respectively. For each B[i]
print the count of elements from A, which is greater than B[i].
Input First line has two space separated integers denoting N and M. The second line
Format has N space separated integers. The third line has M space separated integers.
Output N space separated integers in a single line.
Format
Sample 57
Input 26139
5 1 8 7 0 4 12
Sample 2411520
Output
Test Case 57
Input 1 26169
5 1 1 7 0 4 12
Test Case 3441530
Output 1
Test Case 10 12
Input 2 1 2 3 4 5 6 7 8 9 10
2 22 2 2 2 2 2 8 2 13 2 12
Test Case 808888828080
Output 2

19
All Pairs with Sum K
Given an array having N integers and an integer K. Find all pairs of elements
from this array, such that their sum is K (brute force version of Two Sum
problem). Also discuss the complexity of your approach.
Input First line of input contains the value of N and K. The second line contains N space
Format separated integers.
Output Space separated elements of the pair. Each pair should go into a separate line.
Format
Sample 75
Input 8 -1 3 4 -3 2 1
Sample 8 -3
Output 32
41
Test Case 71
Input 1 8 -1 3 4 -3 2 1
Test Case -1 2
Output 1 4 -3
Test Case 10 2
Input 2 8 -1 3 4 -3 2 1 2 4 6
Test Case -1 3
Output 2

20
Next Greater Right Element
Given an array of N positive integers. For each element, print the first greater
element towards the right of the element. If no greater element is found, print 0.
Input First line of input has N and the next line has N space separated integers.
Format
Output N space separated integers in a single line.
Format
Sample 5
Input 42645
Sample 66050
Output
Test Case 6
Input 1 111144
Test Case 444400
Output 1
Test Case 1
Input 2 1
Test Case 0
Output 2

21
Maximum Profit
Given an array, P, holding N integers, where P[i] represents the price of a
particular stock on ith day. You are allowed to purchase and sell the stock only
once. Find out your maximum profit.
Input First line contains the value of N. The next line has N space separated integers.
Format
Output A single integer in a single line representing the maximum profit.
Format
Sample 5
Input 1 15231
Sample 4
Output 1
Test Case 6
Input 1 1 2 4 9 1 10
Test Case 9
Output 1
Test Case 7
Input 2 10 1 3 8 2 6 9

Test Case 8
Output 2

22
Maximum Subarray Sum
Given an array with N elements. Print the sum of the subarray having maximum
sum (brute force version of Kadane Algorithm). The elements of the array could
be positive, negative or zero. Also discuss the complexity of your approach.
Input Format First line contains the value of N. The next line has N space separated integers.
Output A single integer in a single line.
Format
Sample Input 7
1 8 -1 3 4 -3 2 1
Sample 14
Output 1
Test Case 12
Input 1
5 2 -6 1 4 2 -2 -3 7 2 11 -14
Test Case 23
Output 1
Test Case 16
Input 2 5 2 -6 1 4 2 -2 -3 7 2 10 -14 -18 -2 -3 -4
Test Case 22
Output 2

Row-wise Sum, Max, and Min


23 An input is given in the following [Link] first line has two space separated
integers N and M, denoting the number of rows and columns of a matrix. Next N
lines have M space-separated integers. Input should be taken as a list of lists.
Print the sum, maximum, and minimum of each row, in a single line, separated
by space.
Input First line has 2 space separated integers, denoting N and M. Next N lines have M
Format space separated integers.
Output N lines, each having M values.
Format
Sample 35
Input 12 4 7 9 2
1 5 8 3 13
2 42 1 6 9
Sample 34 12 2
Output 30 13 1
60 42 1
Test Case 45
Input 1 12 16 17 20 21
1 12 33 34 56
12 22 19 18 22
11 10 18 10 20
Test Case 86 21 12
Output 1 136 56 1
93 22 12
69 20 10
Test Case 55
Input 2 10 11 12 13 14
-15 16 33 67 22
33 44 -55 56 22
1 2 3 4 -5
12345
Test Case 60 14 10
Output 2 123 67 -15
100 56 -55
5 4 -5
15 5 1
Submatrix
24 Given a matrix with N rows and M columns (N, M >= 6). Print a submatrix
obtained from mat after removing the first row, first column, last two rows, and
last two columns. Use nested loops.
Input First row consists of N and M. Next N rows contain M space separated elements.
Format Read the elements as matrix (mat).
Output N - 3 rows of integers, each having M - 2 elements. See sample below.
Format
Sample 67
Input 1111111
2222222
3333333
4444444
5555555
6666666
Sample 2222
Output 3333
4444
Test Case 78
Input 1 11111111
22222222
33333333
44444444
55555555
66666666
77777777
Test Case 22222
Output 1 33333
44444
55555

Swap Matrix Elements


26 You are given a matrix with N rows and M columns. Swap each element with the
element in its immediate bottom-right cell (if the cell exists). Print the modified
matrix.
Input Format First line of input contains the two integers denoting the values of N and M. The
next N lines contain M space separated integers.
Output N lines, each with M space separated integers.
Format
Sample Input 34
1 24 29 12 33
25 36 18 17
31 22 43 67
Sample 36 18 17 33
Output 1 22 43 67 12
31 25 24 29
Test case 25
input 1 12345
09876
Test case 98765
output 1 01234

—----------------------------------------------------------------------------------------------------------------------------

Assignment Questions -

Problem 1 Concatenation of String


1 Write a program to read a string from the console, concatenate "a student" to it
and print it back to the console.
Input The only line of input has a string.
Format
Output A single string.
Format
Sample Quick
Input
Sample Quick a student
Output

Name Age Roll Number


2 Read name, age and roll number from three different lines and print them back to
the console, in a single line.
Input Three strings in three separate lines.
Format
Output A string in a single line.
Format
Sample Ram
Input 32
4567
Sample Ram 32 4567
Output

Add an Integer
3 Read a number from the console, add 3 to it and print it.
Input The first line contains the number.
Format
Output A single integer.
Format
Sample 2
Input
Sample 5
Output

Float divided by integer


4 Read a floating-point number from the console, divide it by 2, and print it.
Input The first line contains the number.
Format
Output A single floating-point number.
Format
Sample 2.5
Input
Sample 1.25
Output

Sum of integers
5 Read four integers from the console and print their sum. The input will be a
single line containing the four integers, separated by single whitespaces.
Input Four integers in a single line separated by space.
Format
Output A single integer.
Format
Sample 2 8 9 12
Input
Sample 31
Output

Greater Average
6 You are given 3 numbers A, B, and C. Determine whether the average of A and B is
strictly greater than C or not?
NOTE: Average of A and B is defined as (A+B)/2. For example, average of 5 and 9
is 7, average of 5 and 8 is 6.5.
Input Single line of input contains three space separated integers.
Format
Output "YES" or "NO" without quotes.
Format
Sample 596
Input 1
Sample YES
Output 1

Valid Phone Number


7 In PhoneLand, a valid phone number consists of 5 digits with no leading zeros.

For example, 98765, 10000, and 71023 are valid phone numbers, while 04123,
9231, and 872310 are not.

Alex went to a store and purchased N items, where the cost of each item is Y.
Determine whether the total bill is equivalent to a valid phone number.
Input The first line of input will consists of two space-separated integers N and X — the
Format number of items Alex bought and the cost per item.
Output For each test case, output on a new line, YES, if the total bill is equivalent to a valid
Format phone number and NO otherwise.
Sample 25 785
Input 1
Sample YES
Output 1

String Validation

8 Given a string S, determine if it contains both the characters '&' and '#' and the
length of the string is even. If both conditions are satisfied, print 'YES';
otherwise, print 'NO’.
Input Single line input string
Format
Output "YES" or “NO”.
Format
Sample abc&def#
Input 1
Sample YES
Output 1
Adding first-2 and last element-2 of list
9 Given N integers. Find the sum of the first 2 elements and last 2 elements in
separate lines. You can assume that N >= 4.
Input First line contains N. The next line contains the N space separated integers.
Format
Output Two integer in two different line.
Format
Sample 18
Input 1 2 3 4 5 6 7 8 9 10 12 13 15 21 22 45 90 100
Sample 3
Output 190

Count and Compare Odds and Evens

10 Given a list of N positive integers. You need to find out the count of odd and even
integers. If the count of odd integers are greater than the count of even integers,
print "Odd" (without quotes). If the count of even integers are greater than the
count of odd integers, print "Even" (without quotes). Print "Tie" (without quotes)
if count of odd and even integers are equal.
Input Two lines of input. First line contains an integer N, represting the total number of
Format integers. Second line of input contains N space separated integers.
Output Single string in single line.
Format
Sample 5
Input 1 10 24 3 37 38
Sample Even
Output 1

Group-wise Marks Analysis


11 You are given marks of N groups of students in the format specified below. The
strength of each group may not be the same. Print the maximum, minimum, total,
and average marks (rounded upto one place of decimal) of each group of students.

The built-in function, round(number, count), returns a floating point number that
is a rounded version of the specified number, with the specified number of
decimals (count). You can use this function in this problem.
Input First line contains the value of N, followed by N pairs of lines. The first line of each
Format pair contains the value of M and the second line of each pair contains space
separated M integers, denoting the marks of each student.
Output N lines, each having M space separated values.
Format
Sample 3
Input 1 3
12 14 16
4
11 13 18 19
6
12 14 16 18 20 13
Sample 16 12 42 14.0
Output 1 19 11 61 15.2
20 12 93 15.5

Group Salary Summation


12 You are given salaries of N groups of employees (each group has M employees) in
the format specified below. The count of employees in each group may not be the
same. Print the sum of salaries of each group of employees.
Input First line contains the value of N, followed by N pairs of lines. The first line of each
Format pair contains the value of M and the second line of each pair contains space
separated M integers, denoting the salary of each employees.
Output N lines, each having single integer.
Format
Sample 3
Input 1` 4
12000 10000 5000 8000
2
3000 2000
3
7000 9000 5000
Sample 35000
Output 1 5000
21000

Sum or Length of String


13 Read this input and print the sum of integers (if the elements are integers) or
print the length of the longest word if the elements are strings.
Input The first line of the input contains a single integer, N, denoting the number of
Format upcoming pairs of lines. The first line is followed by N pairs of lines. The first line
of each pair contains two space separated elements. The first element is an
integer, M, denoting the number of elements in the second line of the pair. And
the second element is either INT or STRING, denoting the type of elements in the
second line of the pair. The second line of each pair contains M space separated
elements (integer or string).

Output Three integers in the separated line.


Format
Sample 3
Input 4 INT
13 12 4 2
6 STRING
quick fox jump over the fall
7 INT
9876543
Sample 31
Output 5
42

Pass-Fail Marks Filter


14 You are given pass and fails status of N students. You need to find the count of
students who have a pass status and have marks greater than 75 and also find the
count of students who have a fail status and have marks less than 50.
Input The first line contains the value of N. The next N lines contain space separated two
Format values. The first value is either "Pass" or "Fail" (without the quotes), denoting the
pass/fail status of the student. And the second value is an integer denoting the
marks of the students.
Output Two integers separated by space.
Format
Sample 5
Input 1 Pass 85
Pass 77
Fail 30
Fail 57
Fail 22
Sample 22
Output 1

Insert String at Intervals

15 You are given two strings S1 and S2 and a number N. Insert S2 in S1 after every N
characters (see sample input). Print the modified String. Assume 0 < N < len(S1)
Input Three lines of Input. First line contains the string S1. The second line contains the
Format string S2. And the third line contains the number N.
Output A string in a single line.
Format
Sample quick fox
Input 1 #
2
Sample qu#ic#k #fo#x
Output 1

Matrix Rows in Single Line


16 The first line has two space separated integers N and M, denoting the number of
rows and columns of a matrix. Next N lines have M space-separated integers.
Input should be taken as a list of lists. Print all the rows in a single line.
Input First line has 2 space separated integers, denoting N and M. Next N lines have M
Format space separated integers.
Output N * M space separated integers in a single line.
Format
Sample 35
Input 12 4 7 9 2
13 5 8 3 1
42 4 1 6 9
Sample 12 4 7 9 2 13 5 8 3 1 42 4 1 6 9
Output
Set
Two Sum-1
Problem Two Sum -1
#include<bits/stdc++.h>
using namespace std;

bool fun(vector<int> &arr, int n, int k){


unordered_set<int> st;
for(int i = 0; i<n; i++){
int temp = k - arr[i];
if([Link](temp) != [Link]()) return true;
else [Link](arr[i]);
}
return false;
}

int main(){
int n,k;
cin>>n>>k;
vector<int> arr(n);
for(int i = 0; i<n; i++) cin>>arr[i];
cout<<(fun(arr, n, k)? "YES":"NO");
}
Two Sum-2
Problem Two Sum -1
Sum Equals to Sum
Problem
#include <bits/stdc++.h>
using namespace std;

bool fun(vector<int> &arr, int N) {


unordered_set<int> mySet1([Link](), [Link]());
vector<int> v([Link](), [Link]());
int n = [Link]();
unordered_set<int> mySet2;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
int sum = v[i] + v[j];
if ([Link](sum)) return true;
[Link](sum);
}
}
return false;
}

int main() {
int N;
cin >> N;
vector<int> arr(N);
for (int i = 0; i < N; i++) cin >> arr[i];
cout << (fun(arr, N) ? "YES" : "NO");
return 0;
}
uncommon words
Problem
#include<bits/stdc++.h>
using namespace std;

vector<string> fun(string &str1,


string &str2) {

stringstream s1(str1);
stringstream s2(str2);
unordered_map<string, int> freq;
string word;
vector<string> temp, result;

while(s1 >> word) {


if(freq[word] == 0) temp.push_back(word);
freq[word]++;

while(s2 >> word) {


if(freq[word] == 0) temp.push_back(word);
freq[word]++;
}

for(auto i: temp) {
if(freq[i] == 1) result.push_back(i);
}
return result;
}

int main() {
string s1, s2;
getline(cin, s1);
getline(cin, s2);
vector<string> result = fun(s1, s2);
if([Link]()){
for (auto &r : result) cout << r << " ";}
else
cout<< -1;
}
Majority element
Problem
#include<bits/stdc++.h>
using namespace std;

int fun(vector<int> &arr, int n){


unordered_map<int, int> freq;
int fc = n/2;
for(auto i: arr){
freq[i]++;
}
for(auto i: freq){
if([Link] > fc) return [Link];
}
return -1;
}

int main(){
int n; cin>>n;
vector<int> arr(n);
for(auto &i: arr) cin>>i;
cout<<fun(arr, n);
}
Most frequent even
Problem
#include<bits/stdc++.h>
using namespace std;

bool fun1(pair<int, int> p1, pair<int, int> p2){


if([Link] != [Link])
return [Link] > [Link];
else
return [Link] < [Link];
}

int fun(vector<int> &arr, int n){


unordered_map<int, int> freq;
for(auto i : arr)
freq[i]++;
vector<pair<int, int>> vec([Link](), [Link]());
sort([Link](), [Link](), fun1);
for(auto i : vec){
if([Link] % 2 == 0)
return [Link];
}
return -1;
}

int main(){
int n;
cin >> n;
vector<int> arr(n);
for(auto &i : arr)
cin >> i;
cout << fun(arr, n);
}
Find lucky Integer
Problem
#include<bits/stdc++.h>
using namespace std;

int fun(vector<int> &arr, int n){


int result = -1;
unordered_map<int,int> freq;
for(auto i: arr) freq[i]++;
for(auto i:freq){
if([Link] == [Link]) result = max(result, [Link]);
}
return result;
}

int main(){
int n; cin>>n;
vector<int> arr(n);
for(auto &i: arr) cin>>i;
cout<<fun(arr, n);

}
Count pairs with abs diff k-1
Problem
#include<bits/stdc++.h>
using namespace std;

int fun(vector<int> &arr, int n, int k){


int count = 0;
unordered_map<int, int> mp;
for(auto i : arr){
count += mp[i - k];
count += mp[i + k];
mp[i]++;
}
return count;
}

int main(){
int n, k;
cin >> n >> k;
vector<int> arr(n);
for(auto &i : arr)
cin >> i;
cout << fun(arr, n, k);
}
count pairs with abs diff - 2
Problem
#include<bits/stdc++.h>
using namespace std;

int fun(vector<int> &arr, int n, int k){


int count = 0;
unordered_map<int, int> mp;
for(auto i : arr){
if(k == 0) count += mp[i];
else{
count += mp[i - k];
count += mp[i + k];
}
mp[i]++;
}
return count;
}

int main(){
int n, k;
cin >> n >> k;
vector<int> arr(n);
for(auto &i : arr)
cin >> i;
cout << fun(arr, n, k);
}
Map
Sliding window
K length Window Max-Sum
Problem K length window max sum
C++ #include<bits/stdc++.h>
using namespace std;

int fun(vector<int> &arr, int n, int k){


int start = 0, sum = 0;
int result = INT_MIN;
for(int end = 0; end<n; end++){
sum += arr[end];
if(end - start + 1 == k){
result = max(result, sum);
sum -= arr[start++];
}
}
return result;
}

int main(){
int n, k;
cin>>n>>k;
vector<int> arr(n);
for(auto &i: arr) cin>>i;
cout<<fun(arr, n, k);
}
Python def fun(arr, n, k):
start = 0
sum = 0
result = float('-inf')
for end in range(0, n):
sum += arr[end]
if(end-start+1) == k:
result = max(result, sum)
sum -= arr[start]
start += 1
return result

n, k = map(int, input().split())
arr = list(map(int, input().split()))
print(fun(arr,n,k))
Java
import [Link];
import [Link];
import [Link];
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new
InputStreamReader([Link]));

StringTokenizer st = new StringTokenizer([Link]());


int n = [Link]([Link]());
int k = [Link]([Link]());

int[] arr = new int[n];


st = new StringTokenizer([Link]());
for (int i = 0; i < n; i++) {
arr[i] = [Link]([Link]());
}

[Link](fun(arr, n, k));
}
}
Size k subarray with given avg
Problem Size K subarrays with given average
#include<bits/stdc++.h>
using namespace std;

int fun(vector<int> &arr, int n, int k, int t){


int start = 0;
float sum = 0;
int count = 0;
for(int end = 0; end<n; end++){
sum += arr[end];
if(end - start + 1 == k){
if(sum/k >= t) count++;
sum -= arr[start++];
}
}
return count;
}

int main(){
int n, k, t;
cin>>n>>k>>t;
vector<int> arr(n);
for(auto &i: arr) cin>>i;
cout<<fun(arr, n, k, t);
}
First Negative Integer
Problem
#include<bits/stdc++.h>
using namespace std;

void fun(vector<int> &arr,vector<int> &result, int n, int k)


{
int start = 0;
queue<int> negatives;
for(int end = 0; end<n; end++){
if(arr[end] < 0) [Link](arr[end]);
if(end - start + 1 == k){
if([Link]() != 0)
result.push_back([Link]());
else
result.push_back(0);
if(arr[start] < 0) [Link]();
start++;
}
}
}

int main(){
int n, k;
cin>>n>>k;
vector<int> arr(n);
vector<int> result;
for(auto &i: arr) cin>>i;
fun(arr, result, n, k);
for(auto i: result) cout<<i<<" ";
}
Max vowels in a substring
Problem
#include<bits/stdc++.h>
using namespace std;

int fun(string &s, int n, int k){


int start = 0;
int count = 0;
unordered_set<char> vowels = {'a','e','i','o','u'};
int result = 0;
for(int end = 0; end<n; end++){
if([Link](s[end])!=[Link]()) count++;
if(end - start + 1 == k){
result = max(result, count);
if([Link](s[start])!= [Link]()) count--;
start++;
}
}
return result;
}

int main(){
int n, k;
cin>>n>>k;
string s;
cin>>s;
cout<<fun(s, n, k);
}
K consecutive black blocks
Problem
#include<bits/stdc++.h>
using namespace std;

int fun(string &s, int n, int k){


int start = 0, count = 0, result = INT_MAX;
for(int end = 0; end < n; end++){
if(s[end] == 'W') count++;
if(end - start + 1 == k){
result = min(result, count);
if(s[start] == 'W') count--;
start++;
}
}
return result;
}

int main(){
int n, k;
cin>>n>>k;
string s;
cin>>s;
cout<<fun(s, n, k);
}
Obtain Max Points
Problem
#include<bits/stdc++.h>
using namespace std;

int fun(vector<int> &arr, int n, int k){


int start = 0, sum = 0, result = INT_MAX;
for(int end = 0; end < n; end++){
sum += arr[end];
if(end - start + 1 == k){
result = min(result, sum);
sum -= arr[start];
start++;
}
}
return result;
}

int main(){
int n, k;
cin>>n>>k;
vector<int> arr(n);
for(auto &i: arr) cin>> i;
int sum = accumulate([Link](), [Link](), 0);
if(k == n) cout<< sum;
else
cout<<sum - fun(arr, n, n-k);
}
**permutation in a string
Problem
#include<bits/stdc++.h>
using namespace std;

bool fun(string &s1, string &s2, int n1, int n2){


int start = 0;
unordered_map<char, int> freq1, freq2;
for(auto i: s1) freq1[i]++;
for(int end = 0; end < n2; end++){
freq2[s2[end]]++;
if(end - start + 1 == n1){
if(freq1 == freq2) return true;
freq2[s2[start]]--;
if(freq2[s2[start]] == 0) [Link](s2[start]);
start++;
}
}
return false;
}

int main(){
int n1, n2; cin>>n1>>n2;
string s1, s2; cin>>s1>>s2;
cout<<(fun(s1, s2, n1, n2)?"True":"False");
}
all anagram
#include<bits/stdc++.h>
using namespace std;

vector<int> fun(string &s1, string &s2, int n1, int n2){


int start = 0;
vector<int> result;
unordered_map<char, int> freq1, freq2;
for(auto i: s1) freq1[i]++;
for(int end = 0; end < n2; end++){
freq2[s2[end]]++;
if(end - start + 1 == n1){
if(freq1 == freq2)
result.push_back(start);
freq2[s2[start]]--;
if(freq2[s2[start]] == 0) [Link](s2[start]);
start++;
}
}
return result;
}

int main(){
int n1, n2; cin>>n1>>n2;
string s1, s2; cin>>s1>>s2;
vector<int> result = fun(s1, s2, n1, n2);
if([Link]()){
for(auto i: result)
cout<<i<<" ";
}
else cout<<-1;
}
Longest subarray of 1's
Start of variable length SW
Problem
Best way-
#include<bits/stdc++.h>
using namespace std;

int fun(vector<int> &arr, int n){


int start = 0, len = 0;
for(int end = 0; end < n; end++){
if(arr[end] == 0) start = end + 1;
len = max(len, end - start + 1);
}
return len;
}

int main(){
int n; cin>>n;
vector<int> arr(n);
for(auto &i: arr) cin>>i;
cout<< fun(arr, n);
}

Another Approach
#include<bits/stdc++.h>
using namespace std;

int fun(vector<int> &arr, int n){


int start = 0, len = 0, zero = 0;
for(int end = 0; end < n; end++){
if(arr[end] == 0) zero = 1;
while(zero == 1) {
if(arr[start] == 0){
zero--;
}
start++;
}
len = max(len, end - start + 1);
}
return len;
}

int main(){
int n; cin>>n;
vector<int> arr(n);
for(auto &i: arr) cin>>i;
cout<< fun(arr, n);
}
Longest subarray of 1 - 2
Problem
#include<bits/stdc++.h>
using namespace std;

int fun(vector<int> &arr, int n){


int start = 0, count = 0, result = 0;
for(int end = 0; end < n; end++){
if(arr[end] == 0) count++;
while(count > 1){
if(arr[start] == 0) count--;
start++;
}
result = max(result, end - start + 1);
}
return result;
}

int main(){
int n; cin>>n;
vector<int> arr(n);
for(auto &i: arr) cin>>i;
cout<<fun(arr, n)-1;
}
Longest subarray of 1s with k swaps
Problem
#include<bits/stdc++.h>
using namespace std;

int fun(vector<int> &arr, int n, int k){


int start = 0, count = 0, result = 0;
for(int end = 0; end < n; end++){
if(arr[end] == 0) count++;
while(count > k){
if(arr[start] == 0) count--;
start++;
}
result = max(result, end - start + 1);
}
return result;
}

int main(){
int n,k; cin>>n>>k;
vector<int> arr(n);
for(auto &i: arr) cin>>i;
cout<<fun(arr, n, k);
}
Unique length k substring
Problem
#include<bits/stdc++.h>
using namespace std;

int fun(string &s, int n, int k){


int start = 0, result = -1;
unordered_map<char, int> freq;
for(int end = 0; end < n; end++){
freq[s[end]]++;
while([Link]() > k){
freq[s[start]]--;
if(freq[s[start]] == 0){
[Link](s[start]);
}
start++;
}
if([Link]() == k)
result = max(result, end - start + 1);
}
return result;
}

int main(){
int n,k; cin>>n>>k;
string s; cin>>s;
cout<<fun(s, n, k);
}
collect fruits
#include<bits/stdc++.h>
using namespace std;

int fun(vector<int> &trees, int n){


int start = 0, maxfruits = 0;
unordered_map<int, int> freq;
for(int end = 0; end < n; end++){
freq[trees[end]]++;
while([Link]() > 2){
freq[trees[start]]--;
if(freq[trees[start]] == 0){
[Link](trees[start]);
}
start++;
}
maxfruits = max(maxfruits, end - start + 1);
}
return maxfruits;
}

int main(){
int n; cin>>n;
vector<int> trees(n);
for(auto &i: trees) cin>>i;
cout<<fun(trees, n);
}
Longest substring with distinct words
#include<bits/stdc++.h>
using namespace std;

int fun(vector<string> &arr, int n){


unordered_set<string> freq;
int start = 0, count = 0;
for(int end = 0; end < n; end++){
while([Link](arr[end])){
[Link](arr[start]);
start++;
}
[Link](arr[end]);
count = max(count, end - start + 1);
}
return count;
}

int main(){
int n; cin>>n;
vector<string> words(n);
for(string &i: words) cin>>i;
cout<<fun(words, n);
}
Not all flavors
#include<bits/stdc++.h>
using namespace std;

int fun(vector<int> &arr, int n, int k){


int start = 0, count = 0;
unordered_map<int,int> flavors;
for(int end = 0; end < n; end++){
flavors[arr[end]]++;
while([Link]() == k){
flavors[arr[start]]--;
if(flavors[arr[start]] == 0) [Link](arr[start]);
start++;
}
count = max(count, end - start + 1);
}
return count;
}

int main(){
int n, k; cin>>n>>k;
vector<int> arr(n);
for(auto &i: arr) cin>>i;
cout<<fun(arr, n, k);
}
Prefix Sum
Running sum of 1d array
Prefix Sum, total - 13Q

#include<bits/stdc++.h>
using namespace std;

vector<int> fun(vector<int> &arr, int n){


vector<int> result;
int s = 0;
for(int start = 0; start < n; start++){
s += arr[start];
result.push_back(s);
}
return result;
}

int main(){
int n; cin>>n;
vector<int> arr(n);
for(auto &i: arr) cin>>i;
vector<int> result = fun(arr, n);
for(auto i: result) cout<<i<<" ";
}
Find Pivot Index
#include<bits/stdc++.h>
using namespace std;

int fun(vector<int> &arr, int n){


int sum = accumulate([Link](), [Link](), 0);
int prefixsum = 0;
for(int i = 0; i<n; i++){
if(sum - (prefixsum + arr[i]) == prefixsum) return i;
prefixsum += arr[i];
}
return -1;
}

int main(){
int n; cin>>n;
vector<int> arr(n);
for(auto &i: arr) cin>>i;
cout<<fun(arr, n);
}
Largest Subarray with sum K
Tab 32
Tab 33
Tab 34
Tab 35
Tab 36
Tab 37
Tab 38
Tab 39
q25to50
25. Mod 3
#include<bits/stdc++.h>
using namespace std;

int main(){
int l, h;
cin>>l>>h;
int count = 0;
for(int i = l; i<=h; i++){
if(i%3 == 0){
int sum = 0;
int num = i;
while(num > 0){
int d = num%10;
sum += d;
num = num/10;
}
if((sum % 2) == 0) count++;
}
}
cout<<count;
}
26. Single digit number
#include<bits/stdc++.h>
using namespace std;

int fun(int n){


if(n%9 == 0) return 9;
else return n%9;
}

int main(){
int n, k;
cin>>n>>k;
int singledigit = fun(n);
cout<<fun(singledigit*k);
}
27. Even sum primes
#include<bits/stdc++.h>
using namespace std;

bool isPrime(int n){


if(n == 1) return false;
for(int i = 2; i*i<=n; i++){
if(n%i == 0) return false;
}
return true;
}

bool evenDigitSum(int n){


int digit;
int sum = 0;
while(n > 0){
digit = n%10;
sum += digit;
n /= 10;
}
if(sum % 2 == 0) return true;
else return false;
}

int main(){
int a, b; cin>>a>>b;
int count = 0;
for(int i = a; i <= b; i++){
if(isPrime(i) && evenDigitSum(i)) count++;
}
cout<<count;
}

28. All Arnstrongs


#include<bits/stdc++.h>
using namespace std;

int countDigits(int n){


int count = 0;
while(n > 0){
count++;
n /= 10;
}
return count;
}

bool isArmstrong(int n){


int digitCount = countDigits(n);
int sum = 0;
int temp = n;
while(temp > 0){
int digit = temp%10;
sum += pow(digit, digitCount);
temp /= 10;
}
if(sum == n) return true;
else return false;

int main(){
int lower, upper;
cin>>lower>>upper;
bool flag = false;
for(int i = lower; i <= upper; i++){
if(isArmstrong(i)){
if(!flag) flag = true;
cout<<i<<" ";
}
}
if(!flag) cout<<-1;
}

29. Jumping number


#include<bits/stdc++.h>
using namespace std;

bool isJumping(int n){


int digit;
int cdigit = n%10;
while(n > 9){
n /= 10;
int ndigit = n%10;
if(abs(cdigit - ndigit) != 1) return false;
cdigit = ndigit;
}
return true;
}

int main(){
int n; cin>>n;
for(int i = 0; i<=n; i++){
if(i < 10) cout<<i<<" ";
else{
if(isJumping(i)) cout<<i<<" ";
}
}
}
30. Nth tribonacci number
#include<bits/stdc++.h>
using namespace std;

int tribo(int n){


if(n == 0 || n == 1 || n == 2) return 1;
vector<int> temp(n+1);
temp[0] = 1, temp[1] = 1, temp[2] = 1;
for(int i = 3; i <= n; i++){
temp[i] = temp[i-1]+temp[i-2]+temp[i-3];
}
return temp[n];
}

int main(){
int n; cin>>n;
cout<<tribo(n);
}
31. Prime Multiplied Series

32. Class Position


#include<bits/stdc++.h>
using namespace std;

int main(){
int n; cin>>n;
vector<int> arr(n);
for(auto &i: arr) cin>>i;
for(int i = 0; i < n; i++){
int count = 0;
for(int j = 0; j < n; j++){
if(arr[j] > arr[i]) count++;
}
cout<<count+1<<" ";
}
}
33.

34.
35.

36.

You might also like