60pages PDF Solutions (Code)
60pages PDF Solutions (Code)
// import java.util.Scanner;
// if(dp[t][i]!=0)
// return dp[t][i];
// int include=Integer.MAX_VALUE;
// if(t>=wt[i])
// include=val[i]+solve(wt,val,t-wt[i],i-1,dp);
// int exclude=solve(wt,val,t,i-1,dp);
// // question 2
// import java.util.Scanner;
// Node(int data){
// this.data=data;
// this.left=null;
// this.right=null;
// }
// }
// public static Node bst(Node root,int data){
// if(root==null){
// root=new Node(data);
// return root;
// }
// if(data<root.data){
// root.left=bst(root.left, data);
// }
// if(data>root.data){
// root.right=bst(root.right, data);
// }
// return root;
// }
// System.out.print(root.data+" ");
// pre(root.left);
// pre(root.right);
// }
// public static void main(String[] args) {
// Scanner sc=new Scanner(System.in);
// int n=sc.nextInt();
// int arr[]=new int[n];
// for(int i=0;i<n;i++){
// arr[i]=sc.nextInt();
// }
// Node root=null;
// for(int i=0;i<n;i++){
// root=bst(root,arr[i]);
// }
// pre(root);
// }
// }
// //question 3
// import java.util.Scanner;
// Node(int data){
// this.data=data;
// this.left=null;
// this.right=null;
// }
// }
// root.left=delete(root.left);
// root.right=delete(root.right);
// return root;
// }
// if(data<root.data){
// root.left=bst(root.left, data);
// }
// if(data>root.data){
// root.right=bst(root.right, data);
// }
// return root;
// }
// post(root.left);
// post(root.right);
// System.out.print(root.data+" ");
// }
// public static void main(String[] args) {
// Scanner sc=new Scanner(System.in);
// int n=sc.nextInt();
// int arr[]=new int[n];
// for(int i=0;i<n;i++){
// arr[i]=sc.nextInt();
// }
// Node root=null;
// for(int i=0;i<n;i++){
// root=bst(root,arr[i]);
// }
// root=delete(root);
// post(root);
// }
// }
// question 4
// import java.util.Scanner;
// if(i>=arr.length || j>=arr[0].length)
// return 0; //for maxmimum
// // return Integer.MAX_VALUE; //for minimum
// int left=solve(i+1,j,arr);
// int right=solve(i,j+1,arr);
// return arr[i][j]+Math.max(left,right);
// }
// public static void main(String[] args) {
// Scanner sc=new Scanner(System.in);
// int r=sc.nextInt();
// int c=sc.nextInt();
// question 5
// import java.util.Scanner;
// if(s.charAt(i)==’0’)
// return 0;
// int ans=0;
// ans+=solve(s,i+1);
// return ans;
// }
// public static void main(String[] args) {
// Scanner sc=new Scanner(System.in);
// String s=sc.next();
// System.out.println(solve(s,0));
// }
// }
//question 6
// import java.util.Scanner;
// Node(int data){
// this.data=data;
// this.left=null;
// this.right=null;
// }
// }
// if(data<root.data){
// root.left=bst(root.left, data);
// }
// if(data>root.data){
// root.right=bst(root.right, data);
// }
// return root;
// }
// if(root.data==q)
// return root;
// if(root.data==p)
// return root;
// root.left=solve(root.left, p, q);
// root.right=solve(root.right, p, q);
// return root;
// }
// for(int i=0;i<arr1.length;i++){
// arr[i]=Integer.parseInt(arr1[i]);
// }
// Node root=null;
// for(int i=0;i<arr.length;i++){
// root=bst(root,arr[i]);
// }
// for(int i=0;i<arr.length;i++){
// root=bst(root,arr[i]);
// }
// Node temp=solve(root,p,q);
// if(temp!=null)
// System.out.println(temp.data);
// else
// System.out.println(0);
// }
// }
//question 7
// import java.util.Scanner;
// //prefix
// for(int i=n/2;i>=0;i--){
// String t=s.substring(0, i);
// if(s.endsWith(t))
// return t;
// }
// //suffix
// for(int i=n/2;i>=0;i--){
// String t=s.substring(n-i);
// if(s.startsWith(t))
// return t;
// }
// //check in between
// for(int i=0;i<n;i++){
// for(int j=i+1;j<=n;j++){
// String t=s.substring(i,j);
// if(s.substring(j).contains(t) && s.substring(0,i).contains(t));
// return t;
// }
// }
// System.out.print(solve(s));
// }
// }
// question 8
// import java.util.Scanner;
// return false;
// }
// public static void main(String[] args) {
// Scanner sc=new Scanner(System.in);
// int n=sc.nextInt();
// while(n-->0){
// String s=sc.next();
// if(solve(s))
// System.out.println("YES");
// else
// System.out.println("NO");
// }
// }
// }
// question 9
// import java.util.ArrayDeque;
// import java.util.Deque;
// import java.util.Scanner;
// //first window
// for(int i=0;i<k;i++){
// if(arr[i]<0)
// dq.addLast(arr[i]);
// }
// if(!dq.isEmpty()){
// result[0]=dq.peekFirst();
// }
// else{
// result[0]=0;
// }
// //for n window
// for(int i=k;i<n;i++){
// if(!dq.isEmpty() && dq.getFirst()==arr[i-k])
// dq.pollFirst();
// if(arr[i]<0)
// dq.addLast(arr[i]);
// if(!dq.isEmpty()){
// result[i-k+1]=dq.peekFirst();
// }
// else{
// result[i-k+1]=0;
// }
// }
// for(int i:result)
// System.out.print(i+" ");
// }
// public static void main(String[] args) {
// Scanner sc=new Scanner(System.in);
// int n=sc.nextInt();
// int arr[]=new int[n];
// for(int i=0;i<n;i++){
// arr[i]=sc.nextInt();
// }
// int k=sc.nextInt();
// solve(arr,k);
// }
// }
//question 10
// import java.util.Scanner;
// int include=0;
// if(prev==-1 || arr[curr]>arr[prev])
// include=1+solve(arr,curr,curr+1);
// int exclude=solve(arr,prev,curr+1);
// return Math.max(include,exclude);
// }
// public static void main(String[] args){
// Scanner sc=new Scanner(System.in);
// int n=sc.nextInt();
// int arr[]=new int[n];
// for(int i=0;i<n;i++){
// arr[i]=sc.nextInt();
// }
// int prev=-1;
// int curr=0;
// System.out.println(solve(arr,prev,curr));
// }
// }
//question 11
// import java.util.*;
// class ST2_JAVA{
// public static int solve(int arr[],int b,int l){
// PriorityQueue<Integer> pq=new PriorityQueue<>();
// for(int i=0;i<arr.length-1;i++){
// int diff=arr[i+1]-arr[i];
// if(diff>0){
// pq.add(diff);
// if(pq.size()>l){
// b-=pq.poll();
// }
// if(b<0)
// return i;
// }
// }
// return arr.length-1;
// }
// public static void main(String[] args) {
// Scanner sc=new Scanner(System.in);
// int n=sc.nextInt();
// int arr[]=new int[n];
// for(int i=0;i<n;i++){
// arr[i]=sc.nextInt();
// }
// int b=sc.nextInt();
// int l=sc.nextInt();
// System.out.println(solve(arr,b,l));
// }
// }
// question 12
// import java.util.Scanner;
// for(int i=0;i<arr.length;i++){
// String word=arr[i];
// int sum=0;
// for(char c:word.toCharArray()){
// int value=Character.toLowerCase(c)-’a’+1;
// if(Character.isUpperCase(c))
// sum+=value;
// else
// sum-=value;
// }
// int ans=Math.max(0,Math.min(9,sum));
// ok.append(ans);
// }
// System.out.println(ok);
// }
// }
// question 13
// import java.util.Scanner;
// for(int i=arr.length-1;i>=0;i--){
// System.out.println(i+1+" "+arr[i]);
// }
// if(a<arr.length)
// System.out.println("present");
// else
// System.out.println("not present");
// }
// }
//question 14
// import java.util.Scanner;
// int cnt=0;
// for(int i=0;i<arr.length;i++){
// if(i%2==0){
// even-=arr[i];
// }
// else{
// odd-=arr[i];
// }
// if(even==odd)
// cnt++;
// }
// return cnt;
// }
// public static void main(String[] args) {
// Scanner scanner = new Scanner(System.in);
// int n = scanner.nextInt();
// int[] arr = new int[n];
// for (int i = 0; i < n; i++) {
// arr[i] = scanner.nextInt();
// }
// System.out.println(solve(arr));
// }
// }
//question 15
// import java.util.Scanner;
// while(s.contains(t)){
// int index=s.indexOf(t);
// ss.setCharAt(index, ’@’);
// s=ss.toString();
// cnt++;
// }
// System.out.println(cnt);
// }
// }
// question 16
// import java.util.ArrayList;
// import java.util.*;
// import java.util.Scanner;
// while(start<=end){
// if(s.charAt(start)==s.charAt(end)){
// start++;
// end--;
// }
// else{
// return false;
// }
// }
// return true;
// }
// public static void main(String[] args) {
// Scanner sc=new Scanner(System.in);
// String s=sc.next();
// ArrayList<String> a=new ArrayList<>();
// for(int i=0;i<s.length();i++){
// for(int j=i+1;j<=s.length();j++){
// if(solve(s.substring(i,j)) && s.substring(i,j).length()>=3)
// a.add(s.substring(i,j));
// }
// }
// Collections.sort(a,Comparator.comparing(String::length));
// if(a.size()>0){
// for(String i:a){
// int index=s.indexOf(i);
// System.out.println(i+" "+index+"-"+(index+i.length()-1));
// }
// }
// else{
// System.out.println("No Palindrome string found");
// }
// }
// }
// question 17
// import java.util.Scanner;
// for(int i=0;i<s.length()-1;i++){
// System.out.print(s.charAt(i));
// System.out.print((int)(s.charAt(i+1)-s.charAt(i)));
// }
// System.out.print(s.charAt(s.length()-1));
// }
// }
// question 18
// import java.util.Scanner;
// public class ST2_JAVA {
// public static boolean solve(int n){
// while(n>=5 && n%5==0)
// n/=5;
// return n==1;
// }
// public static void main(String[] args) {
// Scanner sc=new Scanner(System.in);
// int n=sc.nextInt();
// if(solve(n))
// System.out.println("TRUE");
// else
// System.out.println("FALSE");
// }
// }
// question 19
// import java.util.Scanner;
// while(n>0){
// if(n%2==0)
// n/=2;
// else
// n-=1;
// cnt++;
// }
// System.out.println(cnt);
// }
// }
//question 20
// import java.util.Scanner;
// for(int i=0;i<s.length();i++){
// if(i%2==0){
// ss.append((char)(s.charAt(i)+1));
// }
// else{
// ss.append((char)(s.charAt(i)-1));
// }
// }
// System.out.println(ss);
// }
// }
// question 21
// import java.util.Scanner;
// String s=Integer.toString(num);
// int n=s.length();
// if(n%2!=0)
// return false;
// int firstHalf=0;
// int secondHalf=0;
// for(int i=0;i<n/2;i++){
// firstHalf+=s.charAt(i)-’0’;
// secondHalf+=s.charAt(n-i-1)-’0’;
// }
// if(firstHalf==secondHalf)
// return true;
// else
// return false;
// }
// public static void main(String[] args) {
// Scanner sc=new Scanner(System.in);
// int low=sc.nextInt();
// int high=sc.nextInt();
// int cnt=0;
// for(int i=low;i<=high;i++){
// if(solve(i)){
// cnt++;
// }
// }
// System.out.println(cnt);
// }
// }
// question 22
// import java.util.Scanner;
// if(n==0)
// return 1;
// return solve(n-1)+solve(n-2)+solve(n-3);
// }
// public static void main(String[] args) {
// Scanner sc=new Scanner(System.in);
// int n=sc.nextInt();
// System.out.println(solve(n));
// }
// }
// question 23
// import java.util.*;
// class ST2_JAVA{
// public static int solve(int arr[][]){
// int cnt=1;
// int p=0;
// Arrays.sort(arr,(a,b)->Integer.compare(a[1],b[1]));
// for(int i=1;i<arr.length;i++){
// if(arr[i][0]>=arr[p][1]){
// cnt++;
// p=i;
// }
// }
// return arr.length-cnt;
// }
// public static void main(String[] args) {
// Scanner sc=new Scanner(System.in);
// int n=sc.nextInt();
// int arr[][]=new int[n][2];
// for(int i=0;i<n;i++){
// for(int j=0;j<2;j++){
// arr[i][j]=sc.nextInt();
// }
// }
// System.out.println(solve(arr));
// }
// }
// question 24
// import java.util.LinkedList;
// import java.util.*;
// import java.util.Scanner;
// import java.util.TreeMap;
// Node(int data){
// this.data=data;
// this.left=null;
// this.right=null;
// }
// }
// if(data<root.data){
// root.left=bst(root.left, data);
// }
// if(data>root.data){
// root.right=bst(root.right, data);
// }
// return root;
// }
// if(!m.containsKey(temp.data))
// m.put(temp.data,temp.p.data);
// if(temp.p.left!=null)
// q.offer(new Pair(temp.data+1,temp.p.left));
// if(temp.p.right!=null)
// q.offer(new Pair(temp.data+1,temp.p.right));
// }
// for(Map.Entry<Integer,Integer> i:m.entrySet())
// System.out.print(i.getValue()+" ");
// }
// for(int i=0;i<arr1.length;i++){
// arr[i]=Integer.parseInt(arr1[i]);
// }
// Node root=null;
// for(int i=0;i<arr.length;i++){
// root=bst(root,arr[i]);
// }
// for(int i=0;i<arr.length;i++){
// root=bst(root,arr[i]);
// }
// left(root);
// }
// }
//question 25
// import java.util.HashSet;
// import java.util.Scanner;
// import java.util.Set;
// return dp[s.length()];
// }
// if(solve(s,set))
// System.out.println("YES");
// else
// System.out.println("NO");
// }
// }
// question 27 (pending)
// import java.util.Scanner;
// return maxPrefixLength;
// }
// System.out.println(solve(s1,s2));
// }
// }
// question 28
// import java.util.*;
// double val=0;
// for(int i=0;i<arr.length;i++){
// if(t>=arr[i].weight){
// val+=arr[i].value;
// t-=arr[i].weight;
// }
// else if(t>0){
// val+=((double)arr[i].value/arr[i].weight)*t;
// break;
// }
// }
// return val;
// }
// public static void main(String[] args) {
// Scanner sc=new Scanner(System.in);
// int t=sc.nextInt();
// int value[]={60,100,120};
// int weight[]={10,20,30};
// for(int i=0;i<3;i++){
// arr[i]=new Item(value[i],weight[i]);
// }
// System.out.println(solve(t,arr));
// }
// }
// question 29
// import java.io.*;
// import java.util.*;
// class Main {
// static class node {
// int key;
// node left, right;
// };
// return hasPathSum(root.left,targetSum-root.key)||hasPathSum(root.right,targetSum-root.key);
// }
//question 30 (gfg)
// class Solution
// {
// // returns the inorder successor of the Node x in BST (rooted at ’root’)
// public void solve(Node root,ArrayList<Integer> a){
// if(root==null)
// return;
// solve(root.left,a);
// a.add(root.data);
// solve(root.right,a);
// }
// public Node inorderSuccessor(Node root,Node x)
// {
// //add code here.
// ArrayList<Integer> a=new ArrayList<>();
// solve(root,a);
// for(int i=0;i<a.size();i++){
// if(i+1<a.size() &&a.get(i)==x.data)
// return new Node(a.get(i+1));
// }
// return new Node(-1);
// }
// }
//question 31 (diameter)
// import java.util.Arrays;
// import java.util.Scanner;
// if(dp[i][t]!=-1)
// return dp[i][t];
// if(t==0)
// return 1;
// int include=0;
// if(arr[i]<=t)
// include=solve(arr,t-arr[i],i,dp);
// int exclude=solve(arr,t,i-1,dp);
// for(int i[]:dp)
// Arrays.fill(i,-1);
// System.out.print(solve(arr,t,n-1,dp));
// }
// }
//question 34
// import java.util.*;
// int min=Integer.MAX_VALUE;
// for(int i=1;i<=f;i++){
// int Break=solve(e-1,i-1);
// int notBreak=solve(e,f-i);
// min=Math.min(1+Math.max(Break,notBreak),min);
// }
// return min;
// }
// System.out.println(solve(e,f));
// }
// }
//question 35
//question 36
//question 37
//question 38
// import java.util.HashMap;
// import java.util.*;
// for(int i=0;i<arr.length;i++){
// if(!m.containsKey(s1.charAt(i)))
// m.put(s1.charAt(i),arr[i]);
// else if(m.containsKey(s1.charAt(i))){
// if(m.get(s1.charAt(i)).equals(arr[i]))
// continue;
// else{
// System.out.println("false");
// flag=true;
// break;
// }
// }
// else if(m.containsValue(arr[i])){
// System.out.println("false");
// flag=true;
// break;
// }
// }
// if(!flag)
// System.out.println("true");
// }
// }
//question 39
//question 40
//question 41
// import java.util.Scanner;
//question 42
// import java.util.*;
// int temp=arr[minIndex];
// arr[minIndex]=arr[i];
// arr[i]=temp;
// System.out.println(Arrays.toString(arr));
// }
// }
// solve(arr);
// }
// }
//question 43
//question 44
//question 45