Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements ofnums except nums[i].
Solve it without division and in O(n).
For example, given [1,2,3,4], return [24,12,8,6].
Approach:
The idea is to traverse the array couple of times from left...
Saturday, 12 February 2022
Saturday, 22 January 2022
Reverse String
Write a function that takes a string as input and reverse the same.
Example:
Given s = "hello", return "olleh".
Approach: The idea is to swap the last and the first characters till the middle is reached.
1
2
3
4
5
6
7
8
9
10
11
string reverseString(string &s) {
int low = 0, high = s.length() - 1;
while(low < high){
char t = s[low];
s[low] = s[high];
...
Saturday, 8 January 2022
Reverse Vowels of a String
Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:
Given s = "hello", return "holle".
Approach: The idea is to traverse the string from both ends while skipping the non-vowels characters from both ends.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
char* reverseVowels(char* s) {
int low = 0, high = strlen(s) - 1;
...