├── 1-D Array - In Class ├── Donation.java ├── Max-Number.cpp ├── Max_Number.java └── Maximum-Difference-Array.java ├── 1-D Array - Post Class ├── Mohit-And-Array.java ├── NoXor-Simple-Contest.java ├── Pair-Em-Up-Contest.java └── Walter-White-Easy.cpp ├── 2-D Arrays - Post Class ├── A-Boolean-Matrix-Problem.cpp ├── Chess-Board.cpp └── Chessboard-Formation.java ├── 2-D Arrays, Maths - In Class ├── Boundary-Traversal-of-Matrix.java ├── Diagonal-Sum.java ├── Matrix-Zig-Zag-Traversal.java ├── Rotate-the-Matrix.java ├── Simple-Determinant.java └── Simple-transpose.java ├── Basics of Programming, Input, Output, Variables & Operators - In Class ├── Dishes.cpp ├── Operators.cpp └── Print-the-Input.java ├── Basics of Programming, Input, Output, Variables & Operators - In Class1 ├── Fahrenheit-to-celius.cpp └── Quadratic.java ├── Basics of Programming, Java Programming - [Syntax], Input, Output, Variables & O ... - In Class ├── Area-of-Square.cpp ├── RNS-of-an-Array.java └── Ram-Grades.cpp ├── Bit Manipulation - In Class ├── Binary-Representation.cpp ├── Bit-Count.cpp ├── Bit-Difference.cpp ├── Cake-Xor-Contest.java ├── Decide-parity-winner.java ├── Decimal-to-Binary.cpp ├── Decimal-to-Binary.java ├── Gray-Code.cpp ├── HardWorking-AI.cpp ├── Hardworking-al.java ├── Help-Chefina.cpp ├── Highest-Power-of-2.cpp ├── Minimize-XOR.cpp ├── Minimum-Operations.cpp ├── Nth-power-of-2.java ├── Parity-Counter.java └── Square-of-a-number.cpp ├── Bit Manipulation - Post Class ├── And-OR-Operation.cpp ├── Find-Unique.cpp ├── Max-Subset-XOR.cpp ├── Two-Bits.java └── Ultron-Gridding-the-XOR.cpp ├── Circular Linked List - Post Class ├── Delete_Every_Kth_Node.java └── Exchange_Node.java ├── Contest 16-07-22 ├── Easy-Sorting.js ├── Separating-Negative-and-possitive-number.java └── max-sum-column.java ├── Contest 23-07-22 ├── Palindrome-list.java ├── Sara-Student.java └── insert-node-at-the-tail.java ├── Contest Questions - 09 July (May 22) ├── Sort-it-contest.java └── maximim-value-of-x.java ├── Contest-09-07-2022 ├── Grid-filling-contest.java ├── maximum-value-of-X.java └── sort-it.java ├── Control Structures, Loops - Post Class ├── Divisors-Problem.java ├── Loop-Debugging-14.cpp ├── Pattern-Printing.cpp └── Pattern.java ├── Double Linked List - In Class ├── Deletion-in-Doubly-Linked-list.java ├── Deletion_In_Doubly_Linked_List.java ├── Insertion_node_at_kth_postition_in_doubly_linked_list.java ├── Reversing-a-double-linked-list.java ├── Swap-Nodes-at-position-k-from-beginning-and-end.java ├── insert-node-at-kth-position-in-doubly-linked-list.java └── insert-node-at-the-head-in-a-doubly-linked-list.java ├── Double Linked List - Post Class ├── Binary_tree_to_double_linked_list.java └── Remove_Duplicates_from_doubly_linked_list.java ├── GCD, Primes - Post Class ├── Closet-Buddy-Contest.cpp ├── GCD-Pairs-Contest.java ├── Generator-Super-Prime.py ├── Identical-Groups.java ├── Is-this-prime.cpp ├── Nth-number-made-of-prime-Digits.cpp ├── Travelling.java └── Weird-Chapo.cpp ├── Hashing - In Class ├── Longest-sub-array-with-sum-k.java └── subarrays-with-equal-1s-and-0s.java ├── Hashing, Sortings, Searching, Bubble Sort - Post Class ├── EVM-Machine.cpp ├── First-n-n-repeating-character-in-a-string.java └── Group-of-Numbers.java ├── If else ladder, Switch case, Ternary Operator - Post Class ├── Checking-Number.cpp ├── Finding-Roots.java ├── Leap-Year.java ├── Logical-Operators.cpp ├── MCQ-Exam.cpp └── Students-Grades.cpp ├── Input, Output, Variables & Operators - Post Class ├── Magic-Trick.java └── Steps-Execution.java ├── Java Programming - [Syntax] - Post Class ├── Number-Dilemma.java └── String-TypeCast.cpp ├── Linear Search - In Class └── Find-position-X-in-Array.java ├── Linear Search, Binary search - Post Class ├── Kth-smallest-difference.cpp ├── Searching-an-Element-in-a-sorted-array.java ├── check-if-array-is-sorted-and-rotated-clockwise.java ├── counters-and-line.java └── median-oftwo-sorted-arrays.cpp ├── Maths, Geometry - Post Class ├── Dining-Table.java ├── Geometry-Exam.cpp ├── Too-Many-Rectangles.cpp ├── Too-Many-Rectangles.java └── Tringles-love.java ├── Maths, Primes, Geometry - In Class ├── Check-if-Prime.cpp ├── Closet-Prime-Contest.java ├── Divisors-Of-N.java ├── Functions-prime.cpp └── Number-Of-Prime-Easy_Version.cpp ├── Newton-School-Course.pdf ├── OOPS Assignments ├── Adderll.java ├── Classes-in-java.java ├── Cylinder.java ├── Filter.java ├── Pool-Area.java ├── Shapes-Class-INheritance.java └── student.java ├── Queue - In Class ├── Linked_list_implementation_of_queue.java ├── Sum_of_minimum_and_maximum_elements_of_subarrays.cpp └── array_implementation_of_queue.java ├── README.md ├── Recursion new - post class ├── BlackAndWhite.cpp ├── ComputePower.java ├── Cricket-Fever.java ├── Factorualrecursion.cpp ├── FibnocciNumbers.java ├── Gameofexecution.cpp ├── InsertOperator.java ├── Kthpermutation.cpp ├── PalindromeRecursive.java ├── PrintPattern.java ├── Singledigit.cpp ├── Subsets.cpp ├── Sumofdigit.java ├── TowerOfHanoi.cpp └── printAllSubarray.java ├── Recursion(new) - In Class ├── Multiplication.java └── Palindrome-Recursive.java ├── Recursion(new) - Post Class ├── BlackAndWhite.cpp ├── Factorualrecursion.cpp └── InsertOperator.java ├── Single Linked List - Post Class ├── Add_1_to_a_number_represented_as_linked_list.java ├── Add_Two_Numbers.java ├── Merge_two_sorted_linked_list.java └── Nth_node_from_end_of_linked_list.java ├── Single Linked List [In class] ├── Delete_THe_Kth_Node_FRom_the_End.java ├── Delete_the_middle_of_the_Linked_LIst.java ├── Insert_Node_At_The_Head.java ├── Insert_Node_At_The_Tail.java ├── Insert_Node_at_given_Position.java ├── Palindrome_LIst.java ├── Print_The_Linked_LIst.java ├── Reversing_The_Linked_LIst.java └── Zero_at_front_of_linked_List.java ├── Single Linked List [post class] ├── Add_two_Numbers.java ├── Merge_two_sorted_Linked_List.java ├── Nth_Node_FRom_End_of_Linked_list.java └── add_1_to_a_number_Represented_as_linked_list.java ├── Sliding window - In Class ├── Max-window-sum.java ├── Sara-And-Average.java └── Zero-Window-Sum.java ├── Sliding window - Post Class ├── Adjacent-Window-Sum.java ├── DNA-Sequence.java └── Longest-Unique-Character-Substring.java ├── Stack - In Class ├── Array_implementation_Of_Stack.java ├── Greater_Is_Better.java ├── Infix_To_PostFix.java ├── Maximum_of_minimum_of_every_window_size.cpp ├── Stack_implementation_using_LinkedList.java └── SubArray_Minima.cpp ├── Stack - Post Class ├── Lexo-Sequence.java ├── Longest_valid_Parenthesis.cpp ├── Stack_Operation.java └── String-Stack.cpp ├── Two Pointers - In Class └── Merge-Array.java ├── Two Pointers - Post Class ├── Longest-Distinct-character-in-string.java └── Print-squared-sorted-array.java └── Weekday Contest - May 2022 ├── A-Boolean-Matrix-Problem.cpp ├── Pair-Em-UP-Contest.java └── Sara-And-Pattern.cpp /1-D Array - In Class/Donation.java: -------------------------------------------------------------------------------- 1 | import java.io.*; // for handling input/output 2 | import java.util.*; // contains Collections framework.... 3 | 4 | class Main { 5 | public static void main (String[] args) { 6 | // Your code here 7 | Scanner sc=new Scanner(System.in); 8 | int n=sc.nextInt(); 9 | int arr[]=new int[n]; 10 | int start=arr[0]; 11 | int newArr[]=new int[n]; 12 | long sum=0; 13 | for(int i=0;istart){ 18 | newArr[i]=0; 19 | start=arr[i]; 20 | 21 | }else{ 22 | newArr[i]=start-arr[i]; 23 | sum+=newArr[i]; 24 | } 25 | 26 | } 27 | for(int i=0;i 2 | using namespace std; 3 | 4 | void fun(int arr[], int arr_size) 5 | { 6 | int first, second, third; 7 | 8 | 9 | 10 | third = first = second = INT_MIN; 11 | for(int i = 0; i < arr_size; i++) 12 | { 13 | 14 | if (arr[i] > first) 15 | { 16 | third = second; 17 | second = first; 18 | first = arr[i]; 19 | } 20 | 21 | 22 | else if (arr[i] > second) 23 | { 24 | third = second; 25 | second = arr[i]; 26 | } 27 | 28 | else if (arr[i] > third) 29 | third = arr[i]; 30 | } 31 | 32 | cout << first << " " << second << " "<< third << endl; 33 | } 34 | 35 | 36 | int main() 37 | { 38 | int t; 39 | cin>>t; 40 | 41 | for(int i=0;i>n; 44 | int arr[n]; 45 | for(int i=0;i>arr[i]; 47 | } 48 | fun(arr, n); 49 | } 50 | 51 | 52 | return 0; 53 | } -------------------------------------------------------------------------------- /1-D Array - In Class/Max_Number.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | public class Main { 4 | public static void main(String[] args)throws IOException { 5 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 6 | 7 | int t = Integer.parseInt(br.readLine()); 8 | while (t-->0) { 9 | int n = Integer.parseInt(br.readLine()); 10 | 11 | int max, max2, max3; 12 | max3 = max = max2 = -1; 13 | int x = 0; 14 | String srr[] = br.readLine().trim().split(" "); 15 | for (int i = 0; i < n; i++) { 16 | x =Integer.parseInt(srr[i]); 17 | if (x > max) { 18 | max3 = max2; 19 | max2 = max; 20 | max = x; 21 | } else if (x > max2) { 22 | max3 = max2; 23 | max2 = x; 24 | } else if (x > max3) 25 | max3 = x; 26 | } 27 | System.out.println(max + " " + max2 + " " + max3); 28 | 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /1-D Array - In Class/Maximum-Difference-Array.java: -------------------------------------------------------------------------------- 1 | import java.io.*; // for handling input/output 2 | import java.util.*; // contains Collections framework 3 | 4 | // don't change the name of this class 5 | // you can add inner classes if needed 6 | class Main { 7 | public static void main (String[] args) { 8 | // Your code here 9 | Scanner sc = new Scanner(System.in); 10 | int maxDiff = -1; 11 | int n =sc.nextInt(); 12 | int [] arr = new int[n]; 13 | for (int i =0; i < n; i++){ 14 | arr[i] = sc.nextInt(); 15 | } 16 | int []rightMax = new int[n]; 17 | rightMax[n-1] = arr[n-1]; 18 | for(int i = n-2; i>=0; i--) 19 | rightMax[i] = Math.max(rightMax[i+1] , arr[i]); 20 | int maxDist = Integer.MIN_VALUE; 21 | int i = 0 , j = 0; 22 | while(i < n && j < n){ 23 | if(rightMax[j] > arr[i]) 24 | { 25 | maxDist = Math.max(maxDist, j-i); 26 | j++; 27 | }else 28 | i++; 29 | } 30 | System.out.println(maxDist); 31 | } 32 | } -------------------------------------------------------------------------------- /1-D Array - Post Class/Mohit-And-Array.java: -------------------------------------------------------------------------------- 1 | import java.io.*; // for handling input/output 2 | import java.util.*; // contains Collections framework 3 | 4 | class Main { 5 | public static void main (String[] args) { 6 | 7 | int n; 8 | Scanner sc = new Scanner(System.in); 9 | 10 | n = sc.nextInt(); 11 | int arr[]=new int[n]; 12 | 13 | for(int i =1 ;i 2 | using namespace std; 3 | 4 | #define pu push_back 5 | #define fi first 6 | #define se second 7 | #define mp make_pair 8 | // #define int long long 9 | #define pii pair 10 | #define mm (s+e)/2 11 | #define all(x) x.begin(), x.end() 12 | #define For(i, st, en) for(int i=st; i>n; 32 | for(int i=0;i>A[i]; 35 | F[i]=A[i]; 36 | } 37 | cin>>m; 38 | for(int i=0;i>B[i]; 41 | G[i]=B[i]; 42 | C[m-i-1]=B[i]; 43 | } 44 | C[m]=-500000000; 45 | for(int i=0;ir) { 62 | l=i;r=i+E[i]-1; 63 | } 64 | } 65 | for(int i=0;ir) { 92 | l=i;r=i+D[i]-1; 93 | } 94 | } 95 | // cout<<0<<" "; 96 | for(int i=0;i xx,yy; 104 | for(int i=0;i<=n;i++){ 105 | 106 | int a=0; 107 | int b=0; 108 | if(i>0) a=A[i-1]; 109 | if(i=m && (a==0 || b==0 ||(F[i]-F[i-1]==G[0]-G[m-1]))) 112 | {xx.pu(mp(i-a,i+b-m)); } 113 | if(a==m) xx.pu(mp(i-a,i-a)); 114 | if(b==m ) xx.pu(mp(i,i)); 115 | 116 | 117 | } 118 | sort(xx.begin(),xx.end()); 119 | for(int i=0;i=xx[i].se) continue; 127 | if(yy[p].se>=xx[i].fi) yy[p].se=xx[i].se; 128 | else yy.pu(mp(xx[i].fi,xx[i].se)); 129 | } 130 | } 131 | for(int i=0;i 2 | using namespace std; 3 | int32_t main() { 4 | int t; 5 | cin>>t; 6 | int row,col; 7 | 8 | for(int i=1;i<=t;i++) 9 | { 10 | cin>>row; 11 | cin>>col; 12 | int m[row][col]; 13 | 14 | for(int k=0;k>m[k][j]; 19 | } 20 | } 21 | for(int k=0;k // header file includes every Standard library 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int n; 7 | 8 | cin>>n; 9 | int c1=0,c2=0; 10 | for(int i=1;i<=(n);i++){ 11 | for(int j=1;j<=n;j++){ 12 | int p; cin>>p; 13 | int x= (i+j)&1; 14 | if(p==x) c1+=1; 15 | if(p!=x) c2+=1; 16 | 17 | } 18 | } 19 | if(c10) 12 | { 13 | int n = sc.nextInt(); 14 | int a[][] = new int[n][n]; 15 | 16 | for(int i = 0;i 0; j--) { 36 | System.out.print(arr[m - 1][j - 1] + " "); 37 | } 38 | for (int i = m - 1; i > 1; i--) { 39 | System.out.print(arr[i - 1][0] + " "); 40 | } 41 | } 42 | System.out.println(); 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /2-D Arrays, Maths - In Class/Diagonal-Sum.java: -------------------------------------------------------------------------------- 1 | import java.io.*; // for handling input/output 2 | import java.util.*; // contains Collections framework 3 | 4 | // don't change the name of this class 5 | // you can add inner classes if needed 6 | class Main { 7 | public static void main(String[] args) { 8 | Scanner sc = new Scanner(System.in); 9 | int n = sc.nextInt(); 10 | int arr[][] = new int[n][n]; 11 | for (int i = 0; i < n; i++) { 12 | for (int j = 0; j < n; j++) { 13 | arr[i][j] = sc.nextInt(); 14 | } 15 | } 16 | int sumOfDiagonal1 = 0; 17 | for (int i = 0, j = 0; i < n && j < n; i++, j++) { 18 | sumOfDiagonal1 = sumOfDiagonal1 + arr[i][j]; 19 | } 20 | int sumOfDiagonal2 = 0; 21 | for (int i = 0, j = n - 1; i < n && j >= 0; i++, j--) { 22 | sumOfDiagonal2 = sumOfDiagonal2 + arr[i][j]; 23 | 24 | } 25 | System.out.print(sumOfDiagonal1 + " " + sumOfDiagonal2); 26 | } 27 | } -------------------------------------------------------------------------------- /2-D Arrays, Maths - In Class/Matrix-Zig-Zag-Traversal.java: -------------------------------------------------------------------------------- 1 | import java.io.*; // for handling input/output 2 | import java.util.*; // contains Collections framework 3 | 4 | // don't change the name of this class 5 | // you can add inner classes if needed 6 | class Main { 7 | public static void main(String[] args) { 8 | // Your code here 9 | Scanner inputTaker = new Scanner(System.in); 10 | int row = inputTaker.nextInt(); 11 | int coloumn = inputTaker.nextInt(); 12 | int[][] metric = new int[row][coloumn]; 13 | 14 | for (int i = 0; i < row; i++) { 15 | for (int j = 0; j < coloumn; j++) { 16 | metric[i][j] = inputTaker.nextInt(); 17 | } 18 | } 19 | 20 | for (int j = 0; j < row; j++) { 21 | for (int i = j; i >= 0; i--) { 22 | if ((j - i) >= coloumn) { 23 | break; 24 | 25 | } 26 | System.out.print(metric[i][j - i] + " "); 27 | } 28 | System.out.println(""); 29 | } 30 | 31 | for (int j = 1; j < coloumn; j++) { 32 | for (int i = row - 1; i >= 0; i--) { 33 | if ((j + row - 1 - i) >= coloumn) { 34 | break; 35 | } 36 | System.out.print(metric[i][j + (row - 1 - i)] + " "); 37 | } 38 | System.out.println(""); 39 | } 40 | 41 | } 42 | } -------------------------------------------------------------------------------- /2-D Arrays, Maths - In Class/Rotate-the-Matrix.java: -------------------------------------------------------------------------------- 1 | import java.io.*; // for handling input/output 2 | import java.util.*; // contains Collections framework 3 | 4 | // don't change the name of this class 5 | // you can add inner classes if needed 6 | class Main { 7 | public static void main(String[] args) { 8 | // Your code here 9 | Scanner inputTaker = new Scanner(System.in); 10 | 11 | int n = inputTaker.nextInt(); 12 | int[][] arr = new int[n][n]; 13 | for (int i = 0; i < n; i++) { 14 | for (int j = 0; j < n; j++) { 15 | arr[i][j] = inputTaker.nextInt(); 16 | } 17 | } 18 | rotateAndPrint(arr, n); 19 | System.out.println(""); 20 | rotateAndPrint(arr, n); 21 | } 22 | 23 | public static void rotateAndPrint(int[][] arr, int n) { 24 | for (int i = 0; i < n / 2; i++) { 25 | for (int j = i; j < n - i - 1; j++) { 26 | int temp = arr[i][j]; 27 | arr[i][j] = arr[n - 1 - j][i]; 28 | arr[n - 1 - j][i] = arr[n - 1 - i][n - 1 - j]; 29 | arr[n - 1 - i][n - 1 - j] = arr[j][n - 1 - i]; 30 | arr[j][n - 1 - i] = temp; 31 | } 32 | } 33 | 34 | for (int i = 0; i < n; i++) { 35 | for (int j = 0; j < n; j++) { 36 | System.out.print(arr[i][j] + " "); 37 | } 38 | System.out.println(""); 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /2-D Arrays, Maths - In Class/Simple-Determinant.java: -------------------------------------------------------------------------------- 1 | import java.io.*; // for handling input/output 2 | import java.util.*; // contains Collections framework 3 | 4 | // don't change the name of this class 5 | // you can add inner classes if needed 6 | class Main { 7 | public static void main(String[] args) { 8 | // Your code here 9 | Scanner sc = new Scanner(System.in); 10 | int l = 2; 11 | int matrix[][] = new int[l][l]; 12 | for (int i = 0; i < l; i++) { 13 | for (int j = 0; j < l; j++) { 14 | matrix[i][j] = sc.nextInt(); 15 | } 16 | } 17 | int x, y, f; 18 | x = matrix[0][0] * matrix[1][1]; 19 | y = matrix[0][1] * matrix[1][0]; 20 | f = x - y; 21 | System.out.println(f); 22 | } 23 | } -------------------------------------------------------------------------------- /2-D Arrays, Maths - In Class/Simple-transpose.java: -------------------------------------------------------------------------------- 1 | import java.io.*; // for handling input/output 2 | import java.util.*; // contains Collections framework 3 | 4 | // don't change the name of this class 5 | // you can add inner classes if needed 6 | class Main { 7 | public static void main(String[] args) { 8 | // Your code here 9 | Scanner kb = new Scanner(System.in); 10 | int n = kb.nextInt(); 11 | int arr[][] = new int[n][n]; 12 | int brr[][] = new int[n][n]; 13 | for (int i = 0; i < n; i++) { 14 | for (int j = 0; j < n; j++) { 15 | arr[i][j] = kb.nextInt(); 16 | brr[j][i] = arr[i][j]; 17 | } 18 | } 19 | for (int i = 0; i < n; i++) { 20 | for (int j = 0; j < n; j++) { 21 | System.out.print(brr[i][j] + " "); 22 | } 23 | System.out.println(); 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /Basics of Programming, Input, Output, Variables & Operators - In Class/Dishes.cpp: -------------------------------------------------------------------------------- 1 | #include // header file includes every Standard library 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | // Your code here.. 7 | int a,b; 8 | cin>>a>>b; 9 | 10 | cout< // header file includes every Standard library 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | // Your code here 7 | int a,b; 8 | cin>>a>>b; 9 | 10 | cout< // header file includes every 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int f; 7 | cin>>f; 8 | 9 | cout<<(f- 32)*5/9; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /Basics of Programming, Input, Output, Variables & Operators - In Class1/Quadratic.java: -------------------------------------------------------------------------------- 1 | import java.io.*; // for handling input/output 2 | import java.util.*; // contains Collections framework 3 | 4 | // don't change the name of this class 5 | // you can add inner classes if needed 6 | class Main { 7 | public static void main (String[] args) { 8 | Scanner in=new Scanner(System.in); 9 | int a=in.nextInt(); 10 | int b=in.nextInt(); 11 | int c=in.nextInt(); 12 | int x=in.nextInt(); 13 | int z=a*x*x+b*x+c; 14 | System.out.print(z); 15 | 16 | 17 | } 18 | } -------------------------------------------------------------------------------- /Basics of Programming, Java Programming - [Syntax], Input, Output, Variables & O ... - In Class/Area-of-Square.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * author: Abhishek Chauhan 3 | * created: Tue May 24 10:16:10 IST 2022 4 | **/ 5 | 6 | #include // header file includes every Standard library 7 | using namespace std; 8 | 9 | int main() { 10 | 11 | int n; 12 | cin>>n; 13 | 14 | cout< // header file includes every Standard library 8 | using namespace std; 9 | 10 | int main() { 11 | 12 | int a,b,c,d; 13 | 14 | cin>>a>>b>>c>>d; 15 | 16 | int ans=(a+b+c+d)/4; 17 | 18 | cout< 2 | using namespace std; 3 | #define int long long int 4 | void Solve(int n) 5 | { 6 | if (n > 1) 7 | Solve(n / 2); 8 | 9 | cout << n % 2; 10 | } 11 | 12 | int32_t main() 13 | { 14 | int n; 15 | cin>>n; 16 | Solve(n); 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Bit Manipulation - In Class/Bit-Count.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define int long long int 4 | int countSetBits(int n) 5 | { 6 | int count = 0; 7 | while (n) { 8 | count += n & 1; 9 | n >>= 1; 10 | } 11 | return count; 12 | } 13 | 14 | 15 | int32_t main() 16 | { 17 | int n; 18 | cin>>n; 19 | cout << countSetBits(n); 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Bit Manipulation - In Class/Bit-Difference.cpp: -------------------------------------------------------------------------------- 1 | #include // header file includes every Standard library 2 | using namespace std; 3 | 4 | int cntBits(vector &A) { 5 | int n = A.size(); 6 | int ans = 0; 7 | for(int i=0;i<31;i++) { 8 | long long z = 0,o = 0; 9 | for(int j=0;j>i)&1 ) == 1 ) o++; 11 | else z++; 12 | } 13 | ans = ( ans + (z*o)%1000000007 )%1000000007; 14 | } 15 | return (2*ans)%1000000007; 16 | } 17 | 18 | int main() { 19 | 20 | int n; 21 | cin>>n; 22 | vector arr; 23 | int v; 24 | for(int i=0 ; i>v; 26 | arr.push_back(v); 27 | } 28 | cout<=100 && N <=150) { 12 | if (N % 2 == 0) { 13 | System.out.println("EvenRaj"); 14 | } else if (N % 2 != 0) { 15 | System.out.println("OddRaj"); 16 | } 17 | }else if (N > 150 && N <= 180){ 18 | if (N % 2 == 0) { 19 | System.out.println("EvenAjay"); 20 | } else if (N % 2 != 0) { 21 | System.out.println("OddAjay"); 22 | } 23 | 24 | }else { 25 | System.out.println("Wrong"); 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /Bit Manipulation - In Class/Decimal-to-Binary.cpp: -------------------------------------------------------------------------------- 1 | #include // header file includes every Standard library 2 | using namespace std; 3 | void decToBinary(int n) 4 | { 5 | int binaryNum[32]; 6 | int i = 0; 7 | while (n > 0) { 8 | binaryNum[i] = n % 2; 9 | n = n / 2; 10 | i++; 11 | } 12 | for (int j = i - 1; j >= 0; j--) 13 | cout << binaryNum[j]; 14 | } 15 | int main() 16 | { 17 | int n; 18 | cin>>n; 19 | decToBinary(n); 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Bit Manipulation - In Class/Decimal-to-Binary.java: -------------------------------------------------------------------------------- 1 | import java.io.*; // for handling input/output 2 | import java.util.*; // contains Collections framework 3 | class Main { 4 | public static void decToBinary(int n){ 5 | int[] binaryNum = new int[1000]; 6 | int i = 0; 7 | while (n > 0) 8 | { 9 | binaryNum[i] = n % 2; 10 | n = n / 2; 11 | i++; 12 | } 13 | 14 | for (int j = i - 1; j >= 0; j--) 15 | System.out.print(binaryNum[j]); 16 | } 17 | public static void main(String[] args) 18 | { 19 | Scanner sc=new Scanner(System.in); 20 | int k=sc.nextInt(); 21 | decToBinary(k); 22 | } 23 | } -------------------------------------------------------------------------------- /Bit Manipulation - In Class/Gray-Code.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | //ls cd gpppp 3 | using namespace std; 4 | 5 | int greyConverter(int n) { return n ^ (n >> 1); } 6 | 7 | int main() 8 | { 9 | int t; 10 | cin>>t; 11 | 12 | while(t--){ 13 | int n; 14 | cin>>n; 15 | cout << greyConverter(n) << endl; 16 | } 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Bit Manipulation - In Class/HardWorking-AI.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int gma(long long int n) 5 | { 6 | if(n==0){return 0;} 7 | else if(n==1){return 1;} 8 | 9 | long long int a=1; 10 | 11 | while(a*2 <= n) 12 | { 13 | a = a*2; 14 | } 15 | return 1+gma(n-a); 16 | } 17 | 18 | int main() 19 | { 20 | long long int n; 21 | cin>>n; 22 | 23 | int ans = gma(n); 24 | cout< list = new ArrayList(); 7 | int incrementer = 0; 8 | long andd = 1; 9 | long n = sc.nextLong(); 10 | for(;andd <=10000000000000L;){ 11 | list.add(andd); 12 | andd = andd * 2; 13 | } 14 | int nn =( list.size()-1); 15 | for(int i=nn;i>=0;i--){ 16 | if(list.get(i)<=n){incrementer++; 17 | n = n - list.get(i); 18 | } 19 | } 20 | System.out.print(incrementer); 21 | } 22 | } -------------------------------------------------------------------------------- /Bit Manipulation - In Class/Help-Chefina.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define int long long int 4 | void isKthBitSet(int n, int k) 5 | { 6 | if (n & (1 << (k - 1))) 7 | cout << "SET"<>t; 17 | 18 | while(t--){ 19 | int n,k; 20 | cin>>n>>k; 21 | isKthBitSet(n, k); 22 | } 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Bit Manipulation - In Class/Highest-Power-of-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define int long long int 4 | int highestPowerof2(int x) 5 | { 6 | x |= x >> 1; 7 | x |= x >> 2; 8 | x |= x >> 4; 9 | x |= x >> 8; 10 | x |= x >> 16; 11 | return x ^ (x >> 1); 12 | } 13 | 14 | int32_t main() 15 | { 16 | 17 | int n; 18 | cin>>n; 19 | cout << highestPowerof2(n); 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Bit Manipulation - In Class/Minimize-XOR.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int minXOR(int arr[], int n) 4 | { 5 | sort(arr, arr + n); 6 | 7 | int minXor = INT_MAX; 8 | int val = 0; 9 | 10 | for (int i = 0; i < n - 1; i++) { 11 | val = arr[i] ^ arr[i + 1]; 12 | minXor = min(minXor, val); 13 | } 14 | 15 | return minXor; 16 | } 17 | 18 | int main() 19 | { 20 | int n; 21 | cin>>n; 22 | 23 | int arr[n]; 24 | for(int i=0;i>arr[i]; 26 | } 27 | cout << minXOR(arr, n) << endl; 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Bit Manipulation - In Class/Minimum-Operations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int minimumOneBitOperations(int n, int res = 0) 5 | { 6 | if (n == 0) 7 | return res; 8 | 9 | int b = 1; 10 | while ((b << 1) <= n) 11 | b = b << 1; 12 | 13 | return minimumOneBitOperations( 14 | (b >> 1) ^ b ^ n, res + b); 15 | } 16 | 17 | int main() 18 | { 19 | int N; 20 | cin>>N; 21 | 22 | cout << minimumOneBitOperations(N); 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Bit Manipulation - In Class/Nth-power-of-2.java: -------------------------------------------------------------------------------- 1 | import java.io.*; // for handling input/output 2 | import java.util.*; // contains Collections framework 3 | 4 | // don't change the name of this class 5 | // you can add inner classes if needed 6 | class Main { 7 | public static void main (String[] args) { 8 | // Your code here 9 | Scanner sc = new Scanner(System.in); 10 | int t = sc.nextInt(); 11 | while(t>0){ 12 | int n = sc.nextInt(); 13 | System.out.println(setBits(n)); 14 | t--; 15 | } 16 | } 17 | 18 | private static int setBits(int n) { 19 | int count = 0; 20 | 21 | while (n > 0) { 22 | count++; 23 | n = n & (n - 1); 24 | } 25 | return count; 26 | } 27 | } -------------------------------------------------------------------------------- /Bit Manipulation - In Class/Parity-Counter.java: -------------------------------------------------------------------------------- 1 | import java.io.*; // for handling input/output 2 | import java.util.*; // contains Collections framework 3 | 4 | // don't change the name of this clas 5 | // you can add inner classes if needed 6 | class Main { 7 | public static void main (String[] args) { 8 | // Your code here 9 | Scanner sc = new Scanner(System.in); 10 | int n = sc.nextInt(); 11 | int count = 0; 12 | while(n>0){ 13 | int x = n%2; 14 | if(x==1){count++;} 15 | n = n/2; 16 | } 17 | if(count%2==0){System.out.print("Yes");} 18 | else{System.out.print("No");} 19 | } 20 | } -------------------------------------------------------------------------------- /Bit Manipulation - In Class/Square-of-a-number.cpp: -------------------------------------------------------------------------------- 1 | #include // header file includes every Standard library 2 | using namespace std; 3 | #define int long long int 4 | int32_t main() { 5 | 6 | // Your code here 7 | int n; 8 | cin>>n; 9 | 10 | cout< 2 | using namespace std; 3 | #define ll long long int 4 | #define ld long double 5 | #define endl '\n' 6 | #define pb push_back 7 | #define sz(c) (ll)c.size() 8 | #define mp make_pair 9 | #define all(v) v.begin(),v.end() 10 | #define rep(i,a,b) for(ll i=a;i 16 | 17 | const ll INF=1e18; 18 | const ll N=1e5+5; 19 | ll mod=998244353; 20 | typedef pair pi; 21 | typedef vector vi; 22 | typedef vector vb; 23 | typedef vector vc; 24 | typedef vector> vp; 25 | typedef vector> vvi; 26 | typedef vector> vvb; 27 | typedef vector> vvc; 28 | typedef vector>> vvp; 29 | void null(){ 30 | ll a,b,k; 31 | cin>>a>>b>>k; 32 | ll ans=a&b; 33 | if(k==1) 34 | cout<>t; 51 | while(t--){ 52 | null(); 53 | } 54 | end = clock(); 55 | double time_taken = double(end - start) / double(CLOCKS_PER_SEC); 56 | } -------------------------------------------------------------------------------- /Bit Manipulation - Post Class/Find-Unique.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define int long long int 4 | int singlenumber(int a[],int N) 5 | { 6 | unordered_mapfmap; 7 | for(int i = 0; i < N;i++) 8 | { 9 | fmap[a[i]]++; 10 | } 11 | 12 | for(auto it:fmap) 13 | { 14 | 15 | if(it.second == 1) 16 | { 17 | return it.first; 18 | } 19 | } 20 | } 21 | 22 | int32_t main() 23 | { 24 | int n; 25 | cin>>n; 26 | int arr[n]; 27 | for(int i=0;i>arr[i]; 29 | } 30 | cout << singlenumber(arr,n); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Bit Manipulation - Post Class/Max-Subset-XOR.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Solution 4 | { 5 | public: 6 | int maxSubarrayXOR(int arr[], int N) 7 | { 8 | if(N == 0) return 0; 9 | int x = 0; 10 | while(1) 11 | { 12 | int y = *max_element(arr,arr+N); 13 | if(y == 0) return x; 14 | x = max(x,x^y); 15 | for(int i=0;i 2 | using namespace std; 3 | long long f(long long r, long long c, int i) { 4 | r += (1ll << 62) - (1ll << i) + 1; 5 | c += (1ll << i); 6 | if (r & c) return (1ll << i); 7 | return 0; 8 | } 9 | int main() { 10 | long long r1, r2, c1, c2; 11 | scanf("%lld %lld %lld %lld" 12 | , &r1, &r2, &c1, &c2); 13 | r1--; 14 | c1--; 15 | long long ans = 0; 16 | for (int i = 0; i < 60; i++) { 17 | ans ^= f(r2, c2, i); 18 | ans ^= f(r1, c2, i); 19 | ans ^= f(r2, c1, i); 20 | ans ^= f(r1, c1, i); 21 | } 22 | printf("%lld\n" 23 | , ans); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Circular Linked List - Post Class/Delete_Every_Kth_Node.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | class Node 4 | { 5 | int data; 6 | Node next; 7 | Node(int x) 8 | { 9 | data = x; 10 | next = null; 11 | } 12 | }; 13 | */ 14 | static Node deleteK(Node head, int k){ 15 | //Enter your code here 16 | printCircularList(head); 17 | int c=0; 18 | Node p=null; 19 | Node s=head; 20 | while(s!=s.next) 21 | { 22 | if(c==k) 23 | { 24 | p.next=s.next; 25 | c=0; 26 | if(s==head) 27 | head=s.next; 28 | printCircularList(head); 29 | } 30 | p=s; 31 | c++; 32 | s=s.next; 33 | } 34 | return head; 35 | } 36 | 37 | static void printCircularList(Node head) 38 | { 39 | // System.out.print(head.data); 40 | Node s=head; 41 | int k=0; 42 | while(s!=head || k==0) 43 | { 44 | k++; 45 | System.out.print(s.data+"->"); 46 | s=s.next; 47 | } 48 | System.out.print(s.data); 49 | System.out.println(); 50 | } -------------------------------------------------------------------------------- /Circular Linked List - Post Class/Exchange_Node.java: -------------------------------------------------------------------------------- 1 | /* 2 | class Node { 3 | Node next; 4 | int val; 5 | 6 | Node(int val) { 7 | this.val = val; 8 | next = null; 9 | } 10 | } 11 | */ 12 | 13 | static Node exchangeNodes(Node head) 14 | { 15 | 16 | // If list is of length 2 17 | if (head.next.next == head) { 18 | head = head.next; 19 | return head; 20 | } 21 | // Find pointer to previous 22 | // of last node 23 | Node p = head; 24 | while (p.next.next != head) 25 | p = p.next; 26 | 27 | // Exchange first and last 28 | // nodes using head and p 29 | p.next.next = head.next; 30 | head.next = p.next; 31 | p.next = head; 32 | head = head.next; 33 | 34 | return head; 35 | } -------------------------------------------------------------------------------- /Contest 16-07-22/Easy-Sorting.js: -------------------------------------------------------------------------------- 1 | // You need to return the sorted String array 2 | // Backend code will print the result 3 | // arr: Arrays of 5 strings 4 | function easySorting(arr) 5 | { 6 | // Your code here 7 | return arr.sort(); 8 | } -------------------------------------------------------------------------------- /Contest 16-07-22/Separating-Negative-and-possitive-number.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.lang.*; 3 | import java.io.*; 4 | class Main { 5 | public static void main(String[] args) throws IOException 6 | { 7 | BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); 8 | long n = Long.parseLong(br.readLine().trim()); 9 | long a[] = new long[(int)(n)]; 10 | String inputLine[] = br.readLine().trim().split(" "); 11 | for (int i = 0; i < n; i++) { 12 | a[i] = Long.parseLong(inputLine[i]); 13 | } 14 | 15 | Compute obj = new Compute(); 16 | obj.Rearrange(a, n); 17 | 18 | StringBuilder output = new StringBuilder(); 19 | for(int i=0;i=0;j--){ 19 | if(k>0){ 20 | k--; 21 | }else{ 22 | break; 23 | } 24 | x+=arr[j]; 25 | } 26 | System.out.println(x); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Contest-09-07-2022/Grid-filling-contest.java: -------------------------------------------------------------------------------- 1 | import java.io.*; // for handling input/output 2 | import java.util.*; // contains Collections framework 3 | 4 | // don't change the name of this class 5 | // you can add inner classes if needed 6 | class Main { 7 | public static void main (String[] args) { 8 | // Your code here 9 | Scanner sc =new Scanner(System.in); 10 | int t=sc.nextInt(); 11 | while(t>0){ 12 | int n =sc.nextInt(); 13 | int m=sc.nextInt(); 14 | int pt =1+(2*(m-1)); 15 | int ptAllRow=(n-1)*pt; 16 | int totalPt=(m-1)+ptAllRow; 17 | System.out.println(totalPt); 18 | t--;} 19 | } 20 | } -------------------------------------------------------------------------------- /Contest-09-07-2022/maximum-value-of-X.java: -------------------------------------------------------------------------------- 1 | /* Author = Chauhan Abhishek */ 2 | /* Codechef Id = https://www.codechef.com/users/abhishekchauha */ 3 | /* Codeforces Id = https://codeforces.com/profile/abhishekchauhan903 */ 4 | /* Newton School Id = https://my.newtonschool.co/user/abhishekchauhan9036/ */ 5 | /* GitHub Id = https://github.com/AbhishekChauhan9036 */ 6 | /* Language = JAVA */ 7 | /* Address = Ballia,UP,INDIA (221716)*/ 8 | 9 | /******************************************************************************************************************************************/ 10 | /***********Start****************/ 11 | 12 | 13 | import java.io.*; // for handling input/output 14 | import java.util.*; // contains Collections framework 15 | 16 | // don't change the name of this class 17 | // you can add inner classes if needed 18 | class Main { 19 | public static void main (String[] args) { 20 | Scanner sc= new Scanner(System.in); 21 | int n=sc.nextInt(); 22 | int k=sc.nextInt(); 23 | int[] arr=new int[n]; 24 | for(int i=0;i=0;j--){ 30 | if(k>0){ 31 | k--; 32 | }else{ 33 | break; 34 | } 35 | x+=arr[j]; 36 | } 37 | System.out.println(x); 38 | } 39 | } 40 | 41 | /******************************************************************************************************************************************/ 42 | /***********End****************/ 43 | 44 | 45 | -------------------------------------------------------------------------------- /Contest-09-07-2022/sort-it.java: -------------------------------------------------------------------------------- 1 | import java.io.*; // for handling input/output 2 | import java.util.*; // contains Collections framework 3 | 4 | // don't change the name of this class 5 | // you can add inner classes if needed 6 | class Main { 7 | public static void main (String[] args) { 8 | Scanner sc = new Scanner(System.in); 9 | int n = sc.nextInt(); 10 | int arr[] = new int[n]; 11 | 12 | for(int i=0; i1){ 5 | N=second_max(N); 6 | count++; 7 | } 8 | return count; 9 | } 10 | public static int second_max(int N){ 11 | for(int i=2;i*i<=N;i++){ 12 | if(N%i==0){ 13 | return N/i; 14 | } 15 | } 16 | return 1; 17 | 18 | } -------------------------------------------------------------------------------- /Control Structures, Loops - Post Class/Loop-Debugging-14.cpp: -------------------------------------------------------------------------------- 1 | #include // header file includes every Standard library 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | // Your code here 7 | 8 | int n; 9 | cin>>n; 10 | 11 | for(int i=1;i<=n;i++){ 12 | if(i%2==0){ 13 | cout< 0){ 21 | end = end.next; 22 | } 23 | if(end == null){ 24 | start = start.next; 25 | return start; 26 | } 27 | 28 | while(end.next != null){ 29 | start = start.next; 30 | end = end.next; 31 | } 32 | 33 | //create link between s.next and s.next.next 34 | if(start.next.next == null){ 35 | //deleting element; 36 | start.next = null; 37 | return head; 38 | } 39 | 40 | start.next = start.next.next; 41 | 42 | return head; 43 | 44 | } -------------------------------------------------------------------------------- /Double Linked List - In Class/Deletion_In_Doubly_Linked_List.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | class Node { 4 | Node next; 5 | Node prev; 6 | int val; 7 | 8 | Node(int val) { 9 | this.val = val; 10 | next = null; 11 | prev = null; 12 | } 13 | } 14 | */ 15 | 16 | public static Node deleteElement(Node head, int k) { 17 | 18 | Node start = head; 19 | Node end = head; 20 | while(k-- > 0){ 21 | end = end.next; 22 | } 23 | if(end == null){ 24 | start = start.next; 25 | return start; 26 | } 27 | 28 | while(end.next != null){ 29 | start = start.next; 30 | end = end.next; 31 | } 32 | 33 | //create link between s.next and s.next.next 34 | if(start.next.next == null){ 35 | //deleting element; 36 | start.next = null; 37 | return head; 38 | } 39 | 40 | start.next = start.next.next; 41 | 42 | return head; 43 | 44 | } -------------------------------------------------------------------------------- /Double Linked List - In Class/Insertion_node_at_kth_postition_in_doubly_linked_list.java: -------------------------------------------------------------------------------- 1 | public static Node insertnew(Node head, int data ,int position){ 2 | //enter your code here 3 | position=position-1; 4 | Node trackedHeadNode = head; 5 | 6 | Node nodeToInsert = new Node(data); 7 | if(position == 0){nodeToInsert.next = head; 8 | return nodeToInsert; 9 | } 10 | 11 | int currPosition = 0; 12 | while (currPosition < position -1 && head.next != null){ 13 | head = head.next; 14 | currPosition++; 15 | } 16 | 17 | 18 | Node nodeAtPosition = head.next; 19 | head.next = nodeToInsert; 20 | head = head.next; 21 | head.next = nodeAtPosition; 22 | 23 | return trackedHeadNode; 24 | 25 | } -------------------------------------------------------------------------------- /Double Linked List - In Class/Reversing-a-double-linked-list.java: -------------------------------------------------------------------------------- 1 | /* 2 | class Node { 3 | Node next; 4 | Node prev; 5 | int val; 6 | 7 | Node(int val) { 8 | this.val = val; 9 | next = null; 10 | prev = null; 11 | } 12 | } 13 | */ 14 | 15 | 16 | 17 | 18 | public static Node Reverse(Node head) { 19 | 20 | Node prev = null; 21 | Node current = head; 22 | Node next = null; 23 | while (current != null) { 24 | next = current.next; 25 | current.next = prev; 26 | prev = current; 27 | current = next; 28 | } 29 | head = prev; 30 | return head; 31 | 32 | } -------------------------------------------------------------------------------- /Double Linked List - In Class/Swap-Nodes-at-position-k-from-beginning-and-end.java: -------------------------------------------------------------------------------- 1 | /* 2 | class Node 3 | { 4 | int data; 5 | Node next, prev; 6 | }; 7 | */ 8 | 9 | 10 | static void swap(Node pa, Node pb){ 11 | // swap parents 12 | Node a = pa.next, b = pb.next; 13 | pa.next = b; 14 | pb.next = a; 15 | // swap children 16 | Node temp = a.next; 17 | a.next = b.next; 18 | b.next = temp; 19 | } 20 | 21 | public static Node swapNodes(Node head, int k){ 22 | Node dummy = new Node(), pa = dummy, pb = dummy, a = head, b = head; 23 | dummy.next = head; 24 | 25 | // find parent-a 26 | int count = 0; 27 | while(++count < k) { 28 | pa = pa.next; 29 | a = a.next; 30 | } 31 | // find parent-b 32 | while(a.next!= null) { 33 | a = a.next; 34 | pb = pb.next; 35 | b = b.next; 36 | } 37 | swap(pa, pb); 38 | return dummy.next; 39 | } -------------------------------------------------------------------------------- /Double Linked List - In Class/insert-node-at-kth-position-in-doubly-linked-list.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | class Node { 4 | Node next; 5 | Node prev; 6 | int val; 7 | 8 | Node(int val) { 9 | this.val = val; 10 | next = null; 11 | prev = null; 12 | } 13 | } 14 | */ 15 | 16 | 17 | 18 | public static Node insertnew(Node head, int data ,int position){ 19 | //enter your code here 20 | position=position-1; 21 | Node trackedHeadNode = head; 22 | 23 | Node nodeToInsert = new Node(data); 24 | if(position == 0){nodeToInsert.next = head; 25 | return nodeToInsert;} 26 | 27 | int currPosition = 0; 28 | while (currPosition < position -1 && head.next != null){ 29 | head = head.next; 30 | currPosition++; 31 | } 32 | 33 | 34 | Node nodeAtPosition = head.next; 35 | head.next = nodeToInsert; 36 | head = head.next; 37 | head.next = nodeAtPosition; 38 | 39 | return trackedHeadNode; 40 | 41 | } -------------------------------------------------------------------------------- /Double Linked List - In Class/insert-node-at-the-head-in-a-doubly-linked-list.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | class Node { 4 | Node next; 5 | Node prev; 6 | int val; 7 | 8 | Node(int val) { 9 | this.val = val; 10 | next = null; 11 | prev = null; 12 | } 13 | } 14 | */ 15 | 16 | 17 | 18 | public static Node insertnew(Node head, int new_data) { 19 | //enter your code here 20 | Node new_node = new Node(new_data); 21 | new_node.next = head; 22 | head = new_node; 23 | 24 | return head; 25 | } -------------------------------------------------------------------------------- /Double Linked List - Post Class/Binary_tree_to_double_linked_list.java: -------------------------------------------------------------------------------- 1 | /* 2 | class Node { 3 | int data; 4 | Node left; 5 | Node right; 6 | Node(int data) { 7 | this.data = data; 8 | left = null; 9 | right = null; 10 | } 11 | } 12 | */ 13 | 14 | static Node prev = null; 15 | 16 | 17 | static void BToDLL(Node root) 18 | { 19 | 20 | if (root == null) 21 | return; 22 | 23 | 24 | BToDLL(root.left); 25 | 26 | if (prev == null) 27 | head = root; 28 | else 29 | { 30 | root.left = prev; 31 | prev.right = root; 32 | } 33 | prev = root; 34 | 35 | 36 | BToDLL(root.right); 37 | } -------------------------------------------------------------------------------- /Double Linked List - Post Class/Remove_Duplicates_from_doubly_linked_list.java: -------------------------------------------------------------------------------- 1 | /* 2 | class Node { 3 | Node next; 4 | Node prev; 5 | int val; 6 | 7 | Node(int val) { 8 | this.val = val; 9 | next = null; 10 | prev = null; 11 | } 12 | } 13 | */ 14 | 15 | 16 | static Node deleteNode(Node head_ref, Node del) 17 | { 18 | // base case 19 | if (head_ref == null || del == null) 20 | return head_ref; 21 | 22 | 23 | if (head_ref == del) 24 | head_ref = del.next; 25 | 26 | 27 | if (del.next != null) 28 | del.next.prev = del.prev; 29 | 30 | if (del.prev != null) 31 | del.prev.next = del.next; 32 | return head_ref; 33 | } 34 | 35 | static Node deleteDuplicates(Node head_ref) 36 | { 37 | 38 | if ((head_ref) == null || 39 | (head_ref).next == null) 40 | return head_ref;; 41 | 42 | Node ptr1, ptr2; 43 | 44 | // pick elements one by one 45 | for (ptr1 = head_ref; 46 | ptr1 != null; ptr1 = ptr1.next) 47 | { 48 | ptr2 = ptr1.next; 49 | 50 | while (ptr2 != null) 51 | { 52 | 53 | 54 | if (ptr1.val == ptr2.val) 55 | { 56 | 57 | 58 | Node next = ptr2.next; 59 | 60 | 61 | head_ref = deleteNode(head_ref, ptr2); 62 | 63 | 64 | ptr2 = next; 65 | } 66 | 67 | 68 | else 69 | ptr2 = ptr2.next; 70 | } 71 | } 72 | return head_ref; 73 | } -------------------------------------------------------------------------------- /GCD, Primes - Post Class/Closet-Buddy-Contest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define sd(x) scanf("%d", &x) 4 | #define sz(v) (int) v.size() 5 | #define pr(v) For(i, 0, sz(v)) {cout< pii; 20 | typedef vector vi; 21 | #define pi 3.141592653589793238 22 | const int MOD = 1e9+7; 23 | 24 | 25 | const int N = 2e5+5; 26 | 27 | 28 | 29 | int pre[N][55], suf[N][55]; 30 | int arr[N]; 31 | 32 | void solve(){ 33 | int n; cin>>n; 34 | For(i, 1, n+1){ 35 | cin>>arr[i]; 36 | } 37 | For(i, 1, n+1){ 38 | For(j, 1, 51){ 39 | if(arr[i]==j) 40 | pre[i][j]=i; 41 | else 42 | pre[i][j]=pre[i-1][j]; 43 | } 44 | } 45 | for(int i=n; i>=1; i--){ 46 | For(j, 1, 51){ 47 | if(arr[i]==j){ 48 | suf[i][j]=i; 49 | } 50 | else{ 51 | suf[i][j]=suf[i+1][j]; 52 | } 53 | } 54 | } 55 | vector ans(n+1, -1); 56 | For(i, 1, n+1){ 57 | int dist = 3e5; 58 | For(j, 1, 51){ 59 | if(__gcd(arr[i], j)==1){ 60 | if(pre[i][j] && abs(i-pre[i][j])<=dist){ 61 | ans[i]=pre[i][j]; 62 | dist=abs(i-pre[i][j]); 63 | } 64 | if(suf[i][j] && abs(i-suf[i][j]) s; 72 | For(i, 1, n+1){ 73 | s.insert(ans[i]); 74 | cout<= 1; i--) 30 | if (divisors[i] > 1) 31 | return i; 32 | return 1; 33 | } 34 | 35 | public static void main (String[] args) { 36 | // Your code here 37 | Scanner sc = new Scanner(System.in); 38 | int n = sc.nextInt(); 39 | int arr[] = new int[n]; 40 | for(int i=0; iyBlue?yBlue:xRed; 13 | int count=0; 14 | 15 | System.out.println(gcd(xRed,yBlue)); 16 | 17 | 18 | } 19 | 20 | static int gcd(int a, int b) 21 | { 22 | if (b == 0) 23 | return a; 24 | 25 | return gcd(b, a % b); 26 | } 27 | } -------------------------------------------------------------------------------- /GCD, Primes - Post Class/Is-this-prime.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #pragma GCC optimize "03" 3 | using namespace std; 4 | #define Abhishek main 5 | #define int long long int 6 | #define ld long double 7 | #define pi pair 8 | #define pb push_back 9 | #define fi first 10 | #define se second 11 | #define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0) 12 | #ifndef LOCAL 13 | #define endl '\n' 14 | #endif 15 | 16 | const int N = 2e5 + 5; 17 | const int mod = 1e9 + 7; 18 | const int inf = 1e9 + 9; 19 | 20 | int power(int a, int b){ 21 | int ans = 1; 22 | b %= (mod-1); 23 | while(b){ 24 | if(b&1) 25 | ans = (ans*a) % mod; 26 | b >>= 1; 27 | a = (a*a) % mod; 28 | } 29 | return ans; 30 | } 31 | 32 | int f(int n, int p){ 33 | int ans = 1; 34 | int cur = 1; 35 | while(cur <= n/p){ 36 | cur = cur*p; 37 | int z = power(p, n/cur); 38 | ans = (ans*z) % mod; 39 | } 40 | return ans; 41 | } 42 | 43 | signed Abhishek() { 44 | IOS; 45 | int x, n, ans = 1; 46 | cin >> x >> n; 47 | for(int i = 2; i*i <= x; i++){ 48 | if(x%i != 0) continue; 49 | ans = (ans*f(n, i)) % mod; 50 | while(x%i == 0) 51 | x /= i; 52 | } 53 | if(x > 1) 54 | ans = (ans*f(n, x)) % mod; 55 | cout << ans; 56 | return 0; 57 | } -------------------------------------------------------------------------------- /GCD, Primes - Post Class/Nth-number-made-of-prime-Digits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Solution 4 | { 5 | public: 6 | int primeDigits(int n) 7 | { 8 | queueq; 9 | q.push(0); 10 | for(int i=0;i<=n;) 11 | { 12 | int res=q.front(); 13 | q.pop(); 14 | i++; 15 | q.push((res*10)+2); 16 | if(i==n)break; 17 | i++; 18 | q.push((res*10)+3); 19 | if(i==n)break; 20 | i++; 21 | q.push((res*10)+5); 22 | if(i==n)break; 23 | i++; 24 | q.push((res*10)+7); 25 | if(i==n)break; 26 | 27 | } 28 | return q.back(); 29 | } 30 | }; 31 | 32 | int main() 33 | { 34 | int t; 35 | cin>>t; 36 | while(t--) 37 | { 38 | int n; 39 | cin>>n; 40 | Solution ob; 41 | cout << ob.primeDigits(n) << "\n"; 42 | } 43 | } -------------------------------------------------------------------------------- /GCD, Primes - Post Class/Travelling.java: -------------------------------------------------------------------------------- 1 | import java.io.*; // for handling input/output 2 | import java.util.*; // contains Collections framework 3 | class Main { 4 | static int gcd(int a, int b) 5 | { 6 | if (b == 0) 7 | return a; 8 | return gcd(b, a % b); 9 | } 10 | public static void main (String[] args) { 11 | Scanner scn = new Scanner(System.in); 12 | int n= scn.nextInt(); 13 | int s = scn.nextInt(); 14 | int a[]= new int[n]; 15 | int arr[]= new int[n]; 16 | for(int i=1; i 2 | using namespace std; 3 | #define ll long long 4 | #define Chauhan_Abhishek main 5 | ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; } 6 | 7 | 8 | int32_t Chauhan_Abhishek() 9 | { 10 | ll n; 11 | ll rick,morty; 12 | cin>>n>>rick>>morty; 13 | 14 | ll gc; 15 | 16 | for(int i=0;i>s; 18 | gc=(i==0)? s: gcd(gc,s); 19 | } 20 | 21 | ll val = abs(morty-rick); 22 | 23 | if(val%gc==0) { 24 | cout<<"Yes\n"; 25 | } else { 26 | cout<<"No\n"; 27 | } 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Hashing - In Class/Longest-sub-array-with-sum-k.java: -------------------------------------------------------------------------------- 1 | /* Author = Chauhan Abhishek */ 2 | /* Codechef Id = https://www.codechef.com/users/abhishekchauha */ 3 | /* Codeforces Id = https://codeforces.com/profile/abhishekchauhan903 */ 4 | /* Newton School Id = https://my.newtonschool.co/user/abhishekchauhan9036/ */ 5 | /* GitHub Id = https://github.com/AbhishekChauhan9036 */ 6 | /* Language = JAVA */ 7 | /* Address = Ballia,UP,INDIA (221716)*/ 8 | 9 | /******************************************************************************************************************************************/ 10 | /***********Start****************/ 11 | import java.io.*; 12 | import java.util.*; 13 | class Main { 14 | public static void main(String[] args) throws IOException{ 15 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 16 | int t=Integer.parseInt(br.readLine()); 17 | while (t-->0) { 18 | String srr[] = br.readLine().trim().split(" "); 19 | int n = Integer.parseInt(srr[0]); 20 | int k = Integer.parseInt(srr[1]); 21 | String ksrr[] = br.readLine().trim().split(" "); 22 | int[] arr = new int[n]; 23 | for (int i = 0; i < n; i++) { 24 | arr[i] =Integer.parseInt(ksrr[i]); 25 | } 26 | System.out.println(lenOfLongSubarr(arr, n, k)); 27 | } 28 | } 29 | 30 | private static int lenOfLongSubarr(int[] arr, int n, int k) { 31 | HashMap map = new HashMap<>(); 32 | int sum = 0, maxLen = 0; 33 | for (int i = 0; i < n; i++) { 34 | sum += arr[i]; 35 | if (sum == k) { 36 | maxLen = i + 1; 37 | } 38 | if (!map.containsKey(sum)) { 39 | map.put(sum, i); 40 | } 41 | if (map.containsKey(sum - k)) { 42 | if (maxLen < (i - map.get(sum - k))) { 43 | maxLen = i - map.get(sum - k); 44 | } 45 | } 46 | } 47 | return maxLen; 48 | } 49 | } 50 | /******************************************************************************************************************************************/ 51 | /***********End****************/ -------------------------------------------------------------------------------- /Hashing - In Class/subarrays-with-equal-1s-and-0s.java: -------------------------------------------------------------------------------- 1 | /* Author = Chauhan Abhishek */ 2 | /* Codechef Id = https://www.codechef.com/users/abhishekchauha */ 3 | /* Codeforces Id = https://codeforces.com/profile/abhishekchauhan903 */ 4 | /* Newton School Id = https://my.newtonschool.co/user/abhishekchauhan9036/ */ 5 | /* GitHub Id = https://github.com/AbhishekChauhan9036 */ 6 | /* Language = JAVA */ 7 | /* Address = Ballia,UP,INDIA (221716)*/ 8 | 9 | /******************************************************************************************************************************************/ 10 | /***********Start****************/ 11 | import java.io.*; 12 | import java.util.*; 13 | class Main { 14 | private static long SubarrayEqual(long arr[], int n) { 15 | 16 | Map um = new HashMap<>(); 17 | int curr_sum = 0; 18 | for (int i = 0; i < n; i++) { 19 | curr_sum += (arr[i] == 0) ? -1 : arr[i]; 20 | um.put(curr_sum, um.get(curr_sum) == null ? 1 : um.get(curr_sum) + 1); 21 | } 22 | long count = 0; 23 | for (Map.Entry itr : um.entrySet()) { 24 | if (itr.getValue() > 1) { 25 | count += ((itr.getValue() * (itr.getValue() - 1)) / 2); 26 | } 27 | } 28 | if (um.containsKey(0)) { 29 | count += um.get(0); 30 | } 31 | return count; 32 | } 33 | 34 | public static void main(String[] args) { 35 | Scanner sc = new Scanner(System.in); 36 | long n = sc.nextLong(); 37 | int l = (int) n; 38 | long[] arr = new long[l]; 39 | for (int i = 0; i < n; i++) { 40 | arr[i] = sc.nextInt(); 41 | } 42 | System.out.println(SubarrayEqual(arr, l)); 43 | } 44 | } 45 | /******************************************************************************************************************************************/ 46 | /***********End****************/ -------------------------------------------------------------------------------- /Hashing, Sortings, Searching, Bubble Sort - Post Class/EVM-Machine.cpp: -------------------------------------------------------------------------------- 1 | /* Author = Chauhan Abhishek */ 2 | /* Codechef Id = https://www.codechef.com/users/abhishekchauha */ 3 | /* Codeforces Id = https://codeforces.com/profile/abhishekchauhan903 */ 4 | /* Newton School Id = https://my.newtonschool.co/user/abhishekchauhan9036/ */ 5 | /* GitHub Id = https://github.com/AbhishekChauhan9036 */ 6 | /* Language = C++ */ 7 | /* Address = Ballia,UP,INDIA (221716)*/ 8 | 9 | /******************************************************************************************************************************************/ 10 | /***********Start****************/ 11 | #include 12 | using namespace std; 13 | #define lli long long int 14 | bool campare(pair&a,pair&b){ 15 | if(a.second==b.second) 16 | return a.first<=b.first; 17 | return a.second>b.second; 18 | } 19 | int main() { 20 | lli n; 21 | cin>>n; 22 | unordered_mapms,mp; 23 | for(lli i=0;i>s>>sp; 26 | if(ms.find(s)==ms.end()) 27 | mp[sp]++; 28 | ms[s]++; 29 | 30 | } 31 | vector >v; 32 | for(auto x:mp){ 33 | v.push_back({x.first,x.second}); 34 | } 35 | sort(v.begin(),v.end(),campare); 36 | lli g=v[0].second; 37 | for(lli i=0;i m = new HashMap(); 20 | 21 | for (int i = 0; i < non.length(); i++) { 22 | if (m.get(non.charAt(i)) != null) { 23 | m.put(non.charAt(i), m.get(non.charAt(i)) + 1); 24 | } else { 25 | m.put(non.charAt(i), 1); 26 | } 27 | } 28 | 29 | for (int i = 0; i < non.length(); i++) { 30 | 31 | if (m.get(non.charAt(i)) == 1) { 32 | System.out.println(i); 33 | break; 34 | } else { 35 | if (i == non.length() - 1) { 36 | System.out.println("-1"); 37 | } 38 | } 39 | } 40 | } 41 | } 42 | /******************************************************************************************************************************************/ 43 | /***********End****************/ -------------------------------------------------------------------------------- /Hashing, Sortings, Searching, Bubble Sort - Post Class/Group-of-Numbers.java: -------------------------------------------------------------------------------- 1 | /* Author = Chauhan Abhishek */ 2 | /* Codechef Id = https://www.codechef.com/users/abhishekchauha */ 3 | /* Codeforces Id = https://codeforces.com/profile/abhishekchauhan903 */ 4 | /* Newton School Id = https://my.newtonschool.co/user/abhishekchauhan9036/ */ 5 | /* GitHub Id = https://github.com/AbhishekChauhan9036 */ 6 | /* Language = JAVA */ 7 | /* Address = Ballia,UP,INDIA (221716)*/ 8 | 9 | /******************************************************************************************************************************************/ 10 | /***********Start****************/ 11 | import java.io.*; 12 | import java.util.*; 13 | public class Main{ 14 | public static void main(String[] args) { 15 | Scanner sc = new Scanner(System.in); 16 | int n = sc.nextInt(); 17 | int[] arr = new int[n]; 18 | for(int i=0;i 0){ 38 | for(int i=maxE; i>=0;i--){ 39 | if(maxF == freq[i]){ 40 | System.out.print(i + " "); 41 | } 42 | } 43 | maxF--; 44 | }}} 45 | /******************************************************************************************************************************************/ 46 | /***********End****************/ -------------------------------------------------------------------------------- /If else ladder, Switch case, Ternary Operator - Post Class/Checking-Number.cpp: -------------------------------------------------------------------------------- 1 | #include // header file includes every Standard library 2 | using namespace std; 3 | #define int long long 4 | #define Chauhan main 5 | #define rukjo break; 6 | #define Abhishek return 0; 7 | int32_t Chauhan() { 8 | 9 | // Your code here 10 | int n; 11 | cin>>n; 12 | switch(n>0){ 13 | case 1: 14 | cout<<"Positive"; 15 | rukjo 16 | 17 | case 0: 18 | switch (n < 0) 19 | { 20 | case 1: 21 | cout<<"Negative"; 22 | rukjo 23 | case 0: 24 | cout<<"Zero"; 25 | rukjo 26 | } 27 | rukjo 28 | } 29 | Abhishek 30 | } -------------------------------------------------------------------------------- /If else ladder, Switch case, Ternary Operator - Post Class/Finding-Roots.java: -------------------------------------------------------------------------------- 1 | import java.io.*; // for handling input/output 2 | import java.util.*; // contains Collections framework 3 | 4 | // don't change the name of this class 5 | // you can add inner classes if needed 6 | class Main { 7 | public static void main (String[] args) { 8 | // Your code here 9 | 10 | 11 | double a,b,c,root1,root2; 12 | int temp; 13 | Scanner inp=new Scanner(System.in); 14 | a=inp.nextDouble(); 15 | b=inp.nextDouble(); 16 | c=inp.nextDouble(); 17 | double dt=(b*b)-(4.0*a*c); 18 | if(dt>0.0) 19 | temp=1; 20 | else if(dt==0.0) 21 | temp=0; 22 | else 23 | temp=-1; 24 | switch(temp) 25 | { 26 | case 1: 27 | root1=(-b+Math.sqrt(dt))/(2.0*a); 28 | root2=(-b-Math.sqrt(dt))/(2.0*a); 29 | System.out.format("%.2f",root1); 30 | System.out.format("\n%.2f",root2); 31 | break; 32 | case 0: 33 | root1=-b/(2.0*a); 34 | System.out.format("%.2f",root1); 35 | System.out.format("\n%.2f",root1); 36 | break; 37 | case -1: 38 | root1=-b/(2.0*a); 39 | root2=Math.sqrt(-dt)/(2.0*a); 40 | System.out.format("%.2f+",root1); 41 | System.out.format("i%.2f",root2); 42 | System.out.format("\n%.2f-",root1); 43 | System.out.format("i%.2f",root2); 44 | 45 | break; 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /If else ladder, Switch case, Ternary Operator - Post Class/Leap-Year.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | class Main { 3 | public static void main (String[] args) 4 | { 5 | 6 | Scanner scanner = new Scanner(System.in); 7 | int n = scanner.nextInt(); 8 | LeapYear(n); 9 | 10 | 11 | } 12 | static void LeapYear(int n){ 13 | int p=0; 14 | if(n%400==0){ 15 | p=1; 16 | }else if(n%100==0){ 17 | p=0; 18 | }else if(n%4==0){ 19 | p=1; 20 | }else{ 21 | p=0; 22 | } 23 | 24 | if(p==1){ 25 | System.out.print("YES"); 26 | }else{ 27 | System.out.print("NO"); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /If else ladder, Switch case, Ternary Operator - Post Class/Logical-Operators.cpp: -------------------------------------------------------------------------------- 1 | #include // header file includes every Standard library 2 | using namespace std; 3 | int main() { 4 | 5 | int a; 6 | int b; 7 | cin>>a>>b; 8 | if(a<=10 && b >=10){cout<<("true ");} 9 | else {cout<<("false ");} 10 | 11 | if(a%2==0||b%2==0){cout<<("true ");} 12 | else {cout<<("false ");} 13 | 14 | if(a!=b){cout<<("true ");} 15 | else {cout<<("false ");} 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /If else ladder, Switch case, Ternary Operator - Post Class/MCQ-Exam.cpp: -------------------------------------------------------------------------------- 1 | #include // header file includes every Standard library 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | // Your code here 7 | int n=4; 8 | int arr[4]; 9 | 10 | for(int i=0;i>arr[i]; 12 | } 13 | 14 | sort(arr,arr+n); 15 | cout< 2 | using namespace std; 3 | int main() { 4 | int a,b,c,d,e; 5 | int m; 6 | cin>>a>>b>>c>>d>>e; 7 | 8 | m=(a+b+c+d+e)/5; 9 | 10 | if(m>=80 && m<=100){ 11 | cout<<'A'; 12 | }else if(m<80 && m>=60){ 13 | cout<<'B'; 14 | }else if(m<60 && m>=40){ 15 | cout<<'C'; 16 | }else{ 17 | cout<<'D'; 18 | } 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Input, Output, Variables & Operators - Post Class/Magic-Trick.java: -------------------------------------------------------------------------------- 1 | import java.io.*; // for handling input/output 2 | import java.util.*; // contains Collections framework 3 | 4 | // don't change the name of this class 5 | // you can add inner classes if needed 6 | class Main { 7 | public static void main (String[] args) { 8 | Scanner sc = new Scanner(System.in); 9 | int a = sc.nextInt(); 10 | int b = sc.nextInt(); 11 | 12 | System.out.println(a + b/2); 13 | } 14 | } -------------------------------------------------------------------------------- /Input, Output, Variables & Operators - Post Class/Steps-Execution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; // for handling input/output 2 | import java.util.*; // contains Collections framework 3 | 4 | // don't change the name of this class 5 | // you can add inner classes if needed 6 | class Main { 7 | public static void main (String[] args) { 8 | // Your code here 9 | Scanner Sc = new Scanner(System.in); 10 | int a = Sc.nextInt(); 11 | System.out.print((((((a)+8)/3)%5)*5)); 12 | } 13 | } -------------------------------------------------------------------------------- /Java Programming - [Syntax] - Post Class/Number-Dilemma.java: -------------------------------------------------------------------------------- 1 | /** 2 | * author: Abhishek Chauhan 3 | * created: Tue May 24 10:16:10 IST 2022 4 | **/ 5 | 6 | 7 | static int StringToInt(String S){ 8 | return Integer.parseInt(S); 9 | } 10 | 11 | static String IntToString(int N){ 12 | return Integer.toString(N); 13 | } 14 | -------------------------------------------------------------------------------- /Java Programming - [Syntax] - Post Class/String-TypeCast.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * author: Abhishek Chauhan 3 | * created: Tue May 24 10:16:10 IST 2022 4 | **/ 5 | 6 | 7 | #include // header file includes every Standard library 8 | using namespace std; 9 | 10 | int main() { 11 | 12 | // Your code here 13 | int n; 14 | cin>>n; 15 | if(n){ 16 | cout<<"Nice Job"; 17 | } 18 | else{ 19 | cout<<"Wrong answer"; 20 | } 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Linear Search - In Class/Find-position-X-in-Array.java: -------------------------------------------------------------------------------- 1 | /* Author = Chauhan Abhishek */ 2 | /* Codechef Id = https://www.codechef.com/users/abhishekchauha */ 3 | /* Codeforces Id = https://codeforces.com/profile/abhishekchauhan903 */ 4 | /* Newton School Id = https://my.newtonschool.co/user/abhishekchauhan9036/ */ 5 | /* GitHub Id = https://github.com/AbhishekChauhan9036 */ 6 | /* Language = JAVA */ 7 | /* Address = Ballia,UP,INDIA (221716)*/ 8 | 9 | /******************************************************************************************************************************************/ 10 | /***********Start****************/ 11 | import java.io.*; 12 | import java.util.*; 13 | 14 | class Main { 15 | public static void main (String[] args) { 16 | Scanner sc = new Scanner(System.in); 17 | int n = sc.nextInt(); 18 | while(n>=1){ 19 | int l = sc.nextInt(); 20 | int find = sc.nextInt(); 21 | int arr [] = new int[l]; 22 | 23 | for(int i=0; i 12 | using namespace std; 13 | 14 | int countPairs(int *a, int n, int mid) 15 | { 16 | int res = 0; 17 | for (int i = 0; i < n; ++i) 18 | 19 | 20 | res += upper_bound(a+i, a+n, a[i] + mid) -(a + i + 1); 21 | return res; 22 | } 23 | 24 | 25 | int kthDiff(int a[], int n, int k) 26 | { 27 | 28 | sort(a, a+n); 29 | 30 | 31 | int low = a[1] - a[0]; 32 | for (int i = 1; i <= n-2; ++i) 33 | low = min(low, a[i+1] - a[i]); 34 | 35 | 36 | int high = a[n-1] - a[0]; 37 | 38 | 39 | while (low < high) 40 | { 41 | int mid = (low+high)>>1; 42 | if (countPairs(a, n, mid) < k) 43 | low = mid + 1; 44 | else 45 | high = mid; 46 | } 47 | 48 | return low; 49 | } 50 | 51 | 52 | int main() 53 | { 54 | int t; 55 | cin>>t; 56 | 57 | for(int i=0;i>n; 60 | int k; 61 | cin>>k; 62 | 63 | int a[n]; 64 | for(int j=0;j>a[j]; 66 | } 67 | cout << kthDiff(a, n, k)<0){ 18 | long n = Long.parseLong(br.readLine()); 19 | int arr[] = new int[(int)n]; 20 | String inputLine[] = br.readLine().trim().split(" "); 21 | for(long i=0; i arr[(int)i]){ 32 | mini = arr[(int)i]; 33 | min_index = i; 34 | } 35 | } 36 | 37 | int flag = 0; 38 | if(max_index == min_index -1) 39 | flag = 1; 40 | else if(min_index == max_index - 1) 41 | flag = -1; 42 | 43 | if(flag == 1){ 44 | for(long i = 1; flag==1 && i<=max_index; ++i){ 45 | if(arr[(int)i-1] >= arr[(int)i]) 46 | flag = 0; 47 | } 48 | for(long i = min_index+1; flag==1 && i= arr[(int)i]) 50 | flag = 0; 51 | } 52 | if(arr[0]<=arr[(int)n-1]) 53 | flag = 0; 54 | } else if(flag == -1){ 55 | for(long i = 1; flag ==-1 && i<=min_index; ++i){ 56 | if(arr[(int)i-1] <= arr[(int)i]) 57 | flag = 0; 58 | } 59 | for(long i = max_index+1; flag==-1 && i=arr[(int)n-1]) 64 | flag = 0; 65 | } 66 | 67 | if(flag == 0) 68 | System.out.println("No"); 69 | else 70 | System.out.println("Yes"); 71 | } 72 | } 73 | } 74 | /******************************************************************************************************************************************/ 75 | /***********End****************/ -------------------------------------------------------------------------------- /Linear Search, Binary search - Post Class/counters-and-line.java: -------------------------------------------------------------------------------- 1 | /* Author = Chauhan Abhishek */ 2 | /* Codechef Id = https://www.codechef.com/users/abhishekchauha */ 3 | /* Codeforces Id = https://codeforces.com/profile/abhishekchauhan903 */ 4 | /* Newton School Id = https://my.newtonschool.co/user/abhishekchauhan9036/ */ 5 | /* GitHub Id = https://github.com/AbhishekChauhan9036 */ 6 | /* Language = JAVA */ 7 | /* Address = Ballia,UP,INDIA (221716)*/ 8 | 9 | /******************************************************************************************************************************************/ 10 | /***********Start****************/ 11 | import java.io.*; 12 | import java.util.*; 13 | class Main { 14 | public static void main (String[] args) { 15 | Scanner sc = new Scanner(System.in); 16 | Long n, p, k; 17 | n = sc.nextLong(); 18 | p = sc.nextLong(); 19 | k = sc.nextLong(); 20 | Long l = 1l, r = 10000000000l; 21 | 22 | while(l + 1 < r){ 23 | Long m = (l + r) / 2; 24 | if ( get(m, k) + get(m, n - k + 1) - m > p) 25 | r = m; 26 | else l = m; 27 | } 28 | 29 | System.out.println((Long)l); 30 | } 31 | 32 | public static Long get(Long ed, Long cnt){ 33 | Long d = cnt; 34 | if (d > ed) 35 | d = ed; 36 | cnt -= d; 37 | return cnt + d * (2 * ed - d + 1) / 2; 38 | } 39 | } 40 | /******************************************************************************************************************************************/ 41 | /***********End****************/ -------------------------------------------------------------------------------- /Linear Search, Binary search - Post Class/median-oftwo-sorted-arrays.cpp: -------------------------------------------------------------------------------- 1 | /* Author = Chauhan Abhishek */ 2 | /* Codechef Id = https://www.codechef.com/users/abhishekchauha */ 3 | /* Codeforces Id = https://codeforces.com/profile/abhishekchauhan903 */ 4 | /* Newton School Id = https://my.newtonschool.co/user/abhishekchauhan9036/ */ 5 | /* GitHub Id = https://github.com/AbhishekChauhan9036 */ 6 | /* Language = C++ */ 7 | /* Address = Ballia,UP,INDIA (221716)*/ 8 | 9 | /******************************************************************************************************************************************/ 10 | /***********Start****************/ 11 | #include 12 | using namespace std; 13 | #define int long long int 14 | #define Abhishek main 15 | 16 | double getMedian(int ar1[], int ar2[], int n, int m) 17 | { 18 | int i = 0; 19 | int j = 0; 20 | int count; 21 | double m1 = -1, m2 = -1; 22 | 23 | for (count = 0; count <= (m + n)/2; count++) 24 | { 25 | 26 | m2=m1; 27 | if(i != n && j != m) 28 | { 29 | m1 = (ar1[i] > ar2[j]) ? ar2[j++] : ar1[i++]; 30 | } 31 | else if(i < n) 32 | { 33 | m1 = ar1[i++]; 34 | } 35 | 36 | else 37 | { 38 | m1 = ar2[j++]; 39 | } 40 | } 41 | if((m + n) % 2 == 1){ 42 | return m1; 43 | } 44 | else{ 45 | return (m1+m2)/2; 46 | } 47 | } 48 | int32_t Abhishek() 49 | { 50 | int n1,n2; 51 | cin>>n1>>n2; 52 | int ar1[n1]; 53 | int ar2[n2]; 54 | 55 | for(int i=0;i>ar1[i]; 57 | } 58 | 59 | for(int i=0;i>ar2[i]; 61 | } 62 | printf("%0.2f",getMedian(ar1, ar2, n1, n2)); 63 | } 64 | /******************************************************************************************************************************************/ 65 | /***********End****************/ -------------------------------------------------------------------------------- /Maths, Geometry - Post Class/Dining-Table.java: -------------------------------------------------------------------------------- 1 | import java.io.*; // for handling input/output 2 | import java.util.*; // contains Collections framework 3 | import static java.lang.Math.*; 4 | 5 | class Main { 6 | public static void main (String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | double n = sc.nextDouble(); 9 | double R = sc.nextDouble(); 10 | double r = sc.nextDouble(); 11 | 12 | if(r>R || n>1&& (R-r)*sin(acos(-1.0)/n)+1e-8 4 | using namespace std; 5 | #ifdef LOCAL 6 | #include "algo/debug.h" 7 | #else 8 | #define debug(...) 42 9 | #endif 10 | template 11 | T inverse(T a, T m) { 12 | T u = 0, v = 1; 13 | while (a != 0) { 14 | T t = m / a; 15 | m -= t * a; swap(a, m); 16 | u -= t * v; swap(u, v); 17 | } 18 | assert(m == 1); 19 | return u; 20 | } 21 | 22 | template 23 | class Modular { 24 | public: 25 | using Type = typename decay::type; 26 | 27 | constexpr Modular() : value() {} 28 | template 29 | Modular(const U& x) { 30 | value = normalize(x); 31 | } 32 | 33 | template 34 | static Type normalize(const U& x) { 35 | Type v; 36 | if (-mod() <= x && x < mod()) v = static_cast(x); 37 | else v = static_cast(x % mod()); 38 | if (v < 0) v += mod(); 39 | return v; 40 | } 41 | 42 | const Type& operator()() const { return value; } 43 | template 44 | explicit operator U() const { return static_cast(value); } 45 | constexpr static Type mod() { return T::value; } 46 | 47 | Modular& operator+=(const Modular& other) { if ((value += other.value) >= mod()) value -= mod(); return *this; } 48 | Modular& operator-=(const Modular& other) { if ((value -= other.value) < 0) value += mod(); return *this; } 49 | template Modular& operator+=(const U& other) { return *this += Modular(other); } 50 | template Modular& operator-=(const U& other) { return *this -= Modular(other); } 51 | Modular& operator++() { return *this += 1; } 52 | Modular& operator--() { return *this -= 1; } 53 | Modular operator++(int) { Modular result(*this); *this += 1; return result; } 54 | Modular operator--(int) { Modular result(*this); *this -= 1; return result; } 55 | Modular operator-() const { return Modular(-value); } 56 | 57 | template 58 | typename enable_if::Type, int>::value, Modular>::type& operator*=(const Modular& rhs) { 59 | #ifdef _WIN32 60 | uint64_t x = static_cast(value) * static_cast(rhs.value); 61 | uint32_t xh = static_cast(x >> 32), xl = static_cast(x), d, m; 62 | asm( 63 | "divl %4; \n\t" 64 | : "=a" (d), "=d" (m) 65 | : "d" (xh), "a" (xl), "r" (mod()) 66 | ); 67 | value = m; 68 | #else 69 | value = normalize(static_cast(value) * static_cast(rhs.value)); 70 | #endif 71 | return *this; 72 | } 73 | template 74 | typename enable_if::Type, long long>::value, Modular>::type& operator*=(const Modular& rhs) { 75 | value = (long long) ((__int128) value * rhs.value % mod()); 76 | return *this; 77 | } 78 | template 79 | typename enable_if::Type>::value, Modular>::type& operator*=(const Modular& rhs) { 80 | value = normalize(value * rhs.value); 81 | return *this; 82 | } 83 | 84 | Modular& operator/=(const Modular& other) { return *this *= Modular(inverse(other.value, mod())); } 85 | 86 | friend const Type& abs(const Modular& x) { return x.value; } 87 | 88 | template 89 | friend bool operator==(const Modular& lhs, const Modular& rhs); 90 | 91 | template 92 | friend bool operator<(const Modular& lhs, const Modular& rhs); 93 | 94 | template 95 | friend V& operator>>(V& stream, Modular& number); 96 | 97 | private: 98 | Type value; 99 | }; 100 | 101 | template bool operator==(const Modular& lhs, const Modular& rhs) { return lhs.value == rhs.value; } 102 | template bool operator==(const Modular& lhs, U rhs) { return lhs == Modular(rhs); } 103 | template bool operator==(U lhs, const Modular& rhs) { return Modular(lhs) == rhs; } 104 | 105 | template bool operator!=(const Modular& lhs, const Modular& rhs) { return !(lhs == rhs); } 106 | template bool operator!=(const Modular& lhs, U rhs) { return !(lhs == rhs); } 107 | template bool operator!=(U lhs, const Modular& rhs) { return !(lhs == rhs); } 108 | 109 | template bool operator<(const Modular& lhs, const Modular& rhs) { return lhs.value < rhs.value; } 110 | 111 | template Modular operator+(const Modular& lhs, const Modular& rhs) { return Modular(lhs) += rhs; } 112 | template Modular operator+(const Modular& lhs, U rhs) { return Modular(lhs) += rhs; } 113 | template Modular operator+(U lhs, const Modular& rhs) { return Modular(lhs) += rhs; } 114 | 115 | template Modular operator-(const Modular& lhs, const Modular& rhs) { return Modular(lhs) -= rhs; } 116 | template Modular operator-(const Modular& lhs, U rhs) { return Modular(lhs) -= rhs; } 117 | template Modular operator-(U lhs, const Modular& rhs) { return Modular(lhs) -= rhs; } 118 | 119 | template Modular operator*(const Modular& lhs, const Modular& rhs) { return Modular(lhs) *= rhs; } 120 | template Modular operator*(const Modular& lhs, U rhs) { return Modular(lhs) *= rhs; } 121 | template Modular operator*(U lhs, const Modular& rhs) { return Modular(lhs) *= rhs; } 122 | 123 | template Modular operator/(const Modular& lhs, const Modular& rhs) { return Modular(lhs) /= rhs; } 124 | template Modular operator/(const Modular& lhs, U rhs) { return Modular(lhs) /= rhs; } 125 | template Modular operator/(U lhs, const Modular& rhs) { return Modular(lhs) /= rhs; } 126 | 127 | template 128 | Modular power(const Modular& a, const U& b) { 129 | assert(b >= 0); 130 | Modular x = a, res = 1; 131 | U p = b; 132 | while (p > 0) { 133 | if (p & 1) res *= x; 134 | x *= x; 135 | p >>= 1; 136 | } 137 | return res; 138 | } 139 | 140 | template 141 | bool IsZero(const Modular& number) { 142 | return number() == 0; 143 | } 144 | 145 | template 146 | string to_string(const Modular& number) { 147 | return to_string(number()); 148 | } 149 | 150 | 151 | template 152 | U& operator<<(U& stream, const Modular& number) { 153 | return stream << number(); 154 | } 155 | 156 | template 157 | U& operator>>(U& stream, Modular& number) { 158 | typename common_type::Type, long long>::type x; 159 | stream >> x; 160 | number.value = Modular::normalize(x); 161 | return stream; 162 | } 163 | 164 | using ModType = long long; 165 | 166 | struct VarMod { static ModType value; }; 167 | ModType VarMod::value; 168 | ModType& md = VarMod::value; 169 | using Mint = Modular; 170 | 171 | 172 | namespace factorizer { 173 | 174 | template 175 | struct FactorizerVarMod { static T value; }; 176 | template 177 | T FactorizerVarMod::value; 178 | 179 | template 180 | bool IsPrime(T n, const vector& bases) { 181 | if (n < 2) { 182 | return false; 183 | } 184 | vector small_primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; 185 | for (const T& x : small_primes) { 186 | if (n % x == 0) { 187 | return n == x; 188 | } 189 | } 190 | if (n < 31 * 31) { 191 | return true; 192 | } 193 | int s = 0; 194 | T d = n - 1; 195 | while ((d & 1) == 0) { 196 | d >>= 1; 197 | s++; 198 | } 199 | FactorizerVarMod::value = n; 200 | for (const T& a : bases) { 201 | if (a % n == 0) { 202 | continue; 203 | } 204 | Modular> cur = a; 205 | cur = power(cur, d); 206 | if (cur == 1) { 207 | continue; 208 | } 209 | bool witness = true; 210 | for (int r = 0; r < s; r++) { 211 | if (cur == n - 1) { 212 | witness = false; 213 | break; 214 | } 215 | cur *= cur; 216 | } 217 | if (witness) { 218 | return false; 219 | } 220 | } 221 | return true; 222 | } 223 | 224 | bool IsPrime(long long n) { 225 | return IsPrime(n, {2, 325, 9375, 28178, 450775, 9780504, 1795265022}); 226 | } 227 | 228 | bool IsPrime(int32_t n) { 229 | return IsPrime(n, {2, 7, 61}); 230 | } 231 | 232 | // Jab ulong long version ka need ho tab ........................................... 233 | /* 234 | bool IsPrime(ulong long n) { 235 | if (n < 2) { 236 | return false; 237 | } 238 | vector small_primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; 239 | for (uint32_t x : small_primes) { 240 | if (n == x) { 241 | return true; 242 | } 243 | if (n % x == 0) { 244 | return false; 245 | } 246 | } 247 | if (n < 31 * 31) { 248 | return true; 249 | } 250 | uint32_t s = __builtin_ctzll(n - 1); 251 | ulong long d = (n - 1) >> s; 252 | function witness = [&n, &s, &d](ulong long a) { 253 | ulong long cur = 1, p = d; 254 | while (p > 0) { 255 | if (p & 1) { 256 | cur = (__uint128_t) cur * a % n; 257 | } 258 | a = (__uint128_t) a * a % n; 259 | p >>= 1; 260 | } 261 | if (cur == 1) { 262 | return false; 263 | } 264 | for (uint32_t r = 0; r < s; r++) { 265 | if (cur == n - 1) { 266 | return false; 267 | } 268 | cur = (__uint128_t) cur * cur % n; 269 | } 270 | return true; 271 | }; 272 | vector bases_64bit = {2, 325, 9375, 28178, 450775, 9780504, 1795265022}; 273 | for (ulong long a : bases_64bit) { 274 | if (a % n == 0) { 275 | return true; 276 | } 277 | if (witness(a)) { 278 | return false; 279 | } 280 | } 281 | return true; 282 | } 283 | */ 284 | 285 | vector least = {0, 1}; 286 | vector primes; 287 | int precalculated = 1; 288 | 289 | void RunLinearSieve(int n) { 290 | n = max(n, 1); 291 | least.assign(n + 1, 0); 292 | primes.clear(); 293 | for (int i = 2; i <= n; i++) { 294 | if (least[i] == 0) { 295 | least[i] = i; 296 | primes.push_back(i); 297 | } 298 | for (int x : primes) { 299 | if (x > least[i] || i * x > n) { 300 | break; 301 | } 302 | least[i * x] = x; 303 | } 304 | } 305 | precalculated = n; 306 | } 307 | 308 | void RunSlowSieve(int n) { 309 | n = max(n, 1); 310 | least.assign(n + 1, 0); 311 | for (int i = 2; i * i <= n; i++) { 312 | if (least[i] == 0) { 313 | for (int j = i * i; j <= n; j += i) { 314 | if (least[j] == 0) { 315 | least[j] = i; 316 | } 317 | } 318 | } 319 | } 320 | primes.clear(); 321 | for (int i = 2; i <= n; i++) { 322 | if (least[i] == 0) { 323 | least[i] = i; 324 | primes.push_back(i); 325 | } 326 | } 327 | precalculated = n; 328 | } 329 | 330 | void RunSieve(int n) { 331 | RunLinearSieve(n); 332 | } 333 | 334 | template 335 | vector> MergeFactors(const vector>& a, const vector>& b) { 336 | vector> c; 337 | int i = 0; 338 | int j = 0; 339 | while (i < (int) a.size() || j < (int) b.size()) { 340 | if (i < (int) a.size() && j < (int) b.size() && a[i].first == b[j].first) { 341 | c.emplace_back(a[i].first, a[i].second + b[j].second); 342 | ++i; 343 | ++j; 344 | continue; 345 | } 346 | if (j == (int) b.size() || (i < (int) a.size() && a[i].first < b[j].first)) { 347 | c.push_back(a[i++]); 348 | } else { 349 | c.push_back(b[j++]); 350 | } 351 | } 352 | return c; 353 | } 354 | 355 | 356 | 357 | template 358 | vector> RhoC(const T& n, const T& c) { 359 | if (n <= 1) { 360 | return {}; 361 | } 362 | if ((n & 1) == 0) { 363 | return MergeFactors({{2, 1}}, RhoC(n / 2, c)); 364 | } 365 | if (IsPrime(n)) { 366 | return {{n, 1}}; 367 | } 368 | FactorizerVarMod::value = n; 369 | Modular> x = 2; 370 | Modular> saved = 2; 371 | T power = 1; 372 | T lam = 1; 373 | while (true) { 374 | x = x * x + c; 375 | T g = __gcd((x - saved)(), n); 376 | if (g != 1) { 377 | return MergeFactors(RhoC(g, c + 1), RhoC(n / g, c + 1)); 378 | } 379 | if (power == lam) { 380 | saved = x; 381 | power <<= 1; 382 | lam = 0; 383 | } 384 | lam++; 385 | } 386 | return {}; 387 | } 388 | 389 | template 390 | vector> Rho(const T& n) { 391 | return RhoC(n, static_cast(1)); 392 | } 393 | 394 | template 395 | vector> Factorize(T x) { 396 | if (x <= 1) { 397 | return {}; 398 | } 399 | if (x <= precalculated) { 400 | vector> ret; 401 | while (x > 1) { 402 | if (!ret.empty() && ret.back().first == least[x]) { 403 | ret.back().second++; 404 | } else { 405 | ret.emplace_back(least[x], 1); 406 | } 407 | x /= least[x]; 408 | } 409 | return ret; 410 | } 411 | if (x <= static_cast(precalculated) * precalculated) { 412 | vector> ret; 413 | if (!IsPrime(x)) { 414 | for (T i : primes) { 415 | T t = x / i; 416 | if (i > t) { 417 | break; 418 | } 419 | if (x == t * i) { 420 | int cnt = 0; 421 | while (x % i == 0) { 422 | x /= i; 423 | cnt++; 424 | } 425 | ret.emplace_back(i, cnt); 426 | if (IsPrime(x)) { 427 | break; 428 | } 429 | } 430 | } 431 | } 432 | if (x > 1) { 433 | ret.emplace_back(x, 1); 434 | } 435 | return ret; 436 | } 437 | return Rho(x); 438 | } 439 | 440 | template 441 | vector BuildDivisorsFromFactors(const vector>& factors) { 442 | vector divisors = {1}; 443 | for (auto& p : factors) { 444 | int sz = (int) divisors.size(); 445 | for (int i = 0; i < sz; i++) { 446 | T cur = divisors[i]; 447 | for (int j = 0; j < p.second; j++) { 448 | cur *= p.first; 449 | divisors.push_back(cur); 450 | } 451 | } 452 | } 453 | sort(divisors.begin(), divisors.end()); 454 | return divisors; 455 | } 456 | 457 | } 458 | 459 | template 460 | struct PrimitiveVarMod { static T value; }; 461 | template 462 | T PrimitiveVarMod::value; 463 | 464 | template 465 | T GetPrimitiveRoot(const T& modulo, const F& factorize) { 466 | if (modulo <= 0) { 467 | return -1; 468 | } 469 | if (modulo == 1 || modulo == 2 || modulo == 4) { 470 | return modulo - 1; 471 | } 472 | vector> modulo_factors = factorize(modulo); 473 | if (modulo_factors[0].first == 2 && (modulo_factors[0].second != 1 || modulo_factors.size() != 2)) { 474 | return -1; 475 | } 476 | if (modulo_factors[0].first != 2 && modulo_factors.size() != 1) { 477 | return -1; 478 | } 479 | set phi_factors; 480 | T phi = modulo; 481 | for (auto& d : modulo_factors) { 482 | phi = phi / d.first * (d.first - 1); 483 | if (d.second > 1) { 484 | phi_factors.insert(d.first); 485 | } 486 | for (auto& e : factorize(d.first - 1)) { 487 | phi_factors.insert(e.first); 488 | } 489 | } 490 | PrimitiveVarMod::value = modulo; 491 | Modular> gen = 2; 492 | while (gen != 0) { 493 | if (power(gen, phi) != 1) { 494 | continue; 495 | } 496 | bool ok = true; 497 | for (auto& p : phi_factors) { 498 | if (power(gen, phi / p) == 1) { 499 | ok = false; 500 | break; 501 | } 502 | } 503 | if (ok) { 504 | return gen(); 505 | } 506 | gen++; 507 | } 508 | assert(false); 509 | return -1; 510 | } 511 | 512 | template 513 | T GetPrimitiveRoot(const T& modulo) { 514 | return GetPrimitiveRoot(modulo, factorizer::Factorize); 515 | } 516 | void BhagwatGeeta(){ 517 | long double ax,ay,bx,by,cx,cy; 518 | cin>>ax>>ay>>bx>>by>>cx>>cy; 519 | if((by-ay)/(bx-ax)!=(cy-by)/(cx-bx)&&(by-ay)*(by-ay)+(bx-ax)*(bx-ax)==(cy-by)*(cy-by)+(cx-bx)*(cx-bx)) { 520 | printf("Yes\n"); 521 | } else { 522 | printf("No\n"); 523 | } 524 | } 525 | int32_t main() { 526 | ios::sync_with_stdio(false); 527 | cin.tie(0); 528 | int Krishna=1; 529 | // cin>>Krishna; 530 | while(Krishna--){ 531 | BhagwatGeeta(); 532 | } 533 | return 0; 534 | } -------------------------------------------------------------------------------- /Maths, Geometry - Post Class/Too-Many-Rectangles.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | # define lli long long int 3 | # define pb push_back 4 | # define ff first 5 | #define ss second 6 | using namespace std; 7 | void fast(){ 8 | ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0); 9 | } 10 | int main() 11 | { 12 | fast(); 13 | int t=1; 14 | while(t--) 15 | { 16 | 17 | lli n,i,j,c=0,ansx=0,ansy=0,x,y; 18 | cin>>n; 19 | n=4ll*n+1ll; 20 | map mpx; 21 | map mpy; 22 | for(i=0;i>x>>y; 25 | mpx[x]++; 26 | mpy[y]++; 27 | } 28 | for (auto itr = mpx.begin(); itr != mpx.end(); ++itr) 29 | { 30 | x=itr->ss; 31 | if(x%2==0) 32 | continue; 33 | else 34 | { 35 | ansx=itr->ff; 36 | break; 37 | } 38 | } 39 | for(auto itr1 = mpy.begin(); itr1 != mpy.end(); ++itr1) 40 | { 41 | y=itr1->ss; 42 | if(y%2==0) 43 | continue; 44 | else 45 | { 46 | ansy=itr1->ff; 47 | break; 48 | } 49 | } 50 | cout< arr=new ArrayList<>(); 21 | //ArrayList arr=new ArrayList<>(); 22 | while(c-->0){ 23 | int x = input.nextInt(); 24 | int y = input.nextInt(); 25 | arr.add(new pair(x,y)); 26 | } 27 | HashMap X = new HashMap<>(); 28 | HashMap Y = new HashMap<>(); 29 | for(int i = 0;ientry:X.entrySet()){ 48 | if(entry.getValue()%2!= 0){ 49 | x_coordinate = entry.getKey(); 50 | break; 51 | } 52 | } 53 | for(Map.Entryentry:Y.entrySet()){ 54 | if(entry.getValue()%2 != 0){ 55 | y_coordinate = entry.getKey(); 56 | break; 57 | } 58 | } 59 | System.out.println(x_coordinate+" "+y_coordinate); 60 | 61 | 62 | } 63 | } -------------------------------------------------------------------------------- /Maths, Geometry - Post Class/Tringles-love.java: -------------------------------------------------------------------------------- 1 | import java.io.*; // for handling input/output 2 | import java.util.*; // contains Collections framework 3 | class Main { 4 | public static int sq(int side){ 5 | return side*side; 6 | } 7 | public static boolean check(int coordinates[]){ 8 | int a,b,c; 9 | a=sq(coordinates[0]-coordinates[2])+sq(coordinates[1]-coordinates[3]); 10 | b=sq(coordinates[0]-coordinates[4])+sq(coordinates[1]-coordinates[5]); 11 | c=sq(coordinates[2]-coordinates[4])+sq(coordinates[3]-coordinates[5]); 12 | if (a==0 || b==0 || c==0) 13 | return false; 14 | if (a+b==c||a+c==b||b+c==a){ 15 | return true; 16 | } 17 | return false; 18 | } 19 | public static void main(String s[]) 20 | { 21 | Scanner sc = new Scanner(System.in); 22 | int coordinates[]=new int[6]; 23 | for(int i=0;i<6;i++) 24 | coordinates[i]=sc.nextInt(); 25 | if(check(coordinates)){ 26 | System.out.println("Right"); 27 | return ; 28 | } 29 | for(int i=0;i<6;i++){ 30 | coordinates[i]++; 31 | if(check(coordinates)){ 32 | System.out.println("Special"); 33 | return ; 34 | } 35 | coordinates[i]--; 36 | coordinates[i]--; 37 | if(check(coordinates)){ 38 | System.out.println("Special"); 39 | return ; 40 | } 41 | coordinates[i]++; 42 | } 43 | 44 | System.out.println("Simple"); 45 | } 46 | } -------------------------------------------------------------------------------- /Maths, Primes, Geometry - In Class/Check-if-Prime.cpp: -------------------------------------------------------------------------------- 1 | #include // header file includes every Standard library 2 | using namespace std; 3 | 4 | 5 | void fun(int n){ 6 | int i, m=0, flag=0; 7 | m=n; 8 | for(i = 2; i <= sqrt(m); i++) 9 | { 10 | if(n % i == 0) 11 | { 12 | cout<<"No"<>n; 24 | for(int i=1;i<=n;i++){ 25 | int p; 26 | cin>>p; 27 | fun(p); 28 | } 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Maths, Primes, Geometry - In Class/Closet-Prime-Contest.java: -------------------------------------------------------------------------------- 1 | import java.io.*; // for handling input/output 2 | import java.util.*; // contains Collections framework 3 | 4 | // don't change the name of this class 5 | // you can add inner classes if needed 6 | class Main { 7 | public static void main (String[] args) { 8 | Scanner sc= new Scanner(System.in); 9 | 10 | int n=sc.nextInt(); 11 | int ad=1; 12 | for(int i=n;i>=Math.sqrt(n);i--){ 13 | if(prime(i)){ 14 | System.out.println(i); 15 | break; 16 | }else if(prime(n-ad)){ 17 | System.out.println(n-ad); 18 | break; 19 | }else if(prime(n+ad)){ 20 | System.out.println(n+ad); 21 | break; 22 | } 23 | ad++; 24 | } 25 | 26 | } 27 | static boolean prime(int n){ 28 | if(n<=1) 29 | return false; 30 | for(int i=2;i<=Math.sqrt(n);i++){ 31 | if(n%i==0) 32 | return false; 33 | } 34 | 35 | return true; 36 | } 37 | } -------------------------------------------------------------------------------- /Maths, Primes, Geometry - In Class/Divisors-Of-N.java: -------------------------------------------------------------------------------- 1 | import java.io.*; // for handling input/output 2 | import java.util.*; // contains Collections framework 3 | 4 | // don't change the name of this class 5 | // you can add inner classes if needed 6 | class Main { 7 | public static void main (String[] args) { 8 | 9 | Scanner inputTaker = new Scanner(System.in); 10 | int u = inputTaker.nextInt(); 11 | while(u>0){ 12 | int n= inputTaker.nextInt(); 13 | int count=0; 14 | for(int x= 1; x<=Math.sqrt(n); x++){ 15 | 16 | if(n%x== 0){ 17 | 18 | if(x%2 == 0){ 19 | count++; 20 | } 21 | if(x*x != n){ 22 | if((n/x)%2== 0) { 23 | count++; 24 | } 25 | } 26 | } 27 | } 28 | System.out.println(count); 29 | u--; 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /Maths, Primes, Geometry - In Class/Functions-prime.cpp: -------------------------------------------------------------------------------- 1 | #include // header file includes every Standard library 2 | using namespace std; 3 | 4 | int checkPrimeNumber(int n) { 5 | int j, flag = 1; 6 | 7 | for (j = 2; j <= n / 2; ++j) { 8 | 9 | if (n % j == 0) { 10 | flag = 0; 11 | break; 12 | } 13 | } 14 | 15 | return flag; 16 | } 17 | int main() { 18 | 19 | int n1, n2, i, flag; 20 | scanf("%d %d", &n1, &n2); 21 | 22 | if (n1 > n2) { 23 | n1 = n1 + n2; 24 | n2 = n1 - n2; 25 | n1 = n1 - n2; 26 | } 27 | int ans=0; 28 | for (i = n1 + 1; i < n2; ++i) { 29 | flag = checkPrimeNumber(i); 30 | 31 | if (flag == 1) { 32 | ans++; 33 | } 34 | } 35 | 36 | cout< 2 | using namespace std; 3 | bool isPrime(int n) 4 | { 5 | if (n <= 1) 6 | return false; 7 | 8 | 9 | for (int i = 2; i < n; i++) 10 | if (n % i == 0) 11 | return false; 12 | 13 | return true; 14 | } 15 | int ppp=0; 16 | void printPrime(int n) 17 | { 18 | for (int i = 2; i <= n; i++) { 19 | if (isPrime(i)) 20 | ppp++; 21 | } 22 | cout<>t; 30 | 31 | while(t--){ 32 | int n;cin>>n; 33 | printPrime(n); 34 | } 35 | } -------------------------------------------------------------------------------- /Newton-School-Course.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbhishekChauhan9036/Newton-School-May-Batch/6f95ac6c73d6fbe116b5d84156ff7cacdc729742/Newton-School-Course.pdf -------------------------------------------------------------------------------- /OOPS Assignments/Adderll.java: -------------------------------------------------------------------------------- 1 | class AdderII{ 2 | int num1; 3 | int num2; 4 | AdderII() 5 | { 6 | this.num1=0; 7 | this.num2=0; 8 | } 9 | AdderII(int a, int b) 10 | { 11 | num1=a; 12 | num2=b; 13 | } 14 | int add(int a, int b, int c) 15 | { 16 | num1=num1+a; 17 | num2=num2+b; 18 | return (num1+num2 - 2*c); 19 | } 20 | int add() 21 | { 22 | return num1+num2; 23 | } 24 | int add(int a, int b) 25 | { 26 | return a+b; 27 | } 28 | } -------------------------------------------------------------------------------- /OOPS Assignments/Classes-in-java.java: -------------------------------------------------------------------------------- 1 | static class Student 2 | { 3 | //Enter your code here 4 | public String name; 5 | public int eng; 6 | public int maths; 7 | public int hindi; 8 | 9 | public void setName(String name){ 10 | this.name = name; 11 | } 12 | 13 | 14 | public void setEng(int eng) { 15 | this.eng = eng; 16 | } 17 | 18 | 19 | public void setMaths(int maths){ 20 | this.maths = maths; 21 | } 22 | 23 | 24 | public void setHindi(int hindi){ 25 | this.hindi = hindi; 26 | } 27 | 28 | 29 | public int getEng(){ 30 | return eng; 31 | } 32 | 33 | 34 | public int getMaths(){ 35 | return maths; 36 | 37 | } 38 | 39 | public int getHindi(){ 40 | return hindi; 41 | } 42 | 43 | public String getName(){ 44 | return name; 45 | } 46 | } 47 | static Student[] createStudentArray(int n) 48 | { 49 | //Enter your code here 50 | Student[] st = new Student[n]; 51 | 52 | for(int i=0; i0) 14 | this.width = w; 15 | if(l>0) 16 | this.length = l; 17 | } 18 | 19 | public double getWidth() { 20 | return this.width; 21 | } 22 | 23 | public double getLength() { 24 | return this.length; 25 | } 26 | 27 | public double getArea() { 28 | return this.length * this.width; 29 | } 30 | } 31 | 32 | class Cuboid extends Rectangle { 33 | double height; 34 | 35 | Cuboid(double w, double l, double h) { 36 | super(w,l); 37 | this.height = h > 0 ? h : 0; 38 | } 39 | 40 | public double getHeight() { 41 | return this.height; 42 | } 43 | 44 | public double getVolume() { 45 | return getArea() * getHeight(); 46 | } 47 | } -------------------------------------------------------------------------------- /OOPS Assignments/Shapes-Class-INheritance.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | // Do not edit the Quadrilateral class 4 | abstract class Quadrilateral { 5 | double side1; 6 | double side2; 7 | double side3; 8 | double side4; 9 | 10 | public Quadrilateral(double side1, double side2, double side3, double side4) { 11 | this.side1 = side1; 12 | this.side2 = side2; 13 | this.side3 = side3; 14 | this.side4 = side4; 15 | } 16 | 17 | protected abstract double getArea(); 18 | 19 | protected double getPerimeter() { 20 | return (side1+side2+side3+side4); 21 | } 22 | } 23 | 24 | // Implement class Parallelogram and methods getArea() and getPerimeter() 25 | class Parallelogram extends Quadrilateral { 26 | //Write your code here 27 | double height; 28 | Parallelogram(double s1, double s2, double h){ 29 | super(s1,s2,s1,s2); 30 | this.height=h; 31 | } 32 | 33 | public double getArea(){ 34 | return side1*height; 35 | } 36 | 37 | } 38 | 39 | // Implement class Rhombus and methods getArea() and getPerimeter() 40 | class Rhombus extends Parallelogram { 41 | //Write your code here 42 | double height; 43 | Rhombus(double s1, double h){ 44 | super(s1,s1,h); 45 | this.height=h; 46 | } 47 | 48 | } 49 | 50 | //Implement class Rectangle and methods getArea() and getPerimeter() 51 | class Rectangle extends Parallelogram { 52 | //Write your code here 53 | Rectangle(double s1, double s2){ 54 | super(s1,s2,s2); 55 | } 56 | 57 | } 58 | //Implement class Square and methods getArea() and getPerimeter() 59 | class Square extends Rhombus { 60 | //Write your code here 61 | Square(double s1){ 62 | super(s1,s1); 63 | } 64 | 65 | } 66 | 67 | 68 | 69 | // Do not edit the Main class 70 | class Main { 71 | public static void main(String[] args) { 72 | //Write your code here 73 | Scanner sc=new Scanner(System.in); 74 | double s1,s2,h; 75 | s1=sc.nextDouble(); 76 | s2=sc.nextDouble(); 77 | h=sc.nextDouble(); 78 | 79 | double ss1,hs,rs1,rs2,sq1; 80 | ss1=sc.nextDouble(); 81 | hs=sc.nextDouble(); 82 | 83 | rs1=sc.nextDouble(); 84 | rs2=sc.nextDouble(); 85 | 86 | sq1=sc.nextDouble(); 87 | if(s1<0 || s2<0 || h<0 || ss1<0 || hs<0 || rs1<0 || rs2<0 || sq1<0){ 88 | System.out.println("Length of a side cannot be negative. Please Enter a positive integer"); 89 | } 90 | else{ 91 | Parallelogram parallelogram=new Parallelogram(s1,s2,h); 92 | Rhombus rhombus=new Rhombus(ss1,hs); 93 | Rectangle rectangle=new Rectangle(rs1,rs2); 94 | Square square=new Square(sq1); 95 | System.out.println("Perimeter of Parallelogram is " + parallelogram.getPerimeter() +" and Area of Parallelogram is " + parallelogram.getArea()); 96 | System.out.println("Perimeter of Rhombus is " + rhombus.getPerimeter() +" and Area of Rhombus is " + rhombus.getArea()); 97 | System.out.println("Perimeter of Rectangle is " + rectangle.getPerimeter() +" and Area of Rectangle is " + rectangle.getArea()); 98 | System.out.println("Perimeter of Square is " + square.getPerimeter()+ " and Area of Square is " + square.getArea()); 99 | } 100 | sc.close(); 101 | } 102 | } -------------------------------------------------------------------------------- /OOPS Assignments/student.java: -------------------------------------------------------------------------------- 1 | /* 2 | class Person { 3 | protected String firstName; 4 | protected String lastName; 5 | protected int idNumber; 6 | 7 | // Constructor 8 | Person(String firstName, String lastName, int identification){ 9 | this.firstName = firstName; 10 | this.lastName = lastName; 11 | this.idNumber = identification; 12 | } 13 | 14 | // Print person data 15 | public void printPerson(){ 16 | System.out.println( 17 | "Name: " + lastName + ", " + firstName 18 | + "\nID: " + idNumber); 19 | } 20 | 21 | } 22 | */ 23 | 24 | class Student extends Person{ 25 | private int[] testScores; 26 | 27 | /* 28 | * Class Constructor 29 | * 30 | * @param firstName - A string denoting the Person's first name. 31 | * @param lastName - A string denoting the Person's last name. 32 | * @param id - An integer denoting the Person's ID number. 33 | * @param scores - An array of integers denoting the Person's test scores. 34 | */ 35 | // Write your constructor here 36 | Student(String firstName, String lastName, int identification, int[] testScores) { 37 | super(firstName, lastName, identification); 38 | this.testScores = testScores; 39 | } 40 | 41 | 42 | /* 43 | * Method Name: calculate 44 | * @return A character denoting the grade. 45 | */ 46 | // Write your method here 47 | char calculate() { 48 | int total = 0; 49 | for (int testScore : testScores) total += testScore; 50 | int avg = total / testScores.length; 51 | if (avg >= 90 && avg <= 100) return 'O'; 52 | if (avg >= 80 && avg < 90) return 'E'; 53 | if (avg >= 70 && avg < 80) return 'A'; 54 | if (avg >= 55 && avg < 70) return 'P'; 55 | if (avg >= 40 && avg < 55) return 'D'; 56 | return 'T'; 57 | } 58 | } -------------------------------------------------------------------------------- /Queue - In Class/Linked_list_implementation_of_queue.java: -------------------------------------------------------------------------------- 1 | class Queue 2 | { 3 | private Node front, rear; 4 | private int currentSize; 5 | 6 | class Node { 7 | Node next; 8 | int val; 9 | 10 | Node(int val) { 11 | this.val = val; 12 | next = null; 13 | 14 | } 15 | } 16 | 17 | public Queue() 18 | { 19 | front = null; 20 | rear = null; 21 | currentSize = 0; 22 | } 23 | 24 | 25 | public void dequeue() 26 | { 27 | if(this.front == null) return; 28 | this.front = this.front.next; 29 | if(this.front == null) this.rear = null; 30 | } 31 | 32 | 33 | public void enqueue(int data) 34 | { 35 | Node n = new Node(data); 36 | if(this.rear == null){ 37 | this.front = this.rear = n; 38 | } 39 | else{ 40 | this.rear.next = n; 41 | this.rear = n; 42 | } 43 | } 44 | 45 | public void displayfront(){ 46 | if(this.front == null){ 47 | System.out.println(0); 48 | } 49 | else{ 50 | System.out.println(this.front.val); 51 | } 52 | } 53 | } 54 | ///Abhishek.java 55 | -------------------------------------------------------------------------------- /Queue - In Class/Sum_of_minimum_and_maximum_elements_of_subarrays.cpp: -------------------------------------------------------------------------------- 1 | ///abhishek.cpp 2 | 3 | #include 4 | using namespace std; 5 | #define int long long 6 | 7 | int SumOfKsubArray(int arr[] , int n , int k) 8 | { 9 | int sum = 0; 10 | 11 | 12 | deque< int > S(k), G(k); 13 | 14 | 15 | int i = 0; 16 | for (i = 0; i < k; i++) 17 | { 18 | 19 | while ( (!S.empty()) && arr[S.back()] >= arr[i]) 20 | S.pop_back(); 21 | 22 | 23 | while ( (!G.empty()) && arr[G.back()] <= arr[i]) 24 | G.pop_back(); 25 | 26 | G.push_back(i); 27 | S.push_back(i); 28 | } 29 | 30 | for ( ; i < n; i++ ) 31 | { 32 | 33 | sum += arr[S.front()] + arr[G.front()]; 34 | 35 | while ( !S.empty() && S.front() <= i - k) 36 | S.pop_front(); 37 | while ( !G.empty() && G.front() <= i - k) 38 | G.pop_front(); 39 | 40 | while ( (!S.empty()) && arr[S.back()] >= arr[i]) 41 | S.pop_back(); // Remove from rear 42 | 43 | 44 | while ( (!G.empty()) && arr[G.back()] <= arr[i]) 45 | G.pop_back(); 46 | 47 | 48 | G.push_back(i); 49 | S.push_back(i); 50 | } 51 | 52 | 53 | sum += arr[S.front()] + arr[G.front()]; 54 | 55 | return sum; 56 | } 57 | 58 | 59 | int32_t main() 60 | { 61 | int n,k; 62 | cin>>n>>k; 63 | 64 | int arr[n]; 65 | for(int i=0;i>arr[i]; 67 | } 68 | cout << SumOfKsubArray(arr, n, k) ; 69 | return 0; 70 | } -------------------------------------------------------------------------------- /Queue - In Class/array_implementation_of_queue.java: -------------------------------------------------------------------------------- 1 | /* 2 | //... 3 | class Queue{ 4 | static final int MAX = 10000; 5 | int rear=0,front=0; 6 | int a[] = new int[MAX]; 7 | } 8 | int rear=0,front=0; //pointer to rear and front 9 | int a[] //array that contain elements of queue 10 | */ 11 | public static void enqueue(int x, int k) 12 | { 13 | if(rear == k) { 14 | System.out.println("Queue is full") ; 15 | } 16 | else{ 17 | if(front == -1) { 18 | front = 0 ; 19 | } 20 | //rear++ ; 21 | a[rear++] = x ; 22 | 23 | } 24 | 25 | // enter your code here 26 | } 27 | 28 | 29 | public static void dequeue() 30 | { 31 | if(rear == front) { 32 | System.out.println("Queue is empty") ; 33 | } 34 | else { 35 | //int temp = a[front] ; 36 | if(front >= rear) { 37 | front = -1 ; 38 | rear = -1 ; 39 | } 40 | else { 41 | front++ ; 42 | } 43 | } 44 | //enter your code here 45 | } 46 | 47 | public static void displayfront() 48 | { 49 | if(front == rear) { 50 | System.out.println("Queue is empty") ; 51 | } 52 | else{ 53 | System.out.println(a[front]) ; 54 | } 55 | // enter your code here 56 | } 57 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Newton-School-May-Batch 2 | # 100 % assignment Solution available here 3 | -------------------------------------------------------------------------------- /Recursion new - post class/BlackAndWhite.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long numOfWays(int n, int m); 5 | 6 | int main() { 7 | 8 | int t; 9 | cin >> t; 10 | while (t--) { 11 | int n, m; 12 | cin >> n >> m; 13 | cout << numOfWays(n, m) << endl; 14 | } 15 | return 0; 16 | } 17 | 18 | 19 | long long numOfWays(int N, int M) 20 | { 21 | long long ans=0; 22 | long long k=N*M; 23 | int mod=1000000007; 24 | for(int i=0;i=0) 30 | c++; 31 | if(i+2=0) 34 | c++; 35 | if(j-2>=0&&i+1=0&&i-1>=0) 38 | c++; 39 | if(i-2>=0&&j+1=0&&j-1>=0) 42 | c++; 43 | ans=((ans+k)%mod-(c+1)%mod+mod)%mod; 44 | } 45 | } 46 | return ans; 47 | } -------------------------------------------------------------------------------- /Recursion new - post class/ComputePower.java: -------------------------------------------------------------------------------- 1 | static int Power(int x, int y) 2 | { 3 | if (y == 0) 4 | return 1; 5 | else if (y % 2 == 0) 6 | return Power(x, y / 2) * Power(x, y / 2); 7 | else 8 | return x * Power(x, y / 2) * Power(x, y / 2); 9 | } -------------------------------------------------------------------------------- /Recursion new - post class/Cricket-Fever.java: -------------------------------------------------------------------------------- 1 | import java.io.*; // for handling input/output 2 | import java.util.*; // contains Collections framework 3 | 4 | // don't change the name of this class 5 | // you can add inner classes if needed 6 | class Main { 7 | public static int findMaxPoint(int arr[], int n) { 8 | if (n == 2) { 9 | return Math.max(arr[0], arr[1]); 10 | } 11 | int ans[] = new int[n / 2]; 12 | for (int i = 0; i < n; i += 4) { 13 | if (arr[i] >= arr[i + 1]) { 14 | ans[i / 2 + 1] = arr[i + 1]; 15 | ans[i / 2] = arr[i]; 16 | } else { 17 | ans[i / 2 + 1] = arr[i]; 18 | ans[i / 2] = arr[i + 1]; 19 | } 20 | 21 | if (arr[i + 2] >= arr[i + 3]) { 22 | ans[i / 2 + 1] += arr[i + 2]; 23 | ans[i / 2] += arr[i + 3]; 24 | } else { 25 | ans[i / 2 + 1] += arr[i + 3]; 26 | ans[i / 2] += arr[i + 2]; 27 | } 28 | } 29 | return findMaxPoint(ans, n / 2); 30 | } 31 | 32 | public static void main(String[] args) { 33 | Scanner sc = new Scanner(System.in); 34 | int num = sc.nextInt(); 35 | int n = (int) Math.pow(2, num); 36 | int arr[] = new int[n]; 37 | for (int i = 0; i < n; i++) { 38 | arr[i] = sc.nextInt(); 39 | } 40 | System.out.println(findMaxPoint(arr, n)); 41 | 42 | } 43 | } -------------------------------------------------------------------------------- /Recursion new - post class/Factorualrecursion.cpp: -------------------------------------------------------------------------------- 1 | #include // header file includes every Standard library 2 | using namespace std; 3 | 4 | int factorial( int n) 5 | { 6 | if (n == 0) 7 | return 1; 8 | return n * factorial(n - 1); 9 | } 10 | 11 | 12 | int main() 13 | { 14 | int num ; 15 | cin>>num; 16 | cout << factorial(num) << endl; 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Recursion new - post class/FibnocciNumbers.java: -------------------------------------------------------------------------------- 1 | static long Fibonacci(long n) 2 | { 3 | if (n <= 1) 4 | return n; 5 | return Fibonacci(n-1) + Fibonacci(n-2); 6 | } -------------------------------------------------------------------------------- /Recursion new - post class/Gameofexecution.cpp: -------------------------------------------------------------------------------- 1 | #include // header file includes every Standard library 2 | using namespace std; 3 | 4 | int josephus(int n, int k) 5 | { 6 | if (n == 1) 7 | return 1; 8 | else 9 | return (josephus(n - 1, k) + k - 1) % n + 1; 10 | } 11 | 12 | 13 | int main() 14 | { 15 | int t; 16 | cin>>t; 17 | for(int i=0;i>n>>k; 21 | cout<< josephus(n, k)< 5 | using namespace std; 6 | 7 | 8 | int findFirstNumIndex(int& k, int n) 9 | { 10 | 11 | if (n == 1) 12 | return 0; 13 | n--; 14 | 15 | int first_num_index; 16 | 17 | int n_partial_fact = n; 18 | 19 | while (k >= n_partial_fact 20 | && n > 1) { 21 | n_partial_fact 22 | = n_partial_fact 23 | * (n - 1); 24 | n--; 25 | } 26 | 27 | 28 | first_num_index = k / n_partial_fact; 29 | 30 | k = k % n_partial_fact; 31 | 32 | return first_num_index; 33 | } 34 | 35 | 36 | string findKthPermutation(int n, int k) 37 | { 38 | 39 | string ans = ""; 40 | 41 | set s; 42 | 43 | 44 | 45 | for (int i = 1; i <= n; i++) 46 | s.insert(i); 47 | 48 | set::iterator itr; 49 | 50 | 51 | itr = s.begin(); 52 | 53 | k = k - 1; 54 | 55 | for (int i = 0; i < n; i++) { 56 | 57 | int index 58 | = findFirstNumIndex(k, n - i); 59 | 60 | advance(itr, index); 61 | 62 | ans += (to_string(*itr)); 63 | 64 | 65 | s.erase(itr); 66 | 67 | itr = s.begin(); 68 | } 69 | return ans; 70 | } 71 | 72 | // Driver code 73 | int main() 74 | { 75 | 76 | int n , k; 77 | cin>>n>>k; 78 | 79 | string kth_perm_seq 80 | = findKthPermutation(n, k); 81 | 82 | cout << kth_perm_seq << endl; 83 | 84 | return 0; 85 | } -------------------------------------------------------------------------------- /Recursion new - post class/PalindromeRecursive.java: -------------------------------------------------------------------------------- 1 | static boolean check_Palindrome(String str,int s, int e) 2 | { 3 | if (s == e) 4 | return true; 5 | 6 | if ((str.charAt(s)) != (str.charAt(e))) 7 | return false; 8 | 9 | if (s < e + 1) 10 | return check_Palindrome(str, s + 1, e - 1); 11 | 12 | return true; 13 | } -------------------------------------------------------------------------------- /Recursion new - post class/PrintPattern.java: -------------------------------------------------------------------------------- 1 | static void printPattern(int n, int m, boolean flag) 2 | { 3 | 4 | 5 | System.out.print(m + " "); 6 | if (flag == false && n == m) 7 | return; 8 | 9 | if (flag) { 10 | 11 | 12 | if (m - 5 > 0) 13 | printPattern(n, m - 5, true); 14 | 15 | else 16 | printPattern(n, m - 5, false); 17 | } 18 | 19 | else 20 | printPattern(n, m + 5, false); 21 | } -------------------------------------------------------------------------------- /Recursion new - post class/Singledigit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int digSum(int n) 6 | { 7 | int sum = 0; 8 | 9 | while(n > 0 || sum > 9) 10 | { 11 | if(n == 0) 12 | { 13 | n = sum; 14 | sum = 0; 15 | } 16 | sum += n % 10; 17 | n /= 10; 18 | } 19 | return sum; 20 | } 21 | 22 | int main() 23 | { 24 | int n; 25 | cin>>n; 26 | cout << digSum(n); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Recursion new - post class/Subsets.cpp: -------------------------------------------------------------------------------- 1 | #include // header file includes every Standard library 2 | using namespace std; 3 | void func(int [],int ,int ,set>&); 4 | int main() { 5 | int t; 6 | cin>>t; 7 | while(t--) 8 | { 9 | int n,i,pow_set; 10 | cin>>n; 11 | int arr[n]; 12 | 13 | for(i=0;i>arr[i]; 16 | 17 | pow_set=pow(2,n); 18 | 19 | set>s; 20 | 21 | func(arr,n,pow_set,s); 22 | 23 | set>::iterator itr; 24 | 25 | cout<<"()"; 26 | 27 | for(itr=s.begin();itr!=s.end();itr++) 28 | 29 | { 30 | 31 | vectorv; 32 | 33 | v=*itr; 34 | 35 | cout<<"("; 36 | 37 | for(i=0;i>&s) 57 | 58 | { 59 | 60 | int i,j; 61 | 62 | vectorv; 63 | 64 | for(i=0;i 2 | using namespace std; 3 | 4 | void towerOfHanoi(int n, char from_rod,char to_rod, char aux_rod) 5 | { 6 | if (n == 1) 7 | { 8 | cout<" << to_rod<" << to_rod << endl; 13 | towerOfHanoi(n - 1, aux_rod, to_rod, from_rod); 14 | } 15 | 16 | 17 | int main() 18 | { 19 | int n; 20 | cin>>n; 21 | towerOfHanoi(n, 'A', 'C', 'B'); 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Recursion new - post class/printAllSubarray.java: -------------------------------------------------------------------------------- 1 | static void PrintSubarrays(int arr[], int n){ 2 | 3 | for (int i=0; i 2 | using namespace std; 3 | 4 | long long numOfWays(int n, int m); 5 | 6 | int main() { 7 | 8 | int t; 9 | cin >> t; 10 | while (t--) { 11 | int n, m; 12 | cin >> n >> m; 13 | cout << numOfWays(n, m) << endl; 14 | } 15 | return 0; 16 | } 17 | 18 | 19 | long long numOfWays(int N, int M) 20 | { 21 | long long ans=0; 22 | long long k=N*M; 23 | int mod=1000000007; 24 | for(int i=0;i=0) 30 | c++; 31 | if(i+2=0) 34 | c++; 35 | if(j-2>=0&&i+1=0&&i-1>=0) 38 | c++; 39 | if(i-2>=0&&j+1=0&&j-1>=0) 42 | c++; 43 | ans=((ans+k)%mod-(c+1)%mod+mod)%mod; 44 | } 45 | } 46 | return ans; 47 | } -------------------------------------------------------------------------------- /Recursion(new) - Post Class/Factorualrecursion.cpp: -------------------------------------------------------------------------------- 1 | #include // header file includes every Standard library 2 | using namespace std; 3 | 4 | int factorial( int n) 5 | { 6 | if (n == 0) 7 | return 1; 8 | return n * factorial(n - 1); 9 | } 10 | 11 | 12 | int main() 13 | { 14 | int num ; 15 | cin>>num; 16 | cout << factorial(num) << endl; 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Recursion(new) - Post Class/InsertOperator.java: -------------------------------------------------------------------------------- 1 | import java.io.*; // for handling input/output 2 | import java.util.*; // contains Collections framework 3 | 4 | class Main { 5 | public static int rec(long [] arr,long sum,int i) 6 | { 7 | if(i==arr.length){ 8 | if(sum==0) 9 | return 0; 10 | 11 | else 12 | return 1;} 13 | return rec(arr,sum-arr[i],i+1)*rec(arr,sum+arr[i],i+1); 14 | 15 | } 16 | 17 | public static void main (String[] args) { 18 | Scanner sc=new Scanner(System.in); 19 | int n=sc.nextInt(); 20 | long target=sc.nextLong(); 21 | long [] arr=new long[n]; 22 | for(int i=0;i 0) { 42 | curr.next = new Node(carry); 43 | } 44 | return dummyHead.next; 45 | } -------------------------------------------------------------------------------- /Single Linked List - Post Class/Add_Two_Numbers.java: -------------------------------------------------------------------------------- 1 | /* 2 | class Node { 3 | Node next; 4 | int data; 5 | Node(int data) { 6 | this.data = data; 7 | next = null; 8 | } 9 | } 10 | */ 11 | public static Node addNumber(Node l1, Node l2) { 12 | Node dummyHead = new Node(0); 13 | Node p = l1, q = l2, curr = dummyHead; 14 | int carry = 0; 15 | while (p != null || q != null) { 16 | int x = (p != null) ? p.data : 0; 17 | int y = (q != null) ? q.data: 0; 18 | int sum = carry + x + y; 19 | carry = sum / 10; 20 | curr.next = new Node(sum % 10); 21 | curr = curr.next; 22 | if (p != null) p = p.next; 23 | if (q != null) q = q.next; 24 | } 25 | if (carry > 0) { 26 | curr.next = new Node(carry); 27 | } 28 | return dummyHead.next; 29 | } -------------------------------------------------------------------------------- /Single Linked List - Post Class/Merge_two_sorted_linked_list.java: -------------------------------------------------------------------------------- 1 | /* 2 | class Node { 3 | Node next; 4 | int val; 5 | Node(int val) { 6 | this.val = val; 7 | next = null; 8 | } 9 | } 10 | */ 11 | /* 12 | below function is used to insert nodes in the linked list 13 | public static Node insert111(Node head, int val) { 14 | if(head == null) { 15 | return new Node(val); 16 | } else { 17 | Node cur; 18 | cur = insert111(head.next, val); 19 | head.next = cur; 20 | return head; 21 | } 22 | } 23 | */ 24 | 25 | static Node h3, e3; 26 | 27 | //1, 2, 3, 4, 5 28 | //h3 = null, e3 = null 29 | //h3 -> [(1, address(2)) -> e3 -> (2, null)] 30 | public static Node insertEnd(int val){ 31 | if(h3 == null){ 32 | h3 = new Node(val); 33 | e3 = h3; 34 | } 35 | else{ 36 | Node newnode = new Node(val); //(2, null) 37 | e3.next = newnode; 38 | e3 = e3.next; 39 | } 40 | return h3; 41 | } 42 | 43 | 44 | public static Node Merge (Node h1, Node h2){ 45 | h3 = null; 46 | e3 = null; 47 | while(h1!=null && h2!=null){ 48 | if(h1.val <= h2.val){ 49 | h3 = insertEnd(h1.val); 50 | h1 = h1.next; 51 | } 52 | else{ 53 | h3 = insertEnd(h2.val); 54 | h2 = h2.next; 55 | } 56 | } 57 | while(h1 != null){ 58 | h3 = insertEnd(h1.val); 59 | h1 = h1.next; 60 | } 61 | while(h2 != null){ 62 | h3 = insertEnd(h2.val); 63 | h2 = h2.next; 64 | } 65 | return h3; 66 | } -------------------------------------------------------------------------------- /Single Linked List - Post Class/Nth_node_from_end_of_linked_list.java: -------------------------------------------------------------------------------- 1 | /* 2 | // information about the class Node 3 | class Node 4 | { 5 | int data; 6 | Node next; 7 | Node(int d) 8 | { 9 | data = d; 10 | next = null; 11 | } 12 | } 13 | */ 14 | static int getNthFromLast(Node head, int n) 15 | { 16 | // Your code here 17 | Node first = head; 18 | Node second = head; 19 | for(int i=0;i 0){ 19 | end = end.next; 20 | } 21 | if(end == null){ 22 | start = start.next; 23 | return start; 24 | } 25 | 26 | while(end.next != null){ 27 | start = start.next; 28 | end = end.next; 29 | } 30 | 31 | //create link between s.next and s.next.next 32 | if(start.next.next == null){ 33 | //deleting element; 34 | start.next = null; 35 | return head; 36 | } 37 | 38 | start.next = start.next.next; 39 | 40 | return head; 41 | 42 | } -------------------------------------------------------------------------------- /Single Linked List [In class]/Delete_the_middle_of_the_Linked_LIst.java: -------------------------------------------------------------------------------- 1 | /* 2 | class Node { 3 | Node next; 4 | int val; 5 | 6 | Node(int val) { 7 | this.val = val; 8 | next = null; 9 | } 10 | } 11 | */ 12 | 13 | 14 | public static Node deleteMiddleElement(Node head) { 15 | int cnt = 0; 16 | Node temp = head; 17 | while (temp != null) { 18 | cnt++; 19 | temp = temp.next; 20 | } 21 | if (cnt == 1) { 22 | head.val = -1; 23 | return head; 24 | } 25 | if (cnt == 2) { 26 | head.next = null; 27 | return head; 28 | } 29 | temp = head; 30 | int I = 1; 31 | cnt = cnt / 2; 32 | while (I != cnt) { 33 | temp = temp.next; 34 | I++; 35 | } 36 | temp.next = temp.next.next; 37 | return head; 38 | } -------------------------------------------------------------------------------- /Single Linked List [In class]/Insert_Node_At_The_Head.java: -------------------------------------------------------------------------------- 1 | /* 2 | class Node { 3 | Node next; 4 | int val; 5 | 6 | Node(int val) { 7 | this.val = val; 8 | next = null; 9 | } 10 | } 11 | */ 12 | 13 | public static Node addElement(Node head, int new_data) { 14 | //enter your code here 15 | Node new_node = new Node(new_data); 16 | new_node.next = head; 17 | head = new_node; 18 | 19 | return head; 20 | } -------------------------------------------------------------------------------- /Single Linked List [In class]/Insert_Node_At_The_Tail.java: -------------------------------------------------------------------------------- 1 | /* 2 | class Node { 3 | Node next; 4 | int val; 5 | 6 | Node(int val) { 7 | this.val = val; 8 | next = null; 9 | } 10 | } 11 | */ 12 | 13 | 14 | public static Node addElement(Node head, int data) { 15 | Node temp = head; 16 | if(temp == null){ 17 | head = new Node(data); 18 | return head; 19 | } 20 | while(temp.next != null){ 21 | temp = temp.next; 22 | } 23 | temp.next = new Node(data); 24 | return head; 25 | } -------------------------------------------------------------------------------- /Single Linked List [In class]/Insert_Node_at_given_Position.java: -------------------------------------------------------------------------------- 1 | /* 2 | class Node { 3 | Node next; 4 | int val; 5 | 6 | Node(int val) { 7 | this.val = val; 8 | next = null; 9 | } 10 | } 11 | */ 12 | 13 | public static Node addElement(Node head, int data ,int position){ 14 | //enter your code here 15 | position=position-1; 16 | Node trackedHeadNode = head; 17 | 18 | Node nodeToInsert = new Node(data); 19 | if(position == 0){nodeToInsert.next = head; 20 | return nodeToInsert;} 21 | 22 | int currPosition = 0; 23 | while (currPosition < position -1 && head.next != null){ 24 | head = head.next; 25 | currPosition++; 26 | } 27 | 28 | 29 | Node nodeAtPosition = head.next; 30 | head.next = nodeToInsert; 31 | head = head.next; 32 | head.next = nodeAtPosition; 33 | 34 | return trackedHeadNode; 35 | 36 | } -------------------------------------------------------------------------------- /Single Linked List [In class]/Palindrome_LIst.java: -------------------------------------------------------------------------------- 1 | /* 2 | class Node { 3 | Node next; 4 | int val; 5 | 6 | Node(int val) { 7 | this.val = val; 8 | next = null; 9 | } 10 | } 11 | */ 12 | 13 | public static boolean IsPalindrome(Node head) { 14 | if(head == null) 15 | return true; 16 | 17 | Node p = head; 18 | Node prev = new Node(head.val); 19 | 20 | while(p.next != null){ 21 | Node temp = new Node(p.next.val); 22 | temp.next = prev; 23 | prev = temp; 24 | p = p.next; 25 | } 26 | 27 | Node p1 = head; 28 | Node p2 = prev; 29 | 30 | while(p1!=null){ 31 | if(p1.val != p2.val) 32 | return false; 33 | 34 | p1 = p1.next; 35 | p2 = p2.next; 36 | } 37 | 38 | return true; 39 | } -------------------------------------------------------------------------------- /Single Linked List [In class]/Print_The_Linked_LIst.java: -------------------------------------------------------------------------------- 1 | /* 2 | class Node { 3 | Node next; 4 | int val; 5 | 6 | Node(int val) { 7 | this.val = val; 8 | next = null; 9 | } 10 | } 11 | // */ 12 | public static void printList(Node head) { 13 | Node currNode = head; 14 | while (currNode != null) { 15 | System.out.print(currNode.val + " "); 16 | currNode = currNode.next; 17 | } 18 | } 19 | // This Code Contributed By Chauhan Abhishek -------------------------------------------------------------------------------- /Single Linked List [In class]/Reversing_The_Linked_LIst.java: -------------------------------------------------------------------------------- 1 | /* 2 | // information about the class Nodeclass Node { 3 | Node next; 4 | int val; 5 | 6 | Node(int val) { 7 | this.val = val; 8 | next = null; 9 | } 10 | } 11 | */ 12 | 13 | 14 | 15 | public static Node ReverseLinkedList(Node head) { 16 | 17 | Node prev = null; 18 | Node current = head; 19 | Node next = null; 20 | while (current != null) { 21 | next = current.next; 22 | current.next = prev; 23 | prev = current; 24 | current = next; 25 | } 26 | head = prev; 27 | return head; 28 | 29 | } -------------------------------------------------------------------------------- /Single Linked List [In class]/Zero_at_front_of_linked_List.java: -------------------------------------------------------------------------------- 1 | /* 2 | class Node{ 3 | int data; 4 | Node next; 5 | Node(int d){ 6 | data=d; 7 | next=null; 8 | } 9 | } 10 | */ 11 | static public Node moveZeroes(Node head) 12 | { 13 | Node temp=head.next; 14 | Node prev=head; 15 | while(temp!=null){ 16 | if(temp.data==0){ 17 | Node curr = temp; 18 | temp = temp.next; 19 | prev.next = temp; 20 | curr.next=head; 21 | head=curr; 22 | } 23 | else{ 24 | prev=temp; 25 | temp=temp.next; 26 | } 27 | } 28 | return head; 29 | // Your code here 30 | } -------------------------------------------------------------------------------- /Single Linked List [post class]/Add_two_Numbers.java: -------------------------------------------------------------------------------- 1 | /* 2 | class Node { 3 | Node next; 4 | int data; 5 | 6 | Node(int data) { 7 | this.data = data; 8 | next = null; 9 | } 10 | } 11 | */ 12 | public static Node addNumber(Node l1, Node l2) { 13 | Node dummyHead = new Node(0); 14 | Node p = l1, q = l2, curr = dummyHead; 15 | int carry = 0; 16 | while (p != null || q != null) { 17 | int x = (p != null) ? p.data : 0; 18 | int y = (q != null) ? q.data: 0; 19 | int sum = carry + x + y; 20 | carry = sum / 10; 21 | curr.next = new Node(sum % 10); 22 | curr = curr.next; 23 | if (p != null) p = p.next; 24 | if (q != null) q = q.next; 25 | } 26 | if (carry > 0) { 27 | curr.next = new Node(carry); 28 | } 29 | return dummyHead.next; 30 | } -------------------------------------------------------------------------------- /Single Linked List [post class]/Merge_two_sorted_Linked_List.java: -------------------------------------------------------------------------------- 1 | /* 2 | class Node { 3 | Node next; 4 | int val; 5 | 6 | Node(int val) { 7 | this.val = val; 8 | next = null; 9 | } 10 | } 11 | */ 12 | /* 13 | below function is used to insert nodes in the linked list 14 | public static Node insert111(Node head, int val) { 15 | if(head == null) { 16 | return new Node(val); 17 | } else { 18 | Node cur; 19 | cur = insert111(head.next, val); 20 | head.next = cur; 21 | return head; 22 | } 23 | } 24 | 25 | */ 26 | 27 | static Node h3, e3; 28 | 29 | //1, 2, 3, 4, 5 30 | //h3 = null, e3 = null 31 | //h3 -> [(1, address(2)) -> e3 -> (2, null)] 32 | public static Node insertEnd(int val){ 33 | if(h3 == null){ 34 | h3 = new Node(val); 35 | e3 = h3; 36 | } 37 | else{ 38 | Node newnode = new Node(val); //(2, null) 39 | e3.next = newnode; 40 | e3 = e3.next; 41 | } 42 | return h3; 43 | } 44 | 45 | 46 | public static Node Merge (Node h1, Node h2){ 47 | h3 = null; 48 | e3 = null; 49 | while(h1!=null && h2!=null){ 50 | if(h1.val <= h2.val){ 51 | h3 = insertEnd(h1.val); 52 | h1 = h1.next; 53 | } 54 | else{ 55 | h3 = insertEnd(h2.val); 56 | h2 = h2.next; 57 | } 58 | } 59 | while(h1 != null){ 60 | h3 = insertEnd(h1.val); 61 | h1 = h1.next; 62 | } 63 | while(h2 != null){ 64 | h3 = insertEnd(h2.val); 65 | h2 = h2.next; 66 | } 67 | return h3; 68 | } -------------------------------------------------------------------------------- /Single Linked List [post class]/Nth_Node_FRom_End_of_Linked_list.java: -------------------------------------------------------------------------------- 1 | /* 2 | // information about the class Node 3 | class Node 4 | { 5 | int data; 6 | Node next; 7 | Node(int d) 8 | { 9 | data = d; 10 | next = null; 11 | } 12 | } 13 | */ 14 | static int getNthFromLast(Node head, int n) 15 | { 16 | // Your code here 17 | Node first = head; 18 | Node second = head; 19 | for(int i=0;i 0) { 44 | curr.next = new Node(carry); 45 | } 46 | return dummyHead.next; 47 | } -------------------------------------------------------------------------------- /Sliding window - In Class/Max-window-sum.java: -------------------------------------------------------------------------------- 1 | import java.io.*; // for handling input/output 2 | import java.util.*; // contains Collections framework 3 | 4 | // don't change the name of this class 5 | // you can add inner classes if needed 6 | class Main { 7 | public static void main (String[] args) { 8 | Scanner sc = new Scanner(System.in); 9 | int n = sc.nextInt(); 10 | int k= sc.nextInt(); 11 | int[] arr = new int[n]; 12 | for(int i = 0; i hs = new HashSet<>(); 13 | 14 | for(int i=0; i k){ 19 | sum -= arr[i-k]; 20 | count--; 21 | } 22 | 23 | if(count == k){ 24 | int check = s - sum; 25 | if(hs.contains(check)){ 26 | System.out.println("YES"); 27 | flag = true; 28 | break; 29 | } 30 | hs.add(sum); 31 | } 32 | } 33 | if(!flag){ 34 | System.out.println("NO"); 35 | } 36 | } 37 | public static void main (String[] args) { 38 | Scanner sc = new Scanner(System.in); 39 | int n = sc.nextInt(); 40 | int k = sc.nextInt(); 41 | int s = sc.nextInt(); 42 | 43 | int arr[] = new int[n]; 44 | 45 | for(int i=0; i set = new HashSet<>(); 10 | HashSet res = new HashSet<>(); 11 | ArrayList list = new ArrayList<>(); 12 | for(int i=0; i<=n-10 ; i++){ 13 | String result = input.substring(i, i+10); 14 | 15 | if(set.contains(result)){ 16 | res.add(result); 17 | } 18 | set.add(result); 19 | } 20 | for(String r : res){ 21 | list.add(r); 22 | } 23 | Collections.sort(list); 24 | for(String r : list){ 25 | System.out.println(r); 26 | } 27 | } 28 | public static void main (String[] args) { 29 | Scanner sc = new Scanner(System.in); 30 | String input = sc.next(); 31 | 32 | DNASequence(input); 33 | } 34 | } -------------------------------------------------------------------------------- /Sliding window - Post Class/Longest-Unique-Character-Substring.java: -------------------------------------------------------------------------------- 1 | import java.io.*; // for handling input/output 2 | import java.util.*; // contains Collections framework 3 | 4 | // don't change the name of this class 5 | // you can add inner classes if needed 6 | class Main { 7 | public static void main (String[] args) { 8 | Scanner sc = new Scanner(System.in); 9 | String str = sc.nextLine().trim(); 10 | HashMap hm = 11 | new HashMap(); 12 | 13 | int n = str.length(); 14 | int result = 0; 15 | int start = 0; 16 | //O(n) Time Complexity 17 | for(int end = 0; end < n; end++){ 18 | if(hm.containsKey(str.charAt(end))){ 19 | start = Math.max(start, hm.get(str.charAt(end)) + 1); 20 | } 21 | hm.put(str.charAt(end), end); 22 | result = Math.max(result, end - start + 1); 23 | } 24 | System.out.print(result); 25 | } 26 | } -------------------------------------------------------------------------------- /Stack - In Class/Array_implementation_Of_Stack.java: -------------------------------------------------------------------------------- 1 | /* 2 | int top=-1; // index of current top .. 3 | int a[] // array which contain element of stack 4 | */ 5 | // function to add element in the stack 6 | //x=element to be added 7 | //size=maximum size of array 8 | 9 | void push(int x, int size) 10 | { 11 | if(top == size - 1){ 12 | System.out.println("Stack overflow"); 13 | return; 14 | } 15 | top++; 16 | a[top] = x; 17 | } 18 | 19 | // Function to pop element from stack 20 | void pop() 21 | { 22 | if (top == -1){ 23 | System.out.println("Stack underflow"); 24 | 25 | } 26 | else{ 27 | top--; 28 | } 29 | } 30 | 31 | void top() 32 | { 33 | if(top == -1) { 34 | System.out.println("Empty stack"); 35 | } 36 | else{ 37 | System.out.println(a[top]); 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /Stack - In Class/Greater_Is_Better.java: -------------------------------------------------------------------------------- 1 | import java.io.*; // for handling input/output 2 | import java.util.*; // contains Collections framework 3 | 4 | // don't change the name of this class 5 | // you can add inner classes if needed 6 | 7 | class Pair{ 8 | long first; 9 | long second; 10 | Pair(long first, long second){ 11 | this.first = first; 12 | this.second = second; 13 | } 14 | } 15 | 16 | class Main { 17 | public static void main (String[] args) { 18 | Scanner sc = new Scanner(System.in); 19 | int n = sc.nextInt(); 20 | Stack s = new Stack<>(); 21 | Stack s1 = new Stack<>(); 22 | 23 | long a[] = new long[n]; 24 | long b[] = new long[n]; 25 | 26 | for(int i = 0; i < n; i++){ 27 | a[i] = sc.nextLong(); 28 | while(!(s.empty())){ 29 | if(s.peek().first>a[i]){ 30 | break; 31 | } 32 | s.pop(); 33 | } 34 | if(s.empty()){ 35 | b[i]=-1l; 36 | } 37 | else{ 38 | b[i]=s.peek().second+1; 39 | } 40 | Pair p = new Pair(a[i], i); 41 | s.push(p); 42 | } 43 | 44 | for(int i=n-1;i>=0;i--){ 45 | while(!(s1.empty())){ 46 | if(s1.peek().first>a[i]){ 47 | break; 48 | } 49 | s1.pop(); 50 | } 51 | if(s1.empty()){ 52 | b[i]+=-1; 53 | } 54 | else{ 55 | b[i]+=s1.peek().second+1; 56 | } 57 | Pair p = new Pair(a[i], i); 58 | s1.push(p); 59 | } 60 | 61 | for(int i=0;i stk = new Stack<>(); 13 | String postfix = ""; 14 | 15 | for (int i = 0; i < inp.length(); i++) { 16 | Character ch = inp.charAt(i); 17 | if (Character.isWhitespace(ch)) { 18 | continue; 19 | } 20 | if (ch == '(') { 21 | stk.push(ch); 22 | } else if (Character.isLetter(ch) || Character.isDigit(ch)) { 23 | postfix += ch; 24 | } else { 25 | if (stk.isEmpty()) { 26 | stk.push(ch); 27 | } else { 28 | 29 | if (isClosedParanthesis(ch)) { 30 | while (!stk.isEmpty() && !isPairParanthesis(stk.peek() ,ch)) { 31 | postfix += stk.pop(); 32 | } 33 | stk.pop(); 34 | continue; 35 | } 36 | 37 | while (!stk.isEmpty() && stk.peek() != '(' && isCharhasHigherPrecendence(stk.peek(), ch)) { 38 | postfix += stk.pop(); 39 | } 40 | stk.push(ch); 41 | } 42 | } 43 | } 44 | while (!stk.isEmpty()) { 45 | postfix += stk.pop(); 46 | } 47 | return postfix; 48 | } 49 | 50 | private static boolean isPairParanthesis(Character open, Character closed) { 51 | if (open == '(' && closed ==')') { 52 | return true; 53 | } else if (open == '{' && closed == '}') { 54 | return true; 55 | } else if (open =='[' && closed == ']') { 56 | return true; 57 | } 58 | return false; 59 | } 60 | 61 | private static boolean isClosedParanthesis(Character ch) { 62 | return (ch == ')' || ch == '}' || ch == ']') ? true : false; 63 | } 64 | 65 | private static int getIntMapping(Character ch) { 66 | if (ch == '^') { 67 | return 3; 68 | } else if (ch == '*' || ch == '/') { 69 | return 2; 70 | } else if (ch == '+' || ch == '-') { 71 | return 1; 72 | } 73 | return 0; 74 | } 75 | 76 | private static boolean isCharhasHigherPrecendence(Character ts, Character ch) { 77 | return getIntMapping(ts) >= getIntMapping(ch); 78 | } 79 | } -------------------------------------------------------------------------------- /Stack - In Class/Maximum_of_minimum_of_every_window_size.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | vector maxOfMin(int array[], int n) 4 | { 5 | 6 | int size = n; 7 | vector res(size, 0); 8 | stack s; 9 | for (int i = 0; i < size; i++) 10 | { 11 | while ((!s.empty()) && (array[s.top()] >= array[i])) 12 | { 13 | int top = s.top(); 14 | s.pop(); 15 | int temp = (s.empty() ? i : i - s.top() - 1); 16 | res[temp - 1] = max(res[temp - 1], array[top]); 17 | } 18 | s.push(i); 19 | } 20 | while (!s.empty()) 21 | { 22 | int top = s.top(); 23 | s.pop(); 24 | int temp = (s.empty() ? size : size - s.top() - 1); 25 | res[temp - 1] = max(res[temp - 1], array[top]); 26 | } 27 | for (int i = size - 2; i >= 0; i--) 28 | { 29 | if(res[i] < res[i+1]){ 30 | res[i] = res[i+1]; 31 | } 32 | } 33 | return res; 34 | } 35 | int main() { 36 | 37 | int n; 38 | cin >> n; 39 | int a[n]; 40 | for (int i = 0; i < n; i++) cin >> a[i]; 41 | vector res = maxOfMin(a, n); 42 | for (int i : res) cout << i << " "; 43 | cout << endl; 44 | return 0; 45 | } -------------------------------------------------------------------------------- /Stack - In Class/Stack_implementation_using_LinkedList.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | class Node { 4 | Node next; 5 | int val; 6 | 7 | Node(int val) { 8 | this.val = val; 9 | next = null; 10 | 11 | } 12 | } 13 | */ 14 | 15 | Node top = null; 16 | public void push(int x){ 17 | //enter your code here 18 | Node nd = new Node(x); 19 | nd.next = top; 20 | top = nd; 21 | } 22 | 23 | public void pop(){ 24 | //enter your code here 25 | if(top == null){ 26 | return; 27 | } 28 | else{ 29 | top = top.next; 30 | } 31 | } 32 | 33 | public void top(){ 34 | //enter your code here 35 | if(top == null){ 36 | System.out.println(0); 37 | } 38 | else{ 39 | System.out.println(top.val); 40 | } 41 | } -------------------------------------------------------------------------------- /Stack - In Class/SubArray_Minima.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define int long long 4 | int sumSubarrayMins(int A[], int n) 5 | { 6 | int left[n], right[n]; 7 | 8 | stack > s1, s2; 9 | 10 | for (int i = 0; i < n; ++i) { 11 | int cnt = 1; 12 | 13 | while (!s1.empty() && (s1.top().first) > A[i]) { 14 | cnt += s1.top().second; 15 | s1.pop(); 16 | } 17 | 18 | s1.push({ A[i], cnt }); 19 | left[i] = cnt; 20 | } 21 | 22 | 23 | for (int i = n - 1; i >= 0; --i) { 24 | int cnt = 1; 25 | 26 | 27 | while (!s2.empty() && (s2.top().first) >= A[i]) { 28 | cnt += s2.top().second; 29 | s2.pop(); 30 | } 31 | 32 | s2.push({ A[i], cnt }); 33 | right[i] = cnt; 34 | } 35 | 36 | int result = 0; 37 | 38 | for (int i = 0; i < n; ++i) 39 | result = (result + A[i] * left[i] * right[i]); 40 | 41 | return result; 42 | } 43 | 44 | 45 | int32_t main() 46 | { 47 | int n; 48 | cin>>n; 49 | int arr[n]; 50 | for(int i=0;i>arr[i]; 52 | } 53 | 54 | 55 | cout << sumSubarrayMins(arr, n); 56 | 57 | return 0; 58 | } -------------------------------------------------------------------------------- /Stack - Post Class/Lexo-Sequence.java: -------------------------------------------------------------------------------- 1 | import java.io.*; // for handling input/output 2 | import java.util.*; // contains Collections framework 3 | 4 | // don't change the name of this class 5 | // you can add inner classes if needed 6 | class Main { 7 | public static void main (String[] args) { 8 | Scanner sc = new Scanner(System.in); 9 | int n = sc.nextInt(); 10 | int k = sc.nextInt(); 11 | long arr[] = new long[n]; 12 | for(int i=0;i st = new Stack<>(); 16 | long ans[]=new long[k]; 17 | boolean f=true; 18 | for(int i=0;i=k){ 24 | st.pop(); 25 | } 26 | if(st.isEmpty()==true || st.size()=0;i--){ 41 | System.out.print(ans[i]+" "); 42 | } // Your code here 43 | } 44 | } -------------------------------------------------------------------------------- /Stack - Post Class/Longest_valid_Parenthesis.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int findMaxLen(string str) 5 | { 6 | int n = str.length(); 7 | stack stk; 8 | stk.push(-1); 9 | int result = 0; 10 | 11 | for (int i = 0; i < n; i++) 12 | { 13 | if (str[i] == '(') 14 | stk.push(i); 15 | 16 | 17 | else 18 | { 19 | 20 | if (!stk.empty()) 21 | { 22 | stk.pop(); 23 | } 24 | 25 | 26 | if (!stk.empty()) 27 | result = max(result, i - stk.top()); 28 | 29 | else 30 | stk.push(i); 31 | } 32 | } 33 | 34 | return result; 35 | } 36 | int main() 37 | { 38 | string str; 39 | cin>>str; 40 | cout << findMaxLen(str) << endl; 41 | 42 | 43 | return 0; 44 | } -------------------------------------------------------------------------------- /Stack - Post Class/Stack_Operation.java: -------------------------------------------------------------------------------- 1 | public static void push(Stack st, int x) 2 | { 3 | st.push(x); 4 | } 5 | 6 | // Function to pop element from stack 7 | public static void pop(Stack st) 8 | { 9 | if(!st.empty()) 10 | st.pop(); 11 | } 12 | 13 | public static void top(Stack st) 14 | { 15 | if(!st.empty()){ 16 | System.out.println(st.peek()); 17 | } 18 | else{ 19 | System.out.println(0); 20 | } 21 | } -------------------------------------------------------------------------------- /Stack - Post Class/String-Stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N = 2e6 + 20; 4 | string s, t, u, a; 5 | 6 | int b[30]; 7 | int pos[30]; 8 | int h[N]; 9 | stack sta; 10 | int main() 11 | { 12 | while (cin >> s) 13 | { 14 | int len = s.length(); 15 | memset(b, 0, sizeof(b)); 16 | 17 | for (int i = 0; s[i]; i++) 18 | b[s[i] - 'a']++; 19 | int i = 0, k; 20 | while (s[i]) 21 | { 22 | int k1 = 26; 23 | if (!sta.empty()) 24 | k1 = sta.top() - 'a'; 25 | for (k = 0; k < 26; k++) 26 | { 27 | if (b[k]) 28 | break; 29 | } 30 | k = min(k1, k); 31 | while (true) 32 | { 33 | if (!sta.empty() && sta.top() - 'a' == k) 34 | break; 35 | b[s[i] - 'a']--; 36 | sta.push(s[i++]); 37 | if (s[i] == '\0') 38 | break; 39 | } 40 | if (!sta.empty()) 41 | { 42 | printf("%c", sta.top()); 43 | sta.pop(); 44 | } 45 | } 46 | while (!sta.empty()) 47 | { 48 | char c = sta.top(); 49 | sta.pop(); 50 | printf("%c", c); 51 | } 52 | cout << endl; 53 | } 54 | return 0; 55 | } -------------------------------------------------------------------------------- /Two Pointers - In Class/Merge-Array.java: -------------------------------------------------------------------------------- 1 | /* Author = Chauhan Abhishek */ 2 | /* Codechef Id = https://www.codechef.com/users/abhishekchauha */ 3 | /* Codeforces Id = https://codeforces.com/profile/abhishekchauhan903 */ 4 | /* Newton School Id = https://my.newtonschool.co/user/abhishekchauhan9036/ */ 5 | /* GitHub Id = https://github.com/AbhishekChauhan9036 */ 6 | /* Language = C++ */ 7 | /* Address = Ballia,UP,INDIA (221716)*/ 8 | 9 | /******************************************************************************************************************************************/ 10 | /***********Start****************/ 11 | import java.util.*; 12 | import java.io.*; 13 | class Solution{ 14 | public static void merge(long arr1[], long arr2[], int n, int m) 15 | { 16 | int i=0, j=0, k = n-1; 17 | while(i <= k && j < m){ 18 | if(arr1[i] < arr2[j]){ 19 | i++; 20 | } 21 | else{ 22 | long temp = arr2[j]; 23 | arr2[j] = arr1[k]; 24 | arr1[k] = temp; 25 | j++; 26 | k--; 27 | 28 | } 29 | } 30 | Arrays.sort(arr1); 31 | Arrays.sort(arr2); 32 | } 33 | } 34 | public class Main{ 35 | public static void main (String[] args) throws IOException { 36 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 37 | String inputLine[] = br.readLine().trim().split(" "); 38 | int n = Integer.parseInt(inputLine[0]); 39 | int m = Integer.parseInt(inputLine[1]); 40 | long arr1[] = new long[n]; 41 | long arr2[] = new long[m]; 42 | inputLine = br.readLine().trim().split(" "); 43 | for(int i=0; i 0) 10 | { 11 | String S = read.readLine(); 12 | 13 | Solution ob = new Solution(); 14 | System.out.println(ob.longestSubstrDistinctChars(S)); 15 | } 16 | } 17 | } 18 | 19 | class Solution{ 20 | static int longestSubstrDistinctChars(String S){ 21 | HashMapmap=new HashMap<>(); 22 | int count=0,max=0; 23 | for(int i=0;i 0){ 33 | int n = Integer.parseInt(br.readLine()); 34 | int arr[] = new int[n]; 35 | String srr[] = br.readLine().trim().split(" "); 36 | for(int i = 0; i < n; ++i) 37 | arr[i] = Integer.parseInt(srr[i]); 38 | 39 | sortedSquares(arr); 40 | 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /Weekday Contest - May 2022/A-Boolean-Matrix-Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int32_t main() { 4 | int t; 5 | cin>>t; 6 | int row,col; 7 | 8 | for(int i=1;i<=t;i++) 9 | { 10 | cin>>row; 11 | cin>>col; 12 | int m[row][col]; 13 | 14 | for(int k=0;k>m[k][j]; 19 | } 20 | } 21 | for(int k=0;k