0% found this document useful (0 votes)
12 views5 pages

Pdpra

The document contains practical coding exercises focused on algorithms for trapping rainwater, maximizing stock profits, and finding the middle of a linked list. Each section includes the aim, code implementation, and sample inputs with corresponding outputs. The solutions utilize arrays and linked lists to demonstrate efficient problem-solving techniques in Java.

Uploaded by

fjrjfg015
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

Pdpra

The document contains practical coding exercises focused on algorithms for trapping rainwater, maximizing stock profits, and finding the middle of a linked list. Each section includes the aim, code implementation, and sample inputs with corresponding outputs. The solutions utilize arrays and linked lists to demonstrate efficient problem-solving techniques in Java.

Uploaded by

fjrjfg015
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Practical : 01

AIM :Trapping Rain Water


Code :
public int maxWater(int arr[]) {
// code here
int n=arr.length;
int leftmax[]=new int[n];
leftmax[0]=arr[0];
for(int i=1; i<n; i++){
leftmax[i]=Math.max(leftmax[i-1],arr[i]);
}
int rightmax[]=new int[n];
rightmax[n-1]=arr[n-1];
for(int i=n-2; i>=0; i--){
rightmax[i]=Math.max(arr[i],rightmax[i+1]);
}
int traping=0;
for(int i=0; i<n; i++){
int waterlevel =Math.min(leftmax[i],rightmax[i]);
traping +=waterlevel-arr[i];

}
return traping;
}

Output :
For Input:
3010402
Your Output:
10
Practical : 02
AIM : Best Time to Buy and Sell Stock.
Code :
public int maxProfit(int[] prices) {
int maxProfit=0;
int minprice=Integer.MAX_VALUE;
for(int i=0; i<prices.length; i++){
int profit=prices[i]-minprice;
maxProfit=Math.max(maxProfit,profit);
minprice=Math.min(minprice,prices[i]);
}
return maxProfit;
}

Output :
Input
prices =
[3,1,4,8,7,2,5]
Output
7
Practical : 03
AIM : Best Time to Buy and Sell Stock using LinkedList.
Code :
class Node {
int data;
Node next;

Node(int data) {
this.data = data;
this.next = null;
}
}
class Solution {
public int maxProfit(int[] prices) {
Node res=new Node(0);
Node p=res;
for(int i=0; i<prices.length; i++){
Node t=new Node(prices[i]);
p.next=t;
p=p.next;
}
Node temp=res.next;
int maxProfit=0;
int minprice=Integer.MAX_VALUE;
while(temp !=null){
int profit=temp.data-minprice;
maxProfit=Math.max(maxProfit,profit);
minprice=Math.min(minprice,temp.data);
temp=temp.next;
}
return maxProfit;
}
}

Output :
Input
prices =
[3,1,4,8,7,2,5]

Output:
7
Practical : 03
AIM : Middle of the Linked List.
Code :
class Node {
int data;
Node next;
Node(int d) { data = d; next = null; }
}
class Solution {
int getMiddle(Node head) {
Node slow=head;
Node fast=head;
while(fast !=null && fast.next !=null){
slow=slow.next;
fast=fast.next.next;
}
return slow.data;
}
}

Output:
For Input:
12345
Your Output:
3
Expected Output:
3

You might also like