0% found this document useful (0 votes)
20 views11 pages

Self-Practice - Problem Solving - Day 05

The document outlines a set of programming problems for SDE readiness training, covering topics such as basic math, control flow, arrays, strings, functions, and bit manipulation. Each problem includes a statement, examples, and constraints, focusing on various algorithmic challenges like counting ways to climb stairs, finding prime factors, and maximizing values under specific conditions. The problems are designed to test and enhance problem-solving skills in a coding interview context.

Uploaded by

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

Self-Practice - Problem Solving - Day 05

The document outlines a set of programming problems for SDE readiness training, covering topics such as basic math, control flow, arrays, strings, functions, and bit manipulation. Each problem includes a statement, examples, and constraints, focusing on various algorithmic challenges like counting ways to climb stairs, finding prime factors, and maximizing values under specific conditions. The problems are designed to test and enhance problem-solving skills in a coding interview context.

Uploaded by

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

PROBLEM SOLVING

SDE Readiness Training

Self-Practice No. :V

Topics Covered : Basic Math, Control Flow, Arrays, Strings, Functions, Bit
Manipulation

Date : 19.02.2025

Solve the following problems

Q
Question Detail Level
No.

1 Count ways to reach the n'th stair Medium


Problem Statement : There are n stairs, a person standing
at the bottom wants to reach the top. The person can climb
either 1 stair or 2 stairs at a time. Count the number of ways,
the person can reach the top (order does not matter).

Example 1:
Input:
n=4
Output: 3
Explanation:
Three ways to reach at 4th stair. They are {1, 1, 1, 1}, {1, 1,
2}, {2, 2}.

Example 2:
Input:
n=5
Output: 3
Explanation:
Three ways to reach at 5th stair. They are {1, 1, 1, 1, 1}, {1,
1, 2, 1} and {1, 2, 2}.

Constraints:
1 ≤ n ≤ 10^4

Little practice is worth more than a ton of theory


1
PROBLEM SOLVING
SDE Readiness Training
2 Largest prime factor Medium
Problem statement : Given a number N, the task is to find
the largest prime factor of that number.

Example 1:
Input:
N=5
Output:
5
Explanation:
5 has 1 prime factor i.e 5 only.

Example 2:
Input:
N = 24
Output:
3
Explanation:
24 has 2 prime factors 2 and 3 in which 3 is greater.

Constraints:
2 <= N <= 10^9

3 Number of Steps to Reduce a Number in Binary Medium


Representation to One
Problem Statements: Given the binary representation of an
integer as a string s, return the number of steps to reduce it
to 1 under the following rules: If the current number is even,
you have to divide it by 2.If the current number is odd, you
have to add 1 to it.It is guaranteed that you can always reach
one for all test cases.
Example 1:
Input: s = "1101"
Output: 6
Explanation: "1101" corresponds to number 13 in their
decimal representation.
Step 1) 13 is odd, add 1 and obtain 14.

Little practice is worth more than a ton of theory


2
PROBLEM SOLVING
SDE Readiness Training
Step 2) 14 is even, divide by 2 and obtain 7.
Step 3) 7 is odd, add 1 and obtain 8.
Step 4) 8 is even, divide by 2 and obtain 4.
Step 5) 4 is even, divide by 2 and obtain 2.
Step 6) 2 is even, divide by 2 and obtain 1.

Example 2:
Input: s = "10"
Output: 1

Constraints:
● 1 <= s.length <= 500
● s consists of characters '0' or '1'
● s[0] == '1'

4 Rectangular numbers Medium


Problem statement : Print the pattern in such a way that
the outer rectangle is of the number ‘N’ and the number goes
on decreasing as we move inside the rectangles.
For example, if ‘N’ = 4, then pattern will be:

4444444
4333334
4322234
4321234
4322234
4333334
4444444
.
Sample Input 1:
2
2
1
Sample Output 1:
222
212
222

Little practice is worth more than a ton of theory


3
PROBLEM SOLVING
SDE Readiness Training
1
Explanation Of Sample Input 1:
Test case 1:
For the first test case of sample output 1, as the number is 2,
so the outermost rectangle is of number 2. The moment we
get inside the rectangle, we reduce the number by 1 and
make another rectangle.
Test case 2:
For the second test case of sample output 1, as the number is
1, so the outermost rectangle is of number 1.

Sample Input 2:
1
4
Sample Output 2:
4444444
4333334
4322234
4321234
4322234
4333334
4444444
Explanation Of Sample Input 2:
Test case 1:
For the first test case of sample output 2, as the number is 4,
so the outermost rectangle is of number 24. The moment we
get inside the rectangle, we reduce the number by 1 and
make another rectangle. This process goes on till we reach 1.

Constraints:
1 <= T <= 5
1 <= N <= 100
5 Find Xor-Beauty of Array Medium
Problem Statement: You are given a 0-indexed integer
array nums.The effective value of three indices i, j, and k is
defined as ((nums[i] | nums[j]) & nums[k]).The xor-beauty
of the array is the XORing of the effective values of all the

Little practice is worth more than a ton of theory


4
PROBLEM SOLVING
SDE Readiness Training
possible triplets of indices (i, j, k) where 0 <= i, j, k <
n.Return the xor-beauty of nums.
Note that:
● val1 | val2 is bitwise OR of val1 and val2.
● val1 & val2 is bitwise AND of val1 and val2.

Example 1:
Input: nums = [1,4]
Output: 5
Explanation:
The triplets and their corresponding effective values are listed
below:
- (0,0,0) with effective value ((1 | 1) & 1) = 1
- (0,0,1) with effective value ((1 | 1) & 4) = 0
- (0,1,0) with effective value ((1 | 4) & 1) = 1
- (0,1,1) with effective value ((1 | 4) & 4) = 4
- (1,0,0) with effective value ((4 | 1) & 1) = 1
- (1,0,1) with effective value ((4 | 1) & 4) = 4
- (1,1,0) with effective value ((4 | 4) & 1) = 0
- (1,1,1) with effective value ((4 | 4) & 4) = 4
Xor-beauty of array will be bitwise XOR of all beauties = 1 ^
0 ^ 1 ^ 4 ^ 1 ^ 4 ^ 0 ^ 4 = 5.

Example 2:
Input: nums = [15,45,20,2,34,35,5,44,32,30]
Output: 34

Constraints:
● 1 <= nums.length <= 10^5
● 1 <= nums[i] <= 10^9

6 Maximum AND value Medium


Problem Statement: Given an array arr[] of N positive elements.
The task is to find the Maximum AND Value generated by any
pair(arri, arrj) from the array such that i != j.
Note: AND is bitwise '&' operator.

Little practice is worth more than a ton of theory


5
PROBLEM SOLVING
SDE Readiness Training
Example 1:
Input:
N=4
arr[] = {4, 8, 12, 16}
Output: 8
Explanation:
Pair (8,12) has the Maximum AND Value 8.

Example 2:
Input:
N=4
arr[] = {4, 8, 16, 2}
Output: 0
Explanation: Any two pairs of the array has
Maximum AND Value 0.

Constraints:
1 <= N <= 10^5
1 <= arr[i] <= 10^5
7 Stickler Thief Medium
Problem Statement: Stickler the thief wants to loot money
from a society having n houses in a single line. He is a weird
person and follows a certain rule when looting the houses.
According to the rule, he will never loot two consecutive
houses. At the same time, he wants to maximize the amount
he loots. The thief knows which house has what amount of
money but is unable to come up with an optimal looting
strategy. He asks for your help to find the maximum money he
can get if he strictly follows the rule. ith house has a[i] amount
of money present in it.

Example 1:
Input:
n=5
a[] = {6,5,5,7,4}
Output:
15

Little practice is worth more than a ton of theory


6
PROBLEM SOLVING
SDE Readiness Training
Explanation:
Maximum amount he can get by looting 1st, 3rd and 5th
house. Which is 6+5+4=15.

Example 2:
Input:
n=3
a[] = {1,5,3}
Output:
5
Explanation:
Loot only 2nd house and get maximum amount of 5.

Constraints:
1 ≤ n ≤ 10^5
1 ≤ a[i] ≤ 10^4
8 Max Consecutive Ones Medium

Problem Statement : Given a binary array arr[] (containing


only 0s and 1s) and an integer k, you are allowed to flip at
most k 0s to 1s. Find the maximum number of consecutive
1's that can be obtained in the array after performing the
operation at most k times.

Examples:

Input: arr[] = [1, 0, 1], k = 1


Output: 3
Explanation: Maximum subarray of consecutive 1's is of size 3
which can be obtained after flipping the zero present at the
1st index.Input: arr[] = [1, 0, 0, 1, 0, 1, 0, 1], k = 2
Output: 5
Explanation: By flipping the zeroes at indices 4 and 6, we get
the longest subarray from index 3 to 7 containing all 1’s.
Input: arr[] = [1, 1], k = 2
Output: 2
Explanation: Since the array is already having the max
consecutive 1's, hence we dont need to perform any
operation. Hence the answer is 2

Little practice is worth more than a ton of theory


7
PROBLEM SOLVING
SDE Readiness Training
Constraints:
1 <= arr.size() <= 10^5
0 <= k <= arr.size()
0 <= arr[i] <= 1

9 Remove Consecutive Duplicates From String Medium


Problem statement : You are given a string 'STR' consisting of
lower and upper case characters. You need to remove the
consecutive duplicates characters, and return the new string.

Sample Input 1 :
aabccba
Sample Output 1 :
abcba
Explanation of Sample Output 1 :The string basically is a
concatenation of [aa][b][cc][b][a] without considering the brackets.
From each segment we need to choose only 1 character as all the
characters are duplicates, therefore the final answer is
[a][b][c][b][a] = abcba
Sample Input 2 :

xxxyyyzwwzzz
Sample Output 2 :
xyzwz
Explanation of Sample Output 2 :The string basically is a
concatenation of [xxx][yyy][z][ww][zzz]. After choosing 1 character
from each segment our final answer is [x][y][z][w][z] = xyzwz

Constraints :
1 <= |S| <= 10^5
Where |S| represents the length of the string.
10 The Celebrity Problem Medium

Problem statement : There are ‘N’ people at a party. Each

person has been assigned a unique id between 0 to 'N' - 1(both


inclusive). A celebrity is a person who is known to everyone but
does not know anyone at the party.

Little practice is worth more than a ton of theory


8
PROBLEM SOLVING
SDE Readiness Training
Given a helper function ‘knows(A, B)’, It will returns "true" if the
person having id ‘A’ know the person having id ‘B’ in the party,
"false" otherwise. Your task is to find out the celebrity at the party.
Print the id of the celebrity, if there is no celebrity at the party then
print -1.

Note:1. The helper function ‘knows’ is already implemented


for you.
2. ‘knows(A, B)’ returns "false", if A doesn't know B.
3. You should not implement helper function ‘knows’, or
speculate about its implementation.
4. You should minimize the number of calls to function
‘knows(A, B)’.
5. There are at least 2 people at the party.
6. At most one celebrity will exist.

Sample Input 1:

1
2
Call function ‘knows(0, 1)’ // returns false
Call function ‘knows(1, 0)’ // returns true

Sample Output 1:

Explanation For Sample Input 1:

In the first test case, there are 2 people at the party. When
we call function knows(0,1), it returns false. That means the
person having id ‘0’ does not know a person having id ‘1'.
Similarly, the person having id ‘1’ knows a person having id
‘0’ as knows(1,0) returns true. Thus a person having id ‘0’ is
a celebrity because he is known to everyone at the party but
doesn't know anyone.

Little practice is worth more than a ton of theory


9
PROBLEM SOLVING
SDE Readiness Training
Sample Input 2:

1
2
Call ‘knows(0, 1)’ // returns true
Call ‘knows(1, 0)’ // returns true
2
Call ‘knows(0, 1)’ // returns false
Call ‘knows(1, 0)’ // returns false

Sample Output 2:

-1
-1

Explanation For Sample Input 2:

In first test case, there are 2 people at the party. The person
having id ‘0’ knows a person having id ‘1’. The person having
id ‘1’ knows a person having id ‘0’. Thus there is no celebrity
at the party, because both know each other.
In second test case, there are 2 people at the party. The
person having id ‘0’ does not knows a person having id ‘1’.
The person having id ‘1’ also does not knows a person having
id ‘0’. Thus there is no celebrity at the party, because both
does not know each other.

Constraints:

1 <= T <= 50
2 <= N <= 10^4
Where ‘T’ is the total number of test cases, ‘N’ is the number of
people at the party.

11 Product of Array Except Self Medium

Problem Statement : Given an array, arr[], construct a product


array, pro[] where each element pro[i] is the product of all elements
in arr except arr[i]. Return this resultant array, pro[].

Little practice is worth more than a ton of theory


10
PROBLEM SOLVING
SDE Readiness Training
Examples:

Input: arr[] = [10, 3, 5, 6, 2]


Output: [180, 600, 360, 300, 900]
Explanation: For i=0, pro[i] = 3*5*6*2 = 180.
For i=1, pro[i] = 10*5*6*2 = 600.
For i=2, pro[i] = 10*3*6*2 = 360.
For i=3, pro[i] = 10*3*5*2 = 300.
For i=4, pro[i] = 10*3*5*6 = 900.
Input: nums[] = [12,0]
Output: [0, 12]

Input: nums[] = [0]


Output: [0]

Constraints:
1 <= arr.size() <= 10^3
0 <= arr[i] <= 10^2
arr[] may contain duplicates.

Little practice is worth more than a ton of theory


11

You might also like