This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int binarySearch(int arr[], int key){ | |
int start = 0, end = arr.length - 1; | |
while(start<=end){ | |
int mid = start + (end-start)/2; | |
if(arr[mid]==key) return mid; | |
else if(arr[mid]>key) end = mid - 1; | |
else start = mid + 1; | |
} | |
return -1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void reverseArr(int arr[]){ | |
int n = arr.length; | |
for(int i=0; i<n/2;i++){ | |
int temp = arr[i]; | |
arr[i] = arr[n-i-1]; | |
arr[n-i-1] = temp; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void printSubArr(int a[]){ | |
int n = a.length; | |
for (int i = 0; i < n; i++) { | |
for (int j = i; j < n; j++) { | |
System.out.print("["); | |
for (int k = i; k <= j; k++) { | |
System.out.print(a[k]+","); | |
} | |
System.out.print("], "); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void printMaxSumSubArr(int a[]){ | |
int currSum=0, maxSum=Integer.MIN_VALUE, prefix[] = new int[a.length]; | |
prefix[0] = a[0]; | |
for (int i = 1; i < a.length; i++) { | |
prefix[i] = prefix[i-1] + a[i]; | |
} | |
for (int i = 0; i < a.length; i++) { | |
for (int j = i; j < a.length; j++) { | |
currSum = i==0?prefix[j]:prefix[j]-prefix[i-1]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void maxSumKadane(int arr[]){ | |
int currSum = 0, maxSum = Integer.MIN_VALUE; | |
for(int i=0; i< arr.length; i++) { | |
currSum += arr[i]; | |
if(currSum < 0) currSum = 0; | |
maxSum = maxSum>currSum? maxSum:currSum; | |
} | |
System.out.println(maxSum); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int trappedRW(int arr[]){ | |
int n=arr.length, leftMB[] = new int[n], rightMB[] = new int[n],trappedWater=0; | |
leftMB[0] = arr[0]; | |
rightMB[n-1] = arr[n-1]; | |
for (int i = 1; i < n; i++) { | |
leftMB[i] = Math.max(leftMB[i-1], arr[i]); | |
} | |
for (int i = n-2; i >= 0; i--) { | |
rightMB[i] = Math.max(rightMB[i+1],arr[i]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
int precedence(char k){ | |
if(k == '^') return 3; | |
else if(k == '/' || k =='*') return 2; | |
else if(k == '+' || k == '-') return 1; | |
else return -1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stdc++.h> | |
using namespace std; | |
class Node{ | |
public: | |
int data; | |
Node* next; | |
Node(int data){ | |
this->data = data; | |
this->next = NULL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stdc++.h> | |
using namespace std; | |
class Node{ | |
public: | |
int data; | |
Node* next; | |
Node(int data){ | |
this->data = data; | |
this->next = NULL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stdc++.h> | |
using namespace std; | |
class Node{ | |
public: | |
int data; | |
Node* next; | |
Node(int data){ | |
this->data = data; | |
this->next = NULL; |
OlderNewer