Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Find First and Last Position of Element in Sorted Array
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Solution {
public int[] searchRange(int[] nums, int target) {
int ans[] = new int[2];
ans[0] = firstOcc(nums, target);
ans[1] = lastOcc(nums, target);
return ans;
}
int firstOcc(int arr[], int target){
int index = -1;
int l = 0, r = arr.length - 1;
int mid;
while(l <= r){
mid = (l + r)/2;
if(target == arr[mid])
index = mid;
if(target <= arr[mid])
r = mid - 1;
else
l = mid + 1;

}
return index;
}

int lastOcc(int arr[], int target){
int index = -1;
int l = 0, r = arr.length - 1;
int mid;
while(l <= r){
mid = (l + r)/2;
if(target == arr[mid])
index = mid;
if(target >= arr[mid])
l = mid + 1;
else
r = mid - 1;

}
return index;
}
}
65 changes: 65 additions & 0 deletions Largest Rectangle in Histogram.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
class Solution {
public int largestRectangleArea(int[] arr) {
int n= arr.length;
int l[] = new int[n];
int r[] = new int[n];

// int n = sc.nextInt();
for(int i=0; i<n; i++) {
// arr[i]=sc.nextInt();
l[i]=-1;
r[i]=n;
}

Stack<Integer> st = new Stack<Integer>();
for(int i=0; i<n; i++){
if(st.empty()){
st.push(i);
}
else{
while(!st.empty() && arr[i]<arr[st.peek()]){

r[st.peek()]=i;
st.pop();

}
st.push(i);
}
}

while(!st.empty())
{
st.pop();
}


for( int i=n-1; i>=0; i-- ) {


if(st.empty()){
st.push(i);

}
else{
while(!st.empty() && arr[i]<arr[st.peek()]){

l[st.peek()]=i;
st.pop();

}
st.push(i);
}

}

int area = 0;
for(int i=0;i<n;i++)
{
int width = r[i]-l[i]-1;
area = Math.max(area, width*arr[i]);

}

return area;
}
}
29 changes: 29 additions & 0 deletions Maths/gcd of two numbres
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Java program to find GCD of two numbers
class Test
{
// Recursive function to return gcd of a and b
static int gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;

// base case
if (a == b)
return a;

// a is greater
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}

// Driver method
public static void main(String[] args)
{
int a = 98, b = 56;
System.out.println("GCD of " + a +" and " + b + " is " + gcd(a, b));
}
}
32 changes: 32 additions & 0 deletions Maximum Profit of Operating a Centennial Wheel
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
lass Solution {
public:
int minOperationsMaxProfit(vector<int>& customers, int boardingCost, int runningCost) {
int c=0;
int rem=0;
int r=0;
int ans=0;
int p_max=-1;
for(int i=0;i<customers.size()||(rem);i++){
r++;
rem+=i<customers.size()?customers[i]:0;
if(rem<4)
{
c+=rem;
rem=0;
}
else{
c+=4;
rem-=4;
}
int p=c*boardingCost-r*runningCost;
if(p>p_max){
p_max=p;
ans=r;
}
}
if(customers.size()==1)
return -1;

return p_max<0?-1:ans;
}
};
36 changes: 36 additions & 0 deletions Remove Linked List Elements.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeElements(ListNode head, int val) {
if(head== null) return head;
ListNode prev= null;
ListNode curr= head;
while(curr!= null){
if(curr.val==val){
if(curr==head){
head= curr.next;
curr= head;
}
else{
prev.next= curr.next;
curr= curr.next;

}
}
else{
prev= curr;
curr= curr.next;

}
}
return head;
}
}
15 changes: 15 additions & 0 deletions fibonacci numbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<script>
//Fibonacci Series using Recursion
let n = 9;

// function returns the Fibonacci number
function fib(n) {
if (n <= 1)
return n;
return fib(n-1) + fib(n-2);
}

//function call
document.write(fib(n));
//This code is contributed by Surbhi Tyagi
</script>