@V
#include <string.h>
const char hashTable[10][5] = {"", "", "abc", "def", "ghi", "jkl","mno", "pqrs", "tuv", "wxyz"};
void printWordsUtil(int number[], int curr_digit, char output[], int n)
{
int i;
if (curr_digit == n)
{
printf("%s ", output);
return ;
}
for (i=0; i<strlen(hashTable[number[curr_digit]]); i++)
{
output[curr_digit] = hashTable[number[curr_digit]][i];
printWordsUtil(number, curr_digit+1, output, n);
if (number[curr_digit] == 0 || number[curr_digit] == 1)
return;
}
}
void printWords(int number[], int n)
{
char result[n+1];
result[n] ='\0';
printWordsUtil(number, 0, result, n);
}
int main(void)
{
int number[] = {2, 3, 4};
int n = sizeof(number)/sizeof(number[0]);
printWords(number, n);
return 0;
}
Question 3:-
Question 2 :-
Given an array of strictly the characters 'R', 'G', and 'B', segregate the values of the array so that all the Rs come first, the Gs come second, and the Bs come last. You can only swap elements of the array.
Do this in linear time and in-place.
For example, given the array ['G', 'B', 'R', 'R', 'B', 'R', 'G'], it should become ['R', 'R', 'R', 'G', 'G', 'B', 'B'].
FIRST TRY IT YOUR SELF THAN SEE..
If you find Solution in other language comment it comment section Give your Contribution and show your capabilities..
Code Solution :-
link to follow me :- httpgram.com/abhishek._.yadav._/?hl=ens://www.insta
EASY_LEVEL
Question 1:-
Given a array of numbers representing the stock prices of a company in chronological order, write a function that calculates the maximum profit you could have made from buying and selling that stock once. You must buy before you can sell it.
CODE :-
#include<iostream>
using namespace std;
int main(){
int n; //size of an array
int j;
cout<<" Size of an Array :- ";
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i]; //insert an array
}
// purchase stock minimum
int t; // min stock
t=a[0];
for(int i=1;i<n;i++){
if(t>a[i]){
t=a[i];
}
}
// sale max price
int t1;
t1=a[0];
for(int i=0;i<n;i++){
if(a[i]>t1){
t1=a[i];
}
}
cout<<"buy minimum price stock :- "<<t<<endl;
cout<<"sale maximum price stock :- "<<t1<<endl;
cout<<"profit on stock :- "<<t1-t<<endl;
return 0;
}
INPUT :-
6
9 11 8 5 7 10
OUTPUT :-
buy minimum price stock :- 5
buy maximum price stock :- 11
profit on stock :- 6
FOLLOW ME ON INSTAGRAM
httpgram.com/abhishek._.yadav._/?hl=ens://www.insta
google program
Try to Solve it by your self I can give you Solution after a 2 Day's.. on my Solution this Page.
QUESTION OF THE WEEK :-
QUESTION 5 :-
Given a mapping of digits to letters (as in a phone number), and a digit string, return all possible letters the number could represent. You can assume each valid number in the mapping is a single digit.
CODE :-
#include <stdio.h> #include <string.h>
const char hashTable[10][5] = {"", "", "abc", "def", "ghi", "jkl","mno", "pqrs", "tuv", "wxyz"};
void printWordsUtil(int number[], int curr_digit, char output[], int n)
{
int i;
if (curr_digit == n)
{
printf("%s ", output);
return ;
}
for (i=0; i<strlen(hashTable[number[curr_digit]]); i++)
{
output[curr_digit] = hashTable[number[curr_digit]][i];
printWordsUtil(number, curr_digit+1, output, n);
if (number[curr_digit] == 0 || number[curr_digit] == 1)
return;
}
}
void printWords(int number[], int n)
{
char result[n+1];
result[n] ='\0';
printWordsUtil(number, 0, result, n);
}
int main(void)
{
int number[] = {2, 3, 4};
int n = sizeof(number)/sizeof(number[0]);
printWords(number, n);
return 0;
}
Question 4 :-
Using a read7() method that returns 7 characters from a file, implement readN(n) which reads n characters.
For example, given a file with the content “Hello world”, three read7() returns “Hello w”, “orld” and then “”.
CODE :-
#include <iostream>
#include <sstream>
#include <deque>
inline auto read7()
{
static std::istringstream is("1234567890123456789012345678901234567890");
char res[8]{};
is.get(res, 8);
return std::string(res);
}
template <typename T1, typename T2>
auto& operator<<(std::deque<T1>& deq, const T2& seq)
{
for (const auto& elm : seq)
{
deq.push_back(elm);
}
return deq;
}
template <typename T1, typename T2>
auto get(std::deque<T2>& deq, std::size_t n)
{
T1 res;
while (res.size() != n && !deq.empty())
{
res += deq.front();
deq.pop_front();
}
return res;
}
auto readN(std::size_t n)
{
static std::deque<char> buffer;
while (n > buffer.size())
{
auto old_sz = buffer.size();
buffer << read7();
if (old_sz == buffer.size())
break;
}
return get<std::string>(buffer, n);
}
Question 3:-
Given a string, find the palindrome that can be made by inserting the fewest number of characters as possible anywhere in the word.
If there is more than one palindrome of minimum length that can be made, return the lexicographically earliest one (the first one alphabetically).
For example, given the string "race", you should return "ecarace", since we can add three letters to it (which is the smallest amount to make a palindrome).
There are seven other palindromes that can be made from "race" by adding three letters, but "ecarace" comes first alphabetically.
As another example, given the string "google", you should return "elgoogle".
Question 2 :-
Do this in linear time and in-place.
For example, given the array ['G', 'B', 'R', 'R', 'B', 'R', 'G'], it should become ['R', 'R', 'R', 'G', 'G', 'B', 'B'].
FIRST TRY IT YOUR SELF THAN SEE..
If you find Solution in other language comment it comment section Give your Contribution and show your capabilities..
Code Solution :-
def sort(rgbs): | |
left_index, right_index = 0, len(rgbs) - 1 | |
while True: # move Rs to front | |
while rgbs[left_index] == 'R' and left_index < right_index: # advance to first non R | |
left_index += 1 | |
while rgbs[right_index] != 'R' and left_index < right_index: # regress to last R | |
right_index -= 1 | |
if left_index >= right_index: | |
break | |
rgbs[left_index], rgbs[right_index] = rgbs[right_index], rgbs[left_index] | |
right_index = len(rgbs) - 1 | |
while True: # move Bs to tail | |
while rgbs[left_index] != 'B' and left_index < right_index: # advance to first B | |
left_index += 1 | |
while rgbs[right_index] == 'B' and left_index < right_index: # regress to last non B | |
right_index -= 1 | |
if left_index >= right_index: | |
break | |
rgbs[left_index], rgbs[right_index] = rgbs[right_index], rgbs[left_index] | |
return rgbs | |
def main(): | |
print sort(['G', 'B', 'R', 'R', 'B', 'R', 'G']) | |
if __name__ == '__main__': | |
main() |
EASY_LEVEL
Question 1:-
Given a array of numbers representing the stock prices of a company in chronological order, write a function that calculates the maximum profit you could have made from buying and selling that stock once. You must buy before you can sell it.
For example, given [9, 11, 8, 5, 7, 10], you should return 5, since you could buy the stock at 5 dollars and sell it at 11 dollars.
FIRST TRY IT YOUR SELF THAN SAW MY SOLUTION
CODE :-
#include<iostream>
using namespace std;
int main(){
int n; //size of an array
int j;
cout<<" Size of an Array :- ";
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i]; //insert an array
}
// purchase stock minimum
int t; // min stock
t=a[0];
for(int i=1;i<n;i++){
if(t>a[i]){
t=a[i];
}
}
// sale max price
int t1;
t1=a[0];
for(int i=0;i<n;i++){
if(a[i]>t1){
t1=a[i];
}
}
cout<<"buy minimum price stock :- "<<t<<endl;
cout<<"sale maximum price stock :- "<<t1<<endl;
cout<<"profit on stock :- "<<t1-t<<endl;
return 0;
}
INPUT :-
6
9 11 8 5 7 10
OUTPUT :-
buy minimum price stock :- 5
buy maximum price stock :- 11
profit on stock :- 6
FOLLOW ME ON INSTAGRAM
httpgram.com/abhishek._.yadav._/?hl=ens://www.insta
Comments