diff --git a/AWT/Behaviour/ActionListenerDemo.java b/AWT/Behaviour/ActionListenerDemo.java old mode 100755 new mode 100644 diff --git a/AWT/Behaviour/AdapterTest.java b/AWT/Behaviour/AdapterTest.java old mode 100755 new mode 100644 diff --git a/AWT/Behaviour/KeyListenerDemo.java b/AWT/Behaviour/KeyListenerDemo.java old mode 100755 new mode 100644 diff --git a/AWT/Behaviour/MouseListenerDemo.java b/AWT/Behaviour/MouseListenerDemo.java old mode 100755 new mode 100644 diff --git a/AWT/Behaviour/MouseMotionListenerDemo.java b/AWT/Behaviour/MouseMotionListenerDemo.java old mode 100755 new mode 100644 diff --git a/AWT/Behaviour/WindowListenerDemo.java b/AWT/Behaviour/WindowListenerDemo.java old mode 100755 new mode 100644 diff --git a/AWT/FirstCanvas.java b/AWT/FirstCanvas.java old mode 100755 new mode 100644 diff --git a/AWT/FirstFrame.java b/AWT/FirstFrame.java old mode 100755 new mode 100644 diff --git a/AWT/FrameFromApplet.java b/AWT/FrameFromApplet.java old mode 100755 new mode 100644 diff --git a/AWT/FrameTest.java b/AWT/FrameTest.java old mode 100755 new mode 100644 diff --git a/AWT/HelloWorldFrame.java b/AWT/HelloWorldFrame.java old mode 100755 new mode 100644 diff --git a/AWT/Layouts/BorderLayoutExample.java b/AWT/Layouts/BorderLayoutExample.java old mode 100755 new mode 100644 diff --git a/AWT/Layouts/CardLayoutExample.java b/AWT/Layouts/CardLayoutExample.java old mode 100755 new mode 100644 diff --git a/AWT/Layouts/FlowLayoutExample.java b/AWT/Layouts/FlowLayoutExample.java old mode 100755 new mode 100644 diff --git a/AWT/Layouts/GridLayoutExample.java b/AWT/Layouts/GridLayoutExample.java old mode 100755 new mode 100644 diff --git a/AWT/SecondCanvas.java b/AWT/SecondCanvas.java old mode 100755 new mode 100644 diff --git a/AWT/SwingExample.java b/AWT/SwingExample.java old mode 100755 new mode 100644 diff --git a/AWT/UIElements/MyButton.java b/AWT/UIElements/MyButton.java old mode 100755 new mode 100644 diff --git a/AWT/UIElements/MyCheckbox.java b/AWT/UIElements/MyCheckbox.java old mode 100755 new mode 100644 diff --git a/AWT/UIElements/MyChoice.java b/AWT/UIElements/MyChoice.java old mode 100755 new mode 100644 diff --git a/AWT/UIElements/MyLabel.java b/AWT/UIElements/MyLabel.java old mode 100755 new mode 100644 diff --git a/AWT/UIElements/MyList.java b/AWT/UIElements/MyList.java old mode 100755 new mode 100644 diff --git a/AWT/UIElements/MyRadio.java b/AWT/UIElements/MyRadio.java old mode 100755 new mode 100644 diff --git a/AWT/UIElements/MyScrollBar.java b/AWT/UIElements/MyScrollBar.java old mode 100755 new mode 100644 diff --git a/AWT/UIElements/MyTextArea.java b/AWT/UIElements/MyTextArea.java old mode 100755 new mode 100644 diff --git a/AWT/UIElements/MyTextField.java b/AWT/UIElements/MyTextField.java old mode 100755 new mode 100644 diff --git a/AWT/hello.java b/AWT/hello.java deleted file mode 100644 index 0a55a80..0000000 --- a/AWT/hello.java +++ /dev/null @@ -1,8 +0,0 @@ -import java.util.Scanner; -class hello -{ - public static void main(String[] args) - { - System.out.println("Hello"); - } -} diff --git a/AWT/icon.png b/AWT/icon.png old mode 100755 new mode 100644 diff --git a/Anagram.java b/Anagram.java index 7ea74ce..9c7df37 100644 --- a/Anagram.java +++ b/Anagram.java @@ -1,32 +1,32 @@ -import java.io.*; -import java.util.*; -class Anagram{ - public static void main(String args[]){ - String str1,str2; - Scanner sc=new Scanner(System.in); - System.out.println("Enter the String 1"); - str1=sc.nextLine(); - System.out.println("Enter the String 2"); - str2=sc.nextLine(); - if(str1.length()!=str2.length()){ - System.out.println("The Strings "+str1+" and"+str2+" is not a Anagram"); - } - else{ - str1=str1.toLowerCase(); - str2=str2.toLowerCase(); - char[]arr1=str1.toCharArray(); - char []arr2=str2.toCharArray(); - Arrays.sort(arr1); - Arrays.sort(arr2); - if(Arrays.equals(arr1,arr2)==true){ - System.out.println("The Strings "+str1+" and"+str2+" is a Anagram"); - } - else{ - System.out.println("The Strings "+str1+" and"+sātr2+" is not a Anagram"); - } - } - } -} - - +import java.io.*; +import java.util.*; +class Anagram{ + public static void main(String args[]){ + String str1,str2; + Scanner sc=new Scanner(System.in); + System.out.println("Enter the String 1"); + str1=sc.nextLine(); + System.out.println("Enter the String 2"); + str2=sc.nextLine(); + if(str1.length()!=str2.length()){ + System.out.println("The Strings "+str1+" and"+str2+" is not a Anagram"); + } + else{ + str1=str1.toLowerCase(); + str2=str2.toLowerCase(); + char[]arr1=str1.toCharArray(); + char []arr2=str2.toCharArray(); + Arrays.sort(arr1); + Arrays.sort(arr2); + if(Arrays.equals(arr1,arr2)==true){ + System.out.println("The Strings "+str1+" and"+str2+" is a Anagram"); + } + else{ + System.out.println("The Strings "+str1+" and"+sātr2+" is not a Anagram"); + } + } + } +} + + \ No newline at end of file diff --git a/Armstrong.java b/Armstrong.java index e45f127..2401740 100644 --- a/Armstrong.java +++ b/Armstrong.java @@ -1,46 +1,46 @@ -import java.util.Scanner; -import java.lang.Math; -public class Armstrong -{ - static boolean isArmstrong(int n) - { - int temp, digits=0, last=0, sum=0; - temp=n; - - while(temp>0) - { - temp = temp/10; - digits++; - } - - temp = n; - - while(temp>0) - { - last = temp % 10; - sum += (Math.pow(last, digits)); - temp = temp/10; - } - - if(n==sum) - - return true; - - else return false; -} - - public static void main(String args[]) - { - int num; - Scanner sc= new Scanner(System.in); - System.out.print("Enter the max: "); - - num=sc.nextInt(); - System.out.println("Armstrong Number up to "+ num + " are: "); - sc.close(); - - for(int i=0; i<=num; i++) - if(isArmstrong(i)) - System.out.print(i+ ", "); - } +import java.util.Scanner; +import java.lang.Math; +public class Armstrong +{ + static boolean isArmstrong(int n) + { + int temp, digits=0, last=0, sum=0; + temp=n; + + while(temp>0) + { + temp = temp/10; + digits++; + } + + temp = n; + + while(temp>0) + { + last = temp % 10; + sum += (Math.pow(last, digits)); + temp = temp/10; + } + + if(n==sum) + + return true; + + else return false; +} + + public static void main(String args[]) + { + int num; + Scanner sc= new Scanner(System.in); + System.out.print("Enter the max: "); + + num=sc.nextInt(); + System.out.println("Armstrong Number up to "+ num + " are: "); + sc.close(); + + for(int i=0; i<=num; i++) + if(isArmstrong(i)) + System.out.print(i+ ", "); + } } \ No newline at end of file diff --git a/ArrayListIteration.java b/ArrayListIteration.java index 6e14e50..5b7edf6 100644 --- a/ArrayListIteration.java +++ b/ArrayListIteration.java @@ -16,7 +16,7 @@ public static void main(String[] args) { System.out.println(course); } - //basic loop with iterato + //basic loop with iterator for(Iterator iterator = courses.iterator();iterator.hasNext();){ String course = (String)iterator.next(); System.out.println(course); @@ -36,4 +36,4 @@ public static void main(String[] args) { // Java 8 forEach + lambda courses.forEach(course -> System.out.println(course)); } -} +} \ No newline at end of file diff --git a/BasicRSA.java b/BasicRSA.java index 07a3936..879d936 100644 --- a/BasicRSA.java +++ b/BasicRSA.java @@ -1,72 +1,72 @@ - -import java.math.BigInteger; -import java.util.Scanner; - -public class BasicRSA{ - - static int gcd(int a, int b) - { /*Returns Greatest common divisor of 2 numbers - *implementing Euclidean ALgorithm */ - - if (a == 0) return b; - else return gcd(b % a, a); - } - - static int modInv(int a, int b) - { /*Returns modular inverse of a mod b - *implementing Extended Euclidean ALgorithm*/ - return modInv(a,b,b,0,1); - } - - static int modInv(int a,int b,int c,int x,int y) - { //over-loading - - if(b%a==0) return y; - else return modInv(b%a,a,c,y,c+(x-(b/a)*y)%c ); - } - - public static void main(String args[]){ - - Scanner sc= new Scanner(System.in); - System.out.println("You are required to enter 2 prime numbers followed by the message you want to encrypt"); - - System.out.println("Enter first prime :"); - int p= sc.nextInt(); - - System.out.println("Enter Second prime:"); - int q=sc.nextInt(); - - System.out.println("Enter a num to encrypt:"); - int m=sc.nextInt(); - //Plaintext: message to be encrypted - - sc.close(); - - int n=p*q; - int z=(p-1)*(q-1)/gcd(p-1,q-1); - int e,d=0; // e is for public key exponent, d is for private key exponent - - for (e = 2; e < z; e++) { - if (gcd(e, z) == 1) { - break; - } - } - - d=modInv(e,z); - - BigInteger M= BigInteger.valueOf(m); - BigInteger E= BigInteger.valueOf(e); - BigInteger N= BigInteger.valueOf(n); - BigInteger D= BigInteger.valueOf(d); - BigInteger C= M.modPow(E,N); - BigInteger M2= C.modPow(D,N); - - - - System.out.println("Public Key Pair : " + e + " " + n ); - System.out.println("Private Key Pair : " + d + " " + n ); - System.out.println("Encrypted num : "+ C); - System.out.println("Decrypted num : "+ M2); - System.out.println("This is deciphered using Private key"); - } -} + +import java.math.BigInteger; +import java.util.Scanner; + +public class BasicRSA{ + + static int gcd(int a, int b) + { /*Returns Greatest common divisor of 2 numbers + *implementing Euclidean ALgorithm */ + + if (a == 0) return b; + else return gcd(b % a, a); + } + + static int modInv(int a, int b) + { /*Returns modular inverse of a mod b + *implementing Extended Euclidean ALgorithm*/ + return modInv(a,b,b,0,1); + } + + static int modInv(int a,int b,int c,int x,int y) + { //over-loading + + if(b%a==0) return y; + else return modInv(b%a,a,c,y,c+(x-(b/a)*y)%c ); + } + + public static void main(String args[]){ + + Scanner sc= new Scanner(System.in); + System.out.println("You are required to enter 2 prime numbers followed by the message you want to encrypt"); + + System.out.println("Enter first prime :"); + int p= sc.nextInt(); + + System.out.println("Enter Second prime:"); + int q=sc.nextInt(); + + System.out.println("Enter a num to encrypt:"); + int m=sc.nextInt(); + //Plaintext: message to be encrypted + + sc.close(); + + int n=p*q; + int z=(p-1)*(q-1)/gcd(p-1,q-1); + int e,d=0; // e is for public key exponent, d is for private key exponent + + for (e = 2; e < z; e++) { + if (gcd(e, z) == 1) { + break; + } + } + + d=modInv(e,z); + + BigInteger M= BigInteger.valueOf(m); + BigInteger E= BigInteger.valueOf(e); + BigInteger N= BigInteger.valueOf(n); + BigInteger D= BigInteger.valueOf(d); + BigInteger C= M.modPow(E,N); + BigInteger M2= C.modPow(D,N); + + + + System.out.println("Public Key Pair : " + e + " " + n ); + System.out.println("Private Key Pair : " + d + " " + n ); + System.out.println("Encrypted num : "+ C); + System.out.println("Decrypted num : "+ M2); + System.out.println("This is deciphered using Private key"); + } +} diff --git a/Catalan.java b/Catalan.java index dd04ce6..d2a7ad2 100644 --- a/Catalan.java +++ b/Catalan.java @@ -1,45 +1,45 @@ -import java.util.Scanner; - -class Catalan { - - // find nth Catalan number - public static int catalanDP(int n) - { - - int cat[] = new int[n + 2]; - - // Initialize first two values in table - cat[0] = 1; - cat[1] = 1; - - for (int i = 2; i <= n; i++) { - cat[i] = 0; - for (int j = 0; j < i; j++) { - cat[i] += cat[j] * cat[i - j - 1]; - } - } - - // Return last entry - return cat[n]; - } - - // Driver code - public static void main(String[] args) - { - - // Catalan numbers are a sequence of positive integers, where the nth term in the sequence, denoted Cn, - // is found in the following formula: (2n)! / ((n + 1)!n!) - - Scanner in = new Scanner(System.in); - System.out.println("Enter n (upto 20th catalan numbers will be printed): "); - int n = in.nextInt(); - while(n>=20 || n<=0){ - System.out.print("Enter n < 20: "); - n = in.nextInt(); - } - in.close(); - for (int i = 0; i < n; i++) { - System.out.print(catalanDP(i) + " "); - } - } -} +import java.util.Scanner; + +class Catalan { + + // find nth Catalan number + public static int catalanDP(int n) + { + + int cat[] = new int[n + 2]; + + // Initialize first two values in table + cat[0] = 1; + cat[1] = 1; + + for (int i = 2; i <= n; i++) { + cat[i] = 0; + for (int j = 0; j < i; j++) { + cat[i] += cat[j] * cat[i - j - 1]; + } + } + + // Return last entry + return cat[n]; + } + + // Driver code + public static void main(String[] args) + { + + // Catalan numbers are a sequence of positive integers, where the nth term in the sequence, denoted Cn, + // is found in the following formula: (2n)! / ((n + 1)!n!) + + Scanner in = new Scanner(System.in); + System.out.println("Enter n (upto 20th catalan numbers will be printed): "); + int n = in.nextInt(); + while(n>=20 || n<=0){ + System.out.print("Enter n < 20: "); + n = in.nextInt(); + } + in.close(); + for (int i = 0; i < n; i++) { + System.out.print(catalanDP(i) + " "); + } + } +} diff --git a/DesignCircularQueue.java b/DesignCircularQueue.java index f12c917..4d503d9 100644 --- a/DesignCircularQueue.java +++ b/DesignCircularQueue.java @@ -1,81 +1,81 @@ -//Design Circular Queue - - class MyCircularQueue { - - private int[] data; - private int head; - private int tail; - private int size; - - /** Initialize your data structure here. Set the size of the queue to be k. */ - public MyCircularQueue(int k) { - data = new int[k]; - head = -1; - tail = -1; - size = k; - } - - /** Insert an element into the circular queue. Return true if the operation is successful. */ - public boolean enQueue(int value) { - if (isFull() == true) { - return false; - } - if (isEmpty() == true) { - head = 0; - } - tail = (tail + 1) % size; - data[tail] = value; - return true; - } - - /** Delete an element from the circular queue. Return true if the operation is successful. */ - public boolean deQueue() { - if (isEmpty() == true) { - return false; - } - if (head == tail) { - head = -1; - tail = -1; - return true; - } - head = (head + 1) % size; - return true; - } - - /** Get the front item from the queue. */ - public int Front() { - if (isEmpty() == true) { - return -1; - } - return data[head]; - } - - /** Get the last item from the queue. */ - public int Rear() { - if (isEmpty() == true) { - return -1; - } - return data[tail]; - } - - /** Checks whether the circular queue is empty or not. */ - public boolean isEmpty() { - return head == -1; - } - - /** Checks whether the circular queue is full or not. */ - public boolean isFull() { - return ((tail + 1) % size) == head; - } -} - -/** - * Your MyCircularQueue object will be instantiated and called as such: - * MyCircularQueue obj = new MyCircularQueue(k); - * boolean param_1 = obj.enQueue(value); - * boolean param_2 = obj.deQueue(); - * int param_3 = obj.Front(); - * int param_4 = obj.Rear(); - * boolean param_5 = obj.isEmpty(); - * boolean param_6 = obj.isFull(); - */ +//Design Circular Queue + + class MyCircularQueue { + + private int[] data; + private int head; + private int tail; + private int size; + + /** Initialize your data structure here. Set the size of the queue to be k. */ + public MyCircularQueue(int k) { + data = new int[k]; + head = -1; + tail = -1; + size = k; + } + + /** Insert an element into the circular queue. Return true if the operation is successful. */ + public boolean enQueue(int value) { + if (isFull() == true) { + return false; + } + if (isEmpty() == true) { + head = 0; + } + tail = (tail + 1) % size; + data[tail] = value; + return true; + } + + /** Delete an element from the circular queue. Return true if the operation is successful. */ + public boolean deQueue() { + if (isEmpty() == true) { + return false; + } + if (head == tail) { + head = -1; + tail = -1; + return true; + } + head = (head + 1) % size; + return true; + } + + /** Get the front item from the queue. */ + public int Front() { + if (isEmpty() == true) { + return -1; + } + return data[head]; + } + + /** Get the last item from the queue. */ + public int Rear() { + if (isEmpty() == true) { + return -1; + } + return data[tail]; + } + + /** Checks whether the circular queue is empty or not. */ + public boolean isEmpty() { + return head == -1; + } + + /** Checks whether the circular queue is full or not. */ + public boolean isFull() { + return ((tail + 1) % size) == head; + } +} + +/** + * Your MyCircularQueue object will be instantiated and called as such: + * MyCircularQueue obj = new MyCircularQueue(k); + * boolean param_1 = obj.enQueue(value); + * boolean param_2 = obj.deQueue(); + * int param_3 = obj.Front(); + * int param_4 = obj.Rear(); + * boolean param_5 = obj.isEmpty(); + * boolean param_6 = obj.isFull(); + */ diff --git a/Echo.java b/Echo.java index 6b5f764..df1e9a0 100644 --- a/Echo.java +++ b/Echo.java @@ -1,10 +1,10 @@ -class Echo { - public static void main(String args[]) { - for(int i=0;iDigits=new ArrayList(); - int input[]=new int[4]; - int i, temp, result; - for(i=0;i<4;i++) - input[i]=sc.nextInt(); - for(i=0;i<4;i++){ - while(input[i]>0){ - Digits.add(input[i]%10); - input[i]=input[i]/10; - } - } - temp=Collections.frequency(Digits,Digits.get(0)); - result=Digits.get(0); - for(i=1;iresult){ - temp=Collections.frequency(Digits,Digits.get(i)); - result=Digits.get(i); - } - } - System.out.println(result); - } +import java.util.*; +class Frequency{ + public static void main(String args[]){ + Scanner sc=new Scanner(System.in); + ArrayListDigits=new ArrayList(); + int input[]=new int[4]; + int i, temp, result; + for(i=0;i<4;i++) + input[i]=sc.nextInt(); + for(i=0;i<4;i++){ + while(input[i]>0){ + Digits.add(input[i]%10); + input[i]=input[i]/10; + } + } + temp=Collections.frequency(Digits,Digits.get(0)); + result=Digits.get(0); + for(i=1;iresult){ + temp=Collections.frequency(Digits,Digits.get(i)); + result=Digits.get(i); + } + } + System.out.println(result); + } } \ No newline at end of file diff --git a/Harry.java b/Harry.java index 2eef3ea..0cfdfdf 100644 --- a/Harry.java +++ b/Harry.java @@ -1,7 +1,7 @@ -class Harry { - public static void main(String args[]) { - int i = 0; - i = Integer.parseInt(args[0]); - System.out.println("The value of i in Integer is"+i); - } -} +class Harry { + public static void main(String args[]) { + int i = 0; + i = Integer.parseInt(args[0]); + System.out.println("The value of i in Integer is"+i); + } +} diff --git a/JavaApplets/FirstApplet.htm b/JavaApplets/FirstApplet.htm old mode 100755 new mode 100644 diff --git a/JavaApplets/FirstApplet.java b/JavaApplets/FirstApplet.java old mode 100755 new mode 100644 diff --git a/JavaApplets/GraphicsMethods.java b/JavaApplets/GraphicsMethods.java old mode 100755 new mode 100644 diff --git a/JavaApplets/test.txt b/JavaApplets/test.txt old mode 100755 new mode 100644 diff --git a/LongestCommonSubsequence.java b/LongestCommonSubsequence.java index ed7b2e4..a107513 100644 --- a/LongestCommonSubsequence.java +++ b/LongestCommonSubsequence.java @@ -1,29 +1,29 @@ -import java.util.*; - -class Solution8 { - public int solve(String a, String b) { - int dp[][]=new int[a.length()][b.length()]; - for (int i=0;i<=a.length();i++) - { - for (int j=0;j<=b.length();j++) - { - - if (i==0||j==0) - { - dp[i][j]=0; - } - else - { - if (a.charAt(i)==b.charAt(j)) { - dp[i][j] = dp[i - 1][j - 1] + 1; - } - else - { - dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]); - } - } - } - } - return dp[a.length()][b.length()]; - } +import java.util.*; + +class Solution8 { + public int solve(String a, String b) { + int dp[][]=new int[a.length()][b.length()]; + for (int i=0;i<=a.length();i++) + { + for (int j=0;j<=b.length();j++) + { + + if (i==0||j==0) + { + dp[i][j]=0; + } + else + { + if (a.charAt(i)==b.charAt(j)) { + dp[i][j] = dp[i - 1][j - 1] + 1; + } + else + { + dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]); + } + } + } + } + return dp[a.length()][b.length()]; + } } \ No newline at end of file diff --git a/MulticlassDemo.java b/MulticlassDemo.java index 749e8d1..879e282 100644 --- a/MulticlassDemo.java +++ b/MulticlassDemo.java @@ -1,46 +1,46 @@ -class Circle -{ - - double r; - double circumference() - { - return 2*3.14*r; - } - double area() - { - return (22/7)*r*r; - } -} -class Box -{ - double width; - double height; - double depth; - double area() - { - double a; - a=(width*height + height*depth + width*depth)*2; - return a; - } - double volume() - { - double v; - v=width*height*depth; - return v; - } - -} -class MulticlassDemo -{ - public static void main(String args[]) - { - Circle c= new Circle(); - Box b=new Box(); - c.r=5.0; - b.width=3.0;b.height=4.0;b.depth=5.0; - System.out.println("Circumference Circle: "+c.circumference()); - System.out.println("Area circle: "+c.area()); - System.out.println("Area of Box: "+b.area()); - System.out.println("Volume of Box: "+b.volume()); - } -} +class Circle +{ + + double r; + double circumference() + { + return 2*3.14*r; + } + double area() + { + return (22/7)*r*r; + } +} +class Box +{ + double width; + double height; + double depth; + double area() + { + double a; + a=(width*height + height*depth + width*depth)*2; + return a; + } + double volume() + { + double v; + v=width*height*depth; + return v; + } + +} +class MulticlassDemo +{ + public static void main(String args[]) + { + Circle c= new Circle(); + Box b=new Box(); + c.r=5.0; + b.width=3.0;b.height=4.0;b.depth=5.0; + System.out.println("Circumference Circle: "+c.circumference()); + System.out.println("Area circle: "+c.area()); + System.out.println("Area of Box: "+b.area()); + System.out.println("Volume of Box: "+b.volume()); + } +} diff --git a/PrintMazepath.java b/PrintMazepath.java index bdafd7e..fefbe59 100644 --- a/PrintMazepath.java +++ b/PrintMazepath.java @@ -1,32 +1,32 @@ - -package recursion; - - -public class PrintMazepath { - - public static void printmazepath(int cr,int cc,int er,int ec,String ans){ - - if(cr==er && cc==ec){ - System.out.println(ans); - return; - } - - if(cr>er || cc>ec){ - return; - } - - printmazepath(cr,cc+1,er,ec,ans +"H"); - - printmazepath(cr+1,cc,er,ec,ans +"V"); - - printmazepath(cr+1,cc+1,er,ec,ans +"D"); - - - } - - - public static void main(String[] args) { - printmazepath(0,0,2,2,""); - } - -} + +package recursion; + + +public class PrintMazepath { + + public static void printmazepath(int cr,int cc,int er,int ec,String ans){ + + if(cr==er && cc==ec){ + System.out.println(ans); + return; + } + + if(cr>er || cc>ec){ + return; + } + + printmazepath(cr,cc+1,er,ec,ans +"H"); + + printmazepath(cr+1,cc,er,ec,ans +"V"); + + printmazepath(cr+1,cc+1,er,ec,ans +"D"); + + + } + + + public static void main(String[] args) { + printmazepath(0,0,2,2,""); + } + +} diff --git a/QuickSort.java b/QuickSort.java deleted file mode 100644 index d3f8378..0000000 --- a/QuickSort.java +++ /dev/null @@ -1,48 +0,0 @@ -class QuickSort { - public static void main(String[]args) { - int a[]={10,75,-15,17,0,-2}; - QSort(a,0,a.length-1); - System.out.println("Sorted Array : "); - System.out.print("["); - for(int i=0;ipivot) { - end--; - } - if(start pattern - txt -> text - q -> A prime number - */ - static void search(String pat, String txt, int q) - { - int M = pat.length(); - int N = txt.length(); - int i, j; - int p = 0; // hash value for pattern - int t = 0; // hash value for txt - int h = 1; - - // The value of h would be "pow(d, M-1)%q" - for (i = 0; i < M - 1; i++) - h = (h * d) % q; - - // Calculate the hash value of pattern and first - // window of text - for (i = 0; i < M; i++) { - p = (d * p + pat.charAt(i)) % q; - t = (d * t + txt.charAt(i)) % q; - } - - // Slide the pattern over text one by one - for (i = 0; i <= N - M; i++) { - - // Check the hash values of current window of - // text and pattern. If the hash values match - // then only check for characters one by one - if (p == t) { - /* Check for characters one by one */ - for (j = 0; j < M; j++) { - if (txt.charAt(i + j) != pat.charAt(j)) - break; - } - - // if p == t and pat[0...M-1] = txt[i, i+1, - // ...i+M-1] - if (j == M) - System.out.println( - "Pattern found at index " + i); - } - - // Calculate hash value for next window of text: - // Remove leading digit, add trailing digit - if (i < N - M) { - t = (d * (t - txt.charAt(i) * h) - + txt.charAt(i + M)) - % q; - - // We might get negative value of t, - // converting it to positive - if (t < 0) - t = (t + q); - } - } - } - - /* Driver Code */ - public static void main(String[] args) - { - String txt = "GEEKS FOR GEEKS"; - String pat = "GEEK"; - - // A prime number - int q = 101; - - // Function Call - search(pat, txt, q); - } - } - +module Rabin_Karp_Problem { + // Following program is a Java implementation + // of Rabin Karp Algorithm given in the CLRS book + + public class Main { + // d is the number of characters in the input alphabet + public final static int d = 256; + + /* pat -> pattern + txt -> text + q -> A prime number + */ + static void search(String pat, String txt, int q) + { + int M = pat.length(); + int N = txt.length(); + int i, j; + int p = 0; // hash value for pattern + int t = 0; // hash value for txt + int h = 1; + + // The value of h would be "pow(d, M-1)%q" + for (i = 0; i < M - 1; i++) + h = (h * d) % q; + + // Calculate the hash value of pattern and first + // window of text + for (i = 0; i < M; i++) { + p = (d * p + pat.charAt(i)) % q; + t = (d * t + txt.charAt(i)) % q; + } + + // Slide the pattern over text one by one + for (i = 0; i <= N - M; i++) { + + // Check the hash values of current window of + // text and pattern. If the hash values match + // then only check for characters one by one + if (p == t) { + /* Check for characters one by one */ + for (j = 0; j < M; j++) { + if (txt.charAt(i + j) != pat.charAt(j)) + break; + } + + // if p == t and pat[0...M-1] = txt[i, i+1, + // ...i+M-1] + if (j == M) + System.out.println( + "Pattern found at index " + i); + } + + // Calculate hash value for next window of text: + // Remove leading digit, add trailing digit + if (i < N - M) { + t = (d * (t - txt.charAt(i) * h) + + txt.charAt(i + M)) + % q; + + // We might get negative value of t, + // converting it to positive + if (t < 0) + t = (t + q); + } + } + } + + /* Driver Code */ + public static void main(String[] args) + { + String txt = "GEEKS FOR GEEKS"; + String pat = "GEEK"; + + // A prime number + int q = 101; + + // Function Call + search(pat, txt, q); + } + } + } \ No newline at end of file diff --git a/SegmentTreeRMQ.java b/SegmentTreeRMQ.java index 6a4d133..7b4bfa6 100644 --- a/SegmentTreeRMQ.java +++ b/SegmentTreeRMQ.java @@ -1,481 +1,481 @@ -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.PrintWriter; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.InputMismatchException; -import java.util.List; - -class SEG { - InputStream is; - PrintWriter out; - String INPUT = ""; - - void solve() - { - int n = ni(); - int[] a = na(n); - int Q = ni(); - int[][] qs = new int[Q][]; - int[] vs = Arrays.copyOf(a, 2*(n+Q)); - for(int i = 0;i < Q;i++){ - int t = ni(); - if(t == 1){ - qs[i] = new int[]{1, ni()-1, ni()}; - vs[i+n] = qs[i][2]; - }else{ - qs[i] = new int[]{2}; - } - } - - for(int i = 0;i < n+Q;i++){ - vs[i+n+Q] = vs[i] + 1; - } - vs = shrink(vs); - for(int i = 0;i < n;i++)a[i] = vs[i]; - for(int i = 0;i < Q;i++){ - if(qs[i][0] == 1){ - qs[i][2] = vs[i+n]; - } - } - - st = new SegmentTreeRMQ(2*(n+Q)); - sit = new LST(2*(n+Q)+2); - sit.setRange(2*(n+Q)+2); - ans = 0; - for(int i = 0;i < n;i++){ - stand(a[i]); - } - - for(int[] q : qs){ - if(q[0] == 1){ - sit(a[q[1]]); - a[q[1]] = q[2]; - stand(a[q[1]]); - }else{ - out.println(ans); - } - } - } - - SegmentTreeRMQ st; - LST sit; - long ans = 0; - - void stand(int v) - { - int prev = sit.prev(v-1); - int next = sit.next(v+1); - if(sit.get(v)){ - if(prev+1 <= v-1)ans -= st.min(prev+1, v); - if(next-1 >= v+1)ans -= st.min(v+1, next); - }else{ - ans -= st.min(prev+1, next); - } - - sit.unset(v); - st.update(v, st.min(v, v+1)+1); - - prev = sit.prev(v-1); - next = sit.next(v+1); - ans += st.min(prev+1, next); - } - - void sit(int v) - { - int prev = sit.prev(v-1); - int next = sit.next(v+1); - if(sit.get(v)){ - if(prev+1 <= v-1)ans -= st.min(prev+1, v); - if(next-1 >= v+1)ans -= st.min(v+1, next); - }else{ - ans -= st.min(prev+1, next); - } - - int nv = st.min(v, v+1)-1; - if(nv == 0)sit.set(v); - st.update(v, nv); - - prev = sit.prev(v-1); - next = sit.next(v+1); - if(sit.get(v)){ - if(prev+1 <= v-1)ans += st.min(prev+1, v); - if(next-1 >= v+1)ans += st.min(v+1, next); - }else{ - ans += st.min(prev+1, next); - } - } - - public static class LST { - public long[][] set; - public int n; -// public int size; - - public LST(int n) { - this.n = n; - int d = 1; - for(int m = n;m > 1;m>>>=6, d++); - - set = new long[d][]; - for(int i = 0, m = n>>>6;i < d;i++, m>>>=6){ - set[i] = new long[m+1]; - } -// size = 0; - } - - // [0,r) - public LST setRange(int r) - { - for(int i = 0;i < set.length;i++, r=r+63>>>6){ - for(int j = 0;j < r>>>6;j++){ - set[i][j] = -1L; - } - if((r&63) != 0)set[i][r>>>6] |= (1L<= 0){ - for(int i = 0;i < set.length;i++, r=r+63>>>6){ - for(int j = 0;j < r+63>>>6;j++){ - set[i][j] = 0; - } - if((r&63) != 0)set[i][r>>>6] &= ~((1L<= 0 && pos < n){ -// if(!get(pos))size++; - for(int i = 0;i < set.length;i++, pos>>>=6){ - set[i][pos>>>6] |= 1L<= 0 && pos < n){ -// if(get(pos))size--; - for(int i = 0;i < set.length && (i == 0 || set[i-1][pos] == 0L);i++, pos>>>=6){ - set[i][pos>>>6] &= ~(1L<= 0 && pos < n && set[0][pos>>>6]<<~pos<0; - } - - public LST toggle(int pos) - { - return get(pos) ? unset(pos) : set(pos); - } - - public int prev(int pos) - { - for(int i = 0;i < set.length && pos >= 0;i++, pos>>>=6, pos--){ - int pre = prev(set[i][pos>>>6], pos&63); - if(pre != -1){ - pos = pos>>>6<<6|pre; - while(i > 0)pos = pos<<6|63-Long.numberOfLeadingZeros(set[--i][pos]); - return pos; - } - } - return -1; - } - - public int next(int pos) - { - for(int i = 0;i < set.length && pos>>>6 < set[i].length;i++, pos>>>=6, pos++){ - int nex = next(set[i][pos>>>6], pos&63); - if(nex != -1){ - pos = pos>>>6<<6|nex; - while(i > 0)pos = pos<<6|Long.numberOfTrailingZeros(set[--i][pos]); - return pos; - } - } - return -1; - } - - private static int prev(long set, int n) - { - long h = set<<~n; - if(h == 0L)return -1; - return -Long.numberOfLeadingZeros(h)+n; - } - - private static int next(long set, int n) - { - long h = set>>>n; - if(h == 0L)return -1; - return Long.numberOfTrailingZeros(h)+n; - } - - @Override - public String toString() - { - List list = new ArrayList(); - for(int pos = next(0);pos != -1;pos = next(pos+1)){ - list.add(pos); - } - return list.toString(); - } - } - - - public static class SegmentTreeRMQ { - public int M, H, N; - public int[] st; - - public SegmentTreeRMQ(int n) - { - N = n; - M = Integer.highestOneBit(Math.max(N-1, 1))<<2; - H = M>>>1; - st = new int[M]; -// Arrays.fill(st, 0, M, Integer.MAX_VALUE); - } - - public SegmentTreeRMQ(int[] a) - { - N = a.length; - M = Integer.highestOneBit(Math.max(N-1, 1))<<2; - H = M>>>1; - st = new int[M]; - for(int i = 0;i < N;i++){ - st[H+i] = a[i]; - } - Arrays.fill(st, H+N, M, Integer.MAX_VALUE); - for(int i = H-1;i >= 1;i--)propagate(i); - } - - public void update(int pos, int x) - { - st[H+pos] = x; - for(int i = (H+pos)>>>1;i >= 1;i >>>= 1)propagate(i); - } - - private void propagate(int i) - { - st[i] = Math.min(st[2*i], st[2*i+1]); - } - - public int minx(int l, int r){ - int min = Integer.MAX_VALUE; - if(l >= r)return min; - while(l != 0){ - int f = l&-l; - if(l+f > r)break; - int v = st[(H+l)/f]; - if(v < min)min = v; - l += f; - } - - while(l < r){ - int f = r&-r; - int v = st[(H+r)/f-1]; - if(v < min)min = v; - r -= f; - } - return min; - } - - public int min(int l, int r){ return l >= r ? 0 : min(l, r, 0, H, 1);} - - private int min(int l, int r, int cl, int cr, int cur) - { - if(l <= cl && cr <= r){ - return st[cur]; - }else{ - int mid = cl+cr>>>1; - int ret = Integer.MAX_VALUE; - if(cl < r && l < mid){ - ret = Math.min(ret, min(l, r, cl, mid, 2*cur)); - } - if(mid < r && l < cr){ - ret = Math.min(ret, min(l, r, mid, cr, 2*cur+1)); - } - return ret; - } - } - - public int firstle(int l, int v) { - int cur = H+l; - while(true){ - if(st[cur] <= v){ - if(cur < H){ - cur = 2*cur; - }else{ - return cur-H; - } - }else{ - cur++; - if((cur&cur-1) == 0)return -1; - if((cur&1)==0)cur>>>=1; - } - } - } - - public int lastle(int l, int v) { - int cur = H+l; - while(true){ - if(st[cur] <= v){ - if(cur < H){ - cur = 2*cur+1; - }else{ - return cur-H; - } - }else{ - if((cur&cur-1) == 0)return -1; - cur--; - if((cur&1)==1)cur>>>=1; - } - } - } - } - - - public static int[] shrink(int[] a) { - int n = a.length; - long[] b = new long[n]; - for (int i = 0; i < n; i++) - b[i] = (long) a[i] << 32 | i; - Arrays.sort(b); - int[] ret = new int[n]; - int p = 0; - for (int i = 0; i < n; i++) { - if (i > 0 && (b[i] ^ b[i - 1]) >> 32 != 0) - p++; - ret[(int) b[i]] = p; - } - return ret; - } - - - - void run() throws Exception - { - is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); - out = new PrintWriter(System.out); - - long s = System.currentTimeMillis(); - solve(); - out.flush(); - if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); - } - - public static void main(String[] args) throws Exception { - new SEG().run(); - - } - - private byte[] inbuf = new byte[1024]; - public int lenbuf = 0, ptrbuf = 0; - - private int readByte() - { - if(lenbuf == -1)throw new InputMismatchException(); - if(ptrbuf >= lenbuf){ - ptrbuf = 0; - try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } - if(lenbuf <= 0)return -1; - } - return inbuf[ptrbuf++]; - } - - private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } - private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } - - private double nd() { return Double.parseDouble(ns()); } - private char nc() { return (char)skip(); } - - private String ns() - { - int b = skip(); - StringBuilder sb = new StringBuilder(); - while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') - sb.appendCodePoint(b); - b = readByte(); - } - return sb.toString(); - } - - private char[] ns(int n) - { - char[] buf = new char[n]; - int b = skip(), p = 0; - while(p < n && !(isSpaceChar(b))){ - buf[p++] = (char)b; - b = readByte(); - } - return n == p ? buf : Arrays.copyOf(buf, p); - } - - private char[][] nm(int n, int m) - { - char[][] map = new char[n][]; - for(int i = 0;i < n;i++)map[i] = ns(m); - return map; - } - - private int[] na(int n) - { - int[] a = new int[n]; - for(int i = 0;i < n;i++)a[i] = ni(); - return a; - } - - private int ni() - { - int num = 0, b; - boolean minus = false; - while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); - if(b == '-'){ - minus = true; - b = readByte(); - } - - while(true){ - if(b >= '0' && b <= '9'){ - num = num * 10 + (b - '0'); - }else{ - return minus ? -num : num; - } - b = readByte(); - } - } - - private long nl() - { - long num = 0; - int b; - boolean minus = false; - while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); - if(b == '-'){ - minus = true; - b = readByte(); - } - - while(true){ - if(b >= '0' && b <= '9'){ - num = num * 10 + (b - '0'); - }else{ - return minus ? -num : num; - } - b = readByte(); - } - } - - private void tr(Object... o) { if(INPUT.length() > 0)System.out.println(Arrays.deepToString(o)); } +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.InputMismatchException; +import java.util.List; + +class SEG { + InputStream is; + PrintWriter out; + String INPUT = ""; + + void solve() + { + int n = ni(); + int[] a = na(n); + int Q = ni(); + int[][] qs = new int[Q][]; + int[] vs = Arrays.copyOf(a, 2*(n+Q)); + for(int i = 0;i < Q;i++){ + int t = ni(); + if(t == 1){ + qs[i] = new int[]{1, ni()-1, ni()}; + vs[i+n] = qs[i][2]; + }else{ + qs[i] = new int[]{2}; + } + } + + for(int i = 0;i < n+Q;i++){ + vs[i+n+Q] = vs[i] + 1; + } + vs = shrink(vs); + for(int i = 0;i < n;i++)a[i] = vs[i]; + for(int i = 0;i < Q;i++){ + if(qs[i][0] == 1){ + qs[i][2] = vs[i+n]; + } + } + + st = new SegmentTreeRMQ(2*(n+Q)); + sit = new LST(2*(n+Q)+2); + sit.setRange(2*(n+Q)+2); + ans = 0; + for(int i = 0;i < n;i++){ + stand(a[i]); + } + + for(int[] q : qs){ + if(q[0] == 1){ + sit(a[q[1]]); + a[q[1]] = q[2]; + stand(a[q[1]]); + }else{ + out.println(ans); + } + } + } + + SegmentTreeRMQ st; + LST sit; + long ans = 0; + + void stand(int v) + { + int prev = sit.prev(v-1); + int next = sit.next(v+1); + if(sit.get(v)){ + if(prev+1 <= v-1)ans -= st.min(prev+1, v); + if(next-1 >= v+1)ans -= st.min(v+1, next); + }else{ + ans -= st.min(prev+1, next); + } + + sit.unset(v); + st.update(v, st.min(v, v+1)+1); + + prev = sit.prev(v-1); + next = sit.next(v+1); + ans += st.min(prev+1, next); + } + + void sit(int v) + { + int prev = sit.prev(v-1); + int next = sit.next(v+1); + if(sit.get(v)){ + if(prev+1 <= v-1)ans -= st.min(prev+1, v); + if(next-1 >= v+1)ans -= st.min(v+1, next); + }else{ + ans -= st.min(prev+1, next); + } + + int nv = st.min(v, v+1)-1; + if(nv == 0)sit.set(v); + st.update(v, nv); + + prev = sit.prev(v-1); + next = sit.next(v+1); + if(sit.get(v)){ + if(prev+1 <= v-1)ans += st.min(prev+1, v); + if(next-1 >= v+1)ans += st.min(v+1, next); + }else{ + ans += st.min(prev+1, next); + } + } + + public static class LST { + public long[][] set; + public int n; +// public int size; + + public LST(int n) { + this.n = n; + int d = 1; + for(int m = n;m > 1;m>>>=6, d++); + + set = new long[d][]; + for(int i = 0, m = n>>>6;i < d;i++, m>>>=6){ + set[i] = new long[m+1]; + } +// size = 0; + } + + // [0,r) + public LST setRange(int r) + { + for(int i = 0;i < set.length;i++, r=r+63>>>6){ + for(int j = 0;j < r>>>6;j++){ + set[i][j] = -1L; + } + if((r&63) != 0)set[i][r>>>6] |= (1L<= 0){ + for(int i = 0;i < set.length;i++, r=r+63>>>6){ + for(int j = 0;j < r+63>>>6;j++){ + set[i][j] = 0; + } + if((r&63) != 0)set[i][r>>>6] &= ~((1L<= 0 && pos < n){ +// if(!get(pos))size++; + for(int i = 0;i < set.length;i++, pos>>>=6){ + set[i][pos>>>6] |= 1L<= 0 && pos < n){ +// if(get(pos))size--; + for(int i = 0;i < set.length && (i == 0 || set[i-1][pos] == 0L);i++, pos>>>=6){ + set[i][pos>>>6] &= ~(1L<= 0 && pos < n && set[0][pos>>>6]<<~pos<0; + } + + public LST toggle(int pos) + { + return get(pos) ? unset(pos) : set(pos); + } + + public int prev(int pos) + { + for(int i = 0;i < set.length && pos >= 0;i++, pos>>>=6, pos--){ + int pre = prev(set[i][pos>>>6], pos&63); + if(pre != -1){ + pos = pos>>>6<<6|pre; + while(i > 0)pos = pos<<6|63-Long.numberOfLeadingZeros(set[--i][pos]); + return pos; + } + } + return -1; + } + + public int next(int pos) + { + for(int i = 0;i < set.length && pos>>>6 < set[i].length;i++, pos>>>=6, pos++){ + int nex = next(set[i][pos>>>6], pos&63); + if(nex != -1){ + pos = pos>>>6<<6|nex; + while(i > 0)pos = pos<<6|Long.numberOfTrailingZeros(set[--i][pos]); + return pos; + } + } + return -1; + } + + private static int prev(long set, int n) + { + long h = set<<~n; + if(h == 0L)return -1; + return -Long.numberOfLeadingZeros(h)+n; + } + + private static int next(long set, int n) + { + long h = set>>>n; + if(h == 0L)return -1; + return Long.numberOfTrailingZeros(h)+n; + } + + @Override + public String toString() + { + List list = new ArrayList(); + for(int pos = next(0);pos != -1;pos = next(pos+1)){ + list.add(pos); + } + return list.toString(); + } + } + + + public static class SegmentTreeRMQ { + public int M, H, N; + public int[] st; + + public SegmentTreeRMQ(int n) + { + N = n; + M = Integer.highestOneBit(Math.max(N-1, 1))<<2; + H = M>>>1; + st = new int[M]; +// Arrays.fill(st, 0, M, Integer.MAX_VALUE); + } + + public SegmentTreeRMQ(int[] a) + { + N = a.length; + M = Integer.highestOneBit(Math.max(N-1, 1))<<2; + H = M>>>1; + st = new int[M]; + for(int i = 0;i < N;i++){ + st[H+i] = a[i]; + } + Arrays.fill(st, H+N, M, Integer.MAX_VALUE); + for(int i = H-1;i >= 1;i--)propagate(i); + } + + public void update(int pos, int x) + { + st[H+pos] = x; + for(int i = (H+pos)>>>1;i >= 1;i >>>= 1)propagate(i); + } + + private void propagate(int i) + { + st[i] = Math.min(st[2*i], st[2*i+1]); + } + + public int minx(int l, int r){ + int min = Integer.MAX_VALUE; + if(l >= r)return min; + while(l != 0){ + int f = l&-l; + if(l+f > r)break; + int v = st[(H+l)/f]; + if(v < min)min = v; + l += f; + } + + while(l < r){ + int f = r&-r; + int v = st[(H+r)/f-1]; + if(v < min)min = v; + r -= f; + } + return min; + } + + public int min(int l, int r){ return l >= r ? 0 : min(l, r, 0, H, 1);} + + private int min(int l, int r, int cl, int cr, int cur) + { + if(l <= cl && cr <= r){ + return st[cur]; + }else{ + int mid = cl+cr>>>1; + int ret = Integer.MAX_VALUE; + if(cl < r && l < mid){ + ret = Math.min(ret, min(l, r, cl, mid, 2*cur)); + } + if(mid < r && l < cr){ + ret = Math.min(ret, min(l, r, mid, cr, 2*cur+1)); + } + return ret; + } + } + + public int firstle(int l, int v) { + int cur = H+l; + while(true){ + if(st[cur] <= v){ + if(cur < H){ + cur = 2*cur; + }else{ + return cur-H; + } + }else{ + cur++; + if((cur&cur-1) == 0)return -1; + if((cur&1)==0)cur>>>=1; + } + } + } + + public int lastle(int l, int v) { + int cur = H+l; + while(true){ + if(st[cur] <= v){ + if(cur < H){ + cur = 2*cur+1; + }else{ + return cur-H; + } + }else{ + if((cur&cur-1) == 0)return -1; + cur--; + if((cur&1)==1)cur>>>=1; + } + } + } + } + + + public static int[] shrink(int[] a) { + int n = a.length; + long[] b = new long[n]; + for (int i = 0; i < n; i++) + b[i] = (long) a[i] << 32 | i; + Arrays.sort(b); + int[] ret = new int[n]; + int p = 0; + for (int i = 0; i < n; i++) { + if (i > 0 && (b[i] ^ b[i - 1]) >> 32 != 0) + p++; + ret[(int) b[i]] = p; + } + return ret; + } + + + + void run() throws Exception + { + is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); + out = new PrintWriter(System.out); + + long s = System.currentTimeMillis(); + solve(); + out.flush(); + if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); + } + + public static void main(String[] args) throws Exception { + new SEG().run(); + + } + + private byte[] inbuf = new byte[1024]; + public int lenbuf = 0, ptrbuf = 0; + + private int readByte() + { + if(lenbuf == -1)throw new InputMismatchException(); + if(ptrbuf >= lenbuf){ + ptrbuf = 0; + try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } + if(lenbuf <= 0)return -1; + } + return inbuf[ptrbuf++]; + } + + private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } + private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } + + private double nd() { return Double.parseDouble(ns()); } + private char nc() { return (char)skip(); } + + private String ns() + { + int b = skip(); + StringBuilder sb = new StringBuilder(); + while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') + sb.appendCodePoint(b); + b = readByte(); + } + return sb.toString(); + } + + private char[] ns(int n) + { + char[] buf = new char[n]; + int b = skip(), p = 0; + while(p < n && !(isSpaceChar(b))){ + buf[p++] = (char)b; + b = readByte(); + } + return n == p ? buf : Arrays.copyOf(buf, p); + } + + private char[][] nm(int n, int m) + { + char[][] map = new char[n][]; + for(int i = 0;i < n;i++)map[i] = ns(m); + return map; + } + + private int[] na(int n) + { + int[] a = new int[n]; + for(int i = 0;i < n;i++)a[i] = ni(); + return a; + } + + private int ni() + { + int num = 0, b; + boolean minus = false; + while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); + if(b == '-'){ + minus = true; + b = readByte(); + } + + while(true){ + if(b >= '0' && b <= '9'){ + num = num * 10 + (b - '0'); + }else{ + return minus ? -num : num; + } + b = readByte(); + } + } + + private long nl() + { + long num = 0; + int b; + boolean minus = false; + while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); + if(b == '-'){ + minus = true; + b = readByte(); + } + + while(true){ + if(b >= '0' && b <= '9'){ + num = num * 10 + (b - '0'); + }else{ + return minus ? -num : num; + } + b = readByte(); + } + } + + private void tr(Object... o) { if(INPUT.length() > 0)System.out.println(Arrays.deepToString(o)); } } \ No newline at end of file diff --git a/String to LowerCase.java b/String to LowerCase.java index 31652f5..07c4828 100644 --- a/String to LowerCase.java +++ b/String to LowerCase.java @@ -1,32 +1,32 @@ -// Initial template for Java - -import java.util.*; -import java.io.*; - -class GFG { - public static void main(String args[]) throws IOException { - BufferedReader read = - new BufferedReader(new InputStreamReader(System.in)); - System.out.println("Enter no of words you want to check"); - int t = Integer.parseInt(read.readLine()); - - while (t-- > 0) { - System.out.println("Enter Word to conver to lowercase"); - String S = read.readLine(); - Solution ob = new Solution(); - - System.out.println(ob.toLower(S)); - } - } -}// } Driver Code Ends - - -// User function template for Java - -class Solution { - static String toLower(String S) { - String S1=S.toLowerCase(); - return S1; - } - +// Initial template for Java + +import java.util.*; +import java.io.*; + +class GFG { + public static void main(String args[]) throws IOException { + BufferedReader read = + new BufferedReader(new InputStreamReader(System.in)); + System.out.println("Enter no of words you want to check"); + int t = Integer.parseInt(read.readLine()); + + while (t-- > 0) { + System.out.println("Enter Word to conver to lowercase"); + String S = read.readLine(); + Solution ob = new Solution(); + + System.out.println(ob.toLower(S)); + } + } +}// } Driver Code Ends + + +// User function template for Java + +class Solution { + static String toLower(String S) { + String S1=S.toLowerCase(); + return S1; + } + } \ No newline at end of file diff --git a/TopologicalSorting.java b/TopologicalSorting.java deleted file mode 100644 index ec85870..0000000 --- a/TopologicalSorting.java +++ /dev/null @@ -1,53 +0,0 @@ -import java.util.*; -class Graph{ - int v; - LinkedList adj[]; - @SuppressWarnings("unchecked") - Graph(int v){ - this.v=v; - adj=new LinkedList[v]; - for(int i=0;i(); - } -} -class TopologicalSorting{ - public static void addEdge(Graph g,int u,int v){ - g.adj[u].add(v); - } - public static void toposortutil(Graph g,int node,boolean visit[],Stack st) - { - visit[node]=true; - int i; - Iteratorit=g.adj[node].iterator(); - while(it.hasNext()) - { - i=it.next(); - System.out.println(i); - if(!visit[i]) - toposortutil(g,i,visit,st); - } - System.out.println("node"+node); - st.push(node); - } - public static void toposort(Graph g){ - Stack st=new Stack(); - boolean visit[]=new boolean[g.v]; - for(int i=0;i n) { - return 0; - } - if (i == n) { - return 1; - } - if (dp[i] > 0) { - return dp[i]; - } - dp[i] = climb_Stairs(i + 1, n, dp) + climb_Stairs(i + 2, n, dp); - return dp[i]; - } +// https://leetcode.com/problems/climbing-stairs/ + +public class Solution { + public int climbStairs(int n) { + + return climb_Stairs(0, n, new int[n + 1]); + } + + public int climb_Stairs(int i, int n, int dp[]) { + if (i > n) { + return 0; + } + if (i == n) { + return 1; + } + if (dp[i] > 0) { + return dp[i]; + } + dp[i] = climb_Stairs(i + 1, n, dp) + climb_Stairs(i + 2, n, dp); + return dp[i]; + } } \ No newline at end of file diff --git a/evenodd.java b/evenodd.java index cdac70d..694d2d1 100644 --- a/evenodd.java +++ b/evenodd.java @@ -1,13 +1,13 @@ -import java.util.*; -class evenodd{ - public static void main(String args[]){ - Scanner sc= new Scanner(System.in); - int x; - System.out.println("Enter an integer: "); - x=sc.nextInt(); - if(x%2==0) - System.out.println("Number is even"); - else - System.out.println("Number is odd"); - } +import java.util.*; +class evenodd{ + public static void main(String args[]){ + Scanner sc= new Scanner(System.in); + int x; + System.out.println("Enter an integer: "); + x=sc.nextInt(); + if(x%2==0) + System.out.println("Number is even"); + else + System.out.println("Number is odd"); + } } \ No newline at end of file diff --git a/matrixChainMultiply.java b/matrixChainMultiply.java index 99707af..04aa3e6 100644 --- a/matrixChainMultiply.java +++ b/matrixChainMultiply.java @@ -1,41 +1,41 @@ -/* A naive recursive implementation that simply follows -the above optimal substructure property */ -class MatrixChainMultiplication { - // Matrix Ai has dimension p[i-1] x p[i] for i = 1..n - static int MatrixChainOrder(int p[], int i, int j) - { - if (i == j) - return 0; - - int min = Integer.MAX_VALUE; - - // place parenthesis at different places between - // first and last matrix, recursively calculate - // count of multiplications for each parenthesis - // placement and return the minimum count - for (int k = i; k < j; k++) - { - int count = MatrixChainOrder(p, i, k) - + MatrixChainOrder(p, k + 1, j) - + p[i - 1] * p[k] * p[j]; - - if (count < min) - min = count; - } - - // Return minimum count - return min; - } - - // Driver code - public static void main(String args[]) - { - int arr[] = new int[] { 1, 2, 3, 4, 3 }; - int n = arr.length; - - System.out.println( - "Minimum number of multiplications is " - + MatrixChainOrder(arr, 1, n - 1)); - } -} -/* This code is contributed by Rajat Mishra*/ +/* A naive recursive implementation that simply follows +the above optimal substructure property */ +class MatrixChainMultiplication { + // Matrix Ai has dimension p[i-1] x p[i] for i = 1..n + static int MatrixChainOrder(int p[], int i, int j) + { + if (i == j) + return 0; + + int min = Integer.MAX_VALUE; + + // place parenthesis at different places between + // first and last matrix, recursively calculate + // count of multiplications for each parenthesis + // placement and return the minimum count + for (int k = i; k < j; k++) + { + int count = MatrixChainOrder(p, i, k) + + MatrixChainOrder(p, k + 1, j) + + p[i - 1] * p[k] * p[j]; + + if (count < min) + min = count; + } + + // Return minimum count + return min; + } + + // Driver code + public static void main(String args[]) + { + int arr[] = new int[] { 1, 2, 3, 4, 3 }; + int n = arr.length; + + System.out.println( + "Minimum number of multiplications is " + + MatrixChainOrder(arr, 1, n - 1)); + } +} +/* This code is contributed by Rajat Mishra*/ diff --git a/minCost.java b/minCost.java index 68e9323..34abeda 100644 --- a/minCost.java +++ b/minCost.java @@ -1,43 +1,43 @@ -/* A Naive recursive implementation of -MCP(Minimum Cost Path) problem */ -public class GFG { - - /* A utility function that returns - minimum of 3 integers */ - static int min(int x, int y, int z) - { - if (x < y) - return (x < z) ? x : z; - else - return (y < z) ? y : z; - } - - /* Returns cost of minimum cost path - from (0,0) to (m, n) in mat[R][C]*/ - static int minCost(int cost[][], int m, - int n) - { - if (n < 0 || m < 0) - return Integer.MAX_VALUE; - else if (m == 0 && n == 0) - return cost[m][n]; - else - return cost[m][n] + - min( minCost(cost, m-1, n-1), - minCost(cost, m-1, n), - minCost(cost, m, n-1) ); - } - - // Driver code - public static void main(String args[]) - { - - int cost[][] = { {1, 2, 3}, - {4, 8, 2}, - {1, 5, 3} }; - - System.out.print(minCost(cost, 2, 2)); - } -} - -// This code is contributed by Sam007 +/* A Naive recursive implementation of +MCP(Minimum Cost Path) problem */ +public class GFG { + + /* A utility function that returns + minimum of 3 integers */ + static int min(int x, int y, int z) + { + if (x < y) + return (x < z) ? x : z; + else + return (y < z) ? y : z; + } + + /* Returns cost of minimum cost path + from (0,0) to (m, n) in mat[R][C]*/ + static int minCost(int cost[][], int m, + int n) + { + if (n < 0 || m < 0) + return Integer.MAX_VALUE; + else if (m == 0 && n == 0) + return cost[m][n]; + else + return cost[m][n] + + min( minCost(cost, m-1, n-1), + minCost(cost, m-1, n), + minCost(cost, m, n-1) ); + } + + // Driver code + public static void main(String args[]) + { + + int cost[][] = { {1, 2, 3}, + {4, 8, 2}, + {1, 5, 3} }; + + System.out.print(minCost(cost, 2, 2)); + } +} + +// This code is contributed by Sam007 diff --git a/number_diamond.java b/number_diamond.java index d09e164..a624dc1 100644 --- a/number_diamond.java +++ b/number_diamond.java @@ -1,37 +1,37 @@ -import java.util.Scanner; - -public class diamond_pattern -{ - public static void main(String[] args) { - Scanner sc=new Scanner(System.in); - System.out.println("Enter number of rows"); - int n = sc.nextInt(); - System.out.println("Enter number of colums"); - int m = sc.nextInt(); - - for (int i = 1; i <= n; i++) - { - //n = 4; - - for (int j = 1; j<= n - i; j++) { System.out.print(" "); } for (int k = i; k >= 1; k--) - { - System.out.print(k); - } - for (int l = 2; l <= i; l++) { System.out.print(l); } System.out.println(); } for (int i = 3; i >= 1; i--) - { - n = 3; - - for (int j = 0; j<= n - i; j++) { System.out.print(" "); } for (int k = i; k >= 1; k--) - { - System.out.print(k); - } - for (int l = 2; l <= i; l++) - { - System.out.print(l); - } - - System.out.println(); - } - - } +import java.util.Scanner; + +public class diamond_pattern +{ + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + System.out.println("Enter number of rows"); + int n = sc.nextInt(); + System.out.println("Enter number of colums"); + int m = sc.nextInt(); + + for (int i = 1; i <= n; i++) + { + //n = 4; + + for (int j = 1; j<= n - i; j++) { System.out.print(" "); } for (int k = i; k >= 1; k--) + { + System.out.print(k); + } + for (int l = 2; l <= i; l++) { System.out.print(l); } System.out.println(); } for (int i = 3; i >= 1; i--) + { + n = 3; + + for (int j = 0; j<= n - i; j++) { System.out.print(" "); } for (int k = i; k >= 1; k--) + { + System.out.print(k); + } + for (int l = 2; l <= i; l++) + { + System.out.print(l); + } + + System.out.println(); + } + + } } \ No newline at end of file diff --git a/rotate_array.java b/rotate_array.java index 8b612a7..94ae57f 100644 --- a/rotate_array.java +++ b/rotate_array.java @@ -1,47 +1,47 @@ -package functions; -import java.io.*; - -public class rotate_array { - public static void display(int[] a){ - StringBuilder sb = new StringBuilder(); - - for(int val: a){ - sb.append(val + " "); - } - System.out.println(sb); - } - public static void reverse(int[] a, int m, int k){ - for(int i=m;i(); - } -} -class TopologicalSorting{ - public static void addEdge(Graph g,int u,int v){ - g.adj[u].add(v); - } - public static void toposortutil(Graph g,int node,boolean visit[],Stack st) - { - visit[node]=true; - int i; - Iteratorit=g.adj[node].iterator(); - while(it.hasNext()) - { - i=it.next(); - System.out.println(i); - if(!visit[i]) - toposortutil(g,i,visit,st); - } - System.out.println("node"+node); - st.push(node); - } - public static void toposort(Graph g){ - Stack st=new Stack(); - boolean visit[]=new boolean[g.v]; - for(int i=0;i adj[]; + @SuppressWarnings("unchecked") + Graph(int v){ + this.v=v; + adj=new LinkedList[v]; + for(int i=0;i(); + } +} +class TopologicalSorting{ + public static void addEdge(Graph g,int u,int v){ + g.adj[u].add(v); + } + public static void toposortutil(Graph g,int node,boolean visit[],Stack st) + { + visit[node]=true; + int i; + Iteratorit=g.adj[node].iterator(); + while(it.hasNext()) + { + i=it.next(); + System.out.println(i); + if(!visit[i]) + toposortutil(g,i,visit,st); + } + System.out.println("node"+node); + st.push(node); + } + public static void toposort(Graph g){ + Stack st=new Stack(); + boolean visit[]=new boolean[g.v]; + for(int i=0;i