You are on page 1of 7

//Find minimum and maximum element in an array

static pair getMinMax(long a[], long n)


{ long max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;
for(int i=0; i<n; i++){
max = Math.max(max,a[i]);
min = Math.min(min,a[i]);
}
pair ans = new pair(min,max);
return ans;
}
-----------------------------------------------------------------------
//Second Largest

int print2largest(int arr[], int n) {


int max = Integer.MIN_VALUE;
int max2 = Integer.MIN_VALUE;
int ans = 0;
for(int i=0; i<arr.length; i++) {
if (arr[i] > max) {
max2 = max;
max = arr[i];
}
if(arr[i]>max2 && arr[i]!= max){
max2 = arr[i];
}

}
if (max2 == Integer.MIN_VALUE){
return -1;
}
else{
return max2;
}
}

Arr[] = {10, 5, 10}


Output: 5
----------------------------------------------------------------------------
//Reverse an Array

int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int[] arr = new int[n];

for(int i=0; i<n; i++)arr[i] = sc.nextInt();

int i=0, j=n-1;


while(i<j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
for(int k=0; k<n; k++)System.out.print(arr[k]+" ");

System.out.println();
}
1
4
1 2 3 4
Output:
4 3 2 1
----------------------------------------------------------------------------
//number size of the rotation.

rotate(arr,r,n-1); //1 2 5 4 3
rotate(arr,0,r-1); //2 1 5 4 3
rotate(arr,0,n-1); //3 4 5 1 2

input: 5 2
1 2 3 4 5
output: 3 4 5 1 2
------------------------------------------------------------------
//Count of smaller elements

public long countOfElements(long arr[], long n, long x)


{
int start = 0;
int end = (int)(n-1);
long c = 0;
while(start<=end){
int mid = (start+end)/2;
if(arr[mid]<=x){
c = mid+1;
start=mid+1;
}else{
end = mid-1;
}
}
return c;

N = 6
A[] = {1, 2, 4, 5, 8, 10}
X = 9
Output:
5
----------------------------------------------------------------------
//Search a node in BST

boolean search(Node root, int x) {


// Your code here
if(root==null) return false;
if(root.data==x) return true;
if(root.data>x){
return search(root.left,x);
}
else{
return search(root.right,x);
}
}
------------------------------------------------------------------
//Insert a node in a BST

Node insert(Node root, int Key) {


if(root==null){
Node temp = new Node(Key);
return temp;
}

if(root.data==Key) return root;

if(root.data<Key){

root.right = insert(root.right,Key);
}else{
root.left = insert(root.left,Key);
}
return root;
--------------------------------------------------------------------------
//Delete a node from BST

public static Node minimumElement(Node root) {


if (root.left == null)
return root;
else {
return minimumElement(root.left);
}
}
public static Node deleteNode(Node root, int value) {
if (root == null) return null;

if (root.data > value) {


root.left = deleteNode(root.left, value);
}
else if (root.data < value) {
root.right = deleteNode(root.right, value);
}
else {
if (root.left != null && root.right != null) {
Node temp = root;
Node minNodeForRight = minimumElement(temp.right);
root.data = minNodeForRight.data;
root.right = deleteNode(root.right, minNodeForRight.data);
}
else if (root.left != null) {
root = root.left;
}
else if (root.right != null) {
root = root.right;
}
else
root = null;
}
return root;
}
-------------------------------------------------------------------------------
//Minimum element in BST

int minValue(Node node) {


if (node == null) return -1;

Node current = node;


while (current.left != null)
current = current.left;
return (current.data);
}
------------------------------------------------------------------
//Path Sum In Binary Tree

public static boolean hasPathSum(TreeNode root, int targetSum){


if(root==null) return false;
if(root.left==null && root.right==null){
return (targetSum-root.val)==0;
}
return hasPathSum(root.left,targetSum-root.val)
|| hasPathSum(root.right,targetSum-root.val);
}
----------------------------------------------------------------------
//Node To Root Path Binary Tree

public static boolean nodeToRootPath(TreeNode root, int data,


ArrayList<TreeNode>list){
if(root == null) return false;
if(root.val == data){
list.add(root);
return true;
}
boolean res = nodeToRootPath(root.left, data, list)
|| nodeToRootPath(root.right, data, list);
if(res){
list.add(root);
}
return res;
}
public static ArrayList<TreeNode> nodeToRootPath(TreeNode node, int data) {
ArrayList<TreeNode> list = new ArrayList<TreeNode>();
nodeToRootPath(node, data, list);
return list;
}
---------------------------------------------------------------------
//Width Of Shadow Of Binary Tree(hl- horizontal level)

public static void width(TreeNode root, int hl, int[] ans){


if(root == null) return;

ans[0] = Math.min(ans[0], hl);


ans[1] = Math.max(ans[1], hl);

width(root.left, hl-1, ans);


width(root.right, hl+1, ans);
}
public static int width(TreeNode root){
int[] ans = new int[2];
width(root, 0, ans);
return ans[1]-ans[0]+1;
}
----------------------------------------------------------
//Level Order Traversal Of Binary Tree

public static void levelOrder(Node node){


Queue< Node> mq = new ArrayDeque< >();
mq.add(node);
while (mq.size() > 0){
int count = mq.size();
for (int i = 0; i < count; i++){
node = q.remove();
System.out.print(node.data + " ");
if (node.left != null){
mq.add(node.left);
}
if (node.right != null){
mq.add(node.right);
}
}
System.out.println();
}
----------------------------------------------------------

You might also like