Post Only Coding Problems & Solutions Here | All Coding Guru's Dig In

primarily can think of 2 ways to solve the above combination problem.
using for loop and recursion.

prefer loop as i’m not sure you’re language supports tail call optimization

for i → 0 to len(str)
comb = ithStr
print(comb)
  for j → i+1 to len(str)
    comb = comb + jthStr
    print(comb)
 end forj
end fori

Note

comb = comb + jthStr

in some language + can be used for combining string. Some don’t you can use you’re language specific / write a general function todo so

Solution to problem 8
code in javascript

const outsub = (str) => {
  for(var i=0;i<=str.length;i++){
      for(var j=0;j<=str.length-i;j++){
            console.log(str.substr(i,j));
    }
  }
}
outsub('abcd');

I have to learn maps , I guess any problem can b solved by this.:joy::joy::zany_face:

You should!! map(),filter(), reduce() and zip() are really useful.
Btw, I have only used it for taking inputs in single line and type cast string to integer i.e. same as “cin >> a >> b >> c” in C++ :sweat_smile:

1 Like

Problem Number 8 Solution:

#include<iostream>
#include<cstring>
#include<string.h>
using namespace std;
int main()
{
    string str;
    cin>>str;
    int i=0,j=0,k=0;

    int size_str=str.length();
    while(i < size_str)
    {
        j=0;
        while(j<size_str-i)
        {
            cout<<"\t"<<str[j];
            j++;
            k=j;
            while(k<size_str-i)
            {
                cout<<"\t"<<str[k];
                k++;
            }
            cout<<"\n";
        }
        cout<<"\n";

        i++;
    }
    return 0;
}

Problem 9:
You are given an array of nonnegative integers. Let’s say you start at the beginning of the array and are trying to advance to the end. You can advance at most, the number of steps that you’re currently on. Determine whether you can get to the end of the array.

For example, given the array [1, 3, 1, 2, 0, 1] , we can go from indices 0 -> 1 -> 3 -> 5 , so return true .

Given the array [1, 2, 1, 0, 0] , we can’t reach the end, so return false .

the statement seems a little confusing can you tell me the source of this question?

Daily Coding Problem, It is asked by google.

1 Like

Is that the exact statement ?

Yes, this is the exact statement. This problem can be solved by dynamic programming technique.

I wrote a solution but I’m not 100% sure I understood the problem (bad practice I know :p)
what I understood:
we start from the first index, and the next index we jump to is currentindex+the value in that current index.
we keep doing this until either:

  • we get to the end
  • we go out of bound
  • the value we get to is zero (we can’t go forward anymore).

Based on that explanation, here is the function that does the job, in c++:

bool reachesEnd(int arr[] ,const int arrSize, int currentIndex){
if(currentIndex==arrSize-1) return 1;
if(currentIndex>=arrSize) return 0;
if(arr[currentIndex]==0) return 0;

return reachesEnd(arr , arrSize , currentIndex+arr[currentIndex]); }

Please inform me if I understood the problem correctly.

1 Like

Yes, you understood the problem correctly.

1 Like

for Problem 9:
Each state defines number of jumps can be made from one node to other.
for example
state 5 , represent jumps at max 5 and at min 0 and
to solve you must reach the end of the node.

Assumption

  1. All states are whole number.

edge cases

  1. If the current node is the last node.
  2. if all the nodes state are 0.
  3. if node is not end_indx and value is 0.

Pseudocode for top down approach:

func jump_resolver(arr,curr_indx)
  if curr_indx is end_indx  (reached end )
     return true;
end if

 if curr_indx past end_indx (greater than end_indx)
   return false
end if

state_val  = arr[curr_indx]
if state_val == 0 // no increment we stop here
  return false;

 for i=state_val to 1 do
     reached = jump_resolver(arr,curr_indx+i);
      if reached 
      return true;
 end for
return false; // as we tried all the jumps     

Pseudocode for bottom up approach:
a. using stack iterativerly

stack initalized with {fnstate:‘CALL’,ctxt:{indx:0}

dest = false // is end reached

while stack.hasElements()
 fnstate,ctxt= stk.pop() // here pop and get what is the current function state and context
if fnstate == 'CALL'
 
     if curr_indx past end_indx (greater than end_indx)
       dest = false
       continue; // moves to start of while
     end if

    if ctxt.indx == end_indx
        dest = true;
   
     else // inner else
       val = arr[ctxt.indx]; // fetch the value from current index of arr
               if val == 0
                  dest = false  // if it's not nth index and it's value is 0 , return false
                  continue; // move forward with while loop
                end  if
       push to stk {fnstate:'RES',ctxt:{indx:ctxt.indx} // later we will resume at context index

      for i = val to 1 do
        push to stk {fnstate:'CALL',ctxt:{indx:ctxt.indx+i}
        if dest       // on destination reach
           break;
      end for // end inner for
end while  // outer while

Note

  1. There is even better way to do this via disjoint set.
  2. using memoization for recursion would be better.

The problem can be solved by recursion [for better use DP , to avoid TLE].

Is that what you meant?

hello guys
i have a quick simple question , i want to code a food app that have an integrated delivery tracking system and chat
what am i going to need ?

A computer
Decide a tech stack
Firebase
(Search youtube for reference )

1 Like

Problem 10

GIven a string like
java is good but javascript is awesome

sort this string on the basis of number of vowels it has. so the output will be

is but is java good javascript awesome

theory
fetch string , convert to array of strings
assign each string a value based on number of vowels
sort it and display.

pseudocode

func display()
vals[] // array of vals after split(" ")  
use treeMap (modify how elements are 
             compared , here elements are string with help of spit_vowl_count)
as you keep adding str to tree , it sorts and adds the element

for val to treeMap
   console val + ' '
end for    // you can use iterator pattern if you want

end func 

func spit_vowl_count(str)
  sum = 0
  for i 0 to len(str)
  if str(i) belongs to `a` , `e`, `i`, `o`, `u`
  sum + = 1;
  end for
return sum
end func

My solution to Problem 10
Code in Javascript

//function to count vowel of each item
const countVowels = (s)=> {
  let vowels = 'aeiouAEIOU';
  let count = 0;
  for(let i = 0; i < s.length ; i++)
if (vowels.indexOf(s[i]) !== -1)
  count += 1;
  return count;
}

//function to sort accoring to the no of vowels
const vowelSort = (str) => {
  let s = str.split(' ').sort((a,b)=>{
let countA = countVowels(a);
let countB = countVowels(b);
if (countA < countB)
return -1;
if (countA > countB)
return 1;
  })

return s.join(' ');       //returning the final output
}
vowelSort('java is good but javascript is awesome');
1 Like

Problem 11

Write a function persistence that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit.

For example:
persistence(39) === 3 // because 39 = 27, 27 = 14, 14= 4 and 4 has only one digit
persistence(999) === 4 // because 9
99 = 729, 729 = 126, 126 = 12, and finally 12 = 2
persistence(4) === 0 // because 4 is already a one-digit number