Self-Practice - Problem Solving - Day 05
Self-Practice - Problem Solving - Day 05
Self-Practice No. :V
Topics Covered : Basic Math, Control Flow, Arrays, Strings, Functions, Bit
Manipulation
Date : 19.02.2025
Q
Question Detail Level
No.
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
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
Example 2:
Input: s = "10"
Output: 1
Constraints:
● 1 <= s.length <= 500
● s consists of characters '0' or '1'
● s[0] == '1'
4444444
4333334
4322234
4321234
4322234
4333334
4444444
.
Sample Input 1:
2
2
1
Sample Output 1:
222
212
222
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
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
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
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
Examples:
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
Sample Input 1:
1
2
Call function ‘knows(0, 1)’ // returns false
Call function ‘knows(1, 0)’ // returns true
Sample Output 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.
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
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.
Constraints:
1 <= arr.size() <= 10^3
0 <= arr[i] <= 10^2
arr[] may contain duplicates.