## Sunday, July 7, 2019

### Stock Buy & Sell Problem

Question: There is a given array of the stock price, each index denotes the price of the stock on that day. You need to find the max profit you will earn if you buy and sell the stock.

Example - Given an array - [10,5,6,8,9,3] where index 0 shows the prices of the stock on day 1 respectively.

Explanation - first, we purchase at day 2 ( price - 5) and sell at day 5 (price 9 ) profit = 9-5=4

Code -

```package com.vp.learning;

public static void main(String[] args) {

//  given stock data as
int[] prices = {10,5,6,8,9,3};
System.out.println(getMaxProfit(prices));
}

static int getMaxProfit(int[] prices) {

if(prices.length<=1)
return 0;

int i = 0 ;
int peak = prices[0];
int valley = prices[0];
int maxProfit = 0;
while(i<prices.length-1) {
while(i <prices.length-1 && prices[i]>= prices[i+1])
i++;
valley = prices[i];
while(i <prices.length-1 && prices[i]<= prices[i+1])
i++;
peak = prices[i];

maxProfit+=peak - valley ;

}
return maxProfit;
}

}
```

Explanation :

What we are doing here is as below

We are using Peak Valley Approach where are first calculate valley value in array

• valley means lower value
• peak means higher value
Now while(i <prices.length-1 && prices[i]>= prices[i+1])

This is calculating the valley values, means the less value index in the array.

second, while loop is calculating the peak value in the array after this valley index.

if you see prices[i]>= prices[i+1] this means, price at current day is greater then the price at next day , that means the next day is valley or lower value, similarly for the next while as well, the price of current is less than the next day, so the next day will be peak value

and the differences between peak and valley is actually the max profit we will earn for the day.

Time Complexity

In this case, the time complexity will be O(n)

Hope this will help you in the interview. If you have any issue, please leave us a comment.

Noeik

## Friday, May 17, 2019

### Algorithm to find if String contains only Unique characters without using any additional data structure

The string is one of the most asked questions in the Interview of Java developers.

Today we will see one of the most asked questions as to how will you find if String does not contain any duplicate characters.

There are different ways to check them, But we will only discuss the most efficient way to implement this algorithm.

### Logic.

We know that characters have only 256 ascii values and there are only 128 values for alphabet characters.
So what we will do is we will create one array of boolean of size 128 and we will update the index of array as true whenever we find the char value in the string , else if we get the same value in the string , the already true value will make the loop as false.
Also read - Find Longest Common Ancestor(LCA) Program in java

Time Complexity - O(n)
Space Complexity - O(1)

### Code -

```public class ArraysAndStringDemo {

public static void main(String[] args) {

String str = "programinjava";

System.out.println(isUnique(str));

}

public static boolean isUnique(String str) {
boolean[] char_set = new boolean[128];
for(int i =0;i<str.length();i++) {
int value =str.charAt(i); //getting the ascii value of character

if(char_set[value])     // checking if it is already in the array
return false;

char_set[value] = true;

}
return true;
}

}
```

If you have any issue in understanding the same , Please leave us a comment. Happy to help

Noeik

## Saturday, March 9, 2019

### Real time Logs of AWS Elastic Beanstalk by Command Prompt

Many of us know that one of the most important services of AWS is Elastic beanstalks. We deployed our application on Beanstalk and AWS itself manage most of the thing in it. One of them is Logs of Applications.

People usually were not able to see the tail of the logs of the application what they need to do is to go to the beanstalk and under logs, they ask for the last 100 lines of logs or full logs (which include the of the application along with the other logs ).

Today we will learn how to connect with the beanstalk application through EB CLI and see the logs.

1. Install the Python Once installed run below commands
2. python --version
3. pip --version
4. pip install awsebcli --upgrade --user
5. once ebcli installed successfully, you can check by
6. eb --version
7. define the Python Home in your Environment Variable.
eb init --interactive
You will see the below image.
• Now select the region you have deployed your application.
• After that, you will be asked to Create a new Application
• Once your application will be created you will see below screen.
• You need to select the appropriate platform you have used for your application.
• Now Once you do it you will be asked to create the SSH
Select Yes
• Then they will ask you to create the keypair

• Provide your password  and then you will see the below image
• Now you have successfully created your SSH and it is uploaded to your beanstalk as well.
• you need to attach the SSH with the Application.

eb ssh <Application-name> --settings

• You will get the list of options then it will ask for the keypair which you need to attach.
• Select the appropraite one.
• After selecting the one you will be asked to enter the password set earlier.
• You will then have the Applicaiton connected.
• Once connected successfully you will be getting below image.
Now go to
/var/log/tomcat8 ( to see the application logs, if your application is deployed on tomcat)

I Hope you will be able to connect your local machine to EB , If you have any issue  ,Please leave us a comment we will look into it.