├── Add Code Here ├── DART │ └── dart.txt ├── kotlin │ └── kotlin.kt ├── JavaScript │ ├── javascript.js │ ├── Pascal's Triangle.js │ ├── Longest Common Prefix.js │ ├── schemeRandomNota.js │ ├── linearRegressionInJS.js │ ├── kmalhotra08 basketball game │ └── top three repeated numbers.js ├── Java │ ├── README.md │ ├── Monster.java │ ├── Animmal_Main.java │ ├── Main.java │ ├── Project_Main.java │ ├── DateTest.java │ ├── Ford.java │ ├── Truck.java │ ├── Car.java │ ├── Sedan.java │ ├── Animal.java │ ├── MyOwnAutoShop.java │ ├── Item.java │ ├── TestClass_SavingsAccount.java │ ├── SavingsAccount.java │ ├── travelling_Salesman.java │ ├── ProjectClass.java │ ├── Shape.java │ ├── checkstringsanagram.java │ ├── binary_search.java │ ├── Date.java │ ├── Item_Monster_Main.java │ ├── QuickSort.java │ └── MergeSort.java ├── Python │ ├── README.md │ ├── Python Macro Programmer │ │ ├── Macro.json │ │ └── Macro_Programmer.py │ ├── Differential Equation Solver │ │ ├── README.md │ │ ├── Euler.py │ │ ├── linear_function.py │ │ ├── Differential_Equation_Solver.py │ │ ├── testing_diff_eq.py │ │ ├── Euler_Caushy.py │ │ └── Runge_Kutta.py │ ├── password_generator.py │ ├── NthPrime.py │ ├── k_nearest_neighbors.py │ ├── Column_name_from_given_column_number.py │ ├── word Scramble.py │ ├── L2_regularization_Implementation_from_scratch.py │ ├── Logistic_regression_implementation_from_scratch.py │ ├── Linear_Regression_Implementation_from_scratch.py │ ├── Tic_Tac_Toe.py │ ├── quick sort.py │ ├── Rock-Paper-Scissor.py │ ├── Sudoku.py │ └── deepneuralnetwork_mnist.py ├── .DS_Store ├── JAVA │ ├── .DS_Store │ ├── TreeTraversal.class │ ├── TreeTraversal$Node.class │ ├── TreeTraversal$BinaryTree.class │ ├── decoratorPattern │ │ ├── IPizza.java │ │ ├── IPizzaIngr.java │ │ ├── panBase.java │ │ ├── simpleBase.java │ │ ├── cheeseToppings.java │ │ ├── onionToppings.java │ │ └── mainClass.java │ ├── Check.java │ ├── Remove_element.java │ ├── Linked List Cycle.java │ ├── Happy Number.java │ ├── ExecuteBinarySearchOnAVector.java │ ├── Palindrome_Number.java │ ├── FactorialUsingRecursion.java │ ├── armstrong.java │ ├── kadane_algo.java │ ├── TwoSum.java │ ├── CheckBST.java │ ├── Employees.java │ ├── TrappingRainWater.java │ ├── Cyclic sort.java │ ├── MidElementDeletion.java │ ├── binarysearch.java │ ├── LeftViewOfBinaryTree.java │ ├── binary search.java │ ├── bubble_sort.java │ ├── linear_search.java │ ├── Selection_sort.java │ ├── kth _min_max_element │ ├── createdatabase_table.java │ ├── transpose.java │ ├── minValueNodeinBST.java │ ├── constructorOverloading.java │ ├── TernarySearch.java │ ├── intersection_linked_lists.java │ ├── Trafficlight.java │ ├── max_sum_of_array.java │ ├── ListNode.java │ ├── BucketSort.java │ ├── GnomeSort.java │ ├── banking.java │ ├── waveprint.java │ ├── area_shapes.java │ ├── Anagram.java │ ├── SubtreeDifference.java │ ├── RotateMatrixBy90Degrees.java │ ├── Find all possible Palindromic partitions of the given String .java │ ├── Reverse_queue.java │ ├── merge_sort.java │ ├── EvenAfterOddLL.java │ ├── DFS.java │ ├── tic_tac_toe.java │ ├── Radix.java │ ├── TreeTraversal.java │ ├── ReversePath.java │ ├── QuickSort.java │ ├── Details of students │ ├── InorderSuccessorInBinaryTree.java │ ├── minSubtree.java │ └── calculator.java └── CPP │ ├── Tower_of_Hanoi_using_Recursion.cpp │ ├── palindrome_number.cpp │ ├── Kadane's Algorithm │ ├── count_set_bits.cpp │ ├── (15)majorityele.cpp │ ├── Jump_Game.cpp │ ├── Best Time to Buy and Sell Stock │ ├── 2ndMaximumAmong3Numbers.c++ │ ├── Sieve of Eratosthenes.cpp │ ├── Factorial_of_a_Number_using_recursion.cpp │ ├── Swap Nodes in Pairs.cpp │ ├── array-of-vectors.cpp │ ├── decode_ways.cpp │ ├── Binary_search_usingrecursion.cpp │ ├── Check Armstrong Number of any number of digit.cpp │ ├── BubbleSort.cpp │ ├── Binary_search.cpp │ ├── 0-1 Knapsack │ ├── Check_if_a_queue_can_be_sorted_into_another_queue_using_a_stack.cpp │ ├── array1.cpp │ ├── ChocolateProblem.cpp │ ├── Cyclically rotate an array by one.cpp │ ├── Taylor_Series_using_recursion.cpp │ ├── Bit_Difference.cpp │ ├── Prime number within a given range │ ├── 2-Queue_using_Array_C++.cpp │ ├── Insertion_Sort.cpp │ ├── Bubble_Sort.cpp │ ├── cycle_detection_using_dsu.cpp │ ├── Selection_Sort.cpp │ ├── articulation_points.cpp │ ├── basic-math.cpp │ ├── countsort.cpp │ ├── Quick_Sort.cpp │ ├── Calculating Area of 2D shapes.cpp │ ├── add_two_numbers_represented_by_linked_lists.cpp │ ├── LinkedListCycle.cpp │ ├── Boolean_matrix.cpp │ ├── Merge_Sort.cpp │ ├── Heap_Sort.cpp │ ├── Boundary Traversal Matrix.cpp │ ├── CheckTwoNodesOnSamePath.cpp │ ├── sorting_techniques.cpp │ ├── Find all the N bit binary numbers having more than or equal 1’s than 0’s – C │ ├── Bitset.cpp │ └── Dijkstra's_Algo.cpp ├── static ├── images │ ├── content │ │ ├── participant │ │ │ ├── mohit2002.md │ │ │ └── Bhaskarblur.png │ │ └── .DS_Store │ ├── .DS_Store │ ├── AVDeshmukh.jpg │ ├── Bhaskarblur.png │ ├── InvisiblePro.png │ ├── avyay_jain.png │ └── NIGHTFURY2111.png └── .DS_Store ├── content └── participant │ ├── README.md │ ├── InvisiblePro.md │ └── joywin.md ├── .github ├── logo.png └── dependabot.yml ├── Participants ├── kmalhotra08 contributor ├── Divya.md └── Kalyani.md ├── Golang └── CONTRIBUTING.md /Add Code Here/DART/dart.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Add Code Here/kotlin/kotlin.kt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Add Code Here/JavaScript/javascript.js: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Add Code Here/Java/README.md: -------------------------------------------------------------------------------- 1 | # Java Codes 2 | -------------------------------------------------------------------------------- /Add Code Here/Python/README.md: -------------------------------------------------------------------------------- 1 | # Python Codes 2 | -------------------------------------------------------------------------------- /static/images/content/participant /mohit2002.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /content/participant /README.md: -------------------------------------------------------------------------------- 1 | added carry to repo @surabhhingade 2 | -------------------------------------------------------------------------------- /.github/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mohit-dhote/Codify-Platform/HEAD/.github/logo.png -------------------------------------------------------------------------------- /static/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mohit-dhote/Codify-Platform/HEAD/static/.DS_Store -------------------------------------------------------------------------------- /Participants/kmalhotra08 contributor: -------------------------------------------------------------------------------- 1 | -name: Karan 2 | -bio: student and big NBA fan 3 | -Country: USA 4 | -------------------------------------------------------------------------------- /Add Code Here/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mohit-dhote/Codify-Platform/HEAD/Add Code Here/.DS_Store -------------------------------------------------------------------------------- /static/images/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mohit-dhote/Codify-Platform/HEAD/static/images/.DS_Store -------------------------------------------------------------------------------- /Add Code Here/JAVA/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mohit-dhote/Codify-Platform/HEAD/Add Code Here/JAVA/.DS_Store -------------------------------------------------------------------------------- /static/images/AVDeshmukh.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mohit-dhote/Codify-Platform/HEAD/static/images/AVDeshmukh.jpg -------------------------------------------------------------------------------- /static/images/Bhaskarblur.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mohit-dhote/Codify-Platform/HEAD/static/images/Bhaskarblur.png -------------------------------------------------------------------------------- /static/images/InvisiblePro.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mohit-dhote/Codify-Platform/HEAD/static/images/InvisiblePro.png -------------------------------------------------------------------------------- /static/images/avyay_jain.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mohit-dhote/Codify-Platform/HEAD/static/images/avyay_jain.png -------------------------------------------------------------------------------- /Participants/Divya.md: -------------------------------------------------------------------------------- 1 | --- 2 | Name: Divya 3 | institution/company: Infosis, Pune 4 | github: https://github.com/Div 5 | --- 6 | -------------------------------------------------------------------------------- /static/images/NIGHTFURY2111.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mohit-dhote/Codify-Platform/HEAD/static/images/NIGHTFURY2111.png -------------------------------------------------------------------------------- /static/images/content/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mohit-dhote/Codify-Platform/HEAD/static/images/content/.DS_Store -------------------------------------------------------------------------------- /Add Code Here/JAVA/TreeTraversal.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mohit-dhote/Codify-Platform/HEAD/Add Code Here/JAVA/TreeTraversal.class -------------------------------------------------------------------------------- /Add Code Here/JAVA/TreeTraversal$Node.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mohit-dhote/Codify-Platform/HEAD/Add Code Here/JAVA/TreeTraversal$Node.class -------------------------------------------------------------------------------- /Add Code Here/JAVA/TreeTraversal$BinaryTree.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mohit-dhote/Codify-Platform/HEAD/Add Code Here/JAVA/TreeTraversal$BinaryTree.class -------------------------------------------------------------------------------- /static/images/content/participant /Bhaskarblur.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mohit-dhote/Codify-Platform/HEAD/static/images/content/participant /Bhaskarblur.png -------------------------------------------------------------------------------- /Add Code Here/JAVA/decoratorPattern/IPizza.java: -------------------------------------------------------------------------------- 1 | package decoratorPattern; 2 | 3 | public abstract class IPizza { 4 | 5 | public int cost() {return 0;} 6 | } 7 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/decoratorPattern/IPizzaIngr.java: -------------------------------------------------------------------------------- 1 | package decoratorPattern; 2 | 3 | public abstract class IPizzaIngr extends IPizza{ 4 | 5 | public int cost() {return 0;} 6 | } 7 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/decoratorPattern/panBase.java: -------------------------------------------------------------------------------- 1 | package decoratorPattern; 2 | 3 | public class panBase extends IPizza { 4 | 5 | @Override 6 | public int cost() { 7 | return 40; 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/decoratorPattern/simpleBase.java: -------------------------------------------------------------------------------- 1 | package decoratorPattern; 2 | 3 | public class simpleBase extends IPizza { 4 | 5 | @Override 6 | public int cost() { 7 | return 30; 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /Golang: -------------------------------------------------------------------------------- 1 | //provide the package main 2 | package main 3 | 4 | //import the packages 5 | import( 6 | "fmt" 7 | ) 8 | 9 | //Program execution starts here 10 | func main(){ 11 | 12 | //Print statement 13 | fmt.Println("Hello, World") 14 | } 15 | -------------------------------------------------------------------------------- /Add Code Here/Java/Monster.java: -------------------------------------------------------------------------------- 1 | /* 2 | This is the Sub Class of Item - Question Number 7 3 | */ 4 | public class Monster extends Item { 5 | public Monster(int Location, String Description ) 6 | { 7 | super(Location,Description); 8 | } 9 | 10 | 11 | } 12 | -------------------------------------------------------------------------------- /content/participant /InvisiblePro.md: -------------------------------------------------------------------------------- 1 | --- 2 | # InvisiblePro 3 | 4 | ### Location 5 | Gujarat, India 6 | 7 | ### Academics 8 | Gradw 10 9 | 10 | ### Interests 11 | Python 12 | AI 13 | OpenCV 14 | 15 | 16 | ### Profile Link 17 | 18 | Go to profile [InvisiblePro](https://github.com/InvisiblePro/) 19 | -------------------------------------------------------------------------------- /Add Code Here/Java/Animmal_Main.java: -------------------------------------------------------------------------------- 1 | public class Animmal_Main { 2 | public static void main(String[] args) { 3 | Dog dog = new Dog(); 4 | Cat cat = new Cat(); 5 | 6 | dog.makeSound(); 7 | dog.sleep(); 8 | 9 | cat.makeSound(); 10 | cat.sleep(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/decoratorPattern/cheeseToppings.java: -------------------------------------------------------------------------------- 1 | package decoratorPattern; 2 | 3 | public class cheeseToppings extends IPizzaIngr { 4 | 5 | 6 | IPizza pizza; 7 | public cheeseToppings(IPizza pizza) { 8 | this.pizza=pizza; 9 | } 10 | 11 | @Override 12 | public int cost() { 13 | return pizza.cost()+ 40; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/decoratorPattern/onionToppings.java: -------------------------------------------------------------------------------- 1 | package decoratorPattern; 2 | 3 | public class onionToppings extends IPizzaIngr { 4 | 5 | IPizza pizza; 6 | public onionToppings(IPizza pizza) { 7 | this.pizza=pizza; 8 | } 9 | 10 | @Override 11 | public int cost() { 12 | return pizza.cost()+ 20; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Participants/Kalyani.md: -------------------------------------------------------------------------------- 1 | ## Contributor 2 | 3 | - [Kalyani](https://github.com/kalyani2003) 4 | - Name: Kalyani Mhala 5 | - Bio: A passionate developer who loves contributing to open-source projects. 6 | - Institution/Company: PRMCEAM 7 | - GitHub: [Kalyani's GitHub Profile](https://github.com/kalyani2003) 8 | - LinkedIn: https://www.linkedin.com/in/kalyani-mhala-59647921b 9 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/Check.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | class Check 3 | { 4 | public static void main(String args[]) 5 | {Scanner sc=new Scanner(System.in); 6 | System.out.print("enter the no tobe checked"); 7 | int n=sc.nextInt(); 8 | if(n%2==0) 9 | System.out.println("EVEN"); 10 | else 11 | System.out.println("ODD"); 12 | sc.close(); 13 | 14 | } 15 | } -------------------------------------------------------------------------------- /Add Code Here/Java/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | // Polymorphism in action 4 | Shape[] shapes = {new Circle(), new Triangle(), new Square()}; 5 | 6 | for (Shape shape : shapes) { 7 | shape.draw(); 8 | shape.erase(); 9 | System.out.println(); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Add Code Here/CPP/Tower_of_Hanoi_using_Recursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void TOH(int n, int A=1, int B=2, int C=3) 5 | { 6 | if (n>0) 7 | { 8 | TOH(n - 1, A, C, B); 9 | printf("(%d,%d)\n", A, C); 10 | TOH(n - 1, B, A, C); 11 | } 12 | } 13 | 14 | int main() 15 | { 16 | cout << "Tower of Hanoi!\n\n"; 17 | TOH(4); 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/Remove_element.java: -------------------------------------------------------------------------------- 1 | // code by subhash karthik 2 | 3 | class Solution { 4 | public int removeElement(int[] nums, int val) { 5 | 6 | int cnt = 0; 7 | for(int i = 0; i < nums.length; i++){ 8 | if(nums[i] != val){ 9 | nums[cnt] = nums[i]; 10 | cnt++; 11 | } 12 | } 13 | 14 | return cnt; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /content/participant /joywin.md: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | # Joywin 4 | 5 | ### Location 6 | 7 | Bantwal, India 8 | 9 | ### Academics 10 | - St. Aloysius PU College 11 | - St Joseph Engineering College 12 | 13 | ### Interests 14 | - Reading books 15 | - Trying out new frameworks 16 | 17 | ### Development 18 | 19 | - React 20 | - Django 21 | 22 | ### Profile Link 23 | 24 | [Visit my GitHub Profile](https://github.com/joywin2003/) 25 | 26 | --- 27 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/Linked List Cycle.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean hasCycle(ListNode head) { 3 | ListNode a=head; 4 | 5 | ListNode b=head; 6 | 7 | while(a!=null&&b!=null&&b.next!=null){ 8 | a=a.next; 9 | b=b.next.next; 10 | if(a==b){ 11 | return true; 12 | } 13 | } 14 | return false; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Add Code Here/Java/Project_Main.java: -------------------------------------------------------------------------------- 1 | public class Project_Main { 2 | public static void main(String[] args) { 3 | // Create instances of the subclasses and call their debug() methods 4 | ProjectClass obj1 = new Subclass1(); 5 | ProjectClass obj2 = new Subclass2(); 6 | ProjectClass obj3 = new Subclass3(); 7 | 8 | obj1.debug(); 9 | obj2.debug(); 10 | obj3.debug(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Add Code Here/CPP/palindrome_number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool checkPalindrome(int n) 5 | { 6 | int reverse = 0; 7 | int temp = n; 8 | while (temp != 0) { 9 | reverse = (reverse * 10) + (temp % 10); 10 | temp = temp / 10; 11 | } 12 | return (reverse == n); 13 | } 14 | int main() 15 | { 16 | int n = 7007; 17 | if (checkPalindrome(n) == 1) { 18 | cout << "Yes\n"; 19 | } 20 | else { 21 | cout << "No\n"; 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Add Code Here/CPP/Kadane's Algorithm: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // Given an integer array nums, find the subarray with the largest sum, and return its sum. 8 | vectornums={-2,1,-3,4,-1,2,1,-5,4}; 9 | int sum=0; 10 | int mx=INT_MIN;; 11 | for(int i=0; i 5 | using namespace std; 6 | 7 | 8 | unsigned int countSetBits(unsigned int n) 9 | { 10 | unsigned int count = 0; 11 | while (n) { 12 | count += n & 1; 13 | n >>= 1; 14 | } 15 | return count; 16 | } 17 | 18 | 19 | int main() 20 | { 21 | int i = 9; 22 | cout << countSetBits(i); 23 | return 0; 24 | } 25 | 26 | // This code is contributed 27 | // by Nikhil Kumar 28 | -------------------------------------------------------------------------------- /Add Code Here/Java/DateTest.java: -------------------------------------------------------------------------------- 1 | public class DateTest { 2 | public static void main( String [] args ) 3 | { 4 | Date date1 = new Date(8,28,2000); 5 | System.out.println("The Initial Date "); 6 | date1.displayDate(); 7 | 8 | 9 | date1.setMonth(2); 10 | date1.setDay(12); 11 | date1.setYear(2003); 12 | 13 | System.out.println("\nThe Updated Date "); 14 | date1.displayDate(); 15 | 16 | 17 | 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Add Code Here/Python/Python Macro Programmer/Macro.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "RunType": "0", 4 | "Exit": "esc+0", 5 | "Toggle": "1+esc", 6 | "Start": "2+esc", 7 | "Stop": "3+esc" 8 | }, 9 | { 10 | "shift+Q": "hello world 2", 11 | "shift+F": "smaller string", 12 | "shift+V": "noice boi" 13 | }, 14 | { 15 | "f3": "volume neutral", 16 | "f7": "Brightness_Up", 17 | "f8": "Brightness_Down" 18 | } 19 | ] -------------------------------------------------------------------------------- /Add Code Here/Java/Ford.java: -------------------------------------------------------------------------------- 1 | public class Ford extends Car { 2 | private int year,manufacturerDiscount; 3 | public Ford(int speed, double regularprice,String color, int year, int manufacturerDiscount ) 4 | { 5 | super(speed,regularprice,color); 6 | this.year=year; 7 | this.manufacturerDiscount=manufacturerDiscount; 8 | } 9 | 10 | public double getSalePrice() 11 | { 12 | return super.getSalepPrice()-manufacturerDiscount; 13 | } 14 | 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Add Code Here/CPP/(15)majorityele.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int findMajorityElement(int arr[], int n) { 4 | // Write your code here. 5 | int ele; 6 | int cnt=0; 7 | for(int i=0;i=n/2+1) 27 | return ele; 28 | return -1; 29 | } -------------------------------------------------------------------------------- /Add Code Here/CPP/Jump_Game.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | vector memo; 3 | public: 4 | bool canJump(vector& nums) { 5 | int n=nums.size(); 6 | vector dp(n,0); 7 | dp[0]=true; 8 | 9 | for(int i=1;i=0;j--){ 11 | if(dp[j] && j+nums[j]>=i){ 12 | dp[i]=true; 13 | break; 14 | } 15 | } 16 | } 17 | return dp[n-1]; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Add Code Here/JavaScript/Pascal's Triangle.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} numRows 3 | * @return {number[][]} 4 | */ 5 | var generate = function(numRows) { 6 | let outer=[]; 7 | let first_row=[1]; 8 | outer.push(first_row); 9 | for(let i=1;i=strs[i].length || c!=strs[i][j]){ 14 | return ans; 15 | } 16 | } 17 | ans+=c; 18 | j++ 19 | 20 | } 21 | return ans 22 | }; 23 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # To get started with Dependabot version updates, you'll need to specify which 2 | # package ecosystems to update and where the package manifests are located. 3 | # Please see the documentation for all configuration options: 4 | # https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates 5 | 6 | # version: 2 7 | # updates: 8 | # - package-ecosystem: "" # See documentation for possible values 9 | # directory: "/" # Location of package manifests 10 | # schedule: 11 | # interval: "weekly" 12 | -------------------------------------------------------------------------------- /Add Code Here/CPP/Best Time to Buy and Sell Stock: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // You want to maximize your profit by choosing a single day to buy one stock 8 | // and choosing a different day in the future to sell that stock. 9 | 10 | vectorprices={7,1,5,3,6,4}; 11 | int mn=prices[0], mx=0; 12 | 13 | for(int i=1; i 0) { 6 | int d = n % 10; 7 | n = n / 10; 8 | totalSum += d * d; 9 | } 10 | return totalSum; 11 | } 12 | 13 | public boolean isHappy(int n) { 14 | Set seen = new HashSet<>(); 15 | while (n != 1 && !seen.contains(n)) { 16 | seen.add(n); 17 | n = getNext(n); 18 | } 19 | return n == 1; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Add Code Here/Java/Truck.java: -------------------------------------------------------------------------------- 1 | public class Truck extends Car{ 2 | private int weight; 3 | public Truck(int speed, double regularprice, String color, int weight) 4 | { 5 | super(speed,regularprice,color); 6 | this.weight=weight; 7 | } 8 | 9 | public double getSalePrice() 10 | { 11 | if(weight>20000) 12 | { 13 | return regularprice*0.9; //10% discount for weight >2000 14 | } 15 | else 16 | return regularprice*0.8; //20% discount for weight<=2000 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /Add Code Here/JAVA/ExecuteBinarySearchOnAVector.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.Vector; 3 | 4 | public class ExecuteBunarySearchOnVector{ 5 | public static void main(String[] args) { 6 | Vector v = new Vector(); 7 | v.add("X"); 8 | v.add("M"); 9 | v.add("D"); 10 | v.add("A"); 11 | v.add("O"); 12 | Collections.sort(v); 13 | System.out.println(v); 14 | int index = Collections.binarySearch(v, "D"); 15 | System.out.println("Element found at : " + index); 16 | } 17 | } 18 | Result 19 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/Palindrome_Number.java: -------------------------------------------------------------------------------- 1 | //contributed by subhash karthik. 2 | 3 | class Solution { 4 | public boolean isPalindrome(int x) { 5 | int temp = x; 6 | 7 | boolean flag = false; 8 | if(x < 0){ 9 | return flag; 10 | } 11 | 12 | int ans = 0; 13 | while(x != 0){ 14 | ans = ans * 10 + (x % 10); 15 | x = x / 10; 16 | } 17 | 18 | if(ans == temp){ 19 | flag = true; 20 | } 21 | 22 | return flag; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Add Code Here/Java/Car.java: -------------------------------------------------------------------------------- 1 | /* 2 | This is the 9th Question of the Assignment which has many child classes 3 | child classes - Truck,Ford,Sedan 4 | */ 5 | public class Car { 6 | private int speed; 7 | double regularprice; 8 | private String color; 9 | 10 | public Car(int speed, double regularprice, String color) 11 | { 12 | this.speed=speed; 13 | this.regularprice=regularprice; 14 | this.color=color; 15 | } 16 | 17 | public double getSalepPrice() 18 | { 19 | return regularprice; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Add Code Here/Java/Sedan.java: -------------------------------------------------------------------------------- 1 | public class Sedan extends Car{ 2 | int length; 3 | 4 | public Sedan(int speed, double regularprice, String color, int length) 5 | { 6 | super(speed, regularprice, color); 7 | this.length=length; 8 | } 9 | 10 | double getSalePrice() 11 | { 12 | if(length>20) 13 | { 14 | return regularprice*0.95; // 5% discount for length > 20 15 | } 16 | else{ 17 | return regularprice*0.9; // 10% discount for length <=20 18 | } 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # CODE Libriary 2 | 3 | ## GUIDELINES FOR CONTRIBUTNG. 4 | 5 | - Select your preferred programming language folder. 6 | - Either fix the codes that are mentioned on issues or create a small program on your preferred language and create a pull request. 7 | - NOTE : Use the respective language folders for committing your code. 8 | - Happy Coding :) 9 | 10 | 11 | ### Contributing Rules 12 | 13 | - Contribute standard codes 14 | - Look upon the Folders given 15 | - Plagarism is depreciated 16 | 17 | ## This is a repo for anyone who is signed up for Hacktoberfest 18 | 19 | - 4 pull requests must be made to qualify. 20 | -------------------------------------------------------------------------------- /Add Code Here/CPP/2ndMaximumAmong3Numbers.c++: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | void solve(){ 8 | // Storing Numbers in Array 9 | int a[3], minimum = INT_MAX, maximum = INT_MIN, result; 10 | 11 | // Taking Input 12 | cin>>a[0]>>a[1]>>a[2]; 13 | 14 | // Sorting Array 15 | sort(a, a+3); 16 | 17 | cout<>t; 24 | while(t--){ 25 | solve(); 26 | } 27 | } 28 | 29 | // Code Submitted by Rohan Das - https://github.com/rohan26062001 -------------------------------------------------------------------------------- /Add Code Here/CPP/Sieve of Eratosthenes.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | // Sieve of Eratosthenes 9 | //Given an integer n, return the number of prime numbers that are strictly less than n. 10 | int n; 11 | cin>>n; 12 | vectorseen(n,true); 13 | int count=0; 14 | for(int i=2; i0) 12 | { 13 | rem=num%10; 14 | result=result+(rem*rem*rem); 15 | num=num/10; 16 | } 17 | if(result==temp) 18 | System.out.println("armstrong"); 19 | else 20 | System.out.println("not armstrong"); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Add Code Here/CPP/Factorial_of_a_Number_using_recursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //using recursion 5 | int fact1(int n) 6 | { 7 | if (n == 0) 8 | return 1; 9 | 10 | return fact1(n - 1) * n; 11 | } 12 | 13 | //using loops 14 | int fact2(int n) 15 | { 16 | int i, f=1; 17 | for (i = 1; i <= n; i++) 18 | f *= i; 19 | return f; 20 | } 21 | 22 | 23 | int main() 24 | { 25 | cout << "Factorial of a number" << endl; 26 | 27 | //using recursion 28 | cout << "Using recursion : " << fact1(5) << endl; 29 | 30 | //using loop 31 | cout << "Using loop : " << fact2(5) << endl; 32 | } 33 | -------------------------------------------------------------------------------- /Add Code Here/CPP/Swap Nodes in Pairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode* swapPairs(ListNode* head) { 4 | if(!head||!head->next) 5 | return head; 6 | 7 | ListNode* curr=head->next->next; 8 | ListNode* prev=head; 9 | head=head->next; 10 | head->next=prev; 11 | 12 | while(curr&&curr->next) 13 | { 14 | prev->next=curr->next; 15 | prev=curr; 16 | ListNode*temp=curr->next->next; 17 | curr->next->next=prev; 18 | curr=temp; 19 | 20 | } 21 | prev->next=curr; 22 | return head; 23 | 24 | 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/kadane_algo.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class kadane_algo { 3 | public static void main(String[] args) { 4 | Scanner sc=new Scanner(System.in); 5 | int t= sc.nextInt(); 6 | while(t>0) { 7 | int n=sc.nextInt(); 8 | int []arr=new int[n]; 9 | for(int i=0;i=0){ 15 | cm=cm+arr[i]; 16 | } 17 | else{ 18 | cm=arr[i]; 19 | } 20 | if(cm>res){ 21 | res=cm; 22 | } 23 | } 24 | System.out.println(res); 25 | t--; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/decoratorPattern/mainClass.java: -------------------------------------------------------------------------------- 1 | package decoratorPattern; 2 | import java.util.*; 3 | import java.util.concurrent.TimeUnit; 4 | 5 | class mainClass { 6 | public static void main(String[] args) { 7 | mainClass clazz = new mainClass(); 8 | clazz.mainWork(); 9 | 10 | } 11 | 12 | private void mainWork() { 13 | simpleBase base=new simpleBase(); 14 | onionToppings toppings=new onionToppings(base); 15 | cheeseToppings doubleCheese=new cheeseToppings(toppings); 16 | cheeseToppings trippleCheese=new cheeseToppings(doubleCheese); 17 | System.out.println("The cost of your pizza is: "+trippleCheese.cost()); 18 | } 19 | 20 | 21 | } -------------------------------------------------------------------------------- /Add Code Here/CPP/array-of-vectors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | void printv(vector&v){ 6 | 7 | for (int i = 0; i < v.size(); i++){ 8 | 9 | cout<>n; 19 | 20 | vectorv[n];//*n vectors declared 21 | for (int i = 0; i < n; i++){ 22 | int x; 23 | cin>>x; 24 | for (int j = 0; j < x; j++){ 25 | 26 | int y; 27 | cin>> y; 28 | v[i].push_back(y); 29 | } 30 | 31 | } 32 | for (int i = 0; i What are the possibilities: 9 | 10 | 1. Solving the differential equation using the Euler method 11 | 2. Solving the DE using the Euler-Cauchy method 12 | 3. Solving the DE using the Runge-Kutta method 13 | 4. coming soon... 14 | 15 | 16 | 17 | ![image](https://user-images.githubusercontent.com/37026894/145445960-e44a43fd-c758-42d3-b13d-6ddef3535478.png) 18 | 19 | ![image](https://user-images.githubusercontent.com/37026894/145446075-b54c6109-d3c0-4c4a-bb80-aeb198e5556f.png) 20 | -------------------------------------------------------------------------------- /Add Code Here/CPP/decode_ways.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | 6 | int numDecodings(string s) { 7 | int n = s.length(); 8 | vectordp(n + 1, 0); 9 | 10 | dp[0] = 1; 11 | dp[1] = s[0] == '0' ? 0 : 1; 12 | 13 | for (int i = 2; i <= n; i++) 14 | { 15 | dp[i] = 0; 16 | if (s[i - 1] != '0') 17 | dp[i] = dp[i - 1]; 18 | 19 | if (s[i - 2] == '1' || s[i - 2] == '2' && s[i - 1] <= '6') 20 | dp[i] += dp[i - 2]; 21 | } 22 | 23 | return dp[n]; 24 | 25 | } 26 | 27 | int main() 28 | { 29 | #ifndef ONLINE_JUDGE 30 | freopen("input1.txt", "r", stdin); 31 | freopen("output1.txt", "w", stdout); 32 | 33 | #endif 34 | 35 | string s; 36 | cin >> s; 37 | int ans = numDecodings(s); 38 | cout << ans; 39 | return 0; 40 | 41 | } -------------------------------------------------------------------------------- /Add Code Here/JAVA/TwoSum.java: -------------------------------------------------------------------------------- 1 | public class TwoSum{ 2 | static int[] twoSum(int arr[],int n,int target){ 3 | int start=0; 4 | int end=n-1; 5 | while(start<=end) { 6 | int currSum=arr[start]+arr[end]; 7 | if(currSumtarget) 10 | end--; 11 | else 12 | return new int[]{start,end}; 13 | } 14 | return new int[] {}; 15 | } 16 | public static void main(String[] args) { 17 | int arr[]={3,2,4}; 18 | int n=arr.length; 19 | int target=9; 20 | int res[]=twoSum(arr,n,target); 21 | for(int i:res){ 22 | System.out.println(i); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/CheckBST.java: -------------------------------------------------------------------------------- 1 | 2 | public class Solution 3 | { 4 | static boolean BSTUtil(Node root, int min, int max){ 5 | if(root == null){ 6 | return true; 7 | } 8 | 9 | if(root.data < min || root.data > max){ 10 | return false; 11 | } 12 | 13 | return (BSTUtil(root.left, min, root.data-1) && BSTUtil(root.right,root.data+1,max)); 14 | } 15 | 16 | 17 | //Function to check whether a Binary Tree is BST or not. 18 | boolean isBST(Node root) 19 | { 20 | // code here. 21 | int min = Integer.MIN_VALUE,max = Integer.MAX_VALUE; 22 | if(BSTUtil(root,min,max)){ 23 | return true; 24 | } 25 | else{ 26 | return false; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/Employees.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Employee 3 | { 4 | 5 | void display() 6 | { 7 | 8 | System.out.println("Name of class is Employee."); 9 | 10 | } 11 | 12 | void calcSalary() 13 | { 14 | 15 | System.out.println("Salary of Employee is 10000"); 16 | 17 | } 18 | 19 | } 20 | 21 | class Engineer extends Employee 22 | { 23 | 24 | void calcSalary() 25 | { 26 | super.calcSalary(); 27 | System.out.println("Salary of Engineer is 20000"); 28 | 29 | } 30 | 31 | } 32 | class Employees 33 | { 34 | 35 | public static void main(String args[]) 36 | { 37 | 38 | Engineer e = new Engineer(); 39 | e.display(); 40 | e.calcSalary(); 41 | 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/TrappingRainWater.java: -------------------------------------------------------------------------------- 1 | // Problem link: https://leetcode.com/problems/trapping-rain-water/# 2 | class Solution { 3 | public int trap(int[] A) { 4 | int n = A.length; 5 | int temp,hght,lvl=0; 6 | int[] left = new int[n]; 7 | int[] right= new int[n]; 8 | int i,lmax=0,rmax=0; 9 | for(i=0;ilmax) 11 | lmax=A[i]; 12 | left[i]=lmax; 13 | } 14 | for(i=n-1;i>=0;i--){ 15 | if(A[i]>rmax) 16 | rmax=A[i]; 17 | right[i]=rmax; 18 | } 19 | for(i=0;i st, 7 | int n, int curr) 8 | { 9 | 10 | if (st.empty() || curr == n) 11 | return; 12 | 13 | char x = st.pop(); 14 | 15 | deleteMid(st, n, curr+1); 16 | 17 | if (curr != n/2) 18 | st.push(x); 19 | } 20 | 21 | public static void main(String args[]) 22 | { 23 | Stack st = 24 | new Stack(); 25 | 26 | st.push('1'); 27 | st.push('2'); 28 | st.push('3'); 29 | st.push('4'); 30 | st.push('5'); 31 | st.push('6'); 32 | st.push('7'); 33 | 34 | deleteMid(st, st.size(), 0); 35 | 36 | while (!st.empty()) 37 | { 38 | char p=st.pop(); 39 | System.out.print(p + " "); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/binarysearch.java: -------------------------------------------------------------------------------- 1 | public class binarysearch { 2 | public static void main(String[] args) { 3 | int[] nums = {1,2,3,45,65,78,552,1024,2048,4096}; 4 | int bsearch = search(nums, 552); 5 | System.out.println(search(nums,bsearch)); 6 | } 7 | 8 | //return the index 9 | public static int search(int[] nums, int target) { 10 | int start = 0; 11 | int end = nums.length; 12 | while(start nums[mid]){ 18 | start = mid + 1; 19 | } 20 | else{ 21 | return mid; 22 | } 23 | } 24 | return -1; 25 | } 26 | } -------------------------------------------------------------------------------- /Add Code Here/Python/password_generator.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | if __name__ == "__main__": 4 | s1 = ['a','b','c','d','e','f','g','h','i','j','k','l','m','o','p','q','r','s','t','u','v','w','x','y','z'] 5 | s2 = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'] 6 | s3 = ['1','2','3','4','5','6','7','8','9','0'] 7 | s4 = ['!','@','#','$','%','^','&','*','(',')','_','+','{','}','|','[',']','"',';','<','>','?','/',','] 8 | 9 | passLen = int(input("Enter Your password lenght: ")) 10 | s = [] 11 | s.extend(s1) 12 | s.extend(s2) 13 | s.extend(s3) 14 | s.extend(s4) 15 | a =0 16 | print("Choose any one of the 5 passwords given below!") 17 | print("") 18 | while a<5: 19 | random.shuffle(s) 20 | print("\t","".join(s[0:passLen])) 21 | a=a+1 22 | -------------------------------------------------------------------------------- /Add Code Here/Java/Animal.java: -------------------------------------------------------------------------------- 1 | /* 2 | This is the Question No 10 Part two - Example code for Abstract classes and methods 3 | the object code is Animal_Main.java 4 | */ 5 | // Abstract class Animal 6 | public abstract class Animal { 7 | // Abstract method without implementation 8 | abstract void makeSound(); 9 | 10 | // Concrete method with implementation 11 | void sleep() { 12 | System.out.println("Zzzz..."); 13 | } 14 | } 15 | 16 | // Subclass Dog inheriting from Animal 17 | class Dog extends Animal { 18 | @Override 19 | void makeSound() { 20 | System.out.println("Woof Woof!"); 21 | } 22 | } 23 | 24 | // Subclass Cat inheriting from Animal 25 | class Cat extends Animal { 26 | @Override 27 | void makeSound() { 28 | System.out.println("Meow Meow!"); 29 | } 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Add Code Here/CPP/Binary_search_usingrecursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int binary_search(int A[], int beg, int end, int ele) 5 | { 6 | if (end >= beg) 7 | { 8 | int mid = (beg + end - 1) / 2; 9 | 10 | if (A[mid] == ele) 11 | return mid; 12 | else if (A[mid] > ele) 13 | return binary_search(A, beg, mid, ele); 14 | else 15 | return binary_search(A, mid + 1, end, ele); 16 | } 17 | return -1; 18 | } 19 | int main() 20 | { 21 | int n, ele, index; 22 | cin >> n; 23 | int *A = new int[n]; 24 | for (int i = 0; i < n; i++) 25 | { 26 | cin >> A[i]; 27 | } 28 | cin>>ele; 29 | index = binary_search(A, 0, n-1, ele); 30 | if(index!=-1) 31 | cout<<"Element found at index "< 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() 8 | 9 | { 10 | 11 | int n,num,rem,i=0; 12 | 13 | float result=0; 14 | 15 | cout<<"Enter the number:"<>n; 18 | 19 | num=n; 20 | 21 | for(num=n;num!=0;++i) //store the number of digits of n in num 22 | 23 | { 24 | 25 | num/=10; 26 | 27 | } 28 | 29 | for(num=n;num!=0;num/=10) 30 | 31 | { 32 | 33 | rem=num%10; 34 | 35 | result+=pow(rem,i); //store the sum of power of each digits in result 36 | 37 | } 38 | 39 | if((int)result==n) 40 | 41 | cout<<"Yes, it is an Armstrong Number"; 42 | 43 | else 44 | 45 | cout<<"No, it is not an Armstrong Number"; 46 | 47 | return 0; 48 | 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Add Code Here/CPP/BubbleSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void swap(int *ab, int *cd) 5 | { 6 | int temp = *ab; 7 | *ab = *cd; 8 | *cd = temp; 9 | } 10 | 11 | // A function to implement bubble sort 12 | void bubbleSort(int arr[], int n) 13 | { 14 | int i, j; 15 | for (i = 0; i < n-1; i++) 16 | 17 | // Last i elements are already in place 18 | for (j = 0; j < n-i-1; j++) 19 | if (arr[j] > arr[j+1]) 20 | swap(&arr[j], &arr[j+1]); 21 | } 22 | 23 | void printArray(int arr[], int size) 24 | { 25 | int i; 26 | for (i = 0; i < size; i++) 27 | cout << arr[i] << " "; 28 | cout << endl; 29 | } 30 | 31 | int main() 32 | { 33 | int arr[] = {44, 34, 47,1, 89, 32, 90}; 34 | int n = sizeof(arr)/sizeof(arr[0]); 35 | bubbleSort(arr, n); 36 | cout<<"Sorted array: \n"; 37 | printArray(arr, n); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Add Code Here/Java/MyOwnAutoShop.java: -------------------------------------------------------------------------------- 1 | public class MyOwnAutoShop { 2 | public static void main(String [] args) 3 | { 4 | //Creating an instance of Sedan Class 5 | Sedan sedan = new Sedan(280,25000,"Black", 20); 6 | 7 | //Creating two instances of Ford Class 8 | Ford ford1 = new Ford(180,15000,"Blue",2022, 2500); 9 | Ford ford2 = new Ford(190,18000,"White",2023,1500); 10 | 11 | //Creating an instance of Car Class 12 | Car car1 = new Car(290,25000, "Gray"); 13 | 14 | 15 | //Displaying the Sale prices of all the instances 16 | System.out.println("Sedan Sale Price: $" +sedan.getSalePrice()); 17 | System.out.println("Ford1 Sale Price: $" +ford1.getSalePrice()); 18 | System.out.println("Ford2 Sale Price: $" +ford2.getSalepPrice()); 19 | System.out.println("Car Sale Price: $" +car1.getSalepPrice()); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Add Code Here/Python/NthPrime.py: -------------------------------------------------------------------------------- 1 | import itertools as it 2 | import time 3 | 4 | 5 | def nth_prime(length): 6 | Prime_List = [2,3] 7 | 8 | for i in it.count(Prime_List[-1],2): 9 | if i%3 ==0: 10 | continue 11 | 12 | Isprime= True 13 | limit = int(i ** 0.5)+1 14 | 15 | for j in Prime_List: 16 | if (i%j)==0: 17 | Isprime = False 18 | break 19 | 20 | if j > (limit): 21 | break 22 | 23 | if Isprime: 24 | Prime_List.append(i) 25 | if len(Prime_List) == length-2: 26 | break 27 | 28 | print(i) 29 | 30 | if __name__ =="__main__": 31 | start = time.perf_counter() 32 | 33 | n=1000000 34 | nth_prime(n) 35 | print(time.perf_counter() - start) 36 | # you can access all the primes found till N in the (Prime_List) list -------------------------------------------------------------------------------- /Add Code Here/CPP/Binary_search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool binSearch(vector& arr, int x){ 5 | int n=arr.size(); 6 | int start=0, end=n-1, mid; 7 | while(start<=end){ 8 | mid=(start+end)>>1; 9 | if(arr[mid]==x) 10 | return true; 11 | else if(arr[mid]>x) 12 | end=mid-1; 13 | else start=mid+1; 14 | } 15 | return false; 16 | } 17 | int main() 18 | { 19 | 20 | vector arr={12,14, 17, 9, 78, 6}; 21 | int n=arr.size(); 22 | cout<<"Original Array is:"< search={9, 70, 12}; 28 | for(int i=0; i 4 | using namespace std; 5 | 6 | int dp[101][1001]; 7 | memset(dp,-1,sizeof(dp)); 8 | 9 | int knapSack(int W, int wt[], int val[], int n) 10 | { 11 | if (n==0 || W==0) return 0; 12 | 13 | // memoization 14 | if (dp[n][w]!=-1) return dp[n][w]; 15 | 16 | // if weight of the item in array is less than the max weight then we have 2 choice either include it or exclude it 17 | 18 | if (wt[n-1]<=W) { 19 | return dp[n][w] =max(val[n-1] + knapSack(W-wt[n-1],wt,val,n-1), knapSack(W,wt,val,n-1)); 20 | } 21 | 22 | // if weight of item is more than max weight then we have to exclude it 23 | 24 | else if (wt[n-1] >W) { 25 | return dp[n][w] = knapSack(W,wt,val,n-1); 26 | } 27 | } 28 | 29 | int main() { 30 | n = 3 31 | W = 4 32 | val[] = {1,2,3} 33 | wt[] = {4,5,1} 34 | cout<< knapSack(W, wt, val, n)< leftNodes, int level){ 16 | if(root == null){ 17 | return; 18 | } 19 | if(level > maxLevel){ 20 | leftNodes.add(root.data); 21 | maxLevel = level; 22 | } 23 | 24 | traverse(root.left,leftNodes,level+1); 25 | traverse(root.right, leftNodes,level+1); 26 | } 27 | 28 | //Function to return list containing elements of left view of binary tree. 29 | ArrayList leftView(Node root) 30 | { 31 | // Your code here 32 | ArrayList leftNodes = new ArrayList(); 33 | int level = 0; 34 | maxLevel = -1; 35 | traverse(root, leftNodes, level); 36 | return leftNodes; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Add Code Here/CPP/Check_if_a_queue_can_be_sorted_into_another_queue_using_a_stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool checkSorted(int n, queue& q) 5 | { 6 | stack stk; 7 | int expected = 1; 8 | int fnt; 9 | 10 | while (!q.empty()) { 11 | fnt = q.front(); 12 | q.pop(); 13 | 14 | if (fnt == expected) 15 | expected++; 16 | 17 | else { 18 | 19 | if (stk.empty()) { 20 | stk.push(fnt); 21 | } 22 | 23 | 24 | else if (!stk.empty() && stk.top() < fnt) { 25 | return false; 26 | } 27 | 28 | 29 | else 30 | stk.push(fnt); 31 | } 32 | 33 | 34 | while (!stk.empty() && stk.top() == expected) { 35 | stk.pop(); 36 | expected++; 37 | } 38 | } 39 | 40 | if (expected - 1 == n && stk.empty()) 41 | return true; 42 | 43 | return false; 44 | } 45 | 46 | int main() 47 | { 48 | queue q; 49 | q.push(5); 50 | q.push(1); 51 | q.push(2); 52 | q.push(3); 53 | q.push(4); 54 | 55 | int n = q.size(); 56 | 57 | (checkSorted(n, q) ? (cout << "Yes") : 58 | (cout << "No")); 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /Add Code Here/Java/Item.java: -------------------------------------------------------------------------------- 1 | /* 2 | This is the 7th Question of the assignment 3 | */ 4 | public class Item { 5 | protected static int Location; 6 | protected static String Description; 7 | 8 | //constructor - method 9 | public Item (int Location, String Description) 10 | { 11 | this.Description =Description; 12 | this.Location = Location; 13 | } 14 | 15 | // Getter Method for Location 16 | 17 | public static int getLocation() 18 | { 19 | return Location; 20 | } 21 | 22 | //Setter Method for Location 23 | 24 | public static void setLocation(int Location) 25 | { 26 | Item.Location = Location; 27 | } 28 | 29 | 30 | //Getter Method for Description 31 | 32 | public static String getDescription() 33 | { 34 | return Description; 35 | } 36 | 37 | //Setter Method for Description 38 | 39 | public static void setDescription(String Description) 40 | { 41 | Item.Description = Description; 42 | } 43 | 44 | 45 | 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Add Code Here/Python/word Scramble.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | # List of words to be scrambled 4 | word_list = ["apple", "banana", "cherry", "grape", "orange", "strawberry", "blueberry"] 5 | 6 | # Function to choose a random word from the list 7 | def choose_word(): 8 | return random.choice(word_list) 9 | 10 | # Function to scramble a word 11 | def scramble_word(word): 12 | word_chars = list(word) 13 | random.shuffle(word_chars) 14 | return ''.join(word_chars) 15 | 16 | # Main game loop 17 | while True: 18 | # Choose a random word and scramble it 19 | target_word = choose_word() 20 | scrambled_word = scramble_word(target_word) 21 | 22 | print("Unscramble the word:", scrambled_word) 23 | 24 | # Ask the player to guess the unscrambled word 25 | guess = input("Your guess: ") 26 | 27 | if guess.lower() == target_word: 28 | print("Correct! The word is:", target_word) 29 | else: 30 | print("Incorrect. The word is:", target_word) 31 | 32 | play_again = input("Do you want to play again? (yes/no): ") 33 | if play_again.lower() != "yes": 34 | break 35 | -------------------------------------------------------------------------------- /Add Code Here/CPP/array1.cpp: -------------------------------------------------------------------------------- 1 | //Problem: Finding the Longest Increasing Subsequence (LIS) 2 | //Description: 3 | //Given an unsorted array of integers, find the length of longest increasing subsequence. 4 | 5 | 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | int longestIncreasingSubsequence(vector& arr) { 11 | int n = arr.size(); 12 | vector dp(n, 1); 13 | 14 | for (int i = 1; i < n; i++) { 15 | for (int j = 0; j < i; j++) { 16 | if (arr[i] > arr[j] && dp[i] < dp[j] + 1) { 17 | dp[i] = dp[j] + 1; 18 | } 19 | } 20 | } 21 | 22 | int maxLen = 0; 23 | for (int i = 0; i < n; i++) { 24 | if (dp[i] > maxLen) { 25 | maxLen = dp[i]; 26 | } 27 | } 28 | 29 | return maxLen; 30 | } 31 | 32 | int main() { 33 | vector arr = {10, 22, 9, 33, 21, 50, 41, 60, 80}; 34 | int lisLength = longestIncreasingSubsequence(arr); 35 | 36 | cout << "Length of Longest Increasing Subsequence: " << lisLength << endl; 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Add Code Here/Java/TestClass_SavingsAccount.java: -------------------------------------------------------------------------------- 1 | public class TestClass_SavingsAccount { 2 | public static void main(String [] args) 3 | { 4 | SavingsAccount saver1 = new SavingsAccount(2000, 4.00); 5 | SavingsAccount saver2 = new SavingsAccount(3000, 4.00); 6 | saver1.calculateMonthlyInterest(); 7 | saver2.calculateMonthlyInterest(); 8 | 9 | System.out.println("The Monthly Interest of Saver 1 is: " +saver1.getSavingBalance()); 10 | System.out.println("The Monthly Interest of Saver 2 is: " +saver2.getSavingBalance()); 11 | 12 | System.out.println(""); 13 | 14 | //The next Month interest New Balance under the updated interest rate 15 | SavingsAccount.modifyInterestRate(0.05); 16 | 17 | saver1.calculateMonthlyInterest(); 18 | saver2.calculateMonthlyInterest(); 19 | 20 | System.out.println("The New Monthly Interest Rate for Saver 1 is: " +saver1.getSavingBalance()); 21 | System.out.println("The New Monthly Interest Rate for Saver 2 is: " +saver2.getSavingBalance()); 22 | 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Add Code Here/JavaScript/schemeRandomNota.js: -------------------------------------------------------------------------------- 1 | const schemeTransactionNumber = (id) => { 2 | let data = parseInt(id, 10); 3 | 4 | const a = 'abcdefghijklmnopqrstuvwxyz123456789'.toUpperCase().split("") 5 | 6 | let result = '' 7 | 8 | while(true) { 9 | if(data > 35) { 10 | const temp = data % 35; 11 | if(data % 35 === 0) { 12 | result = result.concat(a[34]) 13 | data = parseInt(data/35, 10) - 1 14 | } else { 15 | result = result.concat(a[temp - 1]) 16 | data = parseInt(data/35, 10) 17 | } 18 | } else { 19 | result = result.concat(a[data - 1]) 20 | break; 21 | } 22 | } 23 | 24 | return result.split("").reverse("").join(""); 25 | } 26 | 27 | const createSchemeNumber = (prefix = 'INV', id = '1', suffix = 'DWN') => 28 | `${prefix}/${schemeTransactionNumber(id)}/${suffix}` 29 | 30 | export { createSchemeNumber }; 31 | 32 | //example 33 | console.log(createSchemeNumber()); // => INV/A/DWN 34 | console.log(createSchemeNumber('INV', 100)) // => INV/A/DWN 35 | 36 | //sandbox 37 | //https://codesandbox.io/s/thirsty-liskov-renhi?file=/src/index.js -------------------------------------------------------------------------------- /Add Code Here/Python/Differential Equation Solver/Euler.py: -------------------------------------------------------------------------------- 1 | from differential_input import diff_input 2 | import math 3 | 4 | 5 | def euler_method(function=None, y0=None, a_b=None, n=None): 6 | if function is None or y0 is None or a_b is None or n is None: 7 | function, yzuv, a_b, n = diff_input() 8 | 9 | a, b = a_b 10 | h = (b-a)/n 11 | x0 = a 12 | 13 | answer_list = [(0, x0, *yzuv)] 14 | g = len(yzuv) 15 | while len(yzuv) < 4: 16 | yzuv.append(None) 17 | 18 | yi, zi, ui, vi = [None] * 4 19 | f1, f2, f3, f4 = [None] * 4 20 | k = [yi, zi, ui, vi] 21 | f = [f1, f2, f3, f4] 22 | 23 | for i in range(1, n+1): 24 | x, y, z, u, v = x0, *yzuv 25 | 26 | for j in range(0, g): 27 | f_xy = eval(function[j]) 28 | f[j] = h * f_xy 29 | k[j] = yzuv[j] + f[j] 30 | yzuv[j] = k[j] 31 | 32 | x0 += h 33 | 34 | answer_list.append((i, x0, *k[0:g])) 35 | 36 | return function, answer_list 37 | 38 | 39 | if __name__ == "__main__": 40 | print(euler_method()) 41 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/binary search.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class binarysearch 3 | { 4 | 5 | public static void main(String[] args) 6 | { 7 | Scanner sc = new Scanner(System.in); 8 | binarysearch bs = new binarysearch(); 9 | System.out.print("Enter the size:"); 10 | int n=sc.nextInt(); 11 | int[] A = new int[n]; 12 | System.out.print("Enter the elements:"); 13 | for(int i=0;iA[mid]) 36 | { 37 | beg=mid+1; 38 | } 39 | else 40 | { 41 | end=mid-1; 42 | } 43 | } 44 | return 0; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/bubble_sort.java: -------------------------------------------------------------------------------- 1 | // Java program for implementation of Bubble Sort 2 | class BubbleSort 3 | { 4 | void bubbleSort(int arr[]) 5 | { 6 | int n = arr.length; 7 | for (int i = 0; i < n-1; i++) 8 | for (int j = 0; j < n-i-1; j++) 9 | if (arr[j] > arr[j+1]) 10 | { 11 | // swap arr[j+1] and arr[j] 12 | int temp = arr[j]; 13 | arr[j] = arr[j+1]; 14 | arr[j+1] = temp; 15 | } 16 | } 17 | 18 | /* Prints the array in compilers*/ 19 | void printArray(int arr[]) 20 | { 21 | int n = arr.length; 22 | for (int i=0; i0 && arr[j-1]>temp){ 9 | arr[j]=arr[j-1]; 10 | j=j-1; 11 | } 12 | arr[j]=temp; 13 | 14 | } 15 | for(i=0;i 14 | using namespace std; 15 | 16 | 17 | int countNewChoco(int choco,int wrap) 18 | { 19 | if(choco < wrap) 20 | { 21 | return 0; 22 | } 23 | int newChoco = choco / wrap; 24 | return newChoco + countNewChoco(newChoco + choco%wrap , wrap); 25 | } 26 | 27 | 28 | int countMaxChoco(int money,int price,int wrap) 29 | { 30 | int choco = money / price; 31 | return choco + countNewChoco(choco,wrap); 32 | } 33 | int main() 34 | { 35 | int money = 15; 36 | int price = 1; 37 | int wrap = 3; 38 | 39 | cout<<"Maximum chocolate is : "<> rotateGrid(vector>& grid, int k) { 4 | int y = grid.size(), x = grid[0].size(); 5 | int yend = y - 1, xend = x - 1; 6 | int limit = min(y>>1, x>>1), n = (yend<<1) + (xend<<1); 7 | vector rot(n+n); 8 | 9 | for(int i = 0; i != limit; i++, n -= 8, xend--, yend--){ 10 | int tk = k % n; 11 | if(tk){ 12 | int k1 = 0, k2 = n; 13 | for(int r = i; r < yend; r++) rot[k1++] = rot[k2++] = grid[r][i]; 14 | for(int c = i; c < xend; c++) rot[k1++] = rot[k2++] = grid[yend][c]; 15 | for(int r = yend; r > i; r--) rot[k1++] = rot[k2++] = grid[r][xend]; 16 | for(int c = xend; c > i; c--) rot[k1++] = rot[k2++] = grid[i][c]; 17 | tk = n - tk; 18 | for(int r = i; r < yend; r++) grid[r][i] = rot[tk++]; 19 | for(int c = i; c < xend; c++) grid[yend][c] = rot[tk++]; 20 | for(int r = yend; r > i; r--) grid[r][xend] = rot[tk++]; 21 | for(int c = xend; c > i; c--) grid[i][c] = rot[tk++]; 22 | } 23 | } 24 | 25 | return grid; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/transpose.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class transpose 3 | { 4 | public static void main(String args[]) 5 | { 6 | int i,j; 7 | int a[][]=new int[20][20]; 8 | int t[][]=new int[20][20]; 9 | Scanner sc=new Scanner(System.in); 10 | System.out.println("enter numbr of rows:"); 11 | int m=sc.nextInt(); 12 | System.out.println("enter number of cloumns:"); 13 | int n=sc.nextInt(); 14 | System.out.println("enter the matrix:"); 15 | for(i=0;i 0) 10 | { 11 | ans = Math.min(ans, cost + graph[currPos][0]); 12 | return ans; 13 | } 14 | 15 | 16 | for (int i = 0; i < n; i++) 17 | { 18 | if (v[i] == false && graph[currPos][i] > 0) 19 | { 20 | 21 | 22 | v[i] = true; 23 | ans = tsp(graph, v, i, n, count + 1, 24 | cost + graph[currPos][i], ans); 25 | 26 | 27 | v[i] = false; 28 | } 29 | } 30 | return ans; 31 | } 32 | 33 | 34 | public static void main(String[] args) 35 | { 36 | 37 | 38 | int n = 4; 39 | 40 | int[][] graph = {{0, 10, 15, 20}, 41 | {10, 0, 35, 25}, 42 | {15, 35, 0, 30}, 43 | {20, 25, 30, 0}}; 44 | 45 | 46 | boolean[] v = new boolean[n]; 47 | 48 | 49 | v[0] = true; 50 | int ans = Integer.MAX_VALUE; 51 | 52 | 53 | ans = tsp(graph, v, 0, n, 1, 0, ans); 54 | 55 | 56 | System.out.println(ans); 57 | } 58 | 59 | 60 | 61 | 62 | } -------------------------------------------------------------------------------- /Add Code Here/CPP/Taylor_Series_using_recursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | //using recursion 6 | double e1(int x, int n) 7 | { 8 | static double p = 1, f = 1; 9 | double r; 10 | 11 | if (n == 0) 12 | return 1; 13 | 14 | r = e1(x, n - 1); 15 | p *= x; 16 | f *= n; 17 | 18 | return r + p / f; 19 | } 20 | 21 | //using recursion - horner's rule 22 | double e2(int x, int n) 23 | { 24 | //cout << n << " in\n"; - thanks to boiiii#8502 14.06.23 25 | static double s; 26 | if (n == 0) 27 | return s; 28 | s = 1 + x * s / n; 29 | return e2(x, n - 1); 30 | } 31 | 32 | //using loop 33 | double e3(int x, int n) 34 | { 35 | double s = 1; 36 | int i; 37 | double num = 1; 38 | double den = 1; 39 | 40 | for (i = 1; i <= n; i++) 41 | { 42 | num *= x; 43 | den *= i; 44 | s += num / den; 45 | } 46 | return s; 47 | } 48 | 49 | 50 | int main() 51 | { 52 | //using recursion 53 | printf("%lf \n", e1(2, 10)); 54 | 55 | //using recursion - horner's rule 56 | printf("%lf \n", e2(2, 10)); 57 | 58 | //using loop 59 | printf("%lf \n", e3(2, 10)); 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /Add Code Here/CPP/Bit_Difference.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution{ 5 | public: 6 | long long set(int pattern, long long int arr[], int n) 7 | { 8 | /// pairm; 9 | // m.first=0; 10 | // m.second=0; 11 | long long count=0; 12 | for(int i=0;i=0;bit--) 31 | { 32 | res=set(1<>t; 47 | while(t--){ 48 | int N; 49 | cin>>N; 50 | long long int A[N]; 51 | for(int i = 0;i < N; i++) 52 | cin>>A[i]; 53 | 54 | Solution ob; 55 | cout< 2 | #include 3 | 4 | using namespace std; 5 | 6 | bool isPrime(int n){ 7 | int count = 0; 8 | 9 | // 0, 1 negative numbers are not prime 10 | if(n < 2) 11 | return false; 12 | 13 | // A number n is not a prime, if it can be factored into two factors a & b: 14 | // n = a * b 15 | 16 | /*Now a and b can't be both greater than the square root of n, since 17 | then the product a * b would be greater than sqrt(n) * sqrt(n) = n. 18 | So in any factorization of n, at least one of the factors must be 19 | smaller than the square root of n, and if we can't find any factors 20 | less than or equal to the square root, n must be a prime. 21 | */ 22 | for(int i = 2;i < sqrt(n); i++) 23 | { 24 | if(n % i == 0) 25 | return false; 26 | } 27 | 28 | // if reached here then must be true 29 | return true; 30 | } 31 | 32 | int main() 33 | { 34 | int lower, upper; 35 | 36 | lower=1,upper=100; 37 | 38 | for(int i = lower; i <= upper; i++) 39 | if(isPrime(i)) 40 | cout << i << " "; 41 | 42 | } 43 | // Time Complexity : O(N√N) 44 | // Space Complexity : O(1) 45 | -------------------------------------------------------------------------------- /Add Code Here/CPP/2-Queue_using_Array_C++.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Queue 5 | { 6 | private: 7 | int size; 8 | int front; 9 | int rear; 10 | int* Q; 11 | 12 | public: 13 | Queue() { front = rear = -1; size = 10; Q = new int[size]; } 14 | Queue(int size) { front = rear = -1; this->size = size; Q = new int[this->size]; } 15 | 16 | void enque(int x); 17 | int deque(); 18 | void display(); 19 | }; 20 | 21 | void Queue::enque(int x) 22 | { 23 | if (rear == size - 1) 24 | cout << "Queue full."; 25 | else 26 | { 27 | rear++; 28 | Q[rear] = x; 29 | } 30 | } 31 | 32 | int Queue::deque() 33 | { 34 | int x = -1; 35 | if (front == rear) 36 | { 37 | cout << "Queue is empty"; 38 | } 39 | 40 | else 41 | { 42 | x = Q[front + 1]; 43 | front++; 44 | } 45 | 46 | return x; 47 | } 48 | 49 | 50 | void Queue::display() 51 | { 52 | for (int i = front + 1;i <= rear;i++) 53 | cout << Q[i] << " "; 54 | } 55 | 56 | 57 | 58 | int main() 59 | { 60 | std::cout << "Queue using class!\n"; 61 | Queue q(5); 62 | q.enque(10); 63 | q.enque(6); 64 | q.enque(9); 65 | 66 | q.display(); 67 | 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /Add Code Here/CPP/Insertion_Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* Function to print an array */ 5 | void printArray(int arr[], int size) 6 | { 7 | int i; 8 | for (i=0; i < size; i++) 9 | printf("%d ", arr[i]); 10 | printf("\n"); 11 | } 12 | 13 | // https://practice.geeksforgeeks.org/problems/insertion-sort/1? 14 | 15 | class Solution 16 | { 17 | public: 18 | void insert(int arr[], int i) 19 | { 20 | //code here 21 | int key=arr[i],j=i-1; 22 | while(j>=0 && arr[j]>key){ 23 | arr[j+1]=arr[j]; 24 | j--; 25 | } 26 | arr[j+1]=key; 27 | 28 | } 29 | public: 30 | //Function to sort the array using insertion sort algorithm. 31 | void insertionSort(int arr[], int n) 32 | { 33 | //code here 34 | for(int i=1;i= l) { 6 | int mid1 = l + (r - l) / 3; 7 | int mid2 = r - (r - l) / 3; 8 | 9 | if (ar[mid1] == key) { 10 | return mid1; 11 | } 12 | if (ar[mid2] == key) { 13 | return mid2; 14 | } 15 | 16 | if (key < ar[mid1]) { 17 | return search(l, mid1 - 1, key, ar); 18 | } 19 | else if (key > ar[mid2]) { 20 | return search(mid2 + 1, r, key, ar); 21 | } 22 | else { 23 | return search(mid1 + 1, mid2 - 1, key, ar); 24 | } 25 | } 26 | return -1; 27 | } 28 | public static void main(String args[]) 29 | { 30 | int l, r, p, key; 31 | int ar[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 32 | l = 0; 33 | r = 9; 34 | key = 5; 35 | p = search(l, r, key, ar); 36 | System.out.println("Index of " + key + " is " + p); 37 | key = 50; 38 | p = search(l, r, key, ar); 39 | System.out.println("Index of " + key + " is " + p); 40 | } 41 | } -------------------------------------------------------------------------------- /Add Code Here/Java/ProjectClass.java: -------------------------------------------------------------------------------- 1 | /* 2 | This is the 3rd Part of the Question 10 in the Assignment Regarding Abstract classes and methods 3 | the object code is Project_Main.java 4 | */ 5 | // Abstract base class providing a common interface 6 | public abstract class ProjectClass { 7 | // Common method that all subclasses should implement 8 | abstract void debug(); 9 | 10 | // Other common methods can be included here if needed 11 | } 12 | 13 | // Subclass 1 inheriting from the abstract base class 14 | class Subclass1 extends ProjectClass { 15 | @Override 16 | void debug() { 17 | System.out.println("Debugging Subclass1..."); 18 | // Implement debug-specific functionality here 19 | } 20 | } 21 | 22 | // Subclass 2 inheriting from the abstract base class 23 | class Subclass2 extends ProjectClass { 24 | @Override 25 | void debug() { 26 | System.out.println("Debugging Subclass2..."); 27 | // Implement debug-specific functionality here 28 | } 29 | } 30 | 31 | // Subclass 3 inheriting from the abstract base class 32 | class Subclass3 extends ProjectClass { 33 | @Override 34 | void debug() { 35 | System.out.println("Debugging Subclass3..."); 36 | // Implement debug-specific functionality here 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/intersection_linked_lists.java: -------------------------------------------------------------------------------- 1 | // Given two linked lists, the task is to complete the function findIntersection(), that returns the intersection of two linked lists. Each of the two linked list contains distinct node values. 2 | 3 | // Example 1: 4 | 5 | // Input: 6 | // LinkedList1: 9->6->4->2->3->8 7 | // LinkedList2: 1->2->8->6 8 | // Output: 6 2 8 9 | 10 | // Problem Link: https://practice.geeksforgeeks.org/problems/intersection-of-two-linked-list/1 11 | // Code: 12 | 13 | class Solution 14 | { 15 | public static Node findIntersection(Node head1, Node head2) 16 | { 17 | // add your code here 18 | // return the head of intersection list\ 19 | Node intersection=new Node(2); 20 | Node run=intersection; 21 | 22 | HashSet set=new HashSet<>(); 23 | Node temp=head2; 24 | while(temp!=null){ 25 | set.add(temp.data); 26 | temp=temp.next; 27 | } 28 | 29 | Node temp2=head1; 30 | while(temp2!=null){ 31 | if(set.contains(temp2.data)){ 32 | temp=new Node(temp2.data); 33 | run.next=temp; 34 | run=temp; 35 | } 36 | temp2=temp2.next; 37 | } 38 | 39 | return intersection.next; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Add Code Here/Java/Shape.java: -------------------------------------------------------------------------------- 1 | /* 2 | This is the Question 10 of the Assignemnet which is anout Abstract Classes and Methods 3 | the Object code is Main.java 4 | */ 5 | // Base class Shape 6 | public abstract class Shape { 7 | abstract void draw(); 8 | abstract void erase(); 9 | } 10 | 11 | // Subclass Circle 12 | class Circle extends Shape { 13 | @Override 14 | void draw() { 15 | System.out.println("Drawing a circle"); 16 | } 17 | 18 | @Override 19 | void erase() { 20 | System.out.println("Erasing a circle"); 21 | } 22 | } 23 | 24 | // Subclass Triangle 25 | class Triangle extends Shape { 26 | @Override 27 | void draw() { 28 | System.out.println("Drawing a triangle"); 29 | } 30 | 31 | @Override 32 | void erase() { 33 | System.out.println("Erasing a triangle"); 34 | } 35 | } 36 | 37 | // Subclass Square 38 | class Square extends Shape { 39 | @Override 40 | void draw() { 41 | System.out.println("Drawing a square"); 42 | } 43 | 44 | @Override 45 | void erase() { 46 | System.out.println("Erasing a square"); 47 | } 48 | } 49 | 50 | 51 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/Trafficlight.java: -------------------------------------------------------------------------------- 1 | //TrafficLight.java 2 | public class TrafficLight { 3 | private String color; 4 | private int duration; 5 | 6 | public TrafficLight(String color, int duration) { 7 | this.color = color; 8 | this.duration = duration; 9 | } 10 | 11 | public void changeColor(String newColor) { 12 | color = newColor; 13 | } 14 | 15 | public boolean isRed() { 16 | return color.equals("red"); 17 | } 18 | 19 | public boolean isGreen() { 20 | return color.equals("green"); 21 | } 22 | 23 | public int getDuration() { 24 | return duration; 25 | } 26 | 27 | public void setDuration(int duration) { 28 | this.duration = duration; 29 | } 30 | }//Main.java 31 | public class Main { 32 | public static void main(String[] args) { 33 | TrafficLight light = new TrafficLight("red", 30); 34 | 35 | System.out.println("The light is red: " + light.isRed()); 36 | System.out.println("The light is green: " + light.isGreen()); 37 | 38 | light.changeColor("green"); 39 | 40 | System.out.println("The light is now green: " + light.isGreen()); 41 | System.out.println("The light duration is: " + light.getDuration()); 42 | light.setDuration(20); 43 | System.out.println("The light duration is now: " + light.getDuration()); 44 | } 45 | } -------------------------------------------------------------------------------- /Add Code Here/Java/checkstringsanagram.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Scanner; 3 | 4 | class Main { 5 | public static void main(String[] args) { 6 | 7 | // create an object of Scanner class 8 | Scanner input = new Scanner(System.in); 9 | 10 | // take input from users 11 | System.out.print("Enter first String: "); 12 | String str1 = input.nextLine(); 13 | System.out.print("Enter second String: "); 14 | String str2 = input.nextLine(); 15 | 16 | // check if length is same 17 | if(str1.length() == str2.length()) { 18 | 19 | // convert strings to char array 20 | char[] charArray1 = str1.toCharArray(); 21 | char[] charArray2 = str2.toCharArray(); 22 | 23 | // sort the char array 24 | Arrays.sort(charArray1); 25 | Arrays.sort(charArray2); 26 | 27 | // if sorted char arrays are same 28 | // then the string is anagram 29 | boolean result = Arrays.equals(charArray1, charArray2); 30 | 31 | if(result) { 32 | System.out.println(str1 + " and " + str2 + " are anagram."); 33 | } 34 | else { 35 | System.out.println(str1 + " and " + str2 + " are not anagram."); 36 | } 37 | } 38 | else { 39 | System.out.println(str1 + " and " + str2 + " are not anagram."); 40 | } 41 | 42 | input.close(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Add Code Here/Python/Differential Equation Solver/Differential_Equation_Solver.py: -------------------------------------------------------------------------------- 1 | import random 2 | import math 3 | 4 | def euler_method(func, initial_condition, x_range, step_size): 5 | x_start, x_end = x_range 6 | x_values = [] 7 | y_values = [] 8 | 9 | x = x_start 10 | y = initial_condition 11 | 12 | while x <= x_end: 13 | x_values.append(x) 14 | y_values.append(y) 15 | 16 | # Update y using Euler's method 17 | y += step_size * func(x, y) 18 | 19 | x += step_size 20 | 21 | return x_values, y_values 22 | 23 | def diff_input(): 24 | # ... (Your input code for the ODE and initial conditions) 25 | 26 | if __name__ == '__main__': 27 | funcs, init_conditions, x_range, n = diff_input() 28 | 29 | # Ensure that you have the appropriate functions and initial conditions 30 | if len(funcs) != 1 or len(init_conditions) != 1: 31 | print("Please provide a single first-order ODE and initial conditions.") 32 | else: 33 | func = lambda x, y: eval(funcs[0]) # Convert the user-provided function to a Python function 34 | x_values, y_values = euler_method(func, init_conditions[0], x_range, step_size=0.1) # Adjust step_size as needed 35 | print("Solved ODE values:") 36 | for x, y in zip(x_values, y_values): 37 | print(f"x: {x}, y: {y}") 38 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/max_sum_of_array.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | class Ashish { 4 | 5 | static int maxSum(int arr[], int n) 6 | { 7 | // Compute sum of all array elements 8 | int cum_sum = 0; 9 | for (int i = 0; i < n; i++) 10 | cum_sum += arr[i]; 11 | 12 | // Compute sum of i*arr[i] for 13 | // initial configuration. 14 | int curr_val = 0; 15 | for (int i = 0; i < n; i++) 16 | curr_val += i * arr[i]; 17 | 18 | // Initialize result 19 | int res = curr_val; 20 | 21 | // Compute values for other iterations 22 | for (int i = 1; i < n; i++) 23 | { 24 | // Compute next value using previous 25 | // value in O(1) time 26 | int next_val = curr_val - (cum_sum - 27 | arr[i-1]) + arr[i-1] * 28 | (n-1); 29 | 30 | // Update current value 31 | curr_val = next_val; 32 | 33 | // Update result if required 34 | res = Math.max(res, next_val); 35 | } 36 | 37 | return res; 38 | } 39 | 40 | // Driver code 41 | public static void main(String[] args) 42 | { 43 | int arr[] = {8, 3, 1, 2}; 44 | int n = arr.length; 45 | System.out.println(maxSum(arr, n)); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Add Code Here/Java/binary_search.java: -------------------------------------------------------------------------------- 1 | public class BinarySearch { 2 | public static int binarySearch(int[] arr, int target) { 3 | int left = 0; 4 | int right = arr.length - 1; 5 | 6 | while (left <= right) { 7 | int mid = left + (right - left) / 2; 8 | 9 | // If the target is found at the middle index, return it. 10 | if (arr[mid] == target) { 11 | return mid; 12 | } 13 | 14 | // If the target is smaller, search the left half. 15 | if (arr[mid] > target) { 16 | right = mid - 1; 17 | } 18 | // If the target is larger, search the right half. 19 | else { 20 | left = mid + 1; 21 | } 22 | } 23 | 24 | // If the target element is not found, return -1. 25 | return -1; 26 | } 27 | 28 | public static void main(String[] args) { 29 | int[] sortedArray = {1, 3, 5, 7, 9, 11, 13, 15}; 30 | int targetElement = 7; 31 | 32 | int result = binarySearch(sortedArray, targetElement); 33 | 34 | if (result != -1) { 35 | System.out.println("Element " + targetElement + " found at index " + result + "."); 36 | } else { 37 | System.out.println("Element " + targetElement + " not found in the array."); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Add Code Here/CPP/Bubble_Sort.cpp: -------------------------------------------------------------------------------- 1 | // C program for implementation of Bubble sort 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | // swapping the elements 7 | void swap(int *xp, int *yp) 8 | { 9 | int temp = *xp; 10 | *xp = *yp; 11 | *yp = temp; 12 | } 13 | 14 | 15 | // https://practice.geeksforgeeks.org/problems/bubble-sort/1 16 | 17 | class Solution 18 | { 19 | public: 20 | //Function to sort the array using bubble sort algorithm. 21 | void bubbleSort(int a[], int n) 22 | { 23 | // Your code here 24 | for(int i=0;ia[j+1]) 27 | swap(a[j],a[j+1]); 28 | } 29 | } 30 | } 31 | }; 32 | 33 | 34 | /* Function to print an array */ 35 | void printArray(int arr[], int size) 36 | { 37 | int i; 38 | for (i=0; i < size; i++) 39 | printf("%d ", arr[i]); 40 | printf("\n"); 41 | } 42 | 43 | int main() 44 | { 45 | int arr[1000],n,T,i; 46 | 47 | scanf("%d",&T); 48 | 49 | while(T--){ 50 | 51 | scanf("%d",&n); 52 | 53 | for(i=0;i[] bucket = new ArrayList[n]; 12 | 13 | // Create empty buckets 14 | for (int i = 0; i < n; i++) 15 | bucket[i] = new ArrayList(); 16 | 17 | // Add elements into the buckets 18 | for (int i = 0; i < n; i++) { 19 | int bucketIndex = (int) arr[i] * n; 20 | bucket[bucketIndex].add(arr[i]); 21 | } 22 | 23 | // Sort the elements of each bucket 24 | for (int i = 0; i < n; i++) { 25 | Collections.sort((bucket[i])); 26 | } 27 | 28 | // Get the sorted array 29 | int index = 0; 30 | for (int i = 0; i < n; i++) { 31 | for (int j = 0, size = bucket[i].size(); j < size; j++) { 32 | arr[index++] = bucket[i].get(j); 33 | } 34 | } 35 | } 36 | 37 | // Driver code 38 | public static void main(String[] args) { 39 | BucketSort b = new BucketSort(); 40 | float[] arr = { (float) 0.42, (float) 0.32, (float) 0.33, (float) 0.52, (float) 0.37, (float) 0.47, 41 | (float) 0.51 }; 42 | b.bucketSort(arr, 7); 43 | 44 | for (float i : arr) 45 | System.out.print(i + " "); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/GnomeSort.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class GnomeSort { 4 | 5 | public static void main(String[] args) { 6 | Scanner scanner = new Scanner(System.in); 7 | System.out.print("Enter the number of elements: "); 8 | int n = scanner.nextInt(); 9 | 10 | int[] arr = new int[n]; 11 | System.out.println("Enter " + n + " elements:"); 12 | for (int i = 0; i < n; i++) { 13 | arr[i] = scanner.nextInt(); 14 | } 15 | 16 | gnomeSort(arr); 17 | 18 | System.out.println("Sorted array:"); 19 | printArray(arr); 20 | } 21 | 22 | // Function to print an array 23 | static void printArray(int[] arr) { 24 | for (int num : arr) { 25 | System.out.print(num + " "); 26 | } 27 | System.out.println(); 28 | } 29 | 30 | // Function to perform Gnome Sort 31 | static void gnomeSort(int[] arr) { 32 | int n = arr.length; 33 | int index = 0; 34 | 35 | while (index < n) { 36 | if (index == 0) { 37 | index++; 38 | } 39 | 40 | if (arr[index] >= arr[index - 1]) { 41 | index++; 42 | } else { 43 | int temp = arr[index]; 44 | arr[index] = arr[index - 1]; 45 | arr[index - 1] = temp; 46 | index--; 47 | } 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Add Code Here/CPP/cycle_detection_using_dsu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int N = 1e5 + 6; 5 | 6 | vector parent(N); 7 | vector sz(N); 8 | void make_set(int v){ 9 | parent[v] = v; 10 | sz[v] =1; 11 | } 12 | 13 | int find_set(int v){ 14 | if (parent[v]==v) return v; 15 | return parent[v] = find_set(parent[v]); 16 | } 17 | 18 | void union_sets(int a,int b){ 19 | a = find_set(a); 20 | b = find_set(b); 21 | if (a!=b){ 22 | if (sz[a]>sz[b]){ 23 | parent[b] = a; 24 | sz[a] += sz[b]; 25 | } 26 | else{ 27 | parent[a] = b; 28 | sz[b] += sz[a]; 29 | } 30 | } 31 | } 32 | 33 | int main(){ 34 | for (int i=0;i>n>>m; 38 | vector> edges; 39 | for (int i=0;i>u>>v; 41 | edges.push_back({u,v}); 42 | } 43 | bool cycle = 0; 44 | for (auto ele : edges){ 45 | int u = ele[0]; 46 | int v = ele[1]; 47 | int x = find_set(u); 48 | int y = find_set(v); 49 | if (x==y){ 50 | cycle = 1; 51 | } 52 | else{ 53 | union_sets(u,v); 54 | } 55 | } 56 | if (cylcle){ 57 | cout << "Cycle Present\n"; 58 | } 59 | else{ 60 | cout << "Cycle Not Present\n; 61 | } 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /Add Code Here/Python/L2_regularization_Implementation_from_scratch.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | class RidgeRegression: 4 | def __init__(self, alpha=1.0): 5 | self.alpha = alpha # Regularization parameter (λ) 6 | 7 | def fit(self, X, y): 8 | # Add a column of ones for the bias term 9 | X = np.column_stack((np.ones(len(X)), X)) 10 | 11 | # Identity matrix with 0 in the first element for bias term 12 | I = np.identity(X.shape[1]) 13 | I[0, 0] = 0 14 | 15 | # Closed-form solution for ridge regression 16 | self.theta = np.linalg.inv(X.T @ X + self.alpha * I) @ X.T @ y 17 | 18 | def predict(self, X): 19 | # Add a column of ones for the bias term 20 | X = np.column_stack((np.ones(len(X)), X)) 21 | 22 | # Make predictions 23 | return X @ self.theta 24 | 25 | # Example usage: 26 | if __name__ == "__main__": 27 | # Generate some sample data 28 | np.random.seed(0) 29 | X = 2 * np.random.rand(100, 3) 30 | y = 4 + X[:, 0] + 3 * X[:, 1] + 2 * X[:, 2] + np.random.randn(100) 31 | 32 | # Create and fit the Ridge Regression model 33 | ridge_reg = RidgeRegression(alpha=1.0) 34 | ridge_reg.fit(X, y) 35 | 36 | # Make predictions 37 | y_pred = ridge_reg.predict(X) 38 | 39 | # Print the coefficients and intercept 40 | print("Coefficients:", ridge_reg.theta[1:]) 41 | print("Intercept:", ridge_reg.theta[0]) 42 | -------------------------------------------------------------------------------- /Add Code Here/CPP/Selection_Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void swap(int *xp, int *yp) 4 | { 5 | int temp = *xp; 6 | *xp = *yp; 7 | *yp = temp; 8 | } 9 | 10 | // https://practice.geeksforgeeks.org/problems/selection-sort/1? 11 | 12 | class Solution 13 | { 14 | public: 15 | int select(int arr[], int i, int n) 16 | { 17 | // code here such that selectionSort() sorts arr[] 18 | int mini=i; 19 | for(int j=i+1;j 2 | using namespace std; 3 | 4 | void dfs(int node, int parent, vector& vis, vector& tin, 5 | vector& low, int& timer, vector adj[], vector& isArticulation) { 6 | 7 | vis[node] = 1; 8 | tin[node] = low[node] = timer++; 9 | int child = 0; 10 | for (auto it : adj[node]) { 11 | if (it == parent) continue; 12 | if (!vis[it]) { 13 | dfs(it, node, vis, tin, low, timer, adj, isArticulation); 14 | low[node] = min(low[node], low[it]); 15 | if (low[it] >= tin[node] && parent != -1) { 16 | isArticulation[node] = 1; 17 | } 18 | child++; 19 | } 20 | else { 21 | low[node] = min(low[node], tin[it]); 22 | } 23 | } 24 | if (parent == -1 && child > 1) { 25 | isArticulation[node] = 1; 26 | } 27 | } 28 | 29 | int main() { 30 | int n, m; 31 | cin >> n >> m; 32 | vector adj[n]; 33 | 34 | for (int i = 0; i < m; i++) { 35 | int u, v; 36 | cin >> u >> v; 37 | adj[u].push_back(v); 38 | adj[v].push_back(u); 39 | } 40 | 41 | vector tin(n, -1); 42 | vector low(n, -1); 43 | vector vis(n, 0); 44 | vector isArticulation(n, 0); 45 | int timer = 0; 46 | for (int i = 0; i < n; i++) { 47 | if (!vis[i]) { 48 | dfs(i, -1, vis, tin, low, timer, adj, isArticulation); 49 | } 50 | } 51 | for (int i = 0; i < n; i++) { 52 | if (isArticulation[i]) { 53 | cout << i << " is a articulation point\n"; 54 | } 55 | } 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /Add Code Here/CPP/basic-math.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | // Function to add two or more numbers 4 | double add(int a[], int len){ 5 | double sum = 0; 6 | for(int i = 0; i < len; i++){ 7 | sum += a[i]; 8 | } 9 | return sum; 10 | } 11 | // Function to subtract between two or more numbers 12 | double sub(int a[], int len){ 13 | double sum = 0; 14 | for(int i = 0; i < len; i++){ 15 | sum -= a[i]; 16 | } 17 | return sum; 18 | } 19 | // Function to multiply two or more numbers 20 | double mul(int a[], int len){ 21 | double sum = 1; 22 | for(int i = 0; i < len; i++){ 23 | sum *= a[i]; 24 | } 25 | return sum; 26 | } 27 | // Function to divide between two or more numbers 28 | float div(int a[], int len){ 29 | float sum = 1; 30 | for(int i = 0; i < len; i++){ 31 | sum /= a[i]; 32 | } 33 | return sum; 34 | } 35 | // Function to find average of two or more numbers 36 | float avg(int a[], int len){ 37 | float sum = 0; 38 | for(int i = 0; i < len; i++){ 39 | sum += a[i]; 40 | } 41 | return sum/len; 42 | } 43 | int main(){ 44 | int a[5] = {1,2,3,5,9}; 45 | //cout << add(a, 5) << "\n" << sub(a, 5) << "\n" << mul(a, 5) << "\n" << div(a, 5) << "\n" << avg(a, 5) << endl; For test only 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Add Code Here/Java/Date.java: -------------------------------------------------------------------------------- 1 | /* 2 | This is the question number 6 in the java assignment 3 | the object code can be found as the DateTest class. 4 | refer the DateTest Class to have a good idea regarding this code and use of constructer, set&get methods used. 5 | */ 6 | public class Date { 7 | private int month,day,year; 8 | 9 | public Date(int month, int day, int year) 10 | { 11 | this.month=month; 12 | this.day=day; 13 | this.year=year; 14 | 15 | } 16 | 17 | //Getter Method for Month 18 | public int getMonth() 19 | { 20 | return month; 21 | } 22 | 23 | // Setter Method for Month 24 | public void setMonth(int month) 25 | { 26 | this.month=month; 27 | } 28 | 29 | //Gettter Method for Day 30 | public int getDay() 31 | { 32 | return day; 33 | } 34 | 35 | //Setter Method for Day 36 | public void setDay(int day) 37 | { 38 | this.day=day; 39 | } 40 | 41 | 42 | //Getter Method for Year 43 | public int getYear() 44 | { 45 | return year; 46 | } 47 | 48 | //Setter Method for Year 49 | public void setYear(int year) 50 | { 51 | this.year=year; 52 | } 53 | 54 | public void displayDate() 55 | { 56 | System.out.print(" "+month); 57 | System.out.print("/"+day); 58 | System.out.print("/"+year); 59 | } 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | } 68 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/banking.java: -------------------------------------------------------------------------------- 1 | class Customer { 2 | int amount=10000; 3 | synchronized void withdraw(int amount) { 4 | System.out.println("Going to Withdraw..."); 5 | if(this.amount= 0.5 else 0 for p in probabilities] 26 | 27 | if __name__ == "__main__": 28 | # Sample data 29 | X = np.array([[2.5, 3.5], [1.5, 2.5], [3.5, 4.5], [2.5, 2.5]]) 30 | y = np.array([0, 0, 1, 1]) 31 | 32 | # Create and train the logistic regression model 33 | model = LogisticRegression(learning_rate=0.1, num_iterations=1000) 34 | model.fit(X, y) 35 | 36 | # Make predictions 37 | new_data = np.array([[2.0, 3.0], [4.0, 4.0]]) 38 | predictions = model.predict(new_data) 39 | 40 | print("Predictions:", predictions) 41 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/waveprint.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | class waveprint { 4 | 5 | // Function definition 6 | static void pattern(int wave_height, 7 | int wave_length) 8 | { 9 | int i, j, k, e, n, count, x; 10 | e = 2; 11 | x = 1; 12 | 13 | // for loop for height 14 | // of wave 15 | for (i = 0; i < wave_height; i++) 16 | { 17 | for (j = wave_height; j <= wave_height + i; j++) 18 | System.out.print(" "); 19 | 20 | // for loop for wave 21 | // length 22 | for (count = 1; count <= wave_length; count++) 23 | { 24 | // checking for intermediate 25 | // spaces 26 | for (n = (wave_height + wave_height - 2); n >= x; n--) 27 | System.out.print(" "); 28 | 29 | for (k = 1; k <= e; k++) 30 | { 31 | if (k == 1) 32 | System.out.print("/"); 33 | else if (k == e) 34 | System.out.print("\\"); 35 | else 36 | System.out.print(" "); 37 | } 38 | } 39 | 40 | // incrementing counters 41 | // value by two 42 | x = x + 2; 43 | e = e + 2; 44 | 45 | System.out.println(); 46 | } 47 | } 48 | 49 | // Driver code 50 | public static void main(String args[]) 51 | { 52 | // change value to decrease or 53 | // increase the height of wave 54 | int wave_height = 4; 55 | 56 | // change value to decrease or 57 | // increase the length of wave 58 | int wave_length = 4; 59 | 60 | pattern(wave_height, wave_length); 61 | } 62 | } 63 | 64 | // This code is contributed by Nikita Tiwari. 65 | -------------------------------------------------------------------------------- /Add Code Here/CPP/countsort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | template 6 | void Print(T& vec, int n, string s){ 7 | cout << s << ": [" << flush; 8 | for (int i=0; i max){ 21 | max = A[i]; 22 | } 23 | } 24 | return max; 25 | } 26 | 27 | void CountSort(int A[], int n){ 28 | int max = Max(A, n); 29 | 30 | // Create count array 31 | int* count = new int [max + 1]; 32 | 33 | // Initialize count array with 0 34 | for (int i=0; i 0){ 48 | A[i++] = j; 49 | count[j]--; 50 | } else { 51 | j++; 52 | } 53 | } 54 | 55 | // Delete heap memory 56 | delete [] count; 57 | } 58 | 59 | int main() { 60 | 61 | int A[] = {2, 5, 8, 12, 3, 6, 7, 10}; 62 | int n = sizeof(A)/sizeof(A[0]); 63 | 64 | Print(A, n, "\t\tA"); 65 | CountSort(A, n); 66 | Print(A, n, " Sorted A"); 67 | 68 | return 0; 69 | } -------------------------------------------------------------------------------- /Add Code Here/CPP/Quick_Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | /* Function to print an array */ 6 | void printArray(int arr[], int size) 7 | { 8 | int i; 9 | for (i=0; i < size; i++) 10 | printf("%d ", arr[i]); 11 | printf("\n"); 12 | } 13 | 14 | // https://practice.geeksforgeeks.org/problems/quick-sort/1? 15 | 16 | class Solution 17 | { 18 | public: 19 | //Function to sort an array using quick sort algorithm. 20 | void quickSort(int arr[], int low, int high) 21 | { 22 | int index; 23 | 24 | if(lowpivot) 40 | j--; 41 | if(i 2 | #include 3 | #include 4 | 5 | 6 | using namespace std; 7 | 8 | 9 | int main() 10 | 11 | { 12 | 13 | int n; 14 | 15 | float a,b,c,s,lt,bt,side,r,area; 16 | 17 | cout<<"Press 1 for Area of Square\n"; 18 | 19 | cout<<"Press 2 for Area of Rectangle\n"; 20 | 21 | cout<<"Press 3 for Area of Triangle\n"; 22 | 23 | cout<<"Press 4 for Area of Circle\n\n"; 24 | 25 | cin>>n; 26 | 27 | 28 | 29 | switch(n) 30 | 31 | { 32 | 33 | case 1: 34 | 35 | cout<<"Enter the side of square\n"; 36 | 37 | cin>>side; 38 | 39 | area=side*side; 40 | 41 | cout<<"Area="<>lt>>bt; 52 | 53 | area=lt*bt; 54 | 55 | cout<<"Area="<>a>>b>>c; 66 | 67 | s=(a+b+c)/2; 68 | 69 | area=sqrt(s*(s-a)*(s-b)*(s-c)); 70 | 71 | cout<<"Area="<>r; 82 | 83 | area=3.14*r*r; 84 | 85 | cout<<"Area="< 2 or col < 0 or col > 2 or board[row][col] != " ": 30 | print("Invalid move. Try again.") 31 | continue 32 | 33 | board[row][col] = player_turn 34 | 35 | if check_winner(board, player_turn): 36 | print_board(board) 37 | print(f"Player {player_turn} wins!") 38 | break 39 | elif is_full(board): 40 | print_board(board) 41 | print("It's a draw!") 42 | break 43 | 44 | player_turn = "O" if player_turn == "X" else "X" 45 | 46 | if __name__ == "__main__": 47 | main() 48 | -------------------------------------------------------------------------------- /Add Code Here/Python/Differential Equation Solver/testing_diff_eq.py: -------------------------------------------------------------------------------- 1 | from prettytable import PrettyTable 2 | import matplotlib.pyplot as plt 3 | from Euler import euler_method 4 | from Euler_Caushy import euler_caushy 5 | from Runge_Kutta import runge_kutta 6 | from linear_function import linear_function 7 | 8 | 9 | def comparison(): 10 | eul = euler_method(["math.sin(x+y)"], [0], [0, 1], 100)[1] 11 | eul_cau = euler_caushy(["math.sin(x+y)"], [0], [0, 1], 100) 12 | rung = runge_kutta(["math.sin(x+y)"], [0], [0, 1], 100) 13 | 14 | mytable = PrettyTable() 15 | mytable.title = "y\' = math.sin(x+y)" 16 | mytable.add_column("i", [i[0] for i in eul]) 17 | mytable.add_column("xi", [round(i[1], 2) for i in eul]) 18 | mytable.add_column("Эйлер", [i[2] for i in eul]) 19 | mytable.add_column("Эйлер-Коши", [i[2] for i in eul_cau]) 20 | mytable.add_column("Рунге-Кутта", [i[2] for i in rung]) 21 | print(mytable) 22 | 23 | x = [round(i[1], 2) for i in eul] 24 | y1 = [i[2] for i in eul] 25 | y2 = [i[2] for i in eul_cau] 26 | y3 = [i[2] for i in rung] 27 | plt.plot(x, y1, '-', x, y2, '--', x, y3, '-.') 28 | plt.show() 29 | 30 | # подготовка данных к МНК 31 | euler_mnk = [[x[i], y1[i]] for i in range(100)] 32 | euler_c_mnk = [[x[i], y2[i]] for i in range(100)] 33 | runge_mnk = [[x[i], y3[i]] for i in range(100)] 34 | 35 | print("Результат МНК для Эйлера: ", linear_function(euler_mnk)[0]) 36 | print("Результат МНК для Эйлера-Коши: ", linear_function(euler_c_mnk)[0]) 37 | print("Результат МНК для Рунге-Кутты: ", linear_function(runge_mnk)[0]) 38 | 39 | 40 | comparison() 41 | -------------------------------------------------------------------------------- /Add Code Here/CPP/add_two_numbers_represented_by_linked_lists.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { 14 | ListNode*dummy = new ListNode(); 15 | //a node pointing to dummy 16 | ListNode*temp=dummy; 17 | int carry=0; 18 | 19 | //at last when l1== NULL and l2==NULL and carry!=0 do one more iteration and add carry=1 as a node 20 | while(l1 != NULL || l2!= NULL || carry==1){ 21 | int sum=0; 22 | 23 | //sum from l1 24 | if(l1!=NULL){ 25 | sum+=l1->val; 26 | //move l1 to next node 27 | l1=l1->next; 28 | } 29 | 30 | //sum from l2 31 | if(l2!=NULL){ 32 | sum+=l2->val; 33 | //move l2 to next node 34 | l2=l2->next; 35 | } 36 | 37 | //add sum alongwith carry 38 | sum+=carry; 39 | carry=sum/10; 40 | 41 | //add sum%10 as a new node 42 | ListNode* node= new ListNode(sum%10); 43 | //make temp point to new node created 44 | temp->next = node ; 45 | //move temp to next position 46 | temp=temp->next; 47 | } 48 | 49 | //now our answer is node from duumy->next to last 50 | return dummy->next; 51 | } 52 | }; 53 | -------------------------------------------------------------------------------- /Add Code Here/Python/Differential Equation Solver/Euler_Caushy.py: -------------------------------------------------------------------------------- 1 | from Euler import euler_method 2 | import math 3 | 4 | 5 | def euler_caushy(function=None, y0=None, a_b=None, n=None): 6 | if function is None or y0 is None or a_b is None or n is None: 7 | euler = euler_method() 8 | function = euler[0] 9 | table_euler = euler[1] 10 | else: 11 | table_euler = euler_method(function, y0, a_b, n)[1] 12 | 13 | a = table_euler[0][1] 14 | b = table_euler[-1][1] 15 | n = len(table_euler) - 1 16 | h = (b - a) / n 17 | 18 | yzuv = table_euler[0][2:] 19 | yzuv = list(yzuv) 20 | x0 = a 21 | 22 | answer_list = [(0, x0, *yzuv)] 23 | g = len(yzuv) 24 | while len(yzuv) < 4: 25 | yzuv.append(None) 26 | 27 | f1, f2, f3, f4 = [None] * 4 28 | f = [f1, f2, f3, f4] 29 | new_f1, new_f2, new_f3, new_f4 = [None] * 4 30 | new_f = [new_f1, new_f2, new_f3, new_f4] 31 | yi, zi, ui, vi = [None] * 4 32 | k = [yi, zi, ui, vi] 33 | 34 | for i in range(1, n+1): 35 | x, y, z, u, v = x0, *yzuv 36 | 37 | for j in range(g): 38 | f[j] = eval(function[j]) 39 | 40 | new_yzuv = yzuv 41 | new_yzuv[0:g] = table_euler[i][2:] 42 | x, y, z, u, v = x0+h, *new_yzuv 43 | for j in range(g): 44 | new_f[j] = eval(function[j]) 45 | 46 | for j in range(g): 47 | k[j] = yzuv[j] + (h/2) * (f[j] + new_f[j]) 48 | yzuv[j] = k[j] 49 | 50 | x0 += h 51 | answer_list.append((i, x0, *k[0:g])) 52 | 53 | return answer_list 54 | 55 | 56 | if __name__ == "__main__": 57 | print(euler_caushy()) 58 | 59 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/Anagram.java: -------------------------------------------------------------------------------- 1 | // JAVA program to check if two strings 2 | // are anagrams of each other 3 | import java.io.*; 4 | import java.util.*; 5 | 6 | class Anagram { 7 | 8 | static int NO_OF_CHARS = 256; 9 | 10 | /* function to check whether two strings 11 | are anagram of each other */ 12 | static boolean areAnagram(char str1[], char str2[]) 13 | { 14 | // Create 2 count arrays and initialize 15 | // all values as 0 16 | int count1[] = new int[NO_OF_CHARS]; 17 | Arrays.fill(count1, 0); 18 | int count2[] = new int[NO_OF_CHARS]; 19 | Arrays.fill(count2, 0); 20 | int i; 21 | 22 | // For each character in input strings, 23 | // increment count in the corresponding 24 | // count array 25 | for (i = 0; i < str1.length && i < str2.length; 26 | i++) { 27 | count1[str1[i]]++; 28 | count2[str2[i]]++; 29 | } 30 | 31 | // If both strings are of different length. 32 | // Removing this condition will make the program 33 | // fail for strings like "aaca" and "aca" 34 | if (str1.length != str2.length) 35 | return false; 36 | 37 | // Compare count arrays 38 | for (i = 0; i < NO_OF_CHARS; i++) 39 | if (count1[i] != count2[i]) 40 | return false; 41 | 42 | return true; 43 | } 44 | 45 | /* Driver code*/ 46 | public static void main(String args[]) 47 | { 48 | char str1[] = ("gram").toCharArray(); 49 | char str2[] = ("arm").toCharArray(); 50 | 51 | // Function call 52 | if (areAnagram(str1, str2)) 53 | System.out.println("The two strings are" 54 | + " anagram of each other"); 55 | else 56 | System.out.println("The two strings are not" 57 | + " anagram of each other"); 58 | } 59 | } 60 | 61 | 62 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/SubtreeDifference.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | class Graph 4 | { 5 | 6 | static int res; 7 | 8 | static void dfs (int u, int parent, int totalSum, 9 | ArrayList < Integer >[]edge, int subtree[]) 10 | { 11 | int sum = subtree[u]; 12 | 13 | for (int i = 0; i < edge[u].size (); i++) 14 | { 15 | int v = edge[u].get (i); 16 | 17 | if (v != parent) 18 | { 19 | dfs (v, u, totalSum, edge, subtree); 20 | sum += subtree[v]; 21 | } 22 | } 23 | 24 | subtree[u] = sum; 25 | 26 | if (u != 0 && Math.abs (totalSum - 2 * sum) < res) 27 | res = Math.abs (totalSum - 2 * sum); 28 | } 29 | 30 | static int getMinSubtreeSumDifference (int vertex[], int[][]edges, int N) 31 | { 32 | int totalSum = 0; 33 | int[] subtree = new int[N]; 34 | 35 | for (int i = 0; i < N; i++) 36 | { 37 | subtree[i] = vertex[i]; 38 | totalSum += vertex[i]; 39 | } 40 | 41 | @SuppressWarnings ("unchecked") 42 | ArrayList < Integer >[]edge = new ArrayList[N]; 43 | for (int i = 0; i < N; i++) 44 | { 45 | edge[i] = new ArrayList <> (); 46 | } 47 | for (int i = 0; i < N - 1; i++) 48 | { 49 | edge[edges[i][0]].add (edges[i][1]); 50 | edge[edges[i][1]].add (edges[i][0]); 51 | } 52 | 53 | dfs (0, -1, totalSum, edge, subtree); 54 | return res; 55 | } 56 | 57 | public static void main (String[]args) 58 | { 59 | res = Integer.MAX_VALUE; 60 | 61 | int[] vertex = { 4, 2, 1, 6, 3, 5, 2 }; 62 | int[][] edges = { {0, 1}, {0, 2}, 63 | {0, 3}, {2, 4}, 64 | {2, 5}, {3, 6} 65 | }; 66 | int N = vertex.length; 67 | 68 | System.out.println (getMinSubtreeSumDifference (vertex, edges, N)); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Add Code Here/CPP/LinkedListCycle.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given head, the head of a linked list, determine if the linked list has a cycle in it. 3 | There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter. 4 | Return true if there is a cycle in the linked list. Otherwise, return false. 5 | 6 | Example 1: 7 | Input: head = [3,2,0,-4], pos = 1 8 | Output: true 9 | Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed). 10 | 11 | Example 2: 12 | Input: head = [1,2], pos = 0 13 | Output: true 14 | Explanation: There is a cycle in the linked list, where the tail connects to the 0th node. 15 | 16 | Example 3: 17 | Input: head = [1], pos = -1 18 | Output: false 19 | Explanation: There is no cycle in the linked list. 20 | 21 | Constraints: 22 | The number of the nodes in the list is in the range [0, 104]. 23 | -105 <= Node.val <= 105 24 | pos is -1 or a valid index in the linked-list. 25 | */ 26 | /** 27 | * Definition for singly-linked list. 28 | * struct ListNode { 29 | * int val; 30 | * ListNode *next; 31 | * ListNode(int x) : val(x), next(NULL) {} 32 | * }; 33 | */ 34 | class Solution { 35 | public: 36 | bool hasCycle(ListNode *head) { 37 | if (!head || !head->next) { 38 | return false; 39 | } 40 | 41 | ListNode *runner = head; 42 | ListNode *walker = head; 43 | 44 | do { 45 | runner = runner->next ? runner->next->next : nullptr; 46 | walker = walker->next; 47 | } while (runner && walker && runner != walker); 48 | 49 | return runner == walker; 50 | } 51 | }; 52 | -------------------------------------------------------------------------------- /Add Code Here/JavaScript/linearRegressionInJS.js: -------------------------------------------------------------------------------- 1 | function linearRegression(x, y) { 2 | const n = x.length; 3 | 4 | let sumX = 0; 5 | let sumY = 0; 6 | let sumXY = 0; 7 | let sumX2 = 0; 8 | 9 | for (let i = 0; i < n; i++) { 10 | sumX += x[i]; 11 | sumY += y[i]; 12 | sumXY += x[i] * y[i]; 13 | sumX2 += x[i] ** 2; 14 | } 15 | 16 | const m = (n * sumXY - sumX * sumY) / (n * sumX2 - sumX ** 2); 17 | const b = (sumY - m * sumX) / n; 18 | 19 | return { slope: m, intercept: b }; 20 | } 21 | 22 | function predict(x, slope, intercept) { 23 | return slope * x + intercept; 24 | } 25 | 26 | // Example usage 27 | const x = [1, 2, 3, 4, 5]; 28 | const y = [2, 3, 3, 4, 5]; 29 | 30 | const result = linearRegression(x, y); 31 | console.log(`Slope: ${result.slope}, Intercept: ${result.intercept}`); 32 | 33 | // Make a prediction 34 | const xPrediction = 6; 35 | const yPrediction = predict(xPrediction, result.slope, result.intercept); 36 | console.log(`Prediction for x=${xPrediction}: ${yPrediction}`); 37 | 38 | // Plotting the data 39 | const canvas = document.getElementById('scatterPlot'); 40 | const ctx = canvas.getContext('2d'); 41 | 42 | canvas.width = 400; 43 | canvas.height = 400; 44 | 45 | const scaleX = canvas.width / (Math.max(...x) - Math.min(...x)); 46 | const scaleY = canvas.height / (Math.max(...y) - Math.min(...y)); 47 | 48 | for (let i = 0; i < x.length; i++) { 49 | ctx.fillStyle = 'blue'; 50 | ctx.fillRect((x[i] - Math.min(...x)) * scaleX, canvas.height - y[i] * scaleY, 5, 5); 51 | } 52 | 53 | ctx.beginPath(); 54 | ctx.moveTo(0, canvas.height - predict(0, result.slope, result.intercept) * scaleY); 55 | ctx.lineTo(canvas.width, canvas.height - predict(canvas.width / scaleX, result.slope, result.intercept) * scaleY); 56 | ctx.strokeStyle = 'red'; 57 | ctx.stroke(); 58 | -------------------------------------------------------------------------------- /Add Code Here/CPP/Boolean_matrix.cpp: -------------------------------------------------------------------------------- 1 | // GFG Problem: Boolean Matrix 2 | // https://practice.geeksforgeeks.org/problems/boolean-matrix-problem-1587115620/1 3 | 4 | // Given a boolean matrix of size RxC where each cell contains either 0 or 1, modify it such that if a matrix cell matrix[i][j] is 1 then all the cells in its ith row and jth column will become 1. 5 | 6 | // Solution: 7 | 8 | void booleanMatrix(vector > &matrix) 9 | { 10 | // code here 11 | int n=matrix.size(); 12 | int m=matrix[0].size(); 13 | int row[n]={false}; 14 | int col[m]={false}; 15 | for(int i=0;i>t; 35 | while(t--) 36 | { 37 | int row, col; 38 | cin>> row>> col; 39 | vector > matrix(row); 40 | for(int i=0; i>matrix[i][j]; 46 | } 47 | } 48 | Solution ob; 49 | ob.booleanMatrix(matrix); 50 | for (int i = 0; i < row; ++i) 51 | { 52 | for (int j = 0; j < col; ++j) 53 | { 54 | cout< 2 | #include 3 | using namespace std; 4 | 5 | /* Function to print an array */ 6 | void printArray(int arr[], int size) 7 | { 8 | int i; 9 | for (i=0; i < size; i++) 10 | printf("%d ", arr[i]); 11 | printf("\n"); 12 | } 13 | 14 | // https://practice.geeksforgeeks.org/problems/merge-sort/1? 15 | 16 | class Solution 17 | { 18 | public: 19 | void merge(int arr[], int l, int m, int r) 20 | { 21 | int i=l,j=m+1,k=l,b[100001]; 22 | while(i<=m && j<=r){ 23 | if(arr[i]= arr[left]) and (loc != left): 13 | left += 1 14 | if loc == left: 15 | flag = 1 16 | else: 17 | temp = arr[loc] 18 | arr[loc], arr[left] = arr[left], arr[loc] 19 | loc = left 20 | return loc 21 | 22 | def quick_sort(arr, beg, end): 23 | if beg < end: 24 | loc = partition(arr, beg, end) 25 | print("\nThe array after partition:") 26 | for i in range(beg, end + 1): 27 | print(arr[i], end=" ") 28 | print() 29 | quick_sort(arr, beg, loc - 1) 30 | print("\nThe array after left sub-array sorted is:") 31 | for i in range(beg, loc): 32 | print(arr[i], end=" ") 33 | print() 34 | quick_sort(arr, loc + 1, end) 35 | print("\nThe array after right sub-array sorted is:") 36 | for i in range(loc + 1, end + 1): 37 | print(arr[i], end=" ") 38 | print() 39 | 40 | def main(): 41 | n = int(input("Enter number of elements in the array: ")) 42 | arr = [int(input(f"Enter element {i + 1}: ")) for i in range(n)] 43 | print("\nThe unsorted array is:") 44 | for i in range(n): 45 | print(arr[i], end=" ") 46 | quick_sort(arr, 0, n - 1) 47 | print("\nThe sorted array is:") 48 | for i in range(n): 49 | print(arr[i], end=" ") 50 | 51 | if __name__ == "__main__": 52 | main() 53 | -------------------------------------------------------------------------------- /Add Code Here/CPP/Heap_Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // https://practice.geeksforgeeks.org/problems/heap-sort/1? 5 | 6 | class Solution 7 | { 8 | public: 9 | //Heapify function to maintain heap property. 10 | void heapify(int arr[], int n, int i) 11 | { 12 | // Your Code Here 13 | int largest=i; 14 | int left=2*i; 15 | int right=(2*i)+1; 16 | 17 | if(left<=n && arr[left]>arr[largest]) 18 | largest=left; 19 | if(right<=n && arr[right]>arr[largest]) 20 | largest=right; 21 | 22 | if(largest!=i){ 23 | swap(arr[largest],arr[i]); 24 | heapify(arr,n,largest); 25 | } 26 | } 27 | 28 | 29 | public: 30 | //Function to sort an array using Heap Sort. 31 | void heapSort(int arr[], int n) 32 | { 33 | //code here 34 | int b[n+1]; 35 | for(int i=1;i<=n;i++) 36 | b[i]=arr[i-1]; 37 | 38 | for(int i=n/2;i>=1;i--) 39 | heapify(b,n,i); 40 | 41 | for(int i=n;i>=1;i--){ 42 | swap(b[i],b[1]); 43 | heapify(b,i-1,1); 44 | } 45 | 46 | for(int i=1;i<=n;i++) 47 | arr[i-1]=b[i]; 48 | } 49 | }; 50 | 51 | 52 | /* Function to print an array */ 53 | void printArray(int arr[], int size) 54 | { 55 | int i; 56 | for (i=0; i < size; i++) 57 | printf("%d ", arr[i]); 58 | printf("\n"); 59 | } 60 | 61 | 62 | int main() 63 | { 64 | int arr[1000000],n,T,i; 65 | scanf("%d",&T); 66 | while(T--){ 67 | scanf("%d",&n); 68 | for(i=0;i 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | //traversal of the matrix in a clockwise manner. 8 | vector boundaryTraversal(vector > matrix, int n, int m) 9 | { 10 | vector v; 11 | map,bool> vis; 12 | int i,j; 13 | i=0,j=0; 14 | for(j=0;j=0;j--) 32 | { 33 | if(vis[{i,j}]==false) 34 | {v.push_back(matrix[i][j]); 35 | vis[{i,j}]=true;} 36 | } 37 | 38 | j=0; 39 | for(i=n-2;i>0;i--) 40 | { 41 | if(vis[{i,j}]==false) 42 | {v.push_back(matrix[i][j]); 43 | vis[{i,j}]=true;} 44 | } 45 | return(v); 46 | 47 | } 48 | }; 49 | 50 | int main() { 51 | int t; 52 | cin>>t; 53 | 54 | while(t--) 55 | { 56 | int n,m; 57 | cin>>n>>m; 58 | vector > matrix(n); 59 | 60 | for(int i=0; i>matrix[i][j]; 66 | } 67 | } 68 | 69 | Solution ob; 70 | vector result = ob.boundaryTraversal(matrix, n, m); 71 | for (int i = 0; i < result.size(); ++i) 72 | cout< 2 | 3 | 4 | Basketball Shooting Game 5 | 6 | 7 | 8 | 70 | 71 | allPart = new ArrayList<>(); 19 | 20 | Deque currPart = new LinkedList(); 21 | 22 | allPalPartitonsUtil(allPart, currPart, 0, n, input); 23 | 24 | for (int i = 0; i < allPart.size(); i++) 25 | { 26 | for (int j = 0; j < allPart.get(i).size(); j++) 27 | { 28 | System.out.print(allPart.get(i).get(j) + " "); 29 | } 30 | System.out.println(); 31 | } 32 | 33 | } 34 | 35 | private static void allPalPartitonsUtil(ArrayList allPart, 36 | Deque currPart, int start, int n, String input) 37 | { 38 | if (start >= n) 39 | { 40 | allPart.add(new ArrayList<>(currPart)); 41 | return; 42 | } 43 | 44 | for (int i = start; i < n; i++) 45 | { 46 | if (isPalindrome(input, start, i)) 47 | { 48 | currPart.addLast(input.substring(start, i + 1)); 49 | allPalPartitonsUtil(allPart, currPart, i + 1, n, input); 50 | currPart.removeLast(); 51 | } 52 | } 53 | } 54 | 55 | private static boolean isPalindrome(String input,int start, int i) 56 | { 57 | while (start < i) 58 | { 59 | if (input.charAt(start++) != input.charAt(i--)) 60 | return false; 61 | } 62 | return true; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/Reverse_queue.java: -------------------------------------------------------------------------------- 1 | import java.util.LinkedList; 2 | import java.util.Queue; 3 | import java.util.Stack; 4 | 5 | public class Reverse_k_element_queue { 6 | 7 | static Queue queue; 8 | 9 | // Function to reverse the first 10 | // K elements of the Queue 11 | static void reverseQueueFirstKElements(int k) 12 | { 13 | if (queue.isEmpty() == true 14 | || k > queue.size()) 15 | return; 16 | if (k <= 0) 17 | return; 18 | 19 | Stack stack = new Stack(); 20 | 21 | // Push the first K elements into a Stack 22 | for (int i = 0; i < k; i++) { 23 | stack.push(queue.peek()); 24 | queue.remove(); 25 | } 26 | 27 | // Enqueue the contents of stack 28 | // at the back of the queue 29 | while (!stack.empty()) { 30 | queue.add(stack.peek()); 31 | stack.pop(); 32 | } 33 | 34 | // Remove the remaining elements and enqueue 35 | // them at the end of the Queue 36 | for (int i = 0; i < queue.size() - k; i++) { 37 | queue.add(queue.peek()); 38 | queue.remove(); 39 | } 40 | } 41 | 42 | // Utility Function to print the Queue 43 | static void Print() 44 | { 45 | while (!queue.isEmpty()) { 46 | System.out.print(queue.peek() + " "); 47 | queue.remove(); 48 | } 49 | } 50 | 51 | // Driver code 52 | public static void main(String args[]) 53 | { 54 | queue = new LinkedList(); 55 | queue.add(10); 56 | queue.add(20); 57 | queue.add(30); 58 | queue.add(40); 59 | queue.add(50); 60 | queue.add(60); 61 | queue.add(70); 62 | queue.add(80); 63 | queue.add(90); 64 | queue.add(100); 65 | 66 | int k = 5; 67 | reverseQueueFirstKElements(k); 68 | Print(); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/merge_sort.java: -------------------------------------------------------------------------------- 1 | /* Java program for Merge Sort */ 2 | class MergeSort 3 | { 4 | // Merges two subarrays of arr[]. 5 | // First subarray is arr[l..m] 6 | // Second subarray is arr[m+1..r] 7 | void merge(int arr[], int l, int m, int r) 8 | { 9 | // Find sizes of two subarrays to be merged 10 | int n1 = m - l + 1; 11 | int n2 = r - m; 12 | 13 | /* Create temp arrays */ 14 | int L[] = new int[n1]; 15 | int R[] = new int[n2]; 16 | 17 | /*Copy data to temp arrays*/ 18 | for (int i = 0; i < n1; ++i) 19 | L[i] = arr[l + i]; 20 | for (int j = 0; j < n2; ++j) 21 | R[j] = arr[m + 1 + j]; 22 | 23 | /* Merge the temp arrays */ 24 | 25 | // Initial indexes of first and second subarrays 26 | int i = 0, j = 0; 27 | 28 | // Initial index of merged subarray array 29 | int k = l; 30 | while (i < n1 && j < n2) { 31 | if (L[i] <= R[j]) { 32 | arr[k] = L[i]; 33 | i++; 34 | } 35 | else { 36 | arr[k] = R[j]; 37 | j++; 38 | } 39 | k++; 40 | } 41 | 42 | /* Copy remaining elements of L[] if any */ 43 | while (i < n1) { 44 | arr[k] = L[i]; 45 | i++; 46 | k++; 47 | } 48 | 49 | /* Copy remaining elements of R[] if any */ 50 | while (j < n2) { 51 | arr[k] = R[j]; 52 | j++; 53 | k++; 54 | } 55 | } 56 | 57 | // Main function that sorts arr[l..r] using 58 | // merge() 59 | void sort(int arr[], int l, int r) 60 | { 61 | if (l < r) { 62 | // Find the middle point 63 | int m =l+ (r-l)/2; 64 | 65 | // Sort first and second halves 66 | sort(arr, l, m); 67 | sort(arr, m + 1, r); 68 | 69 | // Merge the sorted halves 70 | merge(arr, l, m, r); 71 | } 72 | } 73 | 74 | } -------------------------------------------------------------------------------- /Add Code Here/CPP/CheckTwoNodesOnSamePath.cpp: -------------------------------------------------------------------------------- 1 | // C++ program to check if given pairs lie on same path or not. 2 | 3 | #include 4 | using namespace std; 5 | const int MAX = 100001; 6 | 7 | // To keep track of visited vertices in DFS 8 | bool visit[MAX] = {0}; 9 | 10 | // To store start and end time of all vertices 11 | // during DFS. 12 | int intime[MAX]; 13 | int outtime[MAX]; 14 | 15 | // initially timer is zero 16 | int timer = 0; 17 | 18 | // Does DFS of given graph and fills arrays 19 | // intime[] and outtime[]. These arrays are used 20 | // to answer given queries. 21 | void dfs(vector graph[], int v) 22 | { 23 | visit[v] = true; 24 | 25 | // Increment the timer as you enter 26 | // the recursion for v 27 | ++timer; 28 | 29 | // Upgrade the in time for the vertex 30 | intime[v] = timer; 31 | 32 | vector::iterator it = graph[v].begin(); 33 | while (it != graph[v].end()) 34 | { 35 | if (visit[*it]==false) 36 | dfs(graph, *it); 37 | it++; 38 | } 39 | 40 | // increment the timer as you exit the 41 | // recursion for v 42 | ++timer; 43 | 44 | // upgrade the outtime for that node 45 | outtime[v] = timer; 46 | } 47 | 48 | // Returns true if 'u' and 'v' lie on same root to leaf path 49 | // else false. 50 | bool query(int u, int v) 51 | { 52 | return ( (intime[u]outtime[v]) || 53 | (intime[v]outtime[u]) ); 54 | } 55 | 56 | // Driver code 57 | int main() 58 | { 59 | // Let us create above shown tree 60 | int n = 9; // total number of nodes 61 | vector graph[n+1]; 62 | graph[1].push_back(2); 63 | graph[1].push_back(3); 64 | graph[3].push_back(6); 65 | graph[2].push_back(4); 66 | graph[2].push_back(5); 67 | graph[5].push_back(7); 68 | graph[5].push_back(8); 69 | graph[5].push_back(9); 70 | 71 | // Start dfs (here root node is 1) 72 | dfs(graph, 1); 73 | 74 | // below are calls for few pairs of nodes 75 | query(1, 5)? cout << "Yes\n" : cout << "No\n"; 76 | query(2, 9)? cout << "Yes\n" : cout << "No\n"; 77 | query(2, 6)? cout << "Yes\n" : cout << "No\n"; 78 | 79 | return 0; 80 | } 81 | -------------------------------------------------------------------------------- /Add Code Here/Python/Differential Equation Solver/Runge_Kutta.py: -------------------------------------------------------------------------------- 1 | from differential_input import diff_input 2 | import math 3 | from copy import deepcopy 4 | 5 | 6 | def runge_kutta(function=None, yzuv=None, a_b=None, n=None): 7 | if function is None or yzuv is None or a_b is None or n is None: 8 | function, yzuv, a_b, n = diff_input() 9 | 10 | a, b = a_b 11 | h = (b-a)/n 12 | x0 = a 13 | 14 | answer_list = [(0, x0, *yzuv)] 15 | g = len(yzuv) 16 | while len(yzuv) < 4: 17 | yzuv.append(None) 18 | 19 | yi, zi, ui, vi = [None] * 4 20 | func_i = [yi, zi, ui, vi] 21 | k_i = [[None] * 4 for i in range(g)] 22 | for i in range(1, n+1): 23 | for j in range(0, g): 24 | x, y, z, u, v = x0, *yzuv 25 | f_xy = eval(function[j]) 26 | k_i[j][0] = h * f_xy 27 | 28 | new_yzuv = deepcopy(yzuv) 29 | new_yzuv[0:g] = [(yzuv[j] + k_i[j][0]/2) for j in range(g)] 30 | for j in range(0, g): 31 | x, y, z, u, v = x0 + h/2, *new_yzuv 32 | f_xy = eval(function[j]) 33 | k_i[j][1] = h * f_xy 34 | 35 | new_yzuv = deepcopy(yzuv) 36 | new_yzuv[0:g] = [(yzuv[j] + k_i[j][1] / 2) for j in range(g)] 37 | for j in range(0, g): 38 | x, y, z, u, v = x0 + h/2, *new_yzuv 39 | f_xy = eval(function[j]) 40 | k_i[j][2] = h * f_xy 41 | 42 | new_yzuv = deepcopy(yzuv) 43 | new_yzuv[0:g] = [(yzuv[j] + k_i[j][2]) for j in range(g)] 44 | for j in range(0, g): 45 | x, y, z, u, v = x0 + h, *new_yzuv 46 | f_xy = eval(function[j]) 47 | k_i[j][3] = h * f_xy 48 | 49 | for j in range(g): 50 | func_i[j] = yzuv[j] + (1/6)*(k_i[j][0] + 2*k_i[j][1] + 2*k_i[j][2] + k_i[j][3]) 51 | yzuv[j] = func_i[j] 52 | 53 | x0 += h 54 | answer_list.append((i, x0, *func_i[0:g])) 55 | 56 | return answer_list 57 | 58 | 59 | if __name__ == "__main__": 60 | print(runge_kutta(["math.sin(x+y)"], [0], [0, 1], 10)) 61 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/EvenAfterOddLL.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class EvenAfterOddLL { 4 | 5 | public static Node takeInput() 6 | { 7 | Nodehead=null,tail=null; 8 | Scanner s=new Scanner(System.in); 9 | int data=s.nextInt(); 10 | 11 | while(data!=-1) 12 | { 13 | Nodenode=new Node(data); 14 | if(head==null) 15 | { 16 | head=node; 17 | tail=node; 18 | } 19 | else 20 | { 21 | // Nodetemp=head; 22 | // while(temp.next!=null) 23 | // { 24 | // temp=temp.next; 25 | // } 26 | // temp.next=node; 27 | 28 | tail.next=node; 29 | tail=tail.next; 30 | 31 | } 32 | data=s.nextInt(); 33 | } 34 | return head; 35 | } 36 | 37 | public static Node evenAfterOdd(Node head) { 38 | 39 | Nodeeh=null; 40 | Nodeet=null; 41 | Nodeoh=null; 42 | Nodeot=null; 43 | 44 | while(head!=null) 45 | { 46 | if(head.data%2!=0) //odd 47 | { 48 | if(oh==null) 49 | { 50 | oh=head; 51 | ot=oh; 52 | } 53 | else 54 | { 55 | ot.next=head; 56 | ot=ot.next; 57 | } 58 | } 59 | else 60 | { 61 | if(eh==null) 62 | { 63 | eh=head; 64 | et=eh; 65 | } 66 | else 67 | { 68 | et.next=head; 69 | et=et.next; 70 | } 71 | } 72 | head=head.next; 73 | } 74 | 75 | if(ot==null) 76 | { 77 | return eh; 78 | } 79 | if(eh==null) 80 | { 81 | return oh; 82 | } 83 | 84 | ot.next=eh; 85 | et.next=null; 86 | return oh; 87 | 88 | } 89 | 90 | public static void print(Nodehead) 91 | { 92 | Nodetemp=head; 93 | while(temp!=null) 94 | { 95 | System.out.print(temp.data+" "); 96 | temp=temp.next; 97 | } 98 | } 99 | public static void main(String[] args) { 100 | Nodehead=takeInput(); 101 | Nodeans=evenAfterOdd(head); 102 | print(ans); 103 | } 104 | 105 | } 106 | -------------------------------------------------------------------------------- /Add Code Here/Python/Rock-Paper-Scissor.py: -------------------------------------------------------------------------------- 1 | 2 | import random 3 | 4 | 5 | def takePlayerInput(): 6 | player = "blank" 7 | while not (player.lower() == "r" or player.lower() == "p" or player.lower() == "s"): 8 | player = input("Please Enter your input out of - R | P | S = ") 9 | return player.lower() 10 | 11 | 12 | takePlayerInput() 13 | 14 | 15 | def getBotInput(): 16 | lst = ["r", "s", "p"] 17 | return random.choice(lst) 18 | 19 | 20 | getBotInput() 21 | 22 | 23 | def checkWinner(player, bot): 24 | if player == "r" and bot == "r": 25 | return "Draw" 26 | elif player == "r" and bot == "p": 27 | return "Bot" 28 | elif player == "r" and bot == "s": 29 | return "Player" 30 | elif player == "p" and bot == "p": 31 | return "Draw" 32 | elif player == "p" and bot == "r": 33 | return "Player" 34 | elif player == "p" and bot == "s": 35 | return "Bot" 36 | elif player == "s" and bot == "s": 37 | return "Draw" 38 | elif player == "s" and bot == "p": 39 | return "Player" 40 | elif player == "s" and bot == "r": 41 | return "Bot" 42 | else: 43 | return "DRAW" 44 | 45 | 46 | checkWinner("s", "p") 47 | 48 | 49 | def rockPaperScissor(): 50 | endTheGame = "n" 51 | player_score = 0 52 | bot_score = 0 53 | while endTheGame.lower() != "y": 54 | ply = takePlayerInput() 55 | bt = getBotInput() 56 | print("Bot Entered -", bt) 57 | winner = checkWinner(player=ply, bot=bt) 58 | print("Winner is - ", winner) 59 | if winner == "Player": 60 | player_score += 2 61 | elif winner == "Bot": 62 | bot_score += 2 63 | else: 64 | player_score += 1 65 | bot_score += 1 66 | 67 | print("-----Score Board-----") 68 | print("-----Player-----", player_score) 69 | print("-----Bot-----", bot_score) 70 | print(" ") 71 | endTheGame = input("You want to end Y/N - ") 72 | 73 | 74 | rockPaperScissor() 75 | 76 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/DFS.java: -------------------------------------------------------------------------------- 1 | // Java program to print DFS 2 | // traversal from a given 3 | // graph 4 | import java.io.*; 5 | import java.util.*; 6 | 7 | // This class represents a 8 | // directed graph using adjacency 9 | // list representation 10 | class Graph { 11 | private int V; // No. of vertices 12 | 13 | // Array of lists for 14 | // Adjacency List Representation 15 | private LinkedList adj[]; 16 | 17 | // Constructor 18 | @SuppressWarnings("unchecked") Graph(int v) 19 | { 20 | V = v; 21 | adj = new LinkedList[v]; 22 | for (int i = 0; i < v; ++i) 23 | adj[i] = new LinkedList(); 24 | } 25 | 26 | // Function to add an edge into the graph 27 | void addEdge(int v, int w) 28 | { 29 | adj[v].add(w); // Add w to v's list. 30 | } 31 | 32 | // A function used by DFS 33 | void DFSUtil(int v, boolean visited[]) 34 | { 35 | // Mark the current node as visited and print it 36 | visited[v] = true; 37 | System.out.print(v + " "); 38 | 39 | // Recur for all the vertices adjacent to this 40 | // vertex 41 | Iterator i = adj[v].listIterator(); 42 | while (i.hasNext()) { 43 | int n = i.next(); 44 | if (!visited[n]) 45 | DFSUtil(n, visited); 46 | } 47 | } 48 | 49 | // The function to do DFS traversal. 50 | // It uses recursive 51 | // DFSUtil() 52 | void DFS(int v) 53 | { 54 | // Mark all the vertices as 55 | // not visited(set as 56 | // false by default in java) 57 | boolean visited[] = new boolean[V]; 58 | 59 | // Call the recursive helper 60 | // function to print DFS 61 | // traversal 62 | DFSUtil(v, visited); 63 | } 64 | 65 | // Driver's Code 66 | public static void main(String args[]) 67 | { 68 | Graph g = new Graph(4); 69 | 70 | g.addEdge(0, 1); 71 | g.addEdge(0, 2); 72 | g.addEdge(1, 2); 73 | g.addEdge(2, 0); 74 | g.addEdge(2, 3); 75 | g.addEdge(3, 3); 76 | 77 | System.out.println( 78 | "Following is Depth First Traversal " 79 | + "(starting from vertex 2)"); 80 | 81 | // Function call 82 | g.DFS(2); 83 | } 84 | } 85 | // This code is contributed by Aakash Hasija 86 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/tic_tac_toe.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class tic_tac_to 3 | { 4 | public static void main(String args[]) 5 | { 6 | Scanner I=new Scanner(System.in); 7 | String a[][]= new String[3][3]; 8 | String nm[]=new String [2]; 9 | String sym[]={"O","X"}; 10 | int n,i,j,k; 11 | for(i=0;i<2;i++) 12 | { 13 | System.out.println("PLAYER "+ (i+1)+":"); 14 | nm[i]=I.nextLine().toUpperCase(); 15 | } 16 | System.out.println("WELCOME TO THE GAME OF TIC-TAC-TO\nYOUR INPUT OPTIONS ARE AS FOLLOWS:\n0)TOP-LEFT \t1)TOP-MID\t2)TOP-RIGHT\n3)MID-LEFT\t4)CENTRE \t5)MID-RIGHT\n6)DOWN-LEFT\t7)DOWN-MID\t8)DOWN-RIGHT\nNOW ENTER THE RELEVENT POSITION NUMBERS ALTERNATIVELY,FIRST TO START IS O AND SECOND GETS X\n"); 17 | for(i=0;i<3;i++) 18 | for(j=0;j<3;j++) 19 | a[i][j]=" "; 20 | for(k=0;k<9;k++) 21 | { 22 | n=I.nextInt(); 23 | System.out.println(nm[k%2]+"'s MOVE:"); 24 | if(n>8||n<0) 25 | { 26 | System.out.println("INVALID INPUT\nRETRY"); 27 | k=k-1; 28 | continue; 29 | } 30 | else 31 | if(a[n/3][n%3]==" ") 32 | a[n/3][n%3]=sym[k%2]; 33 | else 34 | { 35 | System.out.println(nm[k%2]+" THIS SPACE IS ALREADY OCCUPIED KINDLY CHOOSE ANOTHER ONE"); 36 | k=k-1; 37 | continue; 38 | } 39 | for(i=0;i<3;i++) 40 | { 41 | for(j=0;j<3;j++) 42 | System.out.print(a[i][j]+"\t"); 43 | System.out.println(); 44 | } 45 | for(i=0;i<3;i++) 46 | if((a[i][0]==(a[i][1])&&a[i][0]==a[i][2]&&a[i][0]==sym[k%2])||(a[0][i]==a[1][i]&&a[0][i]==a[2][i]&&a[0][i]==sym[k%2])||a[0][0]==a[1][1]&&a[0][0]==a[2][2]&&(a[1][1]==sym[k%2])||a[0][2]==a[1][1]&&a[0][2]==a[2][0]&&(a[1][1]==sym[k%2])) 47 | { 48 | System.out.println(nm[k%2]+" WINS!!!!!!!!!!!!!!!!!!!"); 49 | break; 50 | } 51 | if(i<3) 52 | break; 53 | } 54 | if(i==3) 55 | System.out.println("THE GAME IS TIED"); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Add Code Here/Java/QuickSort.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class QuickSort { 4 | public static void main(String[] args) { 5 | Scanner input = new Scanner(System.in); 6 | int[] arr = new int[100]; 7 | int n; 8 | System.out.print("Enter number of elements in the array: "); 9 | n = input.nextInt(); 10 | System.out.println("Enter elements of the array:"); 11 | for (int i = 0; i < n; i++) { 12 | arr[i] = input.nextInt(); 13 | } 14 | System.out.println("\nThe unsorted array is:"); 15 | for (int i = 0; i < n; i++) { 16 | System.out.print(arr[i] + "\t"); 17 | } 18 | quickSort(arr, 0, n - 1); 19 | System.out.println("\nThe sorted array is:"); 20 | for (int i = 0; i < n; i++) { 21 | System.out.print(arr[i] + "\t"); 22 | } 23 | } 24 | 25 | public static int partition(int[] a, int beg, int end) { 26 | int left, right, temp, loc, flag; 27 | loc = left = beg; 28 | right = end; 29 | flag = 0; 30 | while (flag != 1) { 31 | while ((a[loc] <= a[right]) && (loc != right)) 32 | right--; 33 | if (loc == right) 34 | flag = 1; 35 | else if (a[loc] > a[right]) { 36 | temp = a[loc]; 37 | a[loc] = a[right]; 38 | a[right] = temp; 39 | loc = right; 40 | } 41 | if (flag != 1) { 42 | while ((a[loc] >= a[left]) && (loc != left)) 43 | left++; 44 | if (loc == left) 45 | flag = 1; 46 | else if (a[loc] < a[left]) { 47 | temp = a[loc]; 48 | a[loc] = a[left]; 49 | a[left] = temp; 50 | loc = left; 51 | } 52 | } 53 | } 54 | return loc; 55 | } 56 | 57 | public static void quickSort(int[] a, int beg, int end) { 58 | int loc; 59 | if (beg < end) { 60 | loc = partition(a, beg, end); 61 | quickSort(a, beg, loc - 1); 62 | quickSort(a, loc + 1, end); 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/Radix.java: -------------------------------------------------------------------------------- 1 | // Radix sort Java implementation 2 | import java.io.*; 3 | import java.util.*; 4 | 5 | class Radix { 6 | 7 | // A utility function to get maximum value in arr[] 8 | static int getMax(int arr[], int n) 9 | { 10 | int mx = arr[0]; 11 | for (int i = 1; i < n; i++) 12 | if (arr[i] > mx) 13 | mx = arr[i]; 14 | return mx; 15 | } 16 | 17 | // A function to do counting sort of arr[] according to 18 | // the digit represented by exp. 19 | static void countSort(int arr[], int n, int exp) 20 | { 21 | int output[] = new int[n]; // output array 22 | int i; 23 | int count[] = new int[10]; 24 | Arrays.fill(count, 0); 25 | 26 | // Store count of occurrences in count[] 27 | for (i = 0; i < n; i++) 28 | count[(arr[i] / exp) % 10]++; 29 | 30 | // Change count[i] so that count[i] now contains 31 | // actual position of this digit in output[] 32 | for (i = 1; i < 10; i++) 33 | count[i] += count[i - 1]; 34 | 35 | // Build the output array 36 | for (i = n - 1; i >= 0; i--) { 37 | output[count[(arr[i] / exp) % 10] - 1] = arr[i]; 38 | count[(arr[i] / exp) % 10]--; 39 | } 40 | 41 | // Copy the output array to arr[], so that arr[] now 42 | // contains sorted numbers according to current digit 43 | for (i = 0; i < n; i++) 44 | arr[i] = output[i]; 45 | } 46 | 47 | // The main function to that sorts arr[] of size n using 48 | // Radix Sort 49 | static void radixsort(int arr[], int n) 50 | { 51 | // Find the maximum number to know number of digits 52 | int m = getMax(arr, n); 53 | 54 | // Do counting sort for every digit. Note that 55 | // instead of passing digit number, exp is passed. 56 | // exp is 10^i where i is current digit number 57 | for (int exp = 1; m / exp > 0; exp *= 10) 58 | countSort(arr, n, exp); 59 | } 60 | 61 | // A utility function to print an array 62 | static void print(int arr[], int n) 63 | { 64 | for (int i = 0; i < n; i++) 65 | System.out.print(arr[i] + " "); 66 | } 67 | 68 | /*Driver Code*/ 69 | public static void main(String[] args) 70 | { 71 | int arr[] = { 170, 45, 75, 90, 802, 24, 2, 66 }; 72 | int n = arr.length; 73 | 74 | // Function Call 75 | radixsort(arr, n); 76 | print(arr, n); 77 | } 78 | } 79 | /* This code is contributed by Ujwal Gulhane */ 80 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/TreeTraversal.java: -------------------------------------------------------------------------------- 1 | public class TreeTraversal { 2 | static class Node { 3 | int data; 4 | Node left; 5 | Node right; 6 | 7 | Node(int data) { 8 | this.data = data; 9 | this.left = null; 10 | this.right = null; 11 | } 12 | } 13 | 14 | static class BinaryTree { 15 | static int index = -1; 16 | 17 | public Node BuildTree(int nodes[]) { 18 | index++; 19 | if (nodes[index] == -1) { 20 | return null; 21 | } 22 | Node newNode = new Node(nodes[index]); 23 | newNode.left = BuildTree(nodes); 24 | newNode.right = BuildTree(nodes); 25 | return newNode; 26 | } 27 | } 28 | 29 | public static void preorder(Node root) { 30 | if (root == null) { 31 | return; 32 | } 33 | System.out.print(root.data + " "); 34 | preorder(root.left); 35 | preorder(root.right); 36 | } 37 | 38 | public static void inorder(Node root) { 39 | if (root == null) { 40 | return; 41 | } 42 | inorder(root.left); 43 | System.out.print(root.data + " "); 44 | inorder(root.right); 45 | } 46 | 47 | public static void postorder(Node root) { 48 | if (root == null) { 49 | return; 50 | } 51 | postorder(root.left); 52 | postorder(root.right); 53 | System.out.print(root.data + " "); 54 | } 55 | 56 | public static void main(String[] args) { 57 | int nodes[] = { 1, 2, 4, -1, -1, 5, -1, -1, 3, -1, 6, -1, -1 }; 58 | BinaryTree tree = new BinaryTree(); 59 | Node root = tree.BuildTree(nodes); 60 | System.out.print("ROOT NODE -> "); 61 | System.out.println(root.data); 62 | System.out.print("PREORDER TRAVERSAL -> "); 63 | preorder(root); 64 | System.out.println(""); 65 | System.out.print("INORDER TRAVERSAL -> "); 66 | inorder(root); 67 | System.out.println(""); 68 | System.out.print("POST-ORDER TRAVERSAL -> "); 69 | postorder(root); 70 | System.out.println(""); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /Add Code Here/Python/Sudoku.py: -------------------------------------------------------------------------------- 1 | def is_valid(board, row, col, num): 2 | # Check if num is already present in the row 3 | if num in board[row]: 4 | return False 5 | 6 | # Check if num is already present in the column 7 | if num in [board[i][col] for i in range(9)]: 8 | return False 9 | 10 | # Check if num is already present in the 3x3 grid 11 | start_row, start_col = 3 * (row // 3), 3 * (col // 3) 12 | for i in range(start_row, start_row + 3): 13 | for j in range(start_col, start_col + 3): 14 | if board[i][j] == num: 15 | return False 16 | 17 | return True 18 | 19 | def solve_sudoku(board): 20 | empty_cell = find_empty_cell(board) 21 | 22 | if not empty_cell: 23 | return True 24 | 25 | row, col = empty_cell 26 | 27 | for num in range(1, 10): 28 | if is_valid(board, row, col, num): 29 | board[row][col] = num 30 | 31 | if solve_sudoku(board): 32 | return True 33 | 34 | board[row][col] = 0 35 | 36 | return False 37 | 38 | def find_empty_cell(board): 39 | for i in range(9): 40 | for j in range(9): 41 | if board[i][j] == 0: 42 | return (i, j) 43 | return None 44 | 45 | def print_board(board): 46 | for i in range(9): 47 | if i % 3 == 0 and i != 0: 48 | print("- - - - - - - - - - - -") 49 | for j in range(9): 50 | if j % 3 == 0 and j != 0: 51 | print(" | ", end="") 52 | if j == 8: 53 | print(board[i][j]) 54 | else: 55 | print(str(board[i][j]) + " ", end="") 56 | 57 | # Example Sudoku puzzle (0 represents empty cells) 58 | sudoku_board = [ 59 | [5, 3, 0, 0, 7, 0, 0, 0, 0], 60 | [6, 0, 0, 1, 9, 5, 0, 0, 0], 61 | [0, 9, 8, 0, 0, 0, 0, 6, 0], 62 | [8, 0, 0, 0, 6, 0, 0, 0, 3], 63 | [4, 0, 0, 8, 0, 3, 0, 0, 1], 64 | [7, 0, 0, 0, 2, 0, 0, 0, 6], 65 | [0, 6, 0, 0, 0, 0, 2, 8, 0], 66 | [0, 0, 0, 4, 1, 9, 0, 0, 5], 67 | [0, 0, 0, 0, 8, 0, 0, 7, 9] 68 | ] 69 | 70 | if solve_sudoku(sudoku_board): 71 | print("Solved Sudoku:") 72 | print_board(sudoku_board) 73 | else: 74 | print("No solution exists") 75 | -------------------------------------------------------------------------------- /Add Code Here/CPP/sorting_techniques.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define MAX 5 4 | 5 | class sort 6 | { 7 | public: 8 | int arr[MAX]; 9 | 10 | void input_array(); 11 | void selection_sort_ascen(); 12 | void selection_sort_descen(); 13 | }; 14 | 15 | 16 | void sort :: input_array() 17 | { 18 | cout<<"\nEnter elements in array: "; 19 | 20 | for(int i=0;i>arr[i]; 23 | } 24 | 25 | cout<<"\nYour array is as follows: \n"; 26 | for(int i=0;iarr[j]) 39 | { 40 | int temp=arr[i]; 41 | arr[i]=arr[j]; 42 | arr[j]=temp; 43 | } 44 | } 45 | } 46 | 47 | cout<<"\n\nSorted array is as follows: \n"; 48 | 49 | for(int i=0;i>ch; 92 | 93 | switch(ch) 94 | { 95 | case 1: 96 | s.input_array(); 97 | s.selection_sort_ascen(); 98 | 99 | break; 100 | 101 | case 2: 102 | s.input_array(); 103 | s.selection_sort_descen(); 104 | 105 | break; 106 | 107 | default: 108 | cout<<"\nIncorrect choice !!!"; 109 | } 110 | 111 | cout<<"\n\nDo you want to continue (y/n): "; 112 | cin>>start; 113 | } 114 | 115 | return 0; 116 | } 117 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/ReversePath.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class ReversePath 3 | { 4 | static class node 5 | { 6 | int key; 7 | node left, right; 8 | }; 9 | static node root = null; 10 | static Queue q1; 11 | static int k; 12 | 13 | static node newNode(int item) 14 | { 15 | node temp = new node(); 16 | temp.key = item; 17 | temp.left = temp.right = null; 18 | return temp; 19 | } 20 | 21 | static void inorder(node root) 22 | { 23 | if (root != null) 24 | { 25 | inorder(root.left); 26 | System.out.print(root.key + " "); 27 | inorder(root.right); 28 | } 29 | } 30 | 31 | static void reversePath(node node) 32 | { 33 | if (node == null) 34 | return; 35 | 36 | if ((node).key == k) 37 | { 38 | q1.add((node).key); 39 | (node).key = q1.peek(); 40 | q1.remove(); 41 | return; 42 | } 43 | else if (k < (node).key) 44 | { 45 | q1.add((node).key); 46 | reversePath((node).left); 47 | (node).key = q1.peek(); 48 | q1.remove(); 49 | } 50 | else if (k > (node).key) 51 | { 52 | q1.add((node).key); 53 | reversePath((node).right); 54 | (node).key = q1.peek(); 55 | 56 | q1.remove(); 57 | } 58 | 59 | return; 60 | } 61 | static node insert(node node, int key) 62 | { 63 | if (node == null) 64 | return newNode(key); 65 | 66 | if (key < node.key) 67 | node.left = insert(node.left, key); 68 | else if (key > node.key) 69 | node.right = insert(node.right, key); 70 | 71 | return node; 72 | } 73 | 74 | public static void main(String[] args) 75 | { 76 | q1 = new LinkedList<>(); 77 | 78 | k = 80; 79 | root = insert(root, 50); 80 | root = insert(root, 30); 81 | root = insert(root, 20); 82 | root = insert(root, 40); 83 | root = insert(root, 70); 84 | root = insert(root, 60); 85 | root = insert(root, 80); 86 | System.out.print("Before Reverse :" 87 | + "\n"); 88 | inorder(root); 89 | System.out.print("\n"); 90 | 91 | reversePath(root); 92 | System.out.print("After Reverse :" 93 | + "\n"); 94 | inorder(root); 95 | } 96 | } -------------------------------------------------------------------------------- /Add Code Here/JAVA/QuickSort.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner;//importing scanner class 2 | 3 | public class QuickSort { 4 | public static void main(String[] args) { 5 | Scanner input = new Scanner(System.in);//creating object of scanner class 6 | int[] arr = new int[100];//declaration of array 7 | int n; 8 | System.out.print("Enter number of elements in the array: "); 9 | n = input.nextInt(); 10 | System.out.println("Enter elements of the array:"); 11 | for (int i = 0; i < n; i++) { 12 | arr[i] = input.nextInt(); 13 | } 14 | System.out.println("\nThe unsorted array is:"); 15 | for (int i = 0; i < n; i++) { 16 | System.out.print(arr[i] + "\t"); 17 | } 18 | quickSort(arr, 0, n - 1); 19 | System.out.println("\nThe sorted array is:"); 20 | for (int i = 0; i < n; i++) { 21 | System.out.print(arr[i] + "\t"); 22 | } 23 | } 24 | 25 | public static int partition(int[] a, int beg, int end) { 26 | int left, right, temp, loc, flag; 27 | loc = left = beg; 28 | right = end; 29 | flag = 0; 30 | while (flag != 1) { 31 | while ((a[loc] <= a[right]) && (loc != right)) 32 | right--; 33 | if (loc == right) 34 | flag = 1; 35 | else if (a[loc] > a[right]) { 36 | temp = a[loc]; 37 | a[loc] = a[right]; 38 | a[right] = temp; 39 | loc = right; 40 | } 41 | if (flag != 1) { 42 | while ((a[loc] >= a[left]) && (loc != left)) 43 | left++; 44 | if (loc == left) 45 | flag = 1; 46 | else if (a[loc] < a[left]) { 47 | temp = a[loc]; 48 | a[loc] = a[left]; 49 | a[left] = temp; 50 | loc = left; 51 | } 52 | } 53 | } 54 | return loc; 55 | } 56 | 57 | public static void quickSort(int[] a, int beg, int end) { 58 | int loc; 59 | if (beg < end) { 60 | loc = partition(a, beg, end); 61 | quickSort(a, beg, loc - 1); 62 | quickSort(a, loc + 1, end); 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /Add Code Here/Java/MergeSort.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | // Merge sort in Java 4 | 5 | class Main { 6 | 7 | // Merge two sub arrays L and M into array 8 | void merge(int array[], int p, int q, int r) { 9 | 10 | int n1 = q - p + 1; 11 | int n2 = r - q; 12 | 13 | int L[] = new int[n1]; 14 | int M[] = new int[n2]; 15 | 16 | // fill the left and right array 17 | for (int i = 0; i < n1; i++) 18 | L[i] = array[p + i]; 19 | for (int j = 0; j < n2; j++) 20 | M[j] = array[q + 1 + j]; 21 | 22 | // Maintain current index of sub-arrays and main array 23 | int i, j, k; 24 | i = 0; 25 | j = 0; 26 | k = p; 27 | 28 | // Until we reach either end of either L or M, pick larger among 29 | // elements L and M and place them in the correct position at A[p..r] 30 | // for sorting in descending 31 | // use if(L[i] >= <[j]) 32 | while (i < n1 && j < n2) { 33 | if (L[i] <= M[j]) { 34 | array[k] = L[i]; 35 | i++; 36 | } else { 37 | array[k] = M[j]; 38 | j++; 39 | } 40 | k++; 41 | } 42 | 43 | // When we run out of elements in either L or M, 44 | // pick up the remaining elements and put in A[p..r] 45 | while (i < n1) { 46 | array[k] = L[i]; 47 | i++; 48 | k++; 49 | } 50 | 51 | while (j < n2) { 52 | array[k] = M[j]; 53 | j++; 54 | k++; 55 | } 56 | } 57 | 58 | // Divide the array into two sub arrays, sort them and merge them 59 | void mergeSort(int array[], int left, int right) { 60 | if (left < right) { 61 | 62 | // m is the point where the array is divided into two sub arrays 63 | int mid = (left + right) / 2; 64 | 65 | // recursive call to each sub arrays 66 | mergeSort(array, left, mid); 67 | mergeSort(array, mid + 1, right); 68 | 69 | // Merge the sorted sub arrays 70 | merge(array, left, mid, right); 71 | } 72 | } 73 | 74 | public static void main(String args[]) { 75 | 76 | // created an unsorted array 77 | int[] array = { 6, 5, 12, 10, 9, 1 }; 78 | 79 | Main ob = new Main(); 80 | 81 | // call the method mergeSort() 82 | // pass argument: array, first index and last index 83 | ob.mergeSort(array, 0, array.length - 1); 84 | 85 | System.out.println("Sorted Array:"); 86 | System.out.println(Arrays.toString(array)); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /Add Code Here/CPP/Find all the N bit binary numbers having more than or equal 1’s than 0’s – C: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Function to get the binary representation 5 | // of the number N 6 | string getBinaryRep(int N, int num_of_bits) 7 | { 8 | string s = ""; 9 | num_of_bits--; 10 | 11 | // loop for each bit 12 | while (num_of_bits >= 0) 13 | { 14 | if (N & (1 << num_of_bits)) 15 | s.append("1"); 16 | else 17 | s.append("0"); 18 | num_of_bits--; 19 | } 20 | return s; 21 | } 22 | 23 | 24 | vector NBitBinary(int N) 25 | { 26 | vector s; 27 | int first = 1 << (N - 1); 28 | int last = first * 2; 29 | 30 | for (int i = last - 1; i >= first; --i) 31 | { 32 | int zero_cnt = 0; 33 | int one_cnt = 0; 34 | int t = i; 35 | int num_of_bits = 0; 36 | 37 | // longest prefix check 38 | while (t) 39 | { 40 | if (t & 1) 41 | one_cnt++; 42 | else 43 | zero_cnt++; 44 | num_of_bits++; 45 | t = t >> 1; 46 | } 47 | 48 | if (one_cnt >= zero_cnt) 49 | { 50 | // do sub-prefixes check 51 | bool all_prefix_match = true; 52 | int msk = (1 << num_of_bits) - 2; 53 | int prefix_shift = 1; 54 | while (msk) 55 | { 56 | 57 | int prefix = (msk & i) >> prefix_shift; 58 | int prefix_one_cnt = 0; 59 | int prefix_zero_cnt = 0; 60 | while (prefix) 61 | { 62 | if (prefix & 1) 63 | prefix_one_cnt++; 64 | else 65 | prefix_zero_cnt++; 66 | prefix = prefix >> 1; 67 | } 68 | if (prefix_zero_cnt > prefix_one_cnt) 69 | { 70 | all_prefix_match = false; 71 | break; 72 | } 73 | prefix_shift++; 74 | msk = msk & (msk << 1); 75 | } 76 | if (all_prefix_match) 77 | { 78 | s.push_back(getBinaryRep(i, num_of_bits)); 79 | } 80 | } 81 | } 82 | return s; 83 | } 84 | 85 | // Driver code 86 | int main() 87 | { 88 | int n = 4; 89 | 90 | vector results = NBitBinary(n); 91 | for (int i = 0; i < results.size(); ++i) 92 | cout << results[i] << " "; 93 | 94 | return 0; 95 | } 96 | -------------------------------------------------------------------------------- /Add Code Here/Python/Python Macro Programmer/Macro_Programmer.py: -------------------------------------------------------------------------------- 1 | import keyboard as ky 2 | import pyautogui as pag 3 | import screen_brightness_control as sbc 4 | import json 5 | import re 6 | 7 | 8 | def Toggle(): 9 | global is_running 10 | is_running = not is_running 11 | print("workinggg") 12 | Macro() 13 | 14 | def Start(): 15 | global is_running 16 | is_running = True 17 | Macro() 18 | 19 | def Stop(): 20 | global is_running 21 | is_running = False 22 | Macro() 23 | 24 | def runType(): 25 | 26 | if toggle["RunType"] == "0": 27 | ky.add_hotkey(toggle["Toggle"],Toggle, suppress=True) 28 | 29 | elif toggle["RunType"] =="1": 30 | ky.add_hotkey(toggle["Start"], Start, suppress=True) 31 | ky.add_hotkey(toggle["Stop"], Stop, suppress=True) 32 | 33 | def default_Macro (toPrint): 34 | ky.call_later(pag.write,args=[toPrint],delay= 0.001) 35 | 36 | def remap_Macro(toRemap): 37 | ky.call_later(pag.hotkey,args=[toRemap]) 38 | 39 | def Macro(): 40 | if is_running: 41 | print(macroString) 42 | print(type(macroString)) 43 | 44 | for key, macro in macroString.items(): 45 | print(f"{key} : {macro}") 46 | ky.add_hotkey(key,default_Macro, args= [macro], suppress=True) 47 | 48 | for key, remap in remapString.items(): 49 | print(f"{key} : {remap}") 50 | if remap in sp_Functions: 51 | ky.add_hotkey(key,sp_Functions.get(remap), suppress=True) 52 | 53 | else: 54 | ky.add_hotkey(key,remap_Macro,args=[remap], suppress=True) 55 | 56 | elif not is_running: 57 | for macro in macroString.keys(): 58 | ky.remove_hotkey(macro) 59 | 60 | for remap in remapString.keys(): 61 | ky.remove_hotkey(remap) 62 | 63 | 64 | sp_Functions = { 65 | 'Brightness_Up':lambda:ky.call_later(sbc.set_brightness, args=["+10"]), 66 | 'Brightness_Down':lambda:ky.call_later(sbc.set_brightness, args=["-10"]) 67 | } 68 | 69 | if __name__ =="__main__": 70 | with open("Macro.json", "r") as readFile: 71 | toggle,macroString,remapString = json.load(readFile) 72 | 73 | is_running = False 74 | 75 | 76 | 77 | runType() 78 | 79 | ky.wait(toggle["Exit"], suppress= True, trigger_on_release= True) 80 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/Details of students: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class student 4 | 5 | { 6 | 7 | public static void main(String args[]) 8 | 9 | { 10 | 11 | int rollno,phone,age,cgpa; 12 | 13 | char name; 14 | 15 | void read(student s) 16 | 17 | { 18 | 19 | Scanner in=new Scanner (System.in); 20 | 21 | System.out.println("name"); 22 | 23 | this.name=in.nextLine(); 24 | 25 | System.out.println("Roll no"); 26 | 27 | this.rollno=in.nextInt(); 28 | 29 | 30 | 31 | System.out.prinln("age"); 32 | 33 | this.age=in.nextInt(); 34 | 35 | System.out.prinln("Phone no"); 36 | 37 | this.phoneno=in.nextLong; 38 | 39 | System.out.println("cgpa"); 40 | 41 | this.cgpa=in.nextFloat(); 42 | 43 | System.out.prinln(" "); 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | } 52 | 53 | student() 54 | 55 | { 56 | 57 | read(this); 58 | 59 | } 60 | 61 | } 62 | 63 | public class main 64 | 65 | { 66 | 67 | public static void main(String args[]) 68 | 69 | { 70 | 71 | int l=0,i,n=0,a=0,o=50; 72 | 73 | float r=0; 74 | 75 | student[] s=new student[10]; 76 | 77 | for(i=0;i<10;i++) 78 | 79 | { 80 | 81 | s[i]=new student(); 82 | 83 | 84 | 85 | } 86 | 87 | for(i=0;i<10;i++) 88 | 89 | { 90 | 91 | if s[i].cgpa>r 92 | 93 | { 94 | 95 | r=s[i].cgpa; 96 | 97 | l=i; 98 | 99 | } 100 | 101 | if ((s[i].name.compareTo("ANU"))==0 102 | 103 | { 104 | 105 | n=i 106 | 107 | } 108 | 109 | if (s[i].age 2 | // Javascript Program to Find the top three repeated numbers 3 | 4 | // User defined Pair class 5 | class Pair { 6 | constructor(first, second){ 7 | this.first = first; 8 | this.second = second; 9 | } 10 | } 11 | 12 | // Function to print top three repeated numbers 13 | function top3Repeated(arr, n) 14 | { 15 | // There should be atleast two elements 16 | if (n < 3) { 17 | document.write("Invalid Input"); 18 | return; 19 | } 20 | 21 | // Count Frequency of each element 22 | arr.sort((a, b) => a - b) 23 | let freq = new Map(); 24 | for (let i = 0; i < n; i++) 25 | if (freq.has(arr[i])) 26 | freq.set(arr[i], 1 + freq.get(arr[i])); 27 | else 28 | freq.set(arr[i], 1); 29 | 30 | // Initialize first value of each variable 31 | // of Pair type is INT_MIN 32 | let x = new Pair(); 33 | let y = new Pair(); 34 | let z = new Pair(); 35 | x.first = y.first = z.first = Number.MIN_SAFE_INTEGER; 36 | 37 | for (let curr of freq) { 38 | // If frequency of current element 39 | // is not zero and greater than 40 | // frequency of first largest element 41 | if (parseInt(curr[1]) > x.first) { 42 | 43 | // Update second and third largest 44 | z.first = y.first; 45 | z.second = y.second; 46 | y.first = x.first; 47 | y.second = x.second; 48 | 49 | // Modify values of x Number 50 | x.first = parseInt((curr[1])); 51 | x.second = parseInt((curr[0])); 52 | } 53 | 54 | // If frequency of current element is 55 | // not zero and frequency of current 56 | // element is less than frequency of 57 | // first largest element, but greater 58 | // than y element 59 | else if (parseInt((curr[1])) > y.first) { 60 | // Modify values of third largest 61 | z.first = y.first; 62 | z.second = y.second; 63 | 64 | // Modify values of second largest 65 | y.first = parseInt((curr[1])); 66 | y.second = parseInt((curr[0])); 67 | } 68 | 69 | // If frequency of current element 70 | // is not zero and frequency of 71 | // current element is less than 72 | // frequency of first element and 73 | // second largest, but greater than 74 | // third largest. 75 | else if (parseInt((curr[1])) > z.first) { 76 | 77 | // Modify values of z Number 78 | z.first = parseInt((curr[1])); 79 | z.second = parseInt((curr[0])); 80 | } 81 | } 82 | 83 | document.write("Three largest elements are " + x.second + " " 84 | + y.second + " " + z.second); 85 | } 86 | 87 | // Driver's Code 88 | 89 | let arr = [ 3, 4, 2, 3, 16, 3, 15, 90 | 16, 15, 15, 16, 2, 3 ]; 91 | let n = arr.length; 92 | top3Repeated(arr, n); 93 | 94 | 95 | 96 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/InorderSuccessorInBinaryTree.java: -------------------------------------------------------------------------------- 1 | public class InorderSuccessorInBinaryTree { 2 | 3 | public static class Node { 4 | int data; 5 | Node left; 6 | Node right; 7 | 8 | public Node(int data) { 9 | this.data = data; 10 | } 11 | 12 | public String toString() { 13 | return data + ""; 14 | } 15 | } 16 | 17 | public static Node root; 18 | 19 | public static void main(String[] args) { 20 | 21 | root = new Node(6); 22 | root. left = new Node(4); 23 | root. right = new Node(10); 24 | root. left. left = new Node(1); 25 | root. left. right = new Node(5); 26 | root. right. right = new Node(12); 27 | root. right. left = new Node(8); 28 | root. right. left. left = new Node(7); 29 | root. right. left. right = new Node(9); 30 | 31 | /*for any rightmost node of a tree which is also the greatest 32 | * node in a tree, Inorder successor can not exist as it is 33 | * the very last node to get visited in the inorder */ 34 | System.out.println(getJustLarger(root, 9)); 35 | } 36 | 37 | public static int getJustLarger(Node node, int target) { 38 | /* return a maximum value in the basecase so that it 39 | * can not be able to update final answer to be returned */ 40 | if (node == null) 41 | return Integer.MAX_VALUE; 42 | 43 | /* result of recursive call to left subtree*/ 44 | int lr = getJustLarger(node.left, target); 45 | /* result of recursive call to left subtree*/ 46 | int rr = getJustLarger(node.right, target); 47 | 48 | /*final answer to be returned*/ 49 | int rv = Integer.MAX_VALUE; 50 | 51 | /* if the current node value is greater than target and 52 | * it is lesser than the greater values (greater than target 53 | * node value) explored till now, then update the final 54 | * answer to be returned*/ 55 | if (node.data > target && node.data < rv) { 56 | rv = node.data; 57 | } 58 | 59 | /* if the result of Left recursive call is greater than target and 60 | * it is lesser than the greater values explored till now, 61 | * then update the final answer to be returned*/ 62 | if (lr > target && lr < rv) { 63 | rv = lr; 64 | } 65 | /* if the result of Right recursive call is greater than target and 66 | * it is lesser than the greater values explored till now, 67 | * then update the final answer to be returned*/ 68 | 69 | if (rr > target && rr < rv) { 70 | rv = rr; 71 | } 72 | return rv; 73 | 74 | } 75 | 76 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/minSubtree.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.ArrayList; 3 | 4 | class Graph{ 5 | 6 | static int res; 7 | 8 | // DFS method to traverse through edges, 9 | // calculating subtree sum at each node 10 | // and updating the difference between subtrees 11 | static void dfs(int u, int parent, int totalSum, 12 | ArrayList[] edge, int subtree[]) 13 | { 14 | int sum = subtree[u]; 15 | 16 | // Loop for all neighbors except parent 17 | // and aggregate sum over all subtrees 18 | for(int i = 0; i < edge[u].size(); i++) 19 | { 20 | int v = edge[u].get(i); 21 | 22 | if (v != parent) 23 | { 24 | dfs(v, u, totalSum, edge, subtree); 25 | sum += subtree[v]; 26 | } 27 | } 28 | 29 | // Store sum in current node's subtree index 30 | subtree[u] = sum; 31 | 32 | // At one side subtree sum is 'sum' and other 33 | // side subtree sum is 'totalSum - sum' so 34 | // their difference will be totalSum - 2*sum, 35 | // by which we'll update res 36 | if (u != 0 && Math.abs(totalSum - 2 * sum) < res) 37 | res = Math.abs(totalSum - 2 * sum); 38 | } 39 | 40 | // Method returns minimum subtree sum difference 41 | static int getMinSubtreeSumDifference(int vertex[], 42 | int[][] edges, 43 | int N) 44 | { 45 | int totalSum = 0; 46 | int[] subtree = new int[N]; 47 | 48 | // Calculating total sum of tree and 49 | // initializing subtree sum's by 50 | // vertex values 51 | for(int i = 0; i < N; i++) 52 | { 53 | subtree[i] = vertex[i]; 54 | totalSum += vertex[i]; 55 | } 56 | 57 | // Filling edge data structure 58 | @SuppressWarnings("unchecked") 59 | ArrayList[] edge = new ArrayList[N]; 60 | for(int i = 0; i < N; i++) 61 | { 62 | edge[i] = new ArrayList<>(); 63 | } 64 | for(int i = 0; i < N - 1; i++) 65 | { 66 | edge[edges[i][0]].add(edges[i][1]); 67 | edge[edges[i][1]].add(edges[i][0]); 68 | } 69 | 70 | // int res = Integer.MAX_VALUE; 71 | 72 | // Calling DFS method at node 0, with 73 | // parent as -1 74 | dfs(0, -1, totalSum, edge, subtree); 75 | return res; 76 | } 77 | 78 | // Driver code 79 | public static void main(String[] args) 80 | { 81 | res = Integer.MAX_VALUE; 82 | 83 | int[] vertex = { 4, 2, 1, 6, 3, 5, 2 }; 84 | int[][] edges = { { 0, 1 }, { 0, 2 }, 85 | { 0, 3 }, { 2, 4 }, 86 | { 2, 5 }, { 3, 6 } }; 87 | int N = vertex.length; 88 | 89 | System.out.println(getMinSubtreeSumDifference( 90 | vertex, edges, N)); 91 | } 92 | } -------------------------------------------------------------------------------- /Add Code Here/CPP/Bitset.cpp: -------------------------------------------------------------------------------- 1 | / C++ program to demonstrate various functionality of bitset 2 | #include 3 | using namespace std; 4 | 5 | #define M 32 6 | 7 | int main() 8 | { 9 | // default constructor initializes with all bits 0 10 | bitset bset1; 11 | 12 | // bset2 is initialized with bits of 20 13 | bitset bset2(20); 14 | 15 | // bset3 is initialized with bits of specified binary string 16 | bitset bset3(string("1100")); 17 | 18 | // cout prints exact bits representation of bitset 19 | cout << bset1 << endl; // 00000000000000000000000000000000 20 | cout << bset2 << endl; // 00000000000000000000000000010100 21 | cout << bset3 << endl; // 00000000000000000000000000001100 22 | cout << endl; 23 | 24 | // declaring set8 with capacity of 8 bits 25 | 26 | bitset<8> set8; // 00000000 27 | 28 | // setting first bit (or 6th index) 29 | set8[1] = 1; // 00000010 30 | set8[4] = set8[1]; // 00010010 31 | cout << set8 << endl; 32 | 33 | // count function returns number of set bits in bitset 34 | int numberof1 = set8.count(); 35 | 36 | // size function returns total number of bits in bitset 37 | // so there difference will give us number of unset(0) 38 | // bits in bitset 39 | int numberof0 = set8.size() - numberof1; 40 | 41 | cout << set8 << " has " << numberof1 << " ones and " 42 | << numberof0 << " zeros\n"; 43 | 44 | // test function return 1 if bit is set else returns 0 45 | cout << "bool representation of " << set8 << " : "; 46 | for (int i = 0; i < set8.size(); i++) 47 | cout << set8.test(i) << " "; 48 | 49 | cout << endl; 50 | 51 | // any function returns true, if atleast 1 bit 52 | // is set 53 | if (!set8.any()) 54 | cout << "set8 has no bit set.\n"; 55 | 56 | if (!bset1.any()) 57 | cout << "bset1 has no bit set.\n"; 58 | 59 | // none function returns true, if none of the bit 60 | // is set 61 | if (!bset1.none()) 62 | cout << "bset1 has some bit set\n"; 63 | 64 | // bset.set() sets all bits 65 | cout << set8.set() << endl; 66 | 67 | // bset.set(pos, b) makes bset[pos] = b 68 | cout << set8.set(4, 0) << endl; 69 | 70 | // bset.set(pos) makes bset[pos] = 1 i.e. default 71 | // is 1 72 | cout << set8.set(4) << endl; 73 | 74 | // reset function makes all bits 0 75 | cout << set8.reset(2) << endl; 76 | cout << set8.reset() << endl; 77 | 78 | // flip function flips all bits i.e. 1 <-> 0 79 | // and 0 <-> 1 80 | cout << set8.flip(2) << endl; 81 | cout << set8.flip() << endl; 82 | 83 | // Converting decimal number to binary by using bitset 84 | int num = 100; 85 | cout << "\nDecimal number: " << num 86 | << " Binary equivalent: " << bitset<8>(num); 87 | 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /Add Code Here/CPP/Dijkstra's_Algo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define INF std::numeric_limits::max() 6 | 7 | // Function to find the vertex with the minimum distance value, from the set of vertices not yet included in the shortest path tree 8 | int minDistance(const std::vector &dist, const std::vector &sptSet, int V) { 9 | int min = INF; 10 | int min_index; 11 | for (int v = 0; v < V; ++v) { 12 | if (!sptSet[v] && dist[v] <= min) { 13 | min = dist[v]; 14 | min_index = v; 15 | } 16 | } 17 | return min_index; 18 | } 19 | 20 | // Function to print the constructed distance array 21 | void printSolution(const std::vector &dist, int V) { 22 | std::cout << "Vertex \t\t Distance from Source" << std::endl; 23 | for (int i = 0; i < V; ++i) { 24 | std::cout << i << " \t\t " << dist[i] << std::endl; 25 | } 26 | } 27 | 28 | // Function that implements Dijkstra's single source shortest path algorithm for a graph represented using adjacency matrix representation 29 | void dijkstra(const std::vector> &graph, int src, int V) { 30 | std::vector dist(V, INF); // The output array. dist[i] will hold the shortest distance from src to i 31 | 32 | std::vector sptSet(V, false); // sptSet[i] will be true if vertex i is included in the shortest path tree 33 | 34 | dist[src] = 0; // Distance of source vertex from itself is always 0 35 | 36 | for (int count = 0; count < V - 1; ++count) { 37 | int u = minDistance(dist, sptSet, V); // Pick the minimum distance vertex from the set of vertices not yet processed 38 | sptSet[u] = true; // Mark the picked vertex as processed 39 | 40 | // Update dist value of the adjacent vertices of the picked vertex 41 | for (int v = 0; v < V; ++v) { 42 | if (!sptSet[v] && graph[u][v] && dist[u] != INF && dist[u] + graph[u][v] < dist[v]) { 43 | dist[v] = dist[u] + graph[u][v]; 44 | } 45 | } 46 | } 47 | 48 | printSolution(dist, V); 49 | } 50 | 51 | // Main function 52 | int main() { 53 | std::vector> graph = {{0, 4, 0, 0, 0, 0, 0, 8, 0}, 54 | {4, 0, 8, 0, 0, 0, 0, 11, 0}, 55 | {0, 8, 0, 7, 0, 4, 0, 0, 2}, 56 | {0, 0, 7, 0, 9, 14, 0, 0, 0}, 57 | {0, 0, 0, 9, 0, 10, 0, 0, 0}, 58 | {0, 0, 4, 14, 10, 0, 2, 0, 0}, 59 | {0, 0, 0, 0, 0, 2, 0, 1, 6}, 60 | {8, 11, 0, 0, 0, 0, 1, 0, 7}, 61 | {0, 0, 2, 0, 0, 0, 6, 7, 0}}; 62 | 63 | dijkstra(graph, 0, 9); 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /Add Code Here/Python/deepneuralnetwork_mnist.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """DeepNeuralNetwork_MNIST.ipynb 3 | 4 | Automatically generated by Colaboratory. 5 | 6 | Original file is located at 7 | https://colab.research.google.com/drive/19ggEwug2tbjVzJmZU300-eRckprzE3KI 8 | """ 9 | 10 | import torch 11 | import torch.nn as nn 12 | import torchvision 13 | import torchvision.transforms as transforms 14 | import matplotlib.pyplot as plt 15 | import sys 16 | from torch.utils.tensorboard import SummaryWriter 17 | writer = SummaryWriter("runs/mnist") 18 | 19 | # hyperparameters 20 | input_size = 784 21 | hidden_size = 100 22 | num_classes = 10 # it can be 0 to 9 23 | num_epoch = 2 24 | batch_size = 100 25 | learning_rate = 0.001 26 | 27 | # load dataset 28 | train_dataset = torchvision.datasets.MNIST(root='./data',train=True,transform=transforms.ToTensor(),download=True) 29 | test_dataset = torchvision.datasets.MNIST(root='./data',train=False,transform=transforms.ToTensor()) 30 | 31 | train_loader = torch.utils.data.DataLoader(dataset=train_dataset,batch_size=batch_size,shuffle=True) 32 | test_loader = torch.utils.data.DataLoader(dataset=test_dataset,batch_size=batch_size,shuffle=False) 33 | 34 | print(train_loader) 35 | # example = iter(test_loader) 36 | # example_data,example_targets = example.next() 37 | 38 | # for i in range(6): 39 | # plt.subplot(2,3,i+1) 40 | # plt.imshow(example_data[i][0],cmap="gray") 41 | 42 | # img_grid = torchvision.utils.make_grid(example_data) 43 | # writer.add_image('mnist_images',img_grid) 44 | 45 | # writer.close() 46 | # sys.exit() 47 | 48 | class DeepNeuralNet(nn.Module): 49 | def __init__(self,input_size,hidden_size,num_classes): 50 | super(DeepNeuralNet,self).__init__() 51 | self.l1 = nn.Linear(input_size,hidden_size) 52 | self.relu = nn.ReLU() 53 | self.l2 = nn.Linear(hidden_size,num_classes) 54 | 55 | def forward(self,x): 56 | out = self.l1(x) 57 | out = self.relu(out) 58 | out = self.l2(out) 59 | return out 60 | 61 | model = DeepNeuralNet(input_size,hidden_size,num_classes) 62 | 63 | compute_loss = nn.CrossEntropyLoss() 64 | optimizer = torch.optim.Adam(model.parameters(),lr = learning_rate) 65 | 66 | n_total_steps = len(train_loader) 67 | 68 | for epoch in range(num_epoch): 69 | for i,(images,labels) in enumerate(train_loader): 70 | images = images.reshape(-1,784) 71 | 72 | output = model(images) 73 | loss = compute_loss(output,labels) 74 | loss.backward() 75 | optimizer.step() 76 | optimizer.zero_grad() 77 | 78 | if (i+1)%100==0: 79 | print(f'epochs = {epoch+1} : step = {i+1} : loss = {loss}') 80 | 81 | with torch.no_grad(): 82 | n_correct = 0 83 | n_samples = 0 84 | 85 | for inputs,labels in test_loader: 86 | inputs = inputs.reshape(-1,784) 87 | output = model(inputs) 88 | _, prediction = torch.max(output,1) 89 | n_samples += labels.shape[0] 90 | n_correct += (prediction==labels).sum().item() 91 | 92 | print(n_correct,n_samples) 93 | accuracy = (n_correct*100)/n_samples 94 | 95 | print(f'Accuracy = {accuracy}') 96 | 97 | 98 | 99 | -------------------------------------------------------------------------------- /Add Code Here/JAVA/calculator.java: -------------------------------------------------------------------------------- 1 | import java.awt.event.*; 2 | import java.awt.*; 3 | import javax.swing.*; 4 | public class calculator extends JFrame implements ActionListener 5 | { 6 | JButton b10,b11,b12,b13,b14,b15; 7 | JButton b[]=new JButton[10]; 8 | int i,r,n1,n2; 9 | JTextField res = new JTextField();; 10 | char op; 11 | public calculator() 12 | { 13 | super("Calulator"); 14 | JFrame frame = new JFrame(); 15 | setLayout(new BorderLayout()); 16 | JPanel p=new JPanel(); 17 | res.setBounds(10, 40, 270, 40); 18 | res.setFont(new Font("Arial", Font.BOLD, 20)); 19 | res.setEditable(false); 20 | res.setHorizontalAlignment(SwingConstants.RIGHT); 21 | frame.add(res); 22 | p.setLayout(new GridLayout(4,4)); 23 | for(int i=0;i<10;i++) 24 | { 25 | b[i]=new JButton(i+""); 26 | p.add(b[i]); 27 | b[i].addActionListener(this); 28 | } 29 | b10=new JButton("+"); 30 | p.add(b10); 31 | b10.addActionListener(this); 32 | 33 | b11=new JButton("-"); 34 | p.add(b11); 35 | b11.addActionListener(this); 36 | 37 | b12=new JButton("*"); 38 | p.add(b12); 39 | b12.addActionListener(this); 40 | 41 | b13=new JButton("/"); 42 | p.add(b13); 43 | b13.addActionListener(this); 44 | 45 | b14=new JButton("="); 46 | p.add(b14); 47 | b14.addActionListener(this); 48 | 49 | b15=new JButton("C"); 50 | p.add(b15); 51 | b15.addActionListener(this); 52 | 53 | add(p,BorderLayout.CENTER); 54 | add(res,BorderLayout.NORTH); 55 | setVisible(true); 56 | setSize(200,250); 57 | } 58 | public void actionPerformed(ActionEvent ae) 59 | { 60 | JButton pb=(JButton)ae.getSource(); 61 | if(pb==b15) 62 | { 63 | r=n1=n2=0; 64 | res.setText(""); 65 | } 66 | else 67 | if(pb==b14) 68 | { 69 | n2=Integer.parseInt(res.getText()); 70 | eval(); 71 | res.setText(""+r); 72 | } 73 | 74 | else 75 | { 76 | boolean opf=false; 77 | if(pb==b10) 78 | { op='+'; 79 | opf=true; 80 | } 81 | if(pb==b11) 82 | { op='-';opf=true;} 83 | if(pb==b12) 84 | { op='*';opf=true;} 85 | if(pb==b13) 86 | { op='/';opf=true;} 87 | 88 | if(opf==false) 89 | { 90 | for(i=0;i<10;i++) 91 | { 92 | if(pb==b[i]) 93 | { 94 | String t=res.getText(); 95 | t+=i; 96 | res.setText(t); 97 | } 98 | } 99 | } 100 | else 101 | { 102 | n1=Integer.parseInt(res.getText()); 103 | res.setText(""); 104 | } 105 | } 106 | } 107 | int eval() 108 | { 109 | switch(op) 110 | { 111 | case '+': r=n1+n2; break; 112 | case '-': r=n1-n2; break; 113 | case '*': r=n1*n2; break; 114 | case '/': r=n1/n2; break; 115 | 116 | } 117 | return 0; 118 | } 119 | 120 | public static void main(String arg[]) 121 | { 122 | new calculator(); 123 | } 124 | } --------------------------------------------------------------------------------