├── calci.py ├── a.out ├── Random_number_generator.py ├── SumOfDigits.class ├── SieveOfEratosthenes.cpp ├── Factorial ├── inorder traversal ├── postorder ├── MissingNumber1to100.py ├── No. of a given character in string.cpp ├── Program for Sum of squares of first n natural numbers ├── additionCalci(quiz).py ├── checkIfAPowerOfTwo.py ├── CONTRIBUTING.md ├── preorder_traversal ├── Java ├── redux │ ├── shape │ │ ├── Shape.java │ │ ├── Square.java │ │ ├── Circle.java │ │ └── Rectangle.java │ └── CalculateAreaRedux.java ├── Count_occurence_of_given_character_in_string.java ├── first-non-repeating-character.java ├── BinarySearch.java ├── Rotate90.java ├── Count.java ├── identical-trees.java ├── Max-sum-of-hourglass.java └── right-view-binary-tree.java ├── linearsSearch.c ├── Second Largest Number in an Array-Python ├── bubble_sort.py ├── random number generator.py ├── linearsearch.py ├── trail.cpp ├── sumOfDigits.py ├── reversing number (python).py ├── NumericStringToInt.cpp ├── Sum_of_squares.c ├── pairs.js ├── sum_of_squares_of_n_natural_number.cpp ├── Binary search ├── README.md ├── Swap_Two_Numbers.cpp ├── temperature conversion (celsius to fahrenheit).py ├── Numeric string to int ├── python programs ├── counting_frequency_of_letters.py ├── simplify_fractions.py └── lcm_of_array.py ├── ReversingNumber.java ├── LCM of 2 numbers_in C++.cpp ├── gcd.cpp ├── vkgupta857_reverse_number.py ├── unique_bst.py ├── sum of even and odd digits ├── largest_using_ternary.cpp ├── search_bst.py ├── maxGCD.cpp ├── smallestofthree.c ├── sum_of_squares_of_first_n_natural_numbers.c ├── randomnumber.js ├── GCD_Two_Number.cpp ├── findMin_recursion.cpp ├── reverse_words_in_string.py ├── swap2no ├── sumOfDigit_recursion.cpp ├── count characters.cpp ├── GCD using python.py ├── finding sum of squares of n natural numbers.cpp ├── password.py ├── Happy-Number.c ├── Fibonacci.cpp ├── sum_squares_of_first_n_natural_numbers.cpp ├── template1.cpp ├── vkgupta857_reverse_words_of_string.py ├── coinchangehny.cpp ├── secondLargest ├── string to number .cpp ├── sum of squares of first n natural numbers.py ├── LCM_Two_Number.py ├── factorial.cpp ├── findQuotReminder ├── randomnumber ├── perfectno.java ├── fibonacci.cpp ├── MissingNumInArray.java ├── swap two numbers without using third variable.cpp ├── numberSeries_recursion.cpp ├── isAnagram.cpp ├── invert-binary-tree.py ├── kadane.py ├── reverseNum.cpp ├── Factorial.java ├── LCM_Two_Number.cpp ├── bloom_filter.h ├── object_tracking.py ├── bogoSort.py ├── lcm.java ├── pascal_triangle.cpp ├── vkgupta857_linear_search.py ├── Shell_Sort.py ├── CountCharacter.cpp ├── Pascal Triangle.py ├── Python-graphs ├── minimalsquare.cpp ├── KadanesAlgo.java ├── CODEFORCE-bits.cpp ├── Divisibility_test_for_11.py ├── Additionoftwonumbers.cs ├── Beautiful Matrix.cpp ├── check_if_power_of_two.cpp ├── lexicographicalOrder ├── secondLargest.java ├── warp_perspective.py ├── program.py ├── Reverse2.cpp ├── prime.py ├── UniqueCharStr.java ├── Program to display the Fibonacci sequence ├── Perfect Number.cpp ├── strings_are_anagrams ├── number_triangle.c ├── JoinTwoLists.java ├── Second Largest Number In Array ├── Transposition_Cipher.cpp ├── detectLoopLinkedList.py ├── linear_search.cpp ├── LinearSearch_with_python.py ├── Number on Whiteboard.cpp ├── Team_splitter.py ├── vkgupta857_binary_search.py ├── dfsgrid.cpp ├── replace_all_occurance_of_character_from_string.cpp ├── OccurenceCharInStr.c ├── map.cpp ├── countingsort.c ├── LinearSearch ├── MatrixSearch.cpp ├── RECURSIVE MERGESORT ├── SumOfDigits.java ├── Detect loop in LinkedList.cpp ├── visible_pillars.cpp ├── armstrong number.cpp ├── sequential_search.cpp ├── towerofhanoi.cpp ├── inOrderTreeTraversal.js ├── sum of even odd elements in a one dimensional array.cpp ├── BinarySearchC++.cpp ├── area.c ├── Decimal to binary.cpp ├── LCM_two_numbers.java ├── top2maxnumbers.c ├── binarySearch_ManoJarvis.cpp ├── Linked List ├── Scope.java ├── ceaserCipher.py ├── ConnectedComponent.cpp ├── calc.css ├── gnomeSort.java ├── reduced_strings.cpp ├── Determine Identical Binary Tree.cpp ├── calc.js ├── Calculator.cpp ├── fibonaci.cpp ├── kadane.java ├── pre_order_traversal_binaryTree.cpp ├── encrypt-caesar.c ├── connectedcompgrp.cpp ├── countOccurenceOfCharactersInString.c ├── post_order_traversal_binaryTree.cpp ├── employee.cpp ├── CheckParallel.cpp ├── ReverseLine.java ├── Palindrome.java ├── bubblesort.cpp ├── anagram.py ├── 0-1-Knapsack.c ├── Numtoword.py ├── SelectionSort.java ├── binary_search.c ├── sort012.py ├── kadaneAlgorithm.cpp ├── reverse_linked_list.py ├── SelectionSort.c ├── inv_cloak.py ├── mergesort.cpp ├── Maximum Diagonal Sum.java ├── knapsack.cpp ├── DFS.cpp ├── Group Anagrams.java ├── identicalTrees.js ├── CalculateArea.cpp ├── RootsofaQuadraticEquation.c ├── ThreadDemo.java ├── leavesBinaryTree.js ├── Insertion Sort.cpp ├── bubblesort_vikram.cpp ├── Hulk.java ├── BubbleSort.java ├── bucket_sort.py ├── Recursion.java ├── codechef.cpp ├── second_largest.py ├── Quadratic_Equations_Root.cpp ├── heap_stl.cpp ├── BinarySearch.cpp ├── listPalindrome.js ├── Linked List via C ├── Static Queue using Arrays.cpp ├── Code to search an element in circular Linked List.cpp ├── Linked Stack.cpp └── Linked Queue in C.cpp ├── Basic calculator using switch case.c ├── interpolation_search.c ├── Calculator.java ├── insertionsort.c ├── Selection Sort.cpp ├── heapSort.cpp ├── ReverseALinkedList.cpp ├── maxTwoNumbersInAnArray ├── Linkedlist.py ├── ReverseLinkedList.c ├── matrixMultiplication.c ├── PalindromeRange ├── heightOfBinaryTree.java ├── heapsort.py ├── insertInBST.cpp ├── Count Good Triplets.java ├── calc.c.txt ├── quicksort.cpp ├── triangle patterns.cpp ├── Area of Rectangle, Square and Circle using switch case.c ├── circular-linked-list.js ├── make_strings_anagram.cpp ├── evaluate_prefix.c ├── array_implementation_of_queue.c ├── Round_Robin.cpp ├── mst_ada.py ├── circular-linked-list.java ├── Fractional_knapack_greedy.py ├── HouseRobber.md ├── TopologicalSort.cpp ├── Beautiful_Matrix.java ├── PalindromePartition.cpp ├── NumberOfSquares.cpp ├── Convert to Strictly increasing integer array with minimum changes.cpp ├── LRU.cpp ├── UniquePath.md └── invertbst.cpp /calci.py: -------------------------------------------------------------------------------- 1 | 50*50+80*80+96*96 2 | -------------------------------------------------------------------------------- /a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sat5297/hacktober-coding/HEAD/a.out -------------------------------------------------------------------------------- /Random_number_generator.py: -------------------------------------------------------------------------------- 1 | import random 2 | n = random.random() 3 | print(n) 4 | -------------------------------------------------------------------------------- /SumOfDigits.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sat5297/hacktober-coding/HEAD/SumOfDigits.class -------------------------------------------------------------------------------- /SieveOfEratosthenes.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sat5297/hacktober-coding/HEAD/SieveOfEratosthenes.cpp -------------------------------------------------------------------------------- /Factorial: -------------------------------------------------------------------------------- 1 | # Factorial of a number 2 | def fac(n): 3 | if n==0 or n==1: 4 | return 1 5 | return n*fac(n-1) 6 | a = int(input()) 7 | print(fac(a)) 8 | -------------------------------------------------------------------------------- /inorder traversal: -------------------------------------------------------------------------------- 1 | def inroder(treenode): 2 | if treenode == NULL: 3 | return 4 | inorder(treenode->left) 5 | print(treenode->val) 6 | inorder(treenode->right) 7 | -------------------------------------------------------------------------------- /postorder: -------------------------------------------------------------------------------- 1 | def postoroder(treenode): 2 | if treenode == NULL: 3 | return 4 | postorder(treenode->left) 5 | postorder(treenode->right) 6 | print(treenode->val) 7 | -------------------------------------------------------------------------------- /MissingNumber1to100.py: -------------------------------------------------------------------------------- 1 | # getMissingNo takes list as argument 2 | def getMissingNo(A): 3 | n = len(A) 4 | total = (n + 1)*(n + 2)/2 5 | sum_of_A = sum(A) 6 | return total - sum_of_A 7 | -------------------------------------------------------------------------------- /No. of a given character in string.cpp: -------------------------------------------------------------------------------- 1 | int countChar(string s, char c){ 2 | int cnt = 0; 3 | 4 | for(char x: s){ 5 | if(x==c) cnt++; 6 | } 7 | return cnt; 8 | } 9 | -------------------------------------------------------------------------------- /Program for Sum of squares of first n natural numbers: -------------------------------------------------------------------------------- 1 | # Program for Sum of squares of first n natural numbers 2 | n = int(input()) 3 | s = 0 4 | for i in range(1,n+1): 5 | s = s + i**2 6 | print(s) 7 | -------------------------------------------------------------------------------- /additionCalci(quiz).py: -------------------------------------------------------------------------------- 1 | #Addition calci 2 | 3 | print("enter your 1st number") 4 | n1 = input() 5 | print("enter your 2nd number") 6 | n2 = input() 7 | print("sum of these number is", int(n1)+int(n2)) 8 | -------------------------------------------------------------------------------- /checkIfAPowerOfTwo.py: -------------------------------------------------------------------------------- 1 | def isPowerOfTwo(n: int): 2 | return n>0 and (n & n-1 == 0) 3 | 4 | #Testing to check if the value passes or not 5 | print(isPowerOfTwo(2)) 6 | #Returns True 7 | 8 | 9 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | This repository aims at helping beginners to contribute to the open source and participate in the hacktoberfest. 2 | It contains basic algorithmic programs written in multiple languages. 3 | 4 | -------------------------------------------------------------------------------- /preorder_traversal: -------------------------------------------------------------------------------- 1 | void preorder(Node *root){ 2 | if(root!=NULL) 3 | { 4 | cout << root->key << " "; 5 | preorder(root->left); 6 | preorder(root->right); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Java/redux/shape/Shape.java: -------------------------------------------------------------------------------- 1 | package Java.redux.shape; 2 | 3 | /** 4 | * @author Alessandro Arosio - 06/10/2020 17:06 5 | */ 6 | public interface Shape { 7 | Double getArea(); 8 | String getName(); 9 | } 10 | -------------------------------------------------------------------------------- /linearsSearch.c: -------------------------------------------------------------------------------- 1 | int Linear_Search(int A[], int n, int key) //A is the array, n is size of array 2 | { 3 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() { 5 | long long int a,b,t,i; 6 | cin>>t; 7 | for(i=0;i>a>>b; 10 | cout<0): 5 | digit=number%10 6 | reverse=reverse*10+digit 7 | number=number//10 8 | print("The number when reversed is:",reverse) -------------------------------------------------------------------------------- /NumericStringToInt.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | string s = "999"; 8 | 9 | stringstream degree(s); 10 | 11 | int x = 0; 12 | degree >> x; 13 | 14 | cout << "Value of x: " << x; 15 | } 16 | -------------------------------------------------------------------------------- /Sum_of_squares.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n; 6 | long int sum=0; 7 | printf("Enter the value of n\n"); 8 | scanf("%d",&n); 9 | sum=(n*(n+1)*(2*n+1))/6; 10 | printf("The sum of first %d natural integers is %d\n",n,sum); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /pairs.js: -------------------------------------------------------------------------------- 1 | function pairs(array, number) { 2 | var matchingPairs = []; 3 | for(let i=0; i 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n; 8 | cin>>n; 9 | int s=0; 10 | for(int i=1;i<=n;i++) 11 | { 12 | s=s+(i*i); 13 | } 14 | cout< key: 9 | high = mid-1 10 | else: 11 | low = mid+1 12 | return false 13 | 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HACKTOBERFEST 2020 2 | 3 | The repository was created in order to allow more and more beginners in the field of programming to contribute effectively to the open source community. It contains basic algorithmic programs written in various languages. 4 | 5 | Happy Hacktober, and all the best! :) 6 | -------------------------------------------------------------------------------- /Swap_Two_Numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a, b; 7 | 8 | cout << "Enter two numbers: "; 9 | cin >> a >> b; 10 | swap(a,b); //Function given in STL of C++ 11 | cout << "Swapped two numbers:" << a <<" "<< b; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /temperature conversion (celsius to fahrenheit).py: -------------------------------------------------------------------------------- 1 | #Temperature conversion from celsius to Fahrenheit 2 | #!/usr/bin/env python 3 | Celsius_temp=float(input("Enter the temperature in Celcius: ")) 4 | Fahrenheit_temp=9.0/5.0*Celsius_temp+32 5 | print("Temperature will be:",Celsius_temp,"Celsius,",Fahrenheit_temp,"Fahrenheit") -------------------------------------------------------------------------------- /Numeric string to int: -------------------------------------------------------------------------------- 1 | int StringToInt(string s){ 2 | int ans = 0, i=0; 3 | 4 | if(s.size()==0) return 0; 5 | if(s[0] == '-') i++; 6 | 7 | for( i; i>>frequency_counter("aaabbcbdde") 6 | returns a dictionary with the following data: 7 | 'a' : 3, 8 | 'b' : 3, 9 | 'c' : 1, 10 | 'd' : 2, 11 | 'e' : 1 12 | """ 13 | return c(s) 14 | -------------------------------------------------------------------------------- /ReversingNumber.java: -------------------------------------------------------------------------------- 1 | public static void main(String[] args) { 2 | 3 | Scanner scn = new Scanner(System.in); 4 | int n = scn.nextInt(); 5 | int ans = 0; 6 | while (n > 0) { 7 | 8 | int rem = n % 10; 9 | ans = ans * 10 + rem; 10 | 11 | n = n / 10; 12 | } 13 | 14 | System.out.println(ans); 15 | } 16 | -------------------------------------------------------------------------------- /LCM of 2 numbers_in C++.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | int gcd(int a,int b) 5 | { 6 | if(b==0) 7 | return a; 8 | else 9 | return gcd(b,a%b); 10 | } 11 | int main() 12 | { 13 | int a,b; 14 | cin>>a>>b; 15 | int x=gcd(a,b); 16 | cout< 2 | using namespace std; 3 | int gcd(int a, int b) 4 | { 5 | if (b == 0) 6 | return a; 7 | return gcd(b, a % b); 8 | 9 | } 10 | 11 | int main() 12 | { 13 | int a = 98, b = 56; 14 | cout<<"GCD of "<0): 9 | temp = n % 10 10 | result = result*10 + temp 11 | n = n // 10 12 | print("Reverse of the number is:", result) 13 | -------------------------------------------------------------------------------- /unique_bst.py: -------------------------------------------------------------------------------- 1 | unique binary search tree 2 | class Solution: 3 | def numTrees(self, n: int) -> int: 4 | dp = [0]*(n+1) 5 | dp[0]=dp[1] = 1 6 | for i in range(2,n+1): 7 | for j in range(1, i+1): 8 | dp[i] += dp[j-1]*dp[i-j] 9 | print(dp) 10 | return dp[n] 11 | -------------------------------------------------------------------------------- /sum of even and odd digits: -------------------------------------------------------------------------------- 1 | Num=int(input()) 2 | i=1 3 | sum=0 4 | sum1=0 5 | d=(len(str(Num))) 6 | n=Num 7 | while i<=d: 8 | l=n%10 9 | n=n//10 10 | if l%2==0: 11 | 12 | sum=sum+l 13 | 14 | else: 15 | 16 | sum1=sum1+l 17 | 18 | i=i+1 19 | 20 | 21 | print (sum ,sum1) 22 | -------------------------------------------------------------------------------- /largest_using_ternary.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int findlargest(int a, int b, int c){ 4 | int largest = (a>b) ? ((a>c) ? a : c) : ((b>c) ? b : c); 5 | return largest; 6 | } 7 | int main() { 8 | int a, b, c; 9 | cin>>a>>b>>c; 10 | cout<<"The largest element is "< TreeNode: 3 | if root.val==val: 4 | return root 5 | elif root.val>val and root.left!=None: 6 | return self.searchBST(root.left,val) 7 | elif root.val 2 | using namespace std; 3 | int main() 4 | { 5 | int i,j,k,l; 6 | cin>>i; 7 | int a[i]; 8 | for(j=0;j>k; 10 | if(k%2==0) 11 | {a[j]=k/2;} 12 | else 13 | {a[j]=(k-1)/2;} 14 | } 15 | for(l=0;l 2 | 3 | int main() 4 | { 5 | int a, b, c, temp, min; 6 | 7 | printf ("Enter three nos. using spaces: "); 8 | scanf ("%d%d%d", &a, &b, &c); 9 | 10 | temp = (a < b) ? a : b; 11 | min = (c < temp) ? c : temp; 12 | 13 | printf ("The Minimum of the three is: %d", min); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /sum_of_squares_of_first_n_natural_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | int sumofsquares(int n) 3 | { 4 | return (n * (n + 1) * (2 * n + 1)) / 6; 5 | } 6 | int main() 7 | { 8 | int n; 9 | printf("Please enter the number of terms : "); 10 | scanf("%d",&n); 11 | printf("The sum of squares of the first n natural numbers is : %d\n",sumofsquares(n)); 12 | } -------------------------------------------------------------------------------- /randomnumber.js: -------------------------------------------------------------------------------- 1 | // input any two number and get random number between them 2 | function generateRandom(min, max) { 3 | return Math.floor(Math.random() * (max - min + 1)) + min; 4 | } 5 | 6 | // calling function to generate random number between 1 and 100 7 | // you can pass any parameters to generate random number between any two number 8 | 9 | generateRandom(1, 100); 10 | -------------------------------------------------------------------------------- /GCD_Two_Number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a, b; 7 | 8 | cout << "Enter two numbers: "; 9 | cin >> a >> b; 10 | 11 | while(a != b) 12 | { 13 | if(a > b) 14 | a -= b; 15 | else 16 | b -= a; 17 | } 18 | 19 | cout << "GCD/HCF = " << a; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /findMin_recursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int findMin(int a[], int size) 5 | { 6 | if (size==0) 7 | return a[0]; 8 | else 9 | return min(a[size-1], findMin(a, size - 1)); 10 | } 11 | 12 | int main() 13 | { 14 | int numbers[] = {5, 0, 2, 4, 3, 1, 9}; 15 | int result = findMin(numbers, 7); 16 | cout << result; 17 | } 18 | -------------------------------------------------------------------------------- /reverse_words_in_string.py: -------------------------------------------------------------------------------- 1 | def reverse_words(string): 2 | words_list = string.split() 3 | words_list.reverse() 4 | return ' '.join(words_list) 5 | 6 | 7 | if __name__ == '__main__': 8 | print(reverse_words("hit me baby one more time")) 9 | print(reverse_words("the world is not enough")) 10 | print(reverse_words("water water everywhere but not a drop to drink")) 11 | 12 | -------------------------------------------------------------------------------- /swap2no: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int x=10, y=20; 5 | printf("Before swap x=%d y=%d",x,y); 6 | x=x+y; //x=30 (10+20) 7 | y=x-y; //y=10 (30-20) 8 | x=x-y; //x=20 (30-10) 9 | printf("\nAfter swap x=%d y=%d",x,y); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /sumOfDigit_recursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int sumOfDigits(int x) 5 | { 6 | if (x == 0 || x < 10) 7 | return x; 8 | else 9 | return x%10 + sumOfDigits(x/10); 10 | } 11 | 12 | int main() 13 | { 14 | cout << "enter number: " << endl; 15 | int number; 16 | cin >> number; 17 | int result = sumOfDigits(number); 18 | cout << result; 19 | } 20 | -------------------------------------------------------------------------------- /count characters.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | int i; 6 | char str[100]; 7 | char H[26]={0}; 8 | printf("Enter the string\n"); 9 | gets(str); 10 | for(i=0;str[i]!='\0';i++) 11 | { 12 | H[str[i]-97]+=1; 13 | } 14 | for(i=0;i<26;i++) 15 | { 16 | if(H[i]>1) 17 | { 18 | printf("%c is printed %d times\n",i+97,H[i]); 19 | } 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /GCD using python.py: -------------------------------------------------------------------------------- 1 | #GCD calculation of any two inputted numbers 2 | def Greatest_Common_Divisor(a,b): 3 | print("For numbers",a,"and",b) 4 | while b!=0: 5 | #swapping using another variable 6 | temp=b 7 | b=a%b 8 | a=temp 9 | gcd=a 10 | print("GCD is:",gcd) 11 | print("Enter the numbers: \n") 12 | num1=int(input()) 13 | num2=int(input()) 14 | Greatest_Common_Divisor(num1,num2) -------------------------------------------------------------------------------- /finding sum of squares of n natural numbers.cpp: -------------------------------------------------------------------------------- 1 | //To calculate sum of squares till n number 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int n,i,sum=0; 7 | cout<<"Enter value of number till which sum is to be calculated."; 8 | cin>>n; 9 | for(i=1;i<=n;i++) 10 | sum+=i*i; 11 | cout<<"The sum of squares till the number entered is: "< 2 | #include 3 | main() 4 | { 5 | int i,j,num,temp,sum=0; 6 | printf("Enter number\n"); 7 | scanf("%d",&num); 8 | while(sum!=1 && sum!=4) 9 | { 10 | sum=0; 11 | while(num>0) 12 | { 13 | j=num%10; 14 | sum+=(j*j); 15 | num=num/10; 16 | } 17 | num=sum; 18 | } 19 | 20 | if(sum==1) 21 | printf("Happy Number\n"); 22 | else 23 | printf("UnHappy Number\n"); 24 | } 25 | -------------------------------------------------------------------------------- /Fibonacci.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int fib(int n) 5 | { 6 | int a = 0, b = 1, c, i; 7 | if( n == 0) 8 | return a; 9 | for(i = 2; i <= n; i++) 10 | { 11 | c = a + b; 12 | a = b; 13 | b = c; 14 | } 15 | return b; 16 | } 17 | 18 | // Driver code 19 | int main() 20 | { 21 | int n = 9; 22 | 23 | cout << fib(n); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /sum_squares_of_first_n_natural_numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int n=0; 6 | cout<<"Input the value for 'n': "; //Taking input for the value of 'n' 7 | cin>>n; 8 | int sum=0; 9 | for(int i=1;i 2 | using namespace std; 3 | 4 | template 5 | void printIt(T a, T b) 6 | { 7 | T c = a + b; 8 | cout<< "You gave me " << a << " and " << b << ".\n"; 9 | cout<< "Together they make " << c << "." < 2 | #include 3 | using namespace std; 4 | 5 | int getcoins(vector& a, int n){ 6 | 7 | int c=0; 8 | 9 | 10 | for(int i=0;n>0;i++){ 11 | 12 | 13 | c+=n/a[i]; 14 | 15 | n= n%a[i]; 16 | } 17 | 18 | return c; 19 | 20 | 21 | } 22 | 23 | int main(){ 24 | int t; 25 | cin>>t; 26 | 27 | while(t-->0){ int n; 28 | cin>>n; 29 | vectora= {10,5,1}; 30 | 31 | cout<mx: 8 | secondmax=mx 9 | mx=list1[i] 10 | elif list1[i]>secondmax and mx != list1[i]: 11 | secondmax=list1[i] 12 | else: 13 | if secondmax == mx: 14 | secondmax = list1[i] 15 | 16 | print("Second highest number is : ",str(secondmax)) 17 | -------------------------------------------------------------------------------- /string to number .cpp: -------------------------------------------------------------------------------- 1 | //string inputted should only contain numerical values, also, a numerical value is only reflected in the output if it lies in the range of integer type values in C++. 2 | #include 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | string pp; 8 | cout<<"enter string"<>pp; 10 | int num1 = stoi(pp); 11 | cout << "The numerical value in the inputted string is " << num1 << '\n'; 12 | return 0; 13 | } -------------------------------------------------------------------------------- /sum of squares of first n natural numbers.py: -------------------------------------------------------------------------------- 1 | # Python3 Program to 2 | # find sum of square 3 | # of first n natural 4 | # numbers 5 | 6 | 7 | # Return the sum of 8 | # square of first n 9 | # natural numbers 10 | def squaresum(n) : 11 | 12 | sm = 0 13 | for i in range(1, n+1) : 14 | sm = sm + (i * i) 15 | return sm 16 | 17 | # Driven Program 18 | n = input("enter the number upto which you want the sum of squares") 19 | print(squaresum(n)) 20 | -------------------------------------------------------------------------------- /LCM_Two_Number.py: -------------------------------------------------------------------------------- 1 | def LCM_hack(x,y): 2 | if(x>y): 3 | min_lcm = x 4 | else: 5 | min_lcm = y 6 | while(1): 7 | if((min_lcm % x == 0) and (min_lcm % y == 0)): 8 | lcm = min_lcm 9 | break 10 | min_lcm = min_lcm + 1 11 | return lcm 12 | 13 | input_1 = int(input("Enter the first number : ")) 14 | input_2 = int(input("Enter the second number : ")) 15 | print("LCM of ", input_1,"and ", input_2, "is : ", LCM_hack(input_1,input_2)) 16 | -------------------------------------------------------------------------------- /factorial.cpp: -------------------------------------------------------------------------------- 1 | // C++ program to find factorial of given number 2 | #include 3 | using namespace std; 4 | 5 | // function to find factorial of given number 6 | unsigned int factorial(unsigned int n) 7 | { 8 | if (n == 0) 9 | return 1; 10 | return n * factorial(n - 1); 11 | } 12 | 13 | // Driver code 14 | int main() 15 | { 16 | int num = 5; 17 | cout << "Factorial of " 18 | << num << " is " << factorial(num) << endl; 19 | return 0; 20 | } 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /findQuotReminder: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int divisor, dividend, quotient, remainder; 7 | 8 | cout << "Enter dividend: "; 9 | cin >> dividend; 10 | 11 | cout << "Enter divisor: "; 12 | cin >> divisor; 13 | 14 | quotient = dividend / divisor; 15 | remainder = dividend % divisor; 16 | 17 | cout << "Quotient = " << quotient << endl; 18 | cout << "Remainder = " << remainder; 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /randomnumber: -------------------------------------------------------------------------------- 1 | # Calling a specfic function from namespace increses the code effecincy and makes code faster 2 | from random import randint 3 | 4 | # so to generate random integer or number between 0,100 5 | random_number = radint(0,101) # Remember pyhton excludes the last argument number so to include 100 we will write 101 6 | 7 | #printng random number 8 | print(random_number) 9 | 10 | 11 | # running the code everytime will give a random_number printed 12 | 13 | 14 | 15 | #HAPPY CODING 16 | -------------------------------------------------------------------------------- /perfectno.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | class perfect 3 | { 4 | public static void main(String args[])throws IOException 5 | { 6 | InputStreamReader in=new InputStreamReader(System.in); 7 | BufferedReader bf=new BufferedReader(in); 8 | 9 | int n; 10 | n=Integer.parseInt(bf.readLine()); 11 | 12 | int sum=0; 13 | for(int i=1;i 2 | using namespace std; 3 | 4 | int Fib(int n) 5 | { 6 | if (n == 0 || n == 1) 7 | { 8 | return n; 9 | } 10 | else { 11 | return Fib(n-1) + Fib(n-2); 12 | } 13 | } 14 | 15 | int main(){ 16 | int n,j=0; 17 | cout << "Enter the total number of terms:"; 18 | cin >> n; 19 | 20 | for(int i=1; i<=n; i++) 21 | { 22 | int r = Fib(j); 23 | cout << r << " "; 24 | j++; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /MissingNumInArray.java: -------------------------------------------------------------------------------- 1 | //in a given integer array, this program finds the missing numberin array 2 | public class MissingNumInArray { 3 | public static void main(String[] args) { 4 | int arr[]={1,5,4,3,2,6,7,10,8}; 5 | System.out.println(MissNum(arr)); 6 | } 7 | public static int MissNum(int arr[]){ 8 | int n=arr.length; 9 | int total=((n+1)*(n+2))/2; 10 | int currSum=0; 11 | for(int i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | int num1,num2; 6 | cout<<"Enter first number: "; 7 | cin>>num1; 8 | cout<<"Enter second number: "; 9 | cin>>num2; 10 | cout<<"The numbers before swap are: first= "<> number; 20 | double result = numberSries(number); 21 | cout << result; 22 | } 23 | -------------------------------------------------------------------------------- /isAnagram.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | bool isAnagram(string s, string t) { 5 | sort(s.begin(),s.end()); 6 | sort(t.begin(),t.end()); 7 | if(s.compare(t)==0) 8 | return true; 9 | else 10 | return false; 11 | } 12 | int main() 13 | { 14 | string a="cart", b="trac"; 15 | bool val= isAnagram(a,b); 16 | if(val) 17 | cout << "strings are anagram" << "\n"; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /invert-binary-tree.py: -------------------------------------------------------------------------------- 1 | def invertTree(self, root): 2 | """ 3 | :type root: TreeNode 4 | :rtype: TreeNode 5 | """ 6 | if root is None: 7 | return 8 | root.left, root.right = self.invertTree(root.right), self.invertTree(root.left) 9 | return root 10 | # Example: 11 | 12 | # Input: 13 | 14 | # 5 15 | # / \ 16 | # 3 7 17 | # / \ / \ 18 | # 2 4 6 9 19 | # Output: 20 | 21 | # 5 22 | # / \ 23 | # 7 3 24 | # / \ / \ 25 | # 9 6 4 2 -------------------------------------------------------------------------------- /kadane.py: -------------------------------------------------------------------------------- 1 | def maxSubArraySum(a,size): 2 | 3 | max_so_far = 0 4 | max_ending_here = 0 5 | 6 | for i in range(0, size): 7 | max_ending_here = max_ending_here + a[i] 8 | if max_ending_here < 0: 9 | max_ending_here = 0 10 | 11 | # Do not compare for all elements. Compare only 12 | # when max_ending_here > 0 13 | elif (max_so_far < max_ending_here): 14 | max_so_far = max_ending_here 15 | 16 | return max_so_far -------------------------------------------------------------------------------- /reverseNum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | /* Iterative function to reverse digits of num*/ 5 | int reversDigits(int num) 6 | { 7 | int rev_num = 0; 8 | while(num > 0) 9 | { 10 | rev_num = rev_num*10 + num%10; 11 | num = num/10; 12 | } 13 | return rev_num; 14 | } 15 | 16 | /*Driver program to test reversDigits*/ 17 | int main() 18 | { 19 | int num = 4562; 20 | cout << "Reverse of no. is " 21 | << reversDigits(num); 22 | getchar(); 23 | return 0; 24 | } 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /Factorial.java: -------------------------------------------------------------------------------- 1 | package introduction; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Factorial { 6 | 7 | public static void main(String[] args) { 8 | 9 | int number; 10 | Scanner in = new Scanner(System.in); 11 | System.out.println("Enter Number: "); 12 | number = in.nextInt(); 13 | System.out.println(fact(number)); 14 | in.close(); 15 | 16 | } 17 | 18 | public static int fact(int number) { 19 | if(number == 1) 20 | return 1; 21 | return number*fact(--number); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /LCM_Two_Number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a, b, max; 7 | 8 | cout << "Enter two numbers: "; 9 | cin >> a >> b; 10 | 11 | // maximum value between a and b is stored in max 12 | max = (a > b) ? a : b; 13 | 14 | do 15 | { 16 | if (max % a == 0 && max % b == 0) 17 | { 18 | cout << "LCM = " << max; 19 | break; 20 | } 21 | else 22 | ++max; 23 | } while (true); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Java/Count_occurence_of_given_character_in_string.java: -------------------------------------------------------------------------------- 1 | package tempPack; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Count_occurence_of_given_character_in_string { 6 | 7 | public static void main(String[] args) { 8 | Scanner s = new Scanner(System.in); 9 | String str = s.nextLine(); 10 | int n = str.length(); 11 | char ch = s.next().charAt(0); 12 | int count = 0; 13 | for(int i = 0; i < n; i++) { 14 | if(ch == str.charAt(i)) { 15 | count++; 16 | } 17 | } 18 | System.out.println(count); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /bloom_filter.h: -------------------------------------------------------------------------------- 1 | #ifndef BLOOM_FILTER_H 2 | #define BLOOM_FILTER_H 3 | 4 | #include 5 | 6 | typedef struct { 7 | int hash_count; 8 | int arr_len; 9 | bool *bit_array; 10 | } bloom_filter_t; 11 | 12 | bloom_filter_t *bloom_filter_create( 13 | int elem_count, 14 | float false_positive_probability); 15 | 16 | void bloom_filter_add(bloom_filter_t *filter, char *s); 17 | 18 | bool bloom_filter_might_contain(bloom_filter_t *filter, char *s); 19 | 20 | void bloom_filter_destroy(bloom_filter_t *filter); 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /object_tracking.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | 3 | vid = cv2.VideoCapture('resources/traffic.mp4') 4 | 5 | tracker = cv2.TrackerMOSSE_create() 6 | 7 | while True: 8 | success, frame = vid.read() 9 | timer = cv2.getTickCount() 10 | 11 | fps = cv2.getTickFrequency()/(cv2.getTickCount()-timer) 12 | cv2.putText(frame, str(int(fps)), (70,50), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0,0,255), thickness=1, lineType=None, bottomLeftOrigin=None) 13 | cv2.imshow("tracking", frame) 14 | 15 | if cv2.waitKey(1) & 0xff == ord('q'): 16 | break 17 | 18 | -------------------------------------------------------------------------------- /bogoSort.py: -------------------------------------------------------------------------------- 1 | # Sorts array a[0..n-1] using Bogo sort 2 | # Favorable for good RNG/luck :) 3 | 4 | def bogoSort(a): 5 | n = len(a) 6 | while (is_sorted(a)== False): 7 | shuffle(a) 8 | 9 | # To check if array is sorted or not 10 | def is_sorted(a): 11 | n = len(a) 12 | for i in range(0, n-1): 13 | if (a[i] > a[i+1] ): 14 | return False 15 | return True 16 | 17 | def shuffle(a): 18 | n = len(a) 19 | for i in range (0,n): 20 | r = random.randint(0,n-1) 21 | a[i], a[r] = a[r], a[i] -------------------------------------------------------------------------------- /lcm.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class lcm 3 | { 4 | public static void main(String[] args) 5 | { 6 | Scanner sc=new Scanner(System.in); 7 | System.out.println("Input the first number: "); 8 | int a = sc.nextInt(); 9 | System.out.println("Input the second number: "); 10 | int b = sc.nextInt(); 11 | int max = Math.max(a,b); 12 | while(true) 13 | { 14 | if (max % a == 0 && max % b == 0) 15 | { 16 | System.out.println("LCM is:"+max); 17 | break; 18 | } 19 | else 20 | { 21 | ++max; 22 | } 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /pascal_triangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int Factorial(int n){ 5 | int f=1; 6 | for (int i=1;i<=n;i++) f*=i; 7 | return f; 8 | } 9 | 10 | int NCR(int n, int r){ 11 | return Factorial(n)/(Factorial(r)*Factorial(n-r)); 12 | } 13 | 14 | int main(){ 15 | int n; 16 | cout<<"Enter a number:"; 17 | cin>>n; 18 | cout< 0: 5 | for i in range(interval, n): 6 | temp = array[i] 7 | j = i 8 | while j >= interval and array[j - interval] > temp: 9 | array[j] = array[j - interval] 10 | j -= interval 11 | 12 | array[j] = temp 13 | interval //= 2 #the interval is reduced 14 | 15 | 16 | data = [9,100, 5, 6, 4, 1] 17 | size = len(data) 18 | shellSort(data, size) 19 | print('Sorted Array -->') 20 | print(data) -------------------------------------------------------------------------------- /CountCharacter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | string s; 9 | char c; 10 | int counter = 0; 11 | 12 | cout << "Enter the string you want to check: "; 13 | cin >> s; 14 | 15 | cout << "Enter the character that you want to count: "; 16 | cin >> c; 17 | 18 | for (int i = 0; i < s.length(); i++) 19 | { 20 | if (s[i] == c) 21 | counter++; 22 | } 23 | 24 | cout << "The character \'" << c << "\' occurs " << counter << " times in " << s << endl; 25 | } -------------------------------------------------------------------------------- /Java/redux/shape/Square.java: -------------------------------------------------------------------------------- 1 | package Java.redux.shape; 2 | 3 | /** 4 | * @author Alessandro Arosio - 06/10/2020 17:09 5 | */ 6 | public class Square implements Shape { 7 | private final Double base; 8 | private final String name; 9 | 10 | public Square(Double base, String name) { 11 | this.base = base; 12 | this.name = name; 13 | } 14 | 15 | @Override 16 | public Double getArea() { 17 | return base * base; 18 | } 19 | 20 | @Override 21 | public String getName() { 22 | return this.name; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Pascal Triangle.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def generate(self, A: int): 3 | 4 | pt=[] 5 | if(A==0): 6 | return pt 7 | d=1 8 | k=[] 9 | k.append(1) 10 | pt.append(k) 11 | for i in range(1,A): 12 | k=[] 13 | k.append(1) 14 | 15 | for j in range(1,d): 16 | c=pt[i-1][j]+pt[i-1][j-1] 17 | k.append(c) 18 | d+=1 19 | k.append(1) 20 | pt.append(k) 21 | return pt 22 | -------------------------------------------------------------------------------- /Python-graphs: -------------------------------------------------------------------------------- 1 | # For graphs we have in python we have a module name matplotlib 2 | # This code is for beginner level only 3 | 4 | 5 | from matplotlib import pyplot as plt 6 | 7 | list_points = [4,2,9,6,7] 8 | 9 | # to polt above point on graphs 10 | 11 | plt.plot(list_points) # plot() function is being called from pyplot module 12 | 13 | # to label x and y axis of graph 14 | 15 | plt.xlabel('Something XYZ for x axis') 16 | plt.ylabel('Somethings ABC for y axis') 17 | 18 | # to show the graph 19 | 20 | plt.show() # show() function show the graphs in as GUI (graphical user interface) 21 | -------------------------------------------------------------------------------- /minimalsquare.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int t,a,b,i,k,l; 6 | cin>>t; 7 | for(i=0;i>a>>b; 10 | if(a<=b){ 11 | k=2*a; 12 | l=b; 13 | } 14 | else{ 15 | k=2*b; 16 | l=a; 17 | } 18 | if(k0){ 12 | currsum=sum; 13 | if(maxsumsofar 2 | #include 3 | int main(){ 4 | int n,x=0; 5 | scanf("%d",&n); 6 | char s[n]; 7 | for(int i=0;i 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int a[5][5]; 9 | int x,y; 10 | for(int i = 0; i < 5; i++){ 11 | for(int j = 0; j < 5; j++){ 12 | cin>>a[i][j]; 13 | if(a[i][j] == 1){ 14 | x = i; 15 | y = j; 16 | } 17 | } 18 | } 19 | if ((x-2) < 0){ 20 | x = (-1*(x-2)) + 2; 21 | } 22 | if ((y-2) < 0){ 23 | y = (-1*(y-2)) + 2; 24 | } 25 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | long long number; 6 | cout << "Enter a number:-" << endl; 7 | cin>>number; 8 | 9 | if(number <= 0) { 10 | cout << "The number [" << number << "] is not a power of 2." << endl; 11 | } else { 12 | if(ceil(log2(number)) == floor(log2(number))) { 13 | cout << "The number [" << number << "] is a power of 2." << endl; 14 | } else { 15 | cout << "The number [" << number << "] is not a power of 2." << endl; 16 | } 17 | } 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /lexicographicalOrder: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void lexicographicalOrder(int num, int k) 5 | { 6 | if(k>num) 7 | return; 8 | 9 | for(int i=0; i<=9; i++) 10 | { 11 | if(k<=num) 12 | { 13 | cout<>n; 28 | lexicographicalOrder(n, 1); 29 | } 30 | -------------------------------------------------------------------------------- /secondLargest.java: -------------------------------------------------------------------------------- 1 | public class SecondLargestInArrayExample{ 2 | public static int getSecondLargest(int[] a, int total){ 3 | int temp; 4 | for (int i = 0; i < total; i++) 5 | { 6 | for (int j = i + 1; j < total; j++) 7 | { 8 | if (a[i] > a[j]) 9 | { 10 | temp = a[i]; 11 | a[i] = a[j]; 12 | a[j] = temp; 13 | } 14 | } 15 | } 16 | return a[total-2]; 17 | } 18 | } -------------------------------------------------------------------------------- /warp_perspective.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | img = cv2.imread("resources/google.jpg") 5 | 6 | width, height = 250, 250 7 | pts = np.float32([[279,89],[307,89],[304,121],[284,122]]) 8 | pts2 = np.float32([[0,0],[width,0],[0,height],[width, height]]) 9 | matirx = cv2.getPerspectiveTransform(pts, pts2) 10 | img_op = cv2.warpPerspective(img, matirx, (width, height)) 11 | 12 | 13 | #for loop for denoting the dots 14 | for x in range(4): 15 | cv2.circle(img, (pts[x][0], pts[x][1]),5,(0,0,255), cv2.FILLED) 16 | 17 | cv2.imshow("Image", img) 18 | cv2.imshow("Image_op", img_op) 19 | cv2.waitKey(0) 20 | -------------------------------------------------------------------------------- /program.py: -------------------------------------------------------------------------------- 1 | #Sorting an Array of 0s, 1s and 2s 2 | # Function to sort array 3 | def sort012( a, arr_size): 4 | lo = 0 5 | hi = arr_size - 1 6 | mid = 0 7 | while mid <= hi: 8 | if a[mid] == 0: 9 | a[lo], a[mid] = a[mid], a[lo] 10 | lo = lo + 1 11 | mid = mid + 1 12 | elif a[mid] == 1: 13 | mid = mid + 1 14 | else: 15 | a[mid], a[hi] = a[hi], a[mid] 16 | hi = hi - 1 17 | return a 18 | 19 | # Function to print array 20 | def printArray( a): 21 | for k in a: 22 | print k, 23 | 24 | -------------------------------------------------------------------------------- /Reverse2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void Reverse2(int *A,int n) 6 | { 7 | int i,j,temp; 8 | for(i=0,j=n-1 ; i>n; 21 | int A[n]; 22 | for(i=0;i>A[i]; 26 | } 27 | Reverse2(A,n); 28 | for(i=0;i 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n,count=0; 8 | cout<<"Enter a number: "; 9 | cin>>n; 10 | cout<<"Divisors are: "; 11 | for(int i=1;i 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | string a1,a2; 8 | cin>>a1>>a2; 9 | vectort(26,0); 10 | if(a1.size()!=a2.size()) 11 | { 12 | cout<<"Not anagrams"; 13 | } 14 | for(int i=0;i 2 | #include 3 | 4 | 5 | int main(){ 6 | 7 | int i,j,k,l,n; 8 | 9 | system("cls"); 10 | 11 | printf("enter the range="); 12 | scanf("%d",&n); 13 | 14 | for(i=1;i<=n;i++) 15 | { 16 | for(j=1;j<=n-i;j++) 17 | { 18 | printf(" "); 19 | } 20 | for(k=1;k<=i;k++) 21 | { 22 | printf("%d",k); 23 | } 24 | for(l=i-1;l>=1;l--) 25 | { 26 | printf("%d",l); 27 | } 28 | printf("\n"); 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /JoinTwoLists.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | public class JoinTwoLists { 5 | 6 | public static void main(String[] args) { 7 | 8 | List list1 = new ArrayList(); 9 | list1.add("a"); 10 | 11 | List list2 = new ArrayList(); 12 | list2.add("b"); 13 | 14 | List joined = new ArrayList(); 15 | 16 | joined.addAll(list1); 17 | joined.addAll(list2); 18 | 19 | System.out.println("list1: " + list1); 20 | System.out.println("list2: " + list2); 21 | System.out.println("joined: " + joined); 22 | 23 | } 24 | } -------------------------------------------------------------------------------- /Java/redux/shape/Rectangle.java: -------------------------------------------------------------------------------- 1 | package Java.redux.shape; 2 | 3 | /** 4 | * @author Alessandro Arosio - 06/10/2020 17:07 5 | */ 6 | public class Rectangle implements Shape { 7 | private final Double base; 8 | private final Double height; 9 | private final String name; 10 | 11 | public Rectangle(Double base, Double height, String name) { 12 | this.base = base; 13 | this.height = height; 14 | this.name = name; 15 | } 16 | 17 | @Override 18 | public Double getArea() { 19 | return base * height; 20 | } 21 | 22 | @Override 23 | public String getName() { 24 | return this.name; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Second Largest Number In Array: -------------------------------------------------------------------------------- 1 | public class practice { 2 | 3 | public static void main(String[] args) { 4 | 5 | int a[]={1,2,5,6,3,2}; 6 | 7 | int total = a.length; 8 | 9 | int temp; 10 | for (int i = 0; i < total; i++) 11 | { 12 | for (int j = i + 1; j < total; j++) 13 | { 14 | if (a[i] > a[j]) 15 | { 16 | temp = a[i]; 17 | a[i] = a[j]; 18 | a[j] = temp; 19 | } 20 | } 21 | } 22 | System.out.println( a[total-2]); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Transposition_Cipher.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | int main(){ 6 | string s,final; 7 | int key; 8 | cout<<"Please Enter the Key and cipher text For ex '12 Text to encode'"<>key; 10 | getline(cin, s); 11 | vector list; 12 | int m; 13 | for(int i=0;ibool: 8 | if (head == None): 9 | return False 10 | slow = head 11 | fast = head.next 12 | while (slow != fast): 13 | if (fast == None or fast.next == None): 14 | return False 15 | slow = slow.next 16 | fast = fast.next.next 17 | return True 18 | #Example with only one element in the linkedList 19 | 20 | l = ListNode(1) 21 | detectLoop(l) 22 | 23 | #Output: False 24 | -------------------------------------------------------------------------------- /linear_search.cpp: -------------------------------------------------------------------------------- 1 | // C++ code to linearly search x in arr[]. If x 2 | // is present then return its location, otherwise 3 | // return -1 4 | 5 | #include 6 | using namespace std; 7 | 8 | int search(int arr[], int n, int x) 9 | { 10 | int i; 11 | for (i = 0; i < n; i++) 12 | if (arr[i] == x) 13 | return i; 14 | return -1; 15 | } 16 | 17 | int main(void) 18 | { 19 | int arr[] = { 2, 3, 4, 10, 40 }; 20 | int x = 10; 21 | int n = sizeof(arr) / sizeof(arr[0]); 22 | int result = search(arr, n, x); 23 | (result == -1)? cout<<"Element is not present in array" 24 | : cout<<"Element is present at index " < 2 | #define ll long long int 3 | #define pb push_back 4 | #define fo(i,n) for(int i=0;i=0;i--) 6 | using namespace std; 7 | 8 | int main(){ 9 | int t; 10 | cin>>t; 11 | while(t--){ 12 | int n; 13 | cin>>n; 14 | int m=n-1; 15 | cout<<2<0: 13 | playerA=choice(players) 14 | teamA.append(playerA) 15 | players.remove(playerA) 16 | 17 | if players==[]: 18 | break 19 | 20 | playerB=choice(players) 21 | teamB.append(playerB) 22 | players.remove(playerB) 23 | 24 | team1=choice(teams) 25 | teams.remove(team1) 26 | team2=choice(teams) 27 | teams.remove(team2) 28 | 29 | 30 | print("Team A:",team1,teamA) 31 | print("Team B:",team2,teamB) 32 | -------------------------------------------------------------------------------- /vkgupta857_binary_search.py: -------------------------------------------------------------------------------- 1 | ### Binary Search 2 | # Code by Vinod(https://github.com/vkgupta857) 3 | 4 | n = int(input("Enter the number of elements in list: ")) 5 | a = [] 6 | print("Enter elements of the list: ") 7 | temp = 0 8 | for i in range(n): 9 | temp = int(input()) 10 | a.append(temp) 11 | 12 | ele = int(input("Enter element to be searched: ")) 13 | flag = 0 14 | low = 0 15 | high = n-1 16 | mid = n//2 17 | while(low ele: 23 | high = mid - 1 24 | else: 25 | low = mid + 1 26 | if flag == 0: 27 | print("Element not found in the list.") 28 | -------------------------------------------------------------------------------- /python programs/lcm_of_array.py: -------------------------------------------------------------------------------- 1 | #python program to find lcm of more than 3 numbers 2 | 3 | def find_lcm(num1, num2): 4 | if(num1>num2): 5 | num = num1 6 | den = num2 7 | else: 8 | num = num2 9 | den = num1 10 | rem = num % den 11 | while(rem != 0): 12 | num = den 13 | den = rem 14 | rem = num % den 15 | gcd = den 16 | lcm = int(int(num1 * num2)/int(gcd)) 17 | return lcm 18 | 19 | 20 | l = list(map(int,input().split())) 21 | 22 | 23 | num1 = l[0] 24 | num2 = l[1] 25 | lcm = find_lcm(num1, num2) 26 | 27 | for i in range(2, len(l)): 28 | lcm = find_lcm(lcm, l[i]) 29 | 30 | print(lcm) -------------------------------------------------------------------------------- /dfsgrid.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | int n,m; 6 | vector>vis(1000,vector(1000)); 7 | 8 | bool isvalid(int x,int y){ 9 | 10 | 11 | 12 | if((x>n||x<0||y<0||y>m ) and(vis[x][y]== true)){ 13 | return false; 14 | } 15 | 16 | return true; 17 | } 18 | 19 | void dfs(int x,int y){ 20 | 21 | vis[x][y]= true; 22 | cout<>n>>m; 39 | dfs(1,1); 40 | 41 | 42 | 43 | } -------------------------------------------------------------------------------- /replace_all_occurance_of_character_from_string.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | std::string s = ""; 6 | std::cout << "Enter the String:-" << std::endl; 7 | std::cin >> s; 8 | 9 | std::string x = "", y = ""; 10 | 11 | std::cout << "Enter the character to remove" << std::endl; 12 | std::cin >> x; 13 | 14 | if(x.length() > 1) { 15 | std::cout << "You have entered a string instead of a character" << std::endl; 16 | } else { 17 | size_t pos; 18 | while ((pos = s.find(x)) != std::string::npos) { 19 | s.replace(pos, 1, y); 20 | } 21 | std::cout << s << std::endl; 22 | } 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /OccurenceCharInStr.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int check(char *s,char c) 5 | { 6 | static int i=0,count=0; 7 | if(!s[i]) 8 | { 9 | return count; 10 | } 11 | else 12 | { 13 | if(s[i]==c) 14 | { 15 | count++; 16 | } 17 | i++; 18 | check(s,c); 19 | } 20 | 21 | 22 | 23 | 24 | 25 | } 26 | int main() 27 | { 28 | char s[1000],c; 29 | int count=0; 30 | 31 | printf("Enter the string : "); 32 | gets(s); 33 | printf("Enter character to be searched: "); 34 | c=getchar(); 35 | count=check(s,c); 36 | printf("character '%c' occurs %d times \n ",c,count); 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /map.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | map mp; 8 | mp.insert(pair(6,20)); 9 | mp.insert(pair(1,23)); 10 | mp.insert(pair(2,21)); 11 | mp.insert(pair(3,24)); 12 | mp.insert(pair(6,12)); 13 | mp.insert(pair(6,13)); 14 | mp.insert(pair(5,26)); 15 | 16 | map :: iterator itr; 17 | 18 | //mp.erase(22); 19 | //mp.erase(mp.begin(),mp.find(23)); 20 | cout<<"\n the map is "<first<<'\t'<second< 2 | 3 | void counting_sort(int a[],int n,int max) 4 | { 5 | int count[50]={0},i,j; 6 | 7 | for(i=0;imax) 28 | max=a[i]; 29 | } 30 | 31 | counting_sort(a,n,max); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /LinearSearch: -------------------------------------------------------------------------------- 1 | 2 | // C++ code to linearly search x in arr[]. If x 3 | // is present then return its location, otherwise 4 | // return -1 5 | 6 | #include 7 | using namespace std; 8 | 9 | int search(int arr[], int n, int x) 10 | { 11 | int i; 12 | for (i = 0; i < n; i++) 13 | if (arr[i] == x) 14 | return i; 15 | return -1; 16 | } 17 | 18 | int main(void) 19 | { 20 | int arr[] = { 2, 3, 4, 10, 40 }; 21 | int x = 10; 22 | int n = sizeof(arr) / sizeof(arr[0]); 23 | int result = search(arr, n, x); 24 | (result == -1)? cout<<"Element is not present in array" 25 | : cout<<"Element is present at index " < 4 | using namespace std; 5 | long long matrix[1000][1000]; 6 | int n,m,x; 7 | int main(){ 8 | cout << "Enter the dismension of the matrix (separated by space)" << endl << ": "; 9 | cin >> n >> m; 10 | cout << "Enter the component of the matrix (separated by space)" << endl; 11 | for(int i = 1;i<=n;i++){ 12 | for(int j = 1;j<=m;j++){ 13 | cin >> matrix[i][j]; 14 | } 15 | } 16 | cout << "Find : "; 17 | cin >> x; 18 | for(int i = 1;i<=n;i++){ 19 | for(int j= 1;j<=m;j++){ 20 | if(matrix[i][j] == x){ 21 | cout << x << " is found at " << i << " " << j << endl; 22 | } 23 | } 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /Java/redux/CalculateAreaRedux.java: -------------------------------------------------------------------------------- 1 | package Java.redux; 2 | 3 | import Java.redux.shape.Circle; 4 | import Java.redux.shape.Rectangle; 5 | import Java.redux.shape.Shape; 6 | import Java.redux.shape.Square; 7 | 8 | import java.util.List; 9 | 10 | /** 11 | * @author Alessandro Arosio - 06/10/2020 17:02 12 | */ 13 | public class CalculateAreaRedux { 14 | public static void main(String[] args) { 15 | Shape circle = new Circle(5d, "Circle"); 16 | Shape rectangle = new Rectangle(3d, 4d, "Rectangle"); 17 | Shape square = new Square(8d, "Square"); 18 | 19 | List shapes = List.of(circle, square, rectangle); 20 | 21 | shapes.forEach(element -> System.out.println("Area of " + element.getName() + " : " + element.getArea())); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /RECURSIVE MERGESORT: -------------------------------------------------------------------------------- 1 | Recursive MergeSort 2 | void Merge(int A[],int l,int mid,int h) 3 | { 4 | int i=l,j=mid+1,k=l; 5 | int B[100]; 6 | 7 | while(i<=mid && j<=h) 8 | { 9 | if(A[i] "); 10 | String nums[] = br.readLine().split(" "); 11 | //Loop through all numbers and add 12 | int sum = 0; 13 | for(int i = 0; i < nums.length; i++){ 14 | sum+=Integer.parseInt(nums[i]); 15 | } 16 | System.out.println(); 17 | //Print out sum 18 | System.out.print("Sum: " + sum); 19 | 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /Detect loop in LinkedList.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | bool hasCycle(ListNode *head) { 12 | 13 | if(head== NULL || head->next == NULL) return false; 14 | 15 | ListNode *slow= head->next, *fast= slow->next; 16 | 17 | while(slow!= NULL && fast != NULL && slow!= fast){ 18 | slow= slow->next; 19 | fast= fast->next; 20 | if(fast!=NULL) fast=fast->next; 21 | } 22 | 23 | if(slow==NULL || fast ==NULL) return false; 24 | 25 | return true; 26 | 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /visible_pillars.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | /* 3 | 4 | You are standing facing N pillars of different heights with ith pillar having height h[i]. you try to see all the possible pillars. 5 | you want to know that how many buildings will he be able to see in the range [L, R] both inclusive. 6 | 7 | */ 8 | 9 | 10 | using namespace std; 11 | int main() 12 | { 13 | int n; 14 | cin>>n; 15 | int h[n]; 16 | for(int i =0;i>h[i]; 18 | size_t q; 19 | cin>>q; 20 | while(q--) 21 | { 22 | int l,r; 23 | cin>>l>>r; 24 | int count = 0; 25 | int max = h[l]; 26 | for(int i = l;i<=r;++i) 27 | { 28 | if(max 2 | #include 3 | using namespace std; 4 | int main() { 5 | int num, temp, remainder, result=0; 6 | cout<<"Enter a three-digit integer\n"; 7 | cin>>num; 8 | temp=num; //temp is a temporary int type variable to store and manipulate value of the inputted number, without affecting the original input itself. 9 | while(temp!=0) 10 | { 11 | remainder=temp%10; //to obtain the last digit of the number. 12 | result+=pow(remainder,3); 13 | temp/=10; //to obtain second to last digit of the number. 14 | } 15 | if(result==num) 16 | cout<<"You have entered an armstrong number.\n"; 17 | else 18 | cout<<"The number entered is not an armstrong number.\n"; 19 | return 0; 20 | } -------------------------------------------------------------------------------- /sequential_search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | string id [8]={"2207023006","2207023004","2207023003","2207023007","2207023005","2207023001","2207023008","2207023002"}; 7 | string name [8]={"Tommy", "John", "George", "Mike", "Ronaldo", "Messi", "Neymar", "Puyol"}; 8 | string input; 9 | 10 | int index, data; 11 | 12 | cout << "PERSON DATA" << endl; 13 | cout << "====================" << endl; 14 | cout << "Input ID : "; 15 | cin >> input; 16 | 17 | for (int i=0;i<=8;i++) 18 | { 19 | if(input == id[i]) { 20 | index +=1; 21 | data = i; 22 | } 23 | 24 | } 25 | 26 | if (index > 0){ 27 | cout<< endl << "Name : "; 28 | cout< 2 | using namespace std; 3 | 4 | void towerOfHanoi(int n, char from_rod, 5 | char to_rod, char aux_rod) 6 | { 7 | if (n == 1) 8 | { 9 | cout << "Move disk 1 from rod " << from_rod << 10 | " to rod " << to_rod< 3 | using namespace std; 4 | int main() { 5 | int i,a[100],n,sum=0,evensum=0,oddsum=0; 6 | cout << "Enter array size\n"; 7 | cin>>n; 8 | cout<<"\nEnter elements:\n"; 9 | for(i=0;i>a[i]; 12 | } 13 | for(i=0;i 2 | using namespace std; 3 | int BinarySearch(int arr[], int p, int r, int num) { 4 | if (p <= r) { 5 | int mid = (p + r)/2; 6 | if (arr[mid] == num) 7 | return mid ; 8 | if (arr[mid] > num) 9 | return binarySearch(arr, p, mid-1, num); 10 | if (arr[mid] > num) 11 | return binarySearch(arr, mid+1, r, num); 12 | } 13 | return -1; 14 | } 15 | int main(void) { 16 | int arr[] = {11, 32, 70, 13, 18, 20, 21, 33, 52, 40}; 17 | int n = sizeof(arr)/ sizeof(arr[0]); 18 | int num = 20; 19 | int index = BinarySearch (arr, 0, n-1, num); 20 | if(index == -1) 21 | cout<< num <<" is not present in the array"; 22 | else 23 | cout<< num <<" is present at index "<< index <<" in the array"; 24 | return 0; 25 | } -------------------------------------------------------------------------------- /area.c: -------------------------------------------------------------------------------- 1 | //area of rectangle,circle and square 2 | #include 3 | void main() 4 | { 5 | int n; 6 | int l,b,radius,side; 7 | int area; 8 | 9 | printf("1) RECTANGLE\n"); 10 | printf("2) CIRCLE\n"); 11 | printf("3) SQUARE\n"); 12 | printf("Enter a choice\n"); 13 | scanf("%d",&n); 14 | printf("The area is : %d") 15 | switch(n) 16 | { 17 | case 1: printf("Enter length and breadth\n"); 18 | scanf("%d%d",&l,&b); 19 | area=l*b; 20 | printf("%d\n",area); 21 | break; 22 | case 2: printf("Enter radius\n"); 23 | scanf("%d",&radius); 24 | area=3.14*radius*radius; 25 | printf("%d\n",area); 26 | break; 27 | case 3: printf("Enter the side length\n"); 28 | scanf("%d",&side); 29 | area=side*side; 30 | printf("%d\n",area); 31 | break; 32 | default:printf("Invalid choice,please try again\n"); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Decimal to binary.cpp: -------------------------------------------------------------------------------- 1 | // C++ program to convert a decimal 2 | // number to binary number 3 | 4 | #include 5 | using namespace std; 6 | 7 | // function to convert decimal to binary 8 | void decToBinary(int n) 9 | { 10 | // array to store binary number 11 | int binaryNum[32]; 12 | 13 | // counter for binary array 14 | int i = 0; 15 | while (n > 0) { 16 | 17 | // storing remainder in binary array 18 | binaryNum[i] = n % 2; 19 | n = n / 2; 20 | i++; 21 | } 22 | 23 | // printing binary array in reverse order 24 | for (int j = i - 1; j >= 0; j--) 25 | cout << binaryNum[j]; 26 | } 27 | 28 | // Driver program to test above function 29 | int main() 30 | { 31 | int n ; 32 | cin>>n; 33 | decToBinary(n); 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /LCM_two_numbers.java: -------------------------------------------------------------------------------- 1 | // Java program to find LCM of two numbers. 2 | import java.util.Scanner; 3 | import java.io.*; 4 | class Test 5 | { 6 | // Recursive method to return gcd of a and b 7 | static int gcd(int a, int b) 8 | { 9 | if (a == 0) 10 | return b; 11 | return gcd(b % a, a); 12 | } 13 | 14 | // method to return LCM of two numbers 15 | static int lcm(int a, int b) 16 | { 17 | return (a / gcd(a, b)) * b; 18 | } 19 | 20 | // Driver method 21 | public static void main(String[] args) 22 | { 23 | Scanner in = new Scanner(System.in); 24 | System.out.println("Enter two numbers"); 25 | int a = in.nextInt(); 26 | int b = in.nextInt(); 27 | System.out.println("LCM of " + a + " and " + b + " is " + lcm(a, b)); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /top2maxnumbers.c: -------------------------------------------------------------------------------- 1 | // TOP 2 MAXIMUM NUMBERS 2 | 3 | #include 4 | int main() 5 | { 6 | int a[10],largest,smallest,secondLargest,i; 7 | i=0; 8 | while(i<=9) 9 | { 10 | printf("Enter a number:"); 11 | scanf("%d",&a[i]); 12 | i++; 13 | } 14 | 15 | largest=a[0]; 16 | i=1; 17 | while(i<=9) 18 | { 19 | if(a[i]>largest) 20 | { 21 | largest=a[i]; 22 | } 23 | i++; 24 | } 25 | 26 | smallest=a[0]; 27 | i=1; 28 | while(i<=9) 29 | { 30 | if(a[i]secondLargest && a[i] 5 | using namespace std; 6 | int binarySearch(int arr[], int l, int r, int x) 7 | { 8 | if (r >= l) { 9 | int mid = l + (r - l) / 2; 10 | if (arr[mid] == x) 11 | return mid; 12 | if (arr[mid] > x) 13 | return binarySearch(arr, l, mid - 1, x); 14 | return binarySearch(arr, mid + 1, r, x); 15 | } 16 | return -1; 17 | } 18 | 19 | int main(void) 20 | { 21 | int arr[] = { 2, 3, 4, 10, 40 }; 22 | int x = 10; 23 | int n = sizeof(arr) / sizeof(arr[0]); 24 | int result = binarySearch(arr, 0, n - 1, x); 25 | (result == -1) ? cout << "Element is not present in array" 26 | : cout << "Element is present at index " << result; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Linked List: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct Node 4 | { 5 | int data; 6 | struct Node *next; 7 | }*first=NULL,*second=NULL,*third=NULL; 8 | void create(int A[],int n) 9 | { 10 | int i; 11 | struct Node *t,*last; 12 | first=(struct Node *)malloc(sizeof(struct Node)); 13 | first->data=A[0]; 14 | first->next=NULL; 15 | last=first; 16 | for(i=1;idata=A[i]; 20 | t->next=NULL; 21 | last->next=t; 22 | last=t; 23 | } 24 | } 25 | void create2(int A[],int n) 26 | { 27 | int i; 28 | struct Node *t,*last; 29 | second=(struct Node *)malloc(sizeof(struct 30 | Node)); 31 | second->data=A[0]; 32 | second->next=NULL; 33 | last=second; 34 | for(i=1;idata=A[i]; 38 | t->next=NULL; 39 | last->next=t; 40 | last=t; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Scope.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | 8 | class Difference { 9 | private int[] elements; 10 | public int maximumDifference; 11 | 12 | // Add your code here 13 | Difference(int[] elements) 14 | { 15 | this.elements = elements; 16 | } 17 | public void computeDifference(){ 18 | int m = 0; 19 | for(int i =0; im) 25 | { 26 | m = a; 27 | } 28 | } 29 | } 30 | maximumDifference = m; 31 | } 32 | 33 | } // End of Difference class -------------------------------------------------------------------------------- /ceaserCipher.py: -------------------------------------------------------------------------------- 1 | def encrypt(message, key): 2 | encryptMessage = "" 3 | 4 | for char in message: 5 | 6 | # Checking Upper Case. 7 | if (char.isupper()): 8 | encryptCharacter = chr(((((ord(char)+key)-65) % 26) + 65)) 9 | encryptMessage += encryptCharacter 10 | 11 | # Checking Lower Case. 12 | elif (char.islower()): 13 | encryptCharacter = chr((((ord(char)+key-97) % 26) + 97)) 14 | encryptMessage += encryptCharacter 15 | 16 | # Checking special symbols. 17 | else: 18 | encryptCharacter = chr((((ord(char)+key-32) % 33) + 32)) 19 | encryptMessage += encryptCharacter 20 | 21 | return encryptMessage 22 | 23 | 24 | # Taking input from user. 25 | message = input("Enter the string to encrypt: ") 26 | 27 | print("Ceaser Cipher text: ", encrypt(message, 3)) 28 | -------------------------------------------------------------------------------- /ConnectedComponent.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector adj[100001]; 5 | bool visited[100001]; 6 | 7 | void dfs(int node) 8 | { 9 | visited[node] = 1; 10 | for(int child : adj[node]) 11 | { 12 | if(!visited[child] ) 13 | dfs(child); 14 | } 15 | } 16 | 17 | int main() 18 | { 19 | int nodes,edges,x,y,connectedComponents=0; 20 | cin >> nodes; 21 | cin >> edges; 22 | for(int i=0;i> x >> y; 25 | adj[x].push_back(y); 26 | adj[y].push_back(x); 27 | } 28 | for(int i=1;i<=nodes;i++) 29 | { 30 | if(!visited[i]) 31 | { 32 | dfs(i); 33 | connectedComponents++; 34 | } 35 | } 36 | cout << "Number of connected components: "<< connectedComponents << endl; 37 | } -------------------------------------------------------------------------------- /calc.css: -------------------------------------------------------------------------------- 1 | *{ 2 | margin: 0px; 3 | padding: 0px; 4 | } 5 | #container{ 6 | 7 | margin: 34px 0px 0px 279px; 8 | width: 46%; 9 | display: flex; 10 | justify-content: center; 11 | text-align: center; 12 | border: 2px solid black; 13 | background-color: lightslategray; 14 | height: 325px; 15 | } 16 | #input{ 17 | width: 300px; 18 | border: 4px solid lightblue; 19 | } 20 | .btn-dark:hover { 21 | color: #191818; 22 | background-color: #f2f6f9; 23 | border-color: #f1f4f7; 24 | } 25 | .red{ 26 | background-color: red; 27 | position: absolute; 28 | top: 110px; 29 | left: 205px; 30 | height: 83px; 31 | 32 | } 33 | table{ 34 | position: relative; 35 | margin: 24px 0px 0px 0; 36 | } 37 | #last{ 38 | position: absolute; 39 | top: 160px; 40 | left: 3px; 41 | 42 | } -------------------------------------------------------------------------------- /gnomeSort.java: -------------------------------------------------------------------------------- 1 | //Implementation of gnome sort in java 2 | import java.util.Arrays; 3 | class gnomeSort { 4 | void gnomeSort(int[] nums) 5 | { 6 | int i=1; 7 | int j=2; 8 | 9 | while(i < nums.length) { 10 | if ( nums[i-1] <= nums[i] ) 11 | { 12 | i = j; j++; 13 | } else { 14 | int tmp = nums[i-1]; 15 | nums[i-1] = nums[i]; 16 | nums[i--] = tmp; 17 | i = (i==0) ? j++ : i; 18 | } 19 | } 20 | } 21 | 22 | // Method to test above 23 | public static void main(String args[]) 24 | { 25 | gnomeSort ob = new gnomeSort(); 26 | int nums[] = {7, -5, 3, 2, 1, 0, 45}; 27 | System.out.println("Original Array:"); 28 | System.out.println(Arrays.toString(nums)); 29 | ob.gnomeSort(nums); 30 | System.out.println("Sorted Array"); 31 | System.out.println(Arrays.toString(nums)); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /reduced_strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | /* 3 | reduce a string in the following way: 4 | xx reduces to nothing left and xxx changes to x 5 | 6 | ps :- just take out characters in pair of 2 7 | 8 | for eg: aabbbccccd turns to bd 9 | 10 | if the entire string is reduced print Empty string 11 | 12 | 13 | */ 14 | 15 | using namespace std; 16 | int main() 17 | { 18 | stack a; 19 | string s; 20 | cin>>s; 21 | a.push(s[0]); 22 | for(int i =1;ival != root2->val) return false; 18 | 19 | bool leftTree = identicalTree(root1->left, root2->left), 20 | rightTree = identicalTree(root1->right, root2->right); 21 | 22 | if(leftTree && rightTree) return true; 23 | return false; 24 | } 25 | -------------------------------------------------------------------------------- /calc.js: -------------------------------------------------------------------------------- 1 | 2 | screen = document.getElementById('input').value; 3 | let status=0; 4 | 5 | 6 | function getinput(i){ 7 | if(i==='='){ 8 | document.getElementById('input').value=eval(document.getElementById('input').value) 9 | } 10 | else if(i=='+' || i=='-' || i=='*' || i=='/'){ 11 | if(status==0){ 12 | document.getElementById('input').value += i ; 13 | status=1; 14 | } 15 | 16 | 17 | } 18 | else if(i=='b'){ 19 | var value = document.getElementById("input").value; 20 | document.getElementById("input").value = value.substr(0, value.length - 1); 21 | 22 | 23 | } 24 | else if(i=='c'){ 25 | document.getElementById('input').value=''; 26 | 27 | } 28 | 29 | else{ 30 | document.getElementById('input').value += i ; 31 | status=0; 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /Calculator.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | char op; 7 | float num1, num2; 8 | 9 | cout << "Enter operator either + or - or * or /: "; 10 | cin >> op; 11 | 12 | cout << "Enter two operands: "; 13 | cin >> num1 >> num2; 14 | 15 | switch(op) 16 | { 17 | case '+': 18 | cout << num1+num2 << endl; 19 | break; 20 | 21 | case '-': 22 | cout << num1-num2 << endl; 23 | break; 24 | 25 | case '*': 26 | cout << num1*num2 << endl; 27 | break; 28 | 29 | case '/': 30 | cout << num1/num2 << endl; 31 | break; 32 | 33 | default: 34 | // If the operator is other than +, -, * or /, error message is shown 35 | cout << "Error! operator is not correct" << endl; 36 | break; 37 | } 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /fibonaci.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | 8 | 9 | int fibonacci_naive(int n) { 10 | if (n <= 1) 11 | return n; 12 | 13 | return fibonacci_naive(n - 1) + fibonacci_naive(n - 2); 14 | } 15 | 16 | int fibonacci_fast(int n) { 17 | if (n <= 1) 18 | return n; 19 | 20 | int F1, F2, F; 21 | F1 = 0; 22 | F2 = 1; 23 | for (int i = 2; i <= n; i++) { 24 | F = F1 + F2; 25 | F1 = F2; 26 | F2 = F; 27 | } 28 | return F; 29 | } 30 | 31 | void test_solution() { 32 | assert(fibonacci_fast(3) == 2); 33 | assert(fibonacci_fast(10) == 55); 34 | for (int n = 0; n < 20; ++n) 35 | assert(fibonacci_fast(n) == fibonacci_naive(n)); 36 | } 37 | 38 | int main() { 39 | int n = 0; 40 | //cin >> n; 41 | 42 | test_solution(); 43 | std::cout << fibonacci_fast(n) << '\n'; 44 | return 0; 45 | } 46 | 47 | -------------------------------------------------------------------------------- /kadane.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | // Java program to print largest contiguous array sum 3 | import java.util.*; 4 | 5 | class Kadane 6 | { 7 | public static void main (String[] args) 8 | { 9 | int [] a = {-2, -3, 4, -1, -2, 1, 5, -3}; 10 | System.out.println("Maximum contiguous sum is " + 11 | maxSubArraySum(a)); 12 | } 13 | 14 | static int maxSubArraySum(int a[]) 15 | { 16 | int size = a.length; 17 | int max_so_far = Integer.MIN_VALUE, max_ending_here = 0; 18 | 19 | for (int i = 0; i < size; i++) 20 | { 21 | max_ending_here = max_ending_here + a[i]; 22 | if (max_so_far < max_ending_here) 23 | max_so_far = max_ending_here; 24 | if (max_ending_here < 0) 25 | max_ending_here = 0; 26 | } 27 | return max_so_far; 28 | } 29 | } -------------------------------------------------------------------------------- /pre_order_traversal_binaryTree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node 5 | { 6 | int data; // Data for the tree 7 | struct Node* lt, *rt; //Nodes for the tree 8 | Node(int data) 9 | { 10 | this->data = data; 11 | lt = rt = NULL; 12 | } 13 | }; 14 | 15 | void printPreorder(struct Node* node) 16 | { 17 | if (node == NULL) 18 | { 19 | return; 20 | } 21 | cout << node->data << " "; 22 | printPreorder(node->lt); 23 | printPreorder(node->rt); 24 | } 25 | 26 | int main() 27 | { 28 | struct Node *root = new Node(20); 29 | root->lt = new Node(41); 30 | root->rt = new Node(52); 31 | root->lt->lt = new Node(71); 32 | root->lt->rt = new Node(89); 33 | root->rt->lt = new Node(64); 34 | root->rt->rt = new Node(37); 35 | cout << "\n Preorder traversal of binary tree is: "; 36 | printPreorder(root); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /encrypt-caesar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char message[100], ch; 6 | int i, key; 7 | 8 | printf("Enter a message to encrypt: "); 9 | gets(message); 10 | printf("Enter key: "); 11 | scanf("%d", &key); 12 | 13 | for(i = 0; message[i] != '\0'; ++i){ 14 | ch = message[i]; 15 | 16 | if(ch >= 'a' && ch <= 'z'){ 17 | ch = ch + key; 18 | 19 | if(ch > 'z'){ 20 | ch = ch - 'z' + 'a' - 1; 21 | } 22 | 23 | message[i] = ch; 24 | } 25 | else if(ch >= 'A' && ch <= 'Z'){ 26 | ch = ch + key; 27 | 28 | if(ch > 'Z'){ 29 | ch = ch - 'Z' + 'A' - 1; 30 | } 31 | 32 | message[i] = ch; 33 | } 34 | } 35 | 36 | printf("Encrypted message: %s", message); 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /connectedcompgrp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | vectoradj(10); 7 | 8 | void dfs(int i,vector&vis,vector&adj){ 9 | vis[i]=1; 10 | stacks; 11 | s.push(i); 12 | while(!s.empty()){ 13 | 14 | int t= s.top(); 15 | s.pop(); 16 | for(int it=t;it>v; 44 | int c=0; 45 | int s,k; 46 | 47 | vectorvis(v,0); 48 | for(int i=0;i>s>>k; 51 | 52 | adj[s].push_back(k); 53 | } 54 | 55 | for(int i=1;i<=v;i++){ 56 | 57 | if(vis[i]==0){ 58 | dfs(i,vis,adj); 59 | c++; 60 | } 61 | } 62 | 63 | cout< 2 | #include 3 | int main() 4 | { 5 | char x[1000],a[1000]; 6 | int lookFor,y,i,found,count,lengthOfString; 7 | printf("Enter a string:"); 8 | fgets(a,1000,stdin); 9 | a[strlen(a)-1]='\0'; 10 | lengthOfString=strlen(a); 11 | y=0; 12 | while(y=65 && a[y]<=90) x[y]=a[y]+32; 15 | else x[y]=a[y]; 16 | y++; 17 | } 18 | y=0; 19 | while(y 2 | using namespace std; 3 | 4 | struct Node 5 | { 6 | int data; // Data for the tree 7 | struct Node *lt, *rt; //Nodes for the tree 8 | Node(int data) 9 | { 10 | this->data = data; 11 | lt = rt = NULL; 12 | } 13 | }; 14 | 15 | void printPostorder(struct Node* node) 16 | { 17 | if (node == NULL) 18 | { 19 | return; 20 | } 21 | printPostorder(node->lt); 22 | printPostorder(node->rt); 23 | cout << node->data << " "; 24 | } 25 | 26 | int main() 27 | { 28 | struct Node *root = new Node(20); 29 | root->lt = new Node(41); 30 | root->rt = new Node(52); 31 | root->lt->lt = new Node(71); 32 | root->lt->rt = new Node(89); 33 | root->rt->lt = new Node(64); 34 | root->rt->rt = new Node(37); 35 | cout << "\nOutput for postorder traversal of binary tree is: "; 36 | printPostorder(root); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /employee.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct Employee{ 4 | char name[50]; 5 | int id; 6 | int dob; 7 | float bsal; 8 | }; 9 | int main(){ 10 | int n=0,p=0; 11 | float hig=0.0; 12 | std::cout<<"enter no. of employees\n"; 13 | std::cin>>n; 14 | struct Employee emp[n]; 15 | std::cout<<"enter details of employees\n"; 16 | for(int i=0;i>emp[i].name; 20 | std::cout<<"enter ID\n"; 21 | std::cin>>emp[i].id; 22 | std::cout<<"date of birth,in [ddmmyy] format\n"; 23 | std::cin>>emp[i].dob; 24 | std::cout<<"enter salary\n"; 25 | std::cin>>emp[i].bsal; 26 | } 27 | for(int i=0;i 4 | using namespace std; 5 | 6 | // To check for parallel line 7 | void parallel(int n, int a[][2]) 8 | { 9 | bool x = true, y = true; 10 | 11 | // checking for parallel to X and Y 12 | // axis condition 13 | for (int i = 0; i < n - 1; i++) { 14 | if (a[i][0] != a[i + 1][0]) 15 | x = false; 16 | if (a[i][1] != a[i + 1][1]) 17 | y = false; 18 | } 19 | 20 | // To display the output 21 | if (x) 22 | cout << "parallel to Y Axis" << endl; 23 | else if (y) 24 | cout << "parallel to X Axis" << endl; 25 | else 26 | cout << "Not parallel to X" 27 | << " and Y Axis" << endl; 28 | } 29 | 30 | // Driver's Code 31 | int main() 32 | { 33 | int a[][2] = { { 1, 2 }, 34 | { 1, 4 }, 35 | { 1, 6 }, 36 | { 1, 0 } }; 37 | 38 | int n = sizeof(a) / sizeof(a[0]); 39 | parallel(n, a); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /ReverseLine.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class QuickSort { 4 | 5 | public static void main(String[] args) { 6 | 7 | Scanner sc = new Scanner(System.in); 8 | 9 | String str = sc.nextLine(); 10 | String ans = ""; 11 | 12 | int spaces = 0, len = str.length()-1; 13 | 14 | while(len>=0) { 15 | while(len >= 0 && str.charAt(len) == ' ') { 16 | len--; 17 | spaces++; 18 | } 19 | int j = len; 20 | 21 | if(len<0) { break;} 22 | 23 | while(len >= 0 && str.charAt(len) != ' ') { 24 | len--; 25 | } 26 | 27 | if(ans.isEmpty()) { 28 | ans = ans.concat(str.substring(len+1,j+1)); 29 | 30 | }else { 31 | while(spaces >= 0) { 32 | if(spaces == 0) { 33 | ans = ans.concat(str.substring(len+1,j+1)); 34 | break; 35 | } 36 | ans = ans.concat(" "); 37 | spaces--; 38 | } 39 | spaces = 0; 40 | } 41 | } 42 | 43 | str = ans.toString(); 44 | System.out.println(str); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Palindrome.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Palindrome 3 | { 4 | public static void main(String args[]) 5 | { 6 | Scanner sc = new Scanner(System.in); 7 | System.out.println("Enter the number which needs to be checked for palindrome"); 8 | int n = sc.nextInt(); 9 | int rev =0; 10 | /*Store actual value of number in num as n will be changed in the loop */ 11 | int num = n; 12 | 13 | /*Loop exists till the whole number doesn't become 0 */ 14 | while(n != 0) 15 | { 16 | int rem = n%10; 17 | rev = rev*10 + rem; 18 | n = n/10; 19 | } 20 | 21 | /*Compare the original number and reverse of the number */ 22 | if(num == rev) 23 | { 24 | System.out.println("Palindrome"); 25 | } 26 | else 27 | { 28 | System.out.println("Not Palindrome"); 29 | } 30 | sc.close(); 31 | } 32 | } -------------------------------------------------------------------------------- /bubblesort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void bubblesort(int *arr, int n) 5 | { 6 | 7 | int isanyswapped = 0 ; //check whether array is already sorted or not 8 | for(int i = 0; i < n - 1; i++) 9 | { 10 | for(int j = 0; j < n - i - 1; j++) 11 | { 12 | if(arr[j] > arr[j + 1]) 13 | { 14 | swap(arr[j], arr[j + 1]); //stl built in function 15 | isanyswapped = 1; //array is not already sorted 16 | } 17 | } 18 | if(isanyswapped == 0) 19 | { 20 | //means array get sorted no further iterations required 21 | break; 22 | } 23 | } 24 | 25 | } 26 | 27 | int main() 28 | { 29 | int arr[] = { 2, 9, 10, 11, 4, 12, 13, 15, 16, 16}; 30 | int n = sizeof(arr) / sizeof(arr[0]); //array size 31 | bubblesort(arr, n); 32 | for(int i = 0; i < n; i++) 33 | { 34 | cout << arr[i] << " "; //print sorted array 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /anagram.py: -------------------------------------------------------------------------------- 1 | # GregPikitis 10/9/20 2 | # Check if two strings are anagrams 3 | 4 | # Inputting strings 5 | rawstring1 = raw_input("Enter first string: ") 6 | rawstring2 = raw_input("Enter second string: ") 7 | 8 | # Stripping spaces and case sensitivity 9 | string1 = rawstring1.replace(" ", "").lower() 10 | string2 = rawstring2.replace(" ", "").lower() 11 | 12 | # Getting unique letters 13 | set1 = set(string1) 14 | 15 | # Setting an anagram indicator 16 | anagrams = 1 17 | 18 | if len(string1) == len(string2): 19 | for x in set1: 20 | if string1.count(x) != string2.count(x): 21 | # Checking if strings have the same number of each letter 22 | anagrams = 0 23 | else: # If not the same length, not anagrams 24 | anagrams = 0 25 | 26 | # Printing results based on anagram indicator 27 | if anagrams == 1: 28 | print(rawstring1 + " and " + rawstring2 + " are anagrams!") 29 | else: 30 | print(rawstring1 + " and " + rawstring2 + " are not anagrams!") 31 | -------------------------------------------------------------------------------- /0-1-Knapsack.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int max(int a, int b) { return (a > b)? a : b; } 4 | 5 | int knapSack(int W, int wt[], int val[], int n) 6 | { 7 | int i, w; 8 | int K[n+1][W+1]; 9 | 10 | for (i = 0; i <= n; i++) 11 | { 12 | for (w = 0; w <= W; w++) 13 | { 14 | if (i==0 || w==0) 15 | K[i][w] = 0; 16 | else if (wt[i-1] <= w) 17 | K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]); 18 | else 19 | K[i][w] = K[i-1][w]; 20 | } 21 | } 22 | 23 | return K[n][W]; 24 | } 25 | 26 | int main() 27 | { 28 | int i, n, val[20], wt[20], W; 29 | 30 | printf("Enter number of items:"); 31 | scanf("%d", &n); 32 | 33 | printf("Enter value and weight of items:\n"); 34 | for(i = 0;i < n; ++i){ 35 | scanf("%d%d", &val[i], &wt[i]); 36 | } 37 | 38 | printf("Enter size of knapsack:"); 39 | scanf("%d", &W); 40 | 41 | printf("%d", knapSack(W, wt, val, n)); 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /Numtoword.py: -------------------------------------------------------------------------------- 1 | #it can check if your all braces is balanced or not like {[()]} is balanced 2 | #and [{]} is not balanced 3 | p=input("Input Your paragraph :") 4 | symbollist=["{","}","[","]","(",")"] 5 | opensymbol=["[","{","("] 6 | closesymbol=["]","}",")"] 7 | templist=[] 8 | for word in p: 9 | for char in word: 10 | if char in symbollist: 11 | templist.append(char) 12 | def check(): 13 | global templist 14 | tlen=len(templist) 15 | if tlen==0: 16 | return 0 17 | elif(tlen==1): 18 | return 19 | for i in range(len(templist)): 20 | #item 21 | try: 22 | I=templist[i] 23 | except: 24 | break 25 | if I in opensymbol: 26 | continue 27 | #previousitem 28 | pI=templist[i-1] 29 | if I in closesymbol and i==0: 30 | break 31 | if I in closesymbol: 32 | op=closesymbol.index(I) 33 | #oppositeitem 34 | opI=opensymbol[op] 35 | if pI==opI: 36 | templist.pop(i) 37 | templist.pop(i-1) 38 | else: 39 | print("not Balanced") 40 | return 41 | check() 42 | check() 43 | if len(templist)==0: 44 | print("balanced") 45 | -------------------------------------------------------------------------------- /SelectionSort.java: -------------------------------------------------------------------------------- 1 | // Selection sort in Java 2 | 3 | import java.util.Arrays; 4 | 5 | class SelectionSort { 6 | void selectionSort(int array[]) { 7 | int size = array.length; 8 | 9 | for (int step = 0; step < size - 1; step++) { 10 | int min_idx = step; 11 | 12 | for (int i = step + 1; i < size; i++) { 13 | 14 | // To sort in descending order, change > to < in this line. 15 | // Select the minimum element in each loop. 16 | if (array[i] < array[min_idx]) { 17 | min_idx = i; 18 | } 19 | } 20 | 21 | // put min at the correct position 22 | int temp = array[step]; 23 | array[step] = array[min_idx]; 24 | array[min_idx] = temp; 25 | } 26 | } 27 | 28 | // driver code 29 | public static void main(String args[]) { 30 | int[] data = { 20, 12, 10, 15, 2 }; 31 | SelectionSort ss = new SelectionSort(); 32 | ss.selectionSort(data); 33 | System.out.println("Sorted Array in Ascending Order: "); 34 | System.out.println(Arrays.toString(data)); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /binary_search.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int n, x, result; 4 | int arr[10]; 5 | printf("\nEnter the no of elements in array:" ); 6 | scanf("%d",&n); 7 | printf("\nEnter the elements of array:"); 8 | for(int i=0;i 0 to l ... contains 0s 10 | Second part -> l to mid ... contains 1s 11 | Third part -> mid to h ... contains unknown elements 12 | Fourth part -> h to size ... contains 2s 13 | ''' 14 | def sortarr(a, size): 15 | l = 0 16 | h = size-1 17 | mid = 0 18 | while mid<=h: 19 | if a[mid] == 0: 20 | a[l], a[mid] = a[mid], a[l] 21 | l = l+1 22 | mid = mid +1 23 | elif a[mid] == 1: 24 | mid = mid + 1 25 | else: 26 | a[mid], a[h] = a[h], a[mid] 27 | h = h-1 28 | return a 29 | 30 | print("This is a program to sort a 012 array") 31 | print("Enter the elements of the array: ") 32 | arr = list(map(int, input().split())) 33 | arr = sortarr(arr, len(arr)) 34 | print("Array after sorting or segregation: ") 35 | print(*arr) -------------------------------------------------------------------------------- /kadaneAlgorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | //Kadane's algorithm implementation for 6 | //calculating the max subarray sum of a given array 7 | 8 | int maxSubArraySum(int a[], int size) { 9 | 10 | int maxSoFar = a[0]; 11 | int currentMax = a[0]; 12 | 13 | for (int i = 1; i < size; i++) 14 | { 15 | currentMax = max(a[i], currentMax+a[i]); 16 | maxSoFar = max(maxSoFar, currentMax); 17 | } 18 | return maxSoFar; 19 | } 20 | 21 | int main() { 22 | 23 | //getting the user input 24 | //the first line contains the size of the array 25 | //the second line contains space separated elements of the array 26 | int size; 27 | int arr[size + 1]; 28 | 29 | cin >> size; 30 | for( int i = 0; i < size; i++ ) 31 | cin >> arr[i]; 32 | 33 | int maxSum = maxSubArraySum(arr, size); 34 | cout << "The maximum contiguous sum is: " << maxSum << endl; 35 | 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /reverse_linked_list.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data): 3 | self.data = data 4 | self.next = None 5 | 6 | class LinkedList: 7 | 8 | def __init__(self): 9 | self.head = None 10 | 11 | def reverse(self): 12 | prev = None 13 | current = self.head 14 | while(current is not None): 15 | next = current.next 16 | current.next = prev 17 | prev = current 18 | current = next 19 | self.head = prev 20 | 21 | def push(self, new_data): 22 | new_node = Node(new_data) 23 | new_node.next = self.head 24 | self.head = new_node 25 | 26 | def printList(self): 27 | temp = self.head 28 | while(temp): 29 | print temp.data, 30 | temp = temp.next 31 | 32 | 33 | llist = LinkedList() 34 | llist.push(20) 35 | llist.push(4) 36 | llist.push(15) 37 | llist.push(85) 38 | 39 | print "Given Linked List" 40 | llist.printList() 41 | llist.reverse() 42 | print "\nReversed Linked List" 43 | llist.printList() 44 | -------------------------------------------------------------------------------- /SelectionSort.c: -------------------------------------------------------------------------------- 1 | Pseudocode:- 2 | 3 | procedure selection sort 4 | list : array of items 5 | n : size of list 6 | 7 | for i = 1 to n - 1 8 | /* set current element as minimum*/ 9 | min = i 10 | 11 | /* check the element to be minimum */ 12 | for j = i+1 to n 13 | if list[j] < list[min] then 14 | min = j; 15 | end if 16 | end for 17 | 18 | /* swap the minimum element with the current element*/ 19 | if indexMin != i then 20 | swap list[min] and list[i] 21 | end if 22 | end for 23 | 24 | end procedure 25 | 26 | 27 | Code:- 28 | 29 | #include 30 | 31 | void swap(int *x, int *y) 32 | { 33 | int temp = *x; 34 | *x = *y; 35 | *y = temp; 36 | } 37 | 38 | void selectionSort(int arr[], int n) 39 | { 40 | int i, j, min_idx; 41 | for (i = 0; i < n-1; i++) 42 | { 43 | min_idx = i; 44 | for (j = (i+1); j < n; j++){ 45 | if (arr[j] < arr[min_idx]){ 46 | min_idx = j; 47 | } 48 | } 49 | swap(&arr[min_idx], &arr[i]); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /inv_cloak.py: -------------------------------------------------------------------------------- 1 | ''' 2 | This project is done using only openCV. 3 | No Machine Learning or Deep Learning concepts/algos have been used. 4 | 5 | ''' 6 | import cv2 7 | import numpy as np 8 | 9 | cam = cv2.VideoCapture(0) #captures the video. 10 | 11 | i = 0 12 | 13 | while i<50: 14 | i+=1 15 | _, background = cam.read() 16 | replace = cv2.resize(background,(1000,1000)) 17 | 18 | 19 | while True: 20 | _,frame = cam.read() 21 | frame = cv2.resize(frame,(1000,1000)) 22 | 23 | hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV) 24 | 25 | lower_blue = np.array([64,74,0]) 26 | upper_blue = np.array([111,255,251]) 27 | 28 | mask = cv2.inRange(hsv,lower_blue,upper_blue) 29 | mask_inv = cv2.bitwise_not(mask) 30 | 31 | IL = cv2.bitwise_and(frame, frame, mask=mask_inv) 32 | CL = cv2.bitwise_and(replace, replace, mask=mask) 33 | 34 | final_img = cv2.add(IL,CL) 35 | 36 | cv2.imshow('image', final_img) 37 | 38 | 39 | if cv2.waitKey(50) & 0xFF == ord('q'): 40 | break 41 | 42 | cam.release() 43 | cv2.destroyAllWindows() 44 | 45 | 46 | -------------------------------------------------------------------------------- /mergesort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | void merge(int arr[],int start,int mid,int end){ 7 | 8 | int temp[end-start+1]; 9 | int i=start,j= mid+1; 10 | int k=0; 11 | 12 | while(i<=mid && j<=end){ 13 | 14 | if(arr[i]>n; 58 | int arr[n]; 59 | 60 | for(int i=0;i>arr[i]; 63 | } 64 | 65 | merge_sort(arr,0,n-1); 66 | for(int i=0;i 2 | #include 3 | #include 4 | 5 | using std::vector; 6 | 7 | bool find_greater(int a , int b) 8 | { 9 | return a > b; 10 | } 11 | 12 | int knap(int W, vector &w) 13 | { 14 | vector >vals(w.size() + 1 , vector(W + 1 , 0)); 15 | 16 | for (int i = 0; i <= w.size(); ++i) 17 | { 18 | for (int j = 0; j <= W; ++j) 19 | { 20 | if (i == 0 || j == 0) 21 | vals.at(i).at(j) = 0; 22 | else if (w[i - 1] <= j) 23 | vals.at(i).at(j) = std::max(vals.at(i - 1).at(j) , w[i - 1] + vals.at(i - 1).at(j - w[i - 1])); 24 | else 25 | vals.at(i).at(j) = vals.at(i - 1).at(j); 26 | } 27 | } 28 | return vals.at(w.size()).at(W); 29 | } 30 | 31 | int main() 32 | { 33 | int n, W; 34 | std::cin >> W >> n; 35 | vector w(n); 36 | for (int i = 0; i < n; i++) 37 | { 38 | std::cin >> w[i]; 39 | } 40 | std::sort(w.begin() , w.end() , find_greater); 41 | std::cout << knap(W, w) << '\n'; 42 | } -------------------------------------------------------------------------------- /DFS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | vector adj[10]; 6 | 7 | bool visited[10]; 8 | 9 | void dfs(int s){ // source vertex is passed as a param 10 | visited[s]= true; 11 | for(int i=0;i>nodes; //number of nodes 33 | cin>>edges; // number of edges 34 | 35 | for(int i=0;i>u>>v; 38 | adj[u].push_back(v); 39 | adj[v].push_back(u); 40 | } 41 | 42 | initialize(); 43 | // the above func marked all node as not visited; 44 | 45 | for(int i=1;i<= nodes;++i){ 46 | 47 | 48 | if(visited[i]== false){ 49 | 50 | dfs(i); 51 | connectedComponents++; 52 | } 53 | } 54 | 55 | 56 | if(connectedComponents ==1){ 57 | cout<<"1";} 58 | else{ 59 | cout<<"0"; 60 | } 61 | return 0; 62 | } -------------------------------------------------------------------------------- /Group Anagrams.java: -------------------------------------------------------------------------------- 1 | Question: 2 | Given an array of strings, group anagrams together. 3 | 4 | Example: 5 | 6 | Input: ["eat", "tea", "tan", "ate", "nat", "bat"], 7 | Output: 8 | [ 9 | ["ate","eat","tea"], 10 | ["nat","tan"], 11 | ["bat"] 12 | ] 13 | 14 | Solution: 15 | class Solution { 16 | public List> groupAnagrams(String[] string_list) { 17 | 18 | List> masterList=new ArrayList<>(); 19 | Map> map=new LinkedHashMap<>(); 20 | for (String s : string_list) { 21 | char[] si=s.toCharArray(); 22 | Arrays.sort(si); 23 | String key=String.valueOf(si); 24 | if(map.containsKey(key)){ 25 | map.get(key).add(s); 26 | }else{ 27 | List list=new ArrayList<>(); 28 | list.add(s); 29 | map.put(key,list); 30 | } 31 | } 32 | for(Map.Entry> e:map.entrySet()){ 33 | masterList.add(e.getValue()); 34 | } 35 | 36 | return masterList; 37 | } 38 | } -------------------------------------------------------------------------------- /identicalTrees.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | // write a function to determine if two trees are identical 4 | 5 | // functions signature will be to accept two trees as parameters and return a boolean true if they're identical, false if not 6 | 7 | // approach is to use in-order traversal of each tree to populate an array with the values, and then compare the two arrays to see if they're identical 8 | 9 | // assumes the tree inputs have basic tree properties on them like root, root.left and root.right 10 | 11 | function determineIdentical(tree1, tree2) { 12 | 13 | let tree1Arr = []; 14 | 15 | let tree2Arr = []; 16 | 17 | 18 | function _preOrder1(root){ 19 | 20 | tree1Arr.push(root); 21 | _preOrder1(root.left); 22 | _preOrder1(root.right); 23 | 24 | } 25 | 26 | function _preOrder2(root){ 27 | 28 | tree2Arr.push(root); 29 | _preOrder2(root.left); 30 | _preOrder2(root.right); 31 | 32 | } 33 | 34 | _preOrder1(tree1.root); 35 | 36 | _preOrder2(tree2.root); 37 | 38 | if(tree1Arr === tree2Arr){ 39 | return true; 40 | } else { 41 | return false; 42 | } 43 | 44 | } -------------------------------------------------------------------------------- /CalculateArea.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | int main() 6 | { 7 | float a, b, radius, area; 8 | 9 | int ch; 10 | 11 | cout << "\n1.Area Of Rectangle"; 12 | cout << "\n2.Area Of Square"; 13 | cout << "\n3.Area Of Circle\n"; 14 | 15 | cout<<"\nEnter Your Choice :"; 16 | cin>>ch; 17 | 18 | switch(ch) 19 | { 20 | case 1: 21 | { 22 | cout << "\nEnter the Length and Breadth of Rectangle: "; 23 | cin >> a >> b; 24 | area = a*b; 25 | cout << "Area of Rectangle = " << area << endl; 26 | break; 27 | } 28 | case 2: 29 | { 30 | cout << "\nEnter Side of Square: "; 31 | cin >> a; 32 | area = a*a; 33 | cout << "Area of Square = " << area << endl; 34 | break; 35 | } 36 | case 3: 37 | { 38 | cout << "\nEnter the Radius of Circle: "; 39 | cin >> radius; 40 | area = 3.14159*radius*radius; 41 | cout << "Area of Circle = " << area << endl; 42 | break; 43 | } 44 | 45 | default: 46 | cout<<"\n Invalid Choice Try Again...!!!"; 47 | break; 48 | } 49 | return 0; 50 | } 51 | 52 | -------------------------------------------------------------------------------- /RootsofaQuadraticEquation.c: -------------------------------------------------------------------------------- 1 | Find the Roots of a Quadratic Equation 2 | 3 | code:- 4 | #include 5 | #include 6 | int main() { 7 | double a, b, c, discriminant, root1, root2, realPart, imagPart; 8 | printf("Enter coefficients a, b and c: "); 9 | scanf("%lf %lf %lf", &a, &b, &c); 10 | 11 | discriminant = b * b - 4 * a * c; 12 | 13 | // condition for real and different roots 14 | if (discriminant > 0) { 15 | root1 = (-b + sqrt(discriminant)) / (2 * a); 16 | root2 = (-b - sqrt(discriminant)) / (2 * a); 17 | printf("root1 = %.2lf and root2 = %.2lf", root1, root2); 18 | } 19 | 20 | // condition for real and equal roots 21 | else if (discriminant == 0) { 22 | root1 = root2 = -b / (2 * a); 23 | printf("root1 = root2 = %.2lf;", root1); 24 | } 25 | 26 | // if roots are not real 27 | else { 28 | realPart = -b / (2 * a); 29 | imagPart = sqrt(-discriminant) / (2 * a); 30 | printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart, imagPart); 31 | } 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /ThreadDemo.java: -------------------------------------------------------------------------------- 1 | package pgrms; 2 | 3 | class NewThread implements Runnable 4 | { 5 | Thread t; 6 | NewThread() 7 | { 8 | t = new Thread(this,"Demo Thread"); 9 | System.out.println("Child thred:"+t); 10 | t.start(); 11 | } 12 | 13 | @Override 14 | public void run() { 15 | // TODO Auto-generated method stub 16 | try 17 | { 18 | for(int i=5;i>0;i--) 19 | { 20 | System.out.println("Chiild Thread:"+i); 21 | Thread.sleep(500); 22 | } 23 | } catch(InterruptedException e) 24 | { 25 | System.out.println("child Interrupted"); 26 | } 27 | System.out.println("Exiting child thread"); 28 | 29 | } 30 | 31 | } 32 | 33 | public class ThreadDemo { 34 | public static void main(String args[]) 35 | { 36 | new NewThread(); 37 | 38 | try 39 | { 40 | for(int i=5;i>0;i--) 41 | { 42 | System.out.println("main thread:"+i); 43 | Thread.sleep(1000); 44 | } 45 | } catch (InterruptedException e) 46 | { 47 | System.out.println("Main thread interrupted"); 48 | } 49 | System.out.println("main thread exiting"); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /leavesBinaryTree.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | // JavaScript implementation of a function that finds the number of leaves in a binary tree 4 | // function's signature is a binary tree as a parameter, returns the number of leaves in the tree 5 | // relies on the assumption that the input is a binary tree with a root, and made up of nodes with left and right properties on them, as illustrated below 6 | // function uses preOrder depth traversal method 7 | 8 | class Node { 9 | 10 | constructor(value, left = null, right = null) { 11 | this.value = value; 12 | this.left = left; 13 | this.right = right; 14 | } 15 | 16 | } 17 | 18 | class BinaryTree { 19 | 20 | constructor( root = null) { 21 | this.root = root; 22 | } 23 | 24 | } 25 | 26 | function numLeavesInTree(binaryTree) { 27 | 28 | let numLeaves = 0; 29 | 30 | function _preOrderTraversal(root) { 31 | 32 | if(!root) { 33 | return; 34 | } 35 | 36 | numLeaves += 1; 37 | _preOrderTraversal(root.left); 38 | _preOrderTraversal(root.right); 39 | 40 | } 41 | 42 | _preOrderTraversal(binaryTree.root); 43 | return numLeaves; 44 | 45 | } 46 | -------------------------------------------------------------------------------- /Insertion Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* Function to sort an array using insertion sort*/ 5 | void insertionSort(int arr[], int n) 6 | { 7 | int i, key, j; 8 | for (i = 1; i < n; i++) 9 | { 10 | key = arr[i]; 11 | j = i - 1; 12 | 13 | /* Move elements of arr[0..i-1], that are 14 | greater than key, to one position ahead 15 | of their current position */ 16 | while (j >= 0 && arr[j] > key) 17 | { 18 | arr[j + 1] = arr[j]; 19 | j = j - 1; 20 | } 21 | arr[j + 1] = key; 22 | } 23 | } 24 | 25 | // A utility function to print an array of size n 26 | void printArray(int arr[], int n) 27 | { 28 | int i; 29 | for (i = 0; i < n; i++) 30 | cout << arr[i] << " "; 31 | cout << endl; 32 | } 33 | 34 | /* Driver code */ 35 | int main() 36 | { 37 | int arr[] = { 12, 11, 13, 5, 6 }; 38 | int n = sizeof(arr) / sizeof(arr[0]); 39 | 40 | insertionSort(arr, n); 41 | printArray(arr, n); 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /bubblesort_vikram.cpp: -------------------------------------------------------------------------------- 1 | // Optimized implementation of Bubble sort 2 | #include 3 | 4 | void swap(int *xp, int *yp) 5 | { 6 | int temp = *xp; 7 | *xp = *yp; 8 | *yp = temp; 9 | } 10 | 11 | // An optimized version of Bubble Sort 12 | void bubbleSort(int arr[], int n) 13 | { 14 | int i, j; 15 | bool swapped; 16 | for (i = 0; i < n-1; i++) 17 | { 18 | swapped = false; 19 | for (j = 0; j < n-i-1; j++) 20 | { 21 | if (arr[j] > arr[j+1]) 22 | { 23 | swap(&arr[j], &arr[j+1]); 24 | swapped = true; 25 | } 26 | } 27 | 28 | // IF no two elements were swapped by inner loop, then break 29 | if (swapped == false) 30 | break; 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 | // Driver program to test above functions 44 | int main() 45 | { 46 | int arr[] = {64, 34, 25, 12, 22, 11, 90}; 47 | int n = sizeof(arr)/sizeof(arr[0]); 48 | bubbleSort(arr, n); 49 | printf("Sorted array: \n"); 50 | printArray(arr, n); 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /Hulk.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.*; 3 | import java.lang.*; 4 | import java.io.*; 5 | public class Hulk { 6 | 7 | public static void main(String args[]) throws java.lang.Exception 8 | { 9 | int n = 0 , count = 0; 10 | String hate = "I hate "; 11 | String love = "I love "; 12 | String it = "it"; 13 | String that = "that "; 14 | BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); 15 | try 16 | { 17 | n = Integer.parseInt(br.readLine()); 18 | } 19 | catch(Exception e) 20 | { 21 | return; 22 | } 23 | System.out.print(hate); 24 | 25 | for(int i = 2 ; i < n+1 ; i++) 26 | { 27 | if(i%2 == 0) 28 | { 29 | System.out.print(that); 30 | System.out.print(love); 31 | } 32 | else 33 | { 34 | System.out.print(that); 35 | System.out.print(hate); 36 | } 37 | } 38 | 39 | System.out.print(it); 40 | } 41 | } -------------------------------------------------------------------------------- /BubbleSort.java: -------------------------------------------------------------------------------- 1 | package sorting; 2 | 3 | import java.util.Scanner; 4 | 5 | public class BubbleSort { 6 | 7 | public static void main(String[] args) { 8 | 9 | int n; 10 | Scanner sc = new Scanner(System.in); 11 | System.out.println("Enter array size: "); //Asking User to Enter Size of Array 12 | n = sc.nextInt(); 13 | int a[] = new int[n]; 14 | System.out.println("Enter array: "); //Entering Array Elements 15 | for(int i=0;i 2 | typedef long long int ll; 3 | #define io ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 4 | #define fr(i,l,r) for(ll i=l;i>t;while(t--) 6 | #define maxn 50001 7 | #define k 31 8 | using namespace std; 9 | ll st[maxn][k] ; 10 | int main(){ 11 | io; 12 | lop{ 13 | ll n , z ; cin >> n >> z ; 14 | ll mp[100001] = {0} ; 15 | ll ans = 0 ; 16 | bool b = false ; 17 | fr(i,0,n){ 18 | ll a ; cin >> a ; 19 | mp[a]++ ; 20 | } 21 | for(ll i = 10000 ; i >= 1 ; i--){ 22 | while(mp[i] != 0){ 23 | z -= i ; 24 | mp[i]-- ; 25 | ans++ ; 26 | mp[i / 2]++ ; 27 | if(z <= 0){ 28 | b = true ; 29 | break ; 30 | } 31 | } 32 | if( b == true){ 33 | break ; 34 | } 35 | } 36 | if(b == true) cout << ans << "\n" ; 37 | else cout << "Evacuate" << "\n" ; 38 | } 39 | return 0 ; 40 | } 41 | -------------------------------------------------------------------------------- /second_largest.py: -------------------------------------------------------------------------------- 1 | list_num = input('How many numbers do you want to add to the list ? ---> ') 2 | numList = list() 3 | list_size = 0 4 | flag = 0 5 | 6 | try: 7 | list_size = int(list_num) 8 | flag = 1 9 | except: 10 | print('Please enter an Integer value for the total number of numbers in the list') 11 | flag = 0 12 | 13 | ordinal = lambda n: "%d%s" % (n, "tsnrhtdd"[(n / 10 % 10 != 1) * (n % 10 < 4) * n % 10::4]) 14 | flag_ordinal = 0 15 | if flag == 1 : 16 | inside_flag = 0 17 | for i in range(0,list_size): 18 | flag_ordinal = flag_ordinal + 1 19 | a = input('Enter ' + str(flag_ordinal) + ' number : ') 20 | try: 21 | n = int(a) 22 | numList.append(n) 23 | inside_flag = 1 24 | except: 25 | print('Enter a number.') 26 | inside_flag = 0 27 | break 28 | else: 29 | print('Execution stopped encountered something else instead of numeric value') 30 | 31 | 32 | if flag == 1 and inside_flag == 1: 33 | 34 | numList.sort() 35 | print("Second Largest Element is : "+ str(numList[list_size - 2])) 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /Quadratic_Equations_Root.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(){ 5 | float a,b,c; 6 | float d,root1,root2; 7 | 8 | 9 | printf("Enter a, b and c of quadratic equation: "); 10 | printf("\nA : "); 11 | scanf("%f",&a); 12 | printf("\nB : "); 13 | scanf("%f",&b); 14 | printf("\nC : "); 15 | scanf("%f",&c); 16 | 17 | d = b * b - 4 * a * c; 18 | 19 | if(d < 0){ 20 | printf("Roots are complex number.\n"); 21 | 22 | printf("Roots of quadratic equation are: "); 23 | printf("%.3f%+.3fi",-b/(2*a),sqrt(-d)/(2*a)); 24 | printf(", %.3f%+.3fi",-b/(2*a),-sqrt(-d)/(2*a)); 25 | 26 | return 0; 27 | } 28 | else if(d==0){ 29 | printf("Both roots are equal.\n"); 30 | 31 | root1 = -b /(2* a); 32 | printf("Root of quadratic equation is: %.3f ",root1); 33 | 34 | return 0; 35 | } 36 | else{ 37 | printf("Roots are real numbers.\n"); 38 | 39 | root1 = ( -b + sqrt(d)) / (2* a); 40 | root2 = ( -b - sqrt(d)) / (2* a); 41 | printf("Roots of quadratic equation are: %.3f , %.3f",root1,root2); 42 | } 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /heap_stl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main(){ 5 | 6 | vector v1= {40,20,15,10,5}; 7 | 8 | //converting vector into heap 9 | //using make heap function 10 | 11 | make_heap(v1.begin(),v1.end()); 12 | v1.push_back(70); 13 | push_heap(v1.begin(),v1.end()); 14 | 15 | //Displaying max element of heap 16 | 17 | //using front() 18 | 19 | cout<<"The maximum element of heap is: "; 20 | cout<::iterator it1; 41 | if(is_heap(v1.begin(),v1.end())) 42 | cout<<"The container is heap:"; 43 | else 44 | cout<<"The container is not heap:"; 45 | cout<::iterator it= is_heap_until(v1.begin(),v1.end()); 49 | 50 | cout<<"The heap elements in container are:"; 51 | for(it1 = v1.begin();it1!= it;it1++) 52 | 53 | cout<<*it1<<" "; 54 | cout< 7 | using namespace std; 8 | 9 | int BinarySearch(int arr[],int low,int high,int el) 10 | { 11 | if(low>high) 12 | { 13 | return -1; 14 | } 15 | 16 | int mid = (low+high)/2; 17 | if(arr[mid] == el) 18 | { 19 | return mid; 20 | } 21 | if(arr[mid]el) 26 | { 27 | return BinarySearch(arr,low,mid-1,el); 28 | } 29 | 30 | } 31 | int main() 32 | { 33 | int n; 34 | cout<<"Enter Size of Array"<>n; 36 | cout<<"Enter the elements of array"<>arr[i]; 40 | cout<<"Enter the element that you want to find"<>el; 43 | int index =BinarySearch(arr,0,n-1,el); 44 | if(index<0) 45 | cout<<"Element is not present in the array"< 2 | #include 3 | 4 | int main() 5 | { 6 | int queue[20]; 7 | int front=-1; 8 | int rear=-1; 9 | 10 | //adding elements in the queue at the last 11 | for(int i=0;i<5;i++) 12 | { 13 | int val; 14 | if((front==-1)&&(rear==-1)) 15 | { 16 | front=0;rear=0; 17 | printf("Enter the Value to add:"); 18 | scanf("%d",&val); 19 | queue[rear]=val; 20 | } 21 | else if((rear+1)==20) 22 | printf("The queue is Full\n"); 23 | else 24 | { 25 | printf("Enter the Value to add:"); 26 | scanf("%d",&val); 27 | rear=rear+1; 28 | queue[rear]=val; 29 | } 30 | } 31 | printf("After adding values queue is: "); 32 | for(int i=front;i<=rear;i++) 33 | printf("%d ",queue[i]); 34 | 35 | //removing element from start of the queue 36 | for(int i=0;i<3;i++) 37 | { 38 | if(front==-1) 39 | printf("The queue is Empty\n"); 40 | else 41 | front=front+1; 42 | } 43 | 44 | printf("\nAfter removing values queue is:"); 45 | for(int i=front;i<=rear;i++) 46 | printf("%d ",queue[i]); 47 | 48 | printf("\nFront=%d\nRear=%d",front,rear); 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Basic calculator using switch case.c: -------------------------------------------------------------------------------- 1 | #include 2 | int add(int a, int b); 3 | int sub(int a, int b); 4 | int mul(int a, int b); 5 | float div(int a, int b); 6 | void main() 7 | { 8 | int ch,a,b; 9 | while(1) 10 | { 11 | printf("1.Addition\n"); 12 | printf("2.Subtraction\n"); 13 | printf("3.Multiplication\n"); 14 | printf("4.Division\n"); 15 | printf("5.Quit\n"); 16 | printf("Enter first number: "); 17 | scanf("%d",&a); 18 | printf("Enter second number: "); 19 | scanf("%d",&b); 20 | printf("Enter your choice: "); 21 | scanf("%d",&ch); 22 | switch(ch) 23 | { 24 | case 1: printf("The Sum is: %d\n",add(a,b)); 25 | break; 26 | case 2: printf("The Subtraction is: %d\n",sub(a,b)); 27 | break; 28 | case 3: printf("The Multiplication is: %d\n",mul(a,b)); 29 | break; 30 | case 4: printf("The Division is: %f\n",div(a,b)); 31 | break; 32 | case 5: exit(1); 33 | break; 34 | default:printf("Enter choice is invalid!\n\n"); 35 | 36 | } 37 | } 38 | 39 | 40 | } 41 | int add(a,b) 42 | { 43 | return(a+b); 44 | } 45 | int sub(a,b) 46 | { 47 | return(a-b); 48 | } 49 | int mul(a,b) 50 | { 51 | return(a*b); 52 | } 53 | float div(a,b) 54 | { 55 | return(a/b); 56 | } 57 | -------------------------------------------------------------------------------- /interpolation_search.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int interpolationSearch(int arr[], int n, int x) 5 | { 6 | // Find indexes of two corners 7 | int lo = 0, hi = (n - 1); 8 | 9 | 10 | while (lo <= hi && x >= arr[lo] && x <= arr[hi]) 11 | { 12 | 13 | // uniform distribution in mind 14 | 15 | int pos = lo + (((double)(hi-lo) / (arr[hi]-arr[lo]))*(x - arr[lo])); 16 | 17 | 18 | if (arr[pos] == x) 19 | return pos; 20 | 21 | 22 | if (arr[pos] < x) 23 | lo = pos + 1; 24 | 25 | 26 | else 27 | hi = pos - 1; 28 | } 29 | return -1; 30 | } 31 | 32 | 33 | int main() 34 | { 35 | 36 | // input array 37 | int arr[] = {10, 12, 13, 16, 18, 19, 20, 21, 22, 23, 24, 33, 35, 42, 47}; 38 | 39 | int n = sizeof(arr)/sizeof(arr[0]); 40 | 41 | int x = 18; // Element to be searched 42 | 43 | int index = interpolationSearch(arr, n, x); 44 | 45 | // If element was found 46 | if (index != -1) 47 | printf("Element found at index %d", index); 48 | else 49 | printf("Element not found."); 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /Calculator.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Calculator { 4 | public static void main(String[] args) { 5 | 6 | Scanner sc = new Scanner(System.in); 7 | System.out.print("Enter two numbers: "); 8 | 9 | double first = sc.nextDouble(); 10 | double second = sc.nextDouble(); 11 | 12 | System.out.print("Enter an Operation : (+, -, *, /): "); 13 | char operator = sc.next().charAt(0); 14 | 15 | double result; 16 | 17 | switch (operator) { 18 | case '+': 19 | result = first + second; 20 | break; 21 | 22 | case '-': 23 | result = first - second; 24 | break; 25 | 26 | case '*': 27 | result = first * second; 28 | break; 29 | 30 | case '/': 31 | result = first / second; 32 | break; 33 | 34 | // operator doesn't match any case constant (+, -, *, /) 35 | default: 36 | System.out.printf("Error! operator is not correct"); 37 | return; 38 | } 39 | 40 | System.out.printf("%.1f %c %.1f = %.1f", first, operator, second, result); 41 | } 42 | } -------------------------------------------------------------------------------- /insertionsort.c: -------------------------------------------------------------------------------- 1 | //C Program for Insertion Sort 2 | #include 3 | #include 4 | void insertion_sort(int a[], int size){ 5 | int i,j; 6 | 7 | for(i=0;i0) 13 | { 14 | //not in order 15 | if(a[j-1] > a[j]) 16 | { 17 | //swapping 18 | int temp = a[j-1]; 19 | a[j-1] = a[j]; 20 | a[j] = temp; 21 | } 22 | //in order 23 | else 24 | { 25 | break; 26 | } 27 | j--; 28 | } 29 | } 30 | } 31 | // Function to print an array 32 | void printArray(int A[], int size) 33 | { 34 | int i; 35 | for (i=0; i < size; i++) 36 | printf("%d ", A[i]); 37 | printf("\n"); 38 | } 39 | //Driver Program to test above functions 40 | int main() 41 | { 42 | int a[] = {16, 19, 11, 15, 10, 12, 14}; 43 | int size = sizeof(a)/sizeof(a[0]); 44 | printf("Given array is \n"); 45 | printArray(a,size); 46 | insertion_sort(a,size); 47 | printf("\nSorted array is \n"); 48 | printArray(a,size); 49 | return 0; 50 | } -------------------------------------------------------------------------------- /Java/first-non-repeating-character.java: -------------------------------------------------------------------------------- 1 | // Java program to find first 2 | // non-repeating character 3 | class GFG { 4 | static final int NO_OF_CHARS = 256; 5 | static char count[] = new char[NO_OF_CHARS]; 6 | 7 | /* calculate count of characters 8 | in the passed string */ 9 | static void getCharCountArray(String str) 10 | { 11 | for (int i = 0; i < str.length(); i++) 12 | count[str.charAt(i)]++; 13 | } 14 | 15 | /* The method returns index of first non-repeating 16 | character in a string. If all characters are repeating 17 | then returns -1 */ 18 | static int firstNonRepeating(String str) 19 | { 20 | getCharCountArray(str); 21 | int index = -1, i; 22 | 23 | for (i = 0; i < str.length(); i++) { 24 | if (count[str.charAt(i)] == 1) { 25 | index = i; 26 | break; 27 | } 28 | } 29 | 30 | return index; 31 | } 32 | 33 | // Driver method 34 | public static void main(String[] args) 35 | { 36 | String str = "geeksforgeeks"; 37 | int index = firstNonRepeating(str); 38 | 39 | System.out.println( 40 | index == -1 41 | ? "Either all characters are repeating or string " 42 | + "is empty" 43 | : "First non-repeating character is " 44 | + str.charAt(index)); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Selection Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void swap(int *xp, int *yp) 5 | { 6 | int temp = *xp; 7 | *xp = *yp; 8 | *yp = temp; 9 | } 10 | 11 | void selectionSort(int arr[], int n) 12 | { 13 | int i, j, min_idx; 14 | 15 | // One by one move boundary of unsorted subarray 16 | for (i = 0; i < n-1; i++) 17 | { 18 | // Find the minimum element in unsorted array 19 | min_idx = i; 20 | for (j = i+1; j < n; j++) 21 | if (arr[j] < arr[min_idx]) 22 | min_idx = j; 23 | 24 | // Swap the found minimum element with the first element 25 | swap(&arr[min_idx], &arr[i]); 26 | } 27 | } 28 | 29 | /* Function to print an array */ 30 | void printArray(int arr[], int size) 31 | { 32 | int i; 33 | for (i=0; i < size; i++) 34 | cout << arr[i] << " "; 35 | cout << endl; 36 | } 37 | 38 | // Driver program to test above functions 39 | int main() 40 | { 41 | int arr[] = {64, 25, 12, 22, 11}; 42 | int n = sizeof(arr)/sizeof(arr[0]); 43 | selectionSort(arr, n); 44 | cout << "Sorted array: \n"; 45 | printArray(arr, n); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /heapSort.cpp: -------------------------------------------------------------------------------- 1 | //RISHABH JHA 2 | //git-rishabh-jha 3 | //Program for Heap Sort 4 | 5 | #include 6 | 7 | using namespace std; 8 | void heapify(int arr[], int n, int i) 9 | { 10 | int largest = i; 11 | int l = 2*i + 1; 12 | int r = 2*i + 2; 13 | 14 | if (l < n && arr[l] > arr[largest]) { 15 | largest = l; 16 | } 17 | 18 | if (r < n && arr[r] > arr[largest]){ 19 | largest = r; 20 | } 21 | 22 | if (largest != i) { 23 | swap(arr[i], arr[largest]); 24 | 25 | heapify(arr, n, largest); 26 | } 27 | } 28 | 29 | void heapSort(int arr[], int n) { 30 | for (int i = n / 2 - 1; i >= 0; i--) { 31 | heapify(arr, n, i); 32 | } 33 | 34 | for (int i=n-1; i>0; i--) { 35 | swap(arr[0], arr[i]); 36 | 37 | heapify(arr, i, 0); 38 | } 39 | } 40 | 41 | void printArray(int arr[], int n) { 42 | for (int i=0; i>n; 53 | int arr[n]; 54 | cout<<"\nENTER THE ELEMENTS OF THE ARRAY: "; 55 | for(int i=0;i>arr[i]; 57 | } 58 | heapSort(arr, n); 59 | 60 | cout << "\n\t SORTED ARRAY: "; 61 | printArray(arr, n); 62 | } 63 | -------------------------------------------------------------------------------- /ReverseALinkedList.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | 5 | struct Node { 6 | int data; 7 | struct Node* next; 8 | Node(int data) 9 | { 10 | this->data = data; 11 | next = NULL; 12 | } 13 | }; 14 | 15 | struct LinkedList { 16 | Node* head; 17 | LinkedList() 18 | { 19 | head = NULL; 20 | } 21 | 22 | Node* reverse(Node* node) 23 | { 24 | if (node == NULL) 25 | return NULL; 26 | if (node->next == NULL) { 27 | head = node; 28 | return node; 29 | } 30 | Node* node1 = reverse(node->next); 31 | node1->next = node; 32 | node->next = NULL; 33 | return node; 34 | } 35 | 36 | void print() 37 | { 38 | struct Node* temp = head; 39 | while (temp != NULL) { 40 | cout << temp->data << " "; 41 | temp = temp->next; 42 | } 43 | } 44 | 45 | void push(int data) 46 | { 47 | Node* temp = new Node(data); 48 | temp->next = head; 49 | head = temp; 50 | } 51 | }; 52 | 53 | int main() 54 | { 55 | LinkedList ll; 56 | ll.push(20); 57 | ll.push(4); 58 | ll.push(15); 59 | ll.push(85); 60 | 61 | cout << "Given linked list\n"; 62 | ll.print(); 63 | 64 | ll.reverse(ll.head); 65 | 66 | cout << "\nReversed Linked list \n"; 67 | ll.print(); 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /maxTwoNumbersInAnArray: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | void main() 7 | { 8 | int a[10], i, largest = 0, second_largest = 0, pos1, pos2; 9 | int n; 10 | cout << "Enter Number of elements :"; 11 | cin>>n; 12 | for (i = 0; i> a[i]; 16 | } 17 | //Finding Largest 18 | for (i = 0; i<10; ++i) 19 | { 20 | if (a[i]>largest) 21 | { 22 | largest = a[i]; 23 | pos1 = i; 24 | } 25 | } 26 | //finding second largset 27 | for (i = 0; i<10; ++i) 28 | { 29 | if (a[i]>second_largest) 30 | { 31 | if (a[i] == largest) 32 | continue; 33 | second_largest = a[i]; 34 | pos2 = i; 35 | } 36 | } 37 | cout << "nn Largest Number :" << largest << " at position " << (pos1 + 1); 38 | cout << "nn Second Largest Number :"<< second_largest << " at position " << (pos2 + 1); 39 | 40 | getch(); 41 | return; 42 | } 43 | -------------------------------------------------------------------------------- /Linkedlist.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data = None, next=None): 3 | self.data = data 4 | self.next = next 5 | 6 | class LinkedList: 7 | def __init__(self): 8 | self.head = None 9 | def insert(self, data): 10 | newNode = Node(data) 11 | if(self.head): 12 | current = self.head 13 | while(current.next): 14 | current = current.next 15 | current.next = newNode 16 | else: 17 | self.head = newNode 18 | def printLL(self): 19 | current = self.head 20 | while(current): 21 | print(current.data) 22 | current = current.next 23 | def removeNthFromEnd(self, n): 24 | temp = self.head 25 | k=0 26 | while temp!=None: 27 | k+=1 28 | temp=temp.next 29 | pos=k-n 30 | curr_pos=0 31 | prev=None 32 | temp=self.head 33 | while curr_pos!=pos: 34 | prev=temp 35 | temp=temp.next 36 | curr_pos+=1 37 | if prev: 38 | prev.next=temp.next 39 | else: 40 | self.head=self.head.next 41 | return self.head 42 | keys = list(map(int, input().split(" "))) 43 | LL = LinkedList() 44 | for key in keys: 45 | LL.insert(key) 46 | #LL.printLL() 47 | x=int(input()) 48 | LL.removeNthFromEnd(x) 49 | LL.printLL() -------------------------------------------------------------------------------- /ReverseLinkedList.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct element 5 | { 6 | int data; 7 | struct element* next; 8 | }; 9 | 10 | typedef struct element el; 11 | typedef el* LinkedList; 12 | 13 | void push(LinkedList* l, int data) 14 | { 15 | LinkedList n = malloc(sizeof(el)); 16 | n->data = data; 17 | n->next = NULL; 18 | 19 | while (*l) 20 | { 21 | l = &(*l)->next; 22 | } 23 | 24 | *l = n; 25 | 26 | } 27 | 28 | void removeFirst(LinkedList* l) 29 | { 30 | LinkedList n; 31 | if (*l) 32 | { 33 | n = *l; 34 | *l = (*l)->next; 35 | 36 | free(n); 37 | } 38 | } 39 | 40 | void print(LinkedList l) 41 | { 42 | LinkedList tmp = l; 43 | 44 | while (tmp) 45 | { 46 | printf("%d->", tmp->data); 47 | tmp = tmp->next; 48 | } 49 | 50 | printf("[]\n"); 51 | } 52 | 53 | void reverse(LinkedList* l) 54 | { 55 | LinkedList prev, current, next; 56 | current = next = *l; 57 | prev = NULL; 58 | 59 | while (current) 60 | { 61 | next = current->next; 62 | current->next = prev; 63 | prev = current; 64 | current = next; 65 | } 66 | 67 | *l = prev; 68 | } 69 | 70 | int main() 71 | { 72 | LinkedList l = NULL; 73 | 74 | push(&l, 1); 75 | push(&l, 2); 76 | push(&l, 3); 77 | 78 | print(l); 79 | reverse(&l); 80 | print(l); 81 | 82 | return EXIT_SUCCESS; 83 | } 84 | -------------------------------------------------------------------------------- /matrixMultiplication.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int m, n, p, q, c, d, k, sum = 0; 6 | int first[10][10], second[10][10], multiply[10][10]; 7 | 8 | printf("Enter number of rows and columns of first matrix\n"); 9 | scanf("%d%d", &m, &n); 10 | printf("Enter elements of first matrix\n"); 11 | 12 | for (c = 0; c < m; c++) 13 | for (d = 0; d < n; d++) 14 | scanf("%d", &first[c][d]); 15 | 16 | printf("Enter number of rows and columns of second matrix\n"); 17 | scanf("%d%d", &p, &q); 18 | 19 | if (n != p) 20 | printf("The multiplication isn't possible.\n"); 21 | else 22 | { 23 | printf("Enter elements of second matrix\n"); 24 | 25 | for (c = 0; c < p; c++) 26 | for (d = 0; d < q; d++) 27 | scanf("%d", &second[c][d]); 28 | 29 | for (c = 0; c < m; c++) { 30 | for (d = 0; d < q; d++) { 31 | for (k = 0; k < p; k++) { 32 | sum = sum + first[c][k]*second[k][d]; 33 | } 34 | 35 | multiply[c][d] = sum; 36 | sum = 0; 37 | } 38 | } 39 | 40 | printf("Product of the matrices:\n"); 41 | 42 | for (c = 0; c < m; c++) { 43 | for (d = 0; d < q; d++) 44 | printf("%d\t", multiply[c][d]); 45 | 46 | printf("\n"); 47 | } 48 | } 49 | 50 | return 0; 51 | } -------------------------------------------------------------------------------- /PalindromeRange: -------------------------------------------------------------------------------- 1 | import java.util.HashSet; 2 | import java.util.Scanner; 3 | import java.util.Set; 4 | 5 | public class PalindromeRange { 6 | private static final Set PALINDROMIC_NUMBERS = new HashSet<>(); 7 | private static final Set NON_PALINDROMIC_NUMBERS = new HashSet<>(); 8 | 9 | public static void main(String[] args) { 10 | Scanner scanner = new Scanner(System.in); 11 | int totalTests = scanner.nextInt(); 12 | int lowerLimit = 0; 13 | int upperLimit = 0; 14 | 15 | for (int i = 0; i < totalTests; i++) { 16 | lowerLimit = scanner.nextInt(); 17 | upperLimit = scanner.nextInt(); 18 | for (int j = lowerLimit; j <= upperLimit; j++) { 19 | if (!NON_PALINDROMIC_NUMBERS.contains(j)) { 20 | if (PALINDROMIC_NUMBERS.contains(j) || isPalindromicNumber(j)) { 21 | System.out.print(j + " "); 22 | PALINDROMIC_NUMBERS.add(j); 23 | } else { 24 | NON_PALINDROMIC_NUMBERS.add(j); 25 | } 26 | } 27 | } 28 | } 29 | System.out.println(); 30 | } 31 | 32 | public static boolean isPalindromicNumber(int x) { 33 | if (x < 0 || (x % 10 == 0 && x != 0)) { 34 | return false; 35 | } 36 | int revNum = 0; 37 | while (revNum < x) { 38 | revNum = revNum * 10 + x % 10; 39 | x /= 10; 40 | } 41 | return x == revNum || x == revNum / 10; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /heightOfBinaryTree.java: -------------------------------------------------------------------------------- 1 | // Java program to find height of tree 2 | 3 | // A binary tree node 4 | class Node 5 | { 6 | int data; 7 | Node left, right; 8 | 9 | Node(int item) 10 | { 11 | data = item; 12 | left = right = null; 13 | } 14 | } 15 | 16 | class BinaryTree 17 | { 18 | Node root; 19 | 20 | /* Compute the "maxDepth" of a tree -- the number of 21 | nodes along the longest path from the root node 22 | down to the farthest leaf node.*/ 23 | int maxDepth(Node node) 24 | { 25 | if (node == null) 26 | return 0; 27 | else 28 | { 29 | /* compute the depth of each subtree */ 30 | int lDepth = maxDepth(node.left); 31 | int rDepth = maxDepth(node.right); 32 | 33 | /* use the larger one */ 34 | if (lDepth > rDepth) 35 | return (lDepth + 1); 36 | else 37 | return (rDepth + 1); 38 | } 39 | } 40 | 41 | /* Driver program to test above functions */ 42 | public static void main(String[] args) 43 | { 44 | BinaryTree tree = new BinaryTree(); 45 | 46 | tree.root = new Node(1); 47 | tree.root.left = new Node(2); 48 | tree.root.right = new Node(3); 49 | tree.root.left.left = new Node(4); 50 | tree.root.left.right = new Node(5); 51 | 52 | System.out.println("Height of tree is : " + 53 | tree.maxDepth(tree.root)); 54 | } 55 | } 56 | 57 | -------------------------------------------------------------------------------- /Java/BinarySearch.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | 7 | /** 8 | * 9 | * @author a 10 | */ 11 | public class BinarySearch { 12 | int binarySearch(int arr[], int l, int r, int x) 13 | { 14 | if (r >= l) { 15 | int mid = l + (r - l) / 2; 16 | 17 | 18 | if (arr[mid] == x) 19 | return mid; 20 | 21 | 22 | if (arr[mid] > x) 23 | return binarySearch(arr, l, mid - 1, x); 24 | 25 | 26 | return binarySearch(arr, mid + 1, r, x); 27 | } 28 | 29 | 30 | return -1; 31 | } 32 | 33 | // Driver method to test above 34 | public static void main(String args[]) 35 | { 36 | BinarySearch ob = new BinarySearch(); 37 | int arr[] = { 2, 7, 4, 18, 90 }; 38 | int n = arr.length; 39 | int x = 18; 40 | int result = ob.binarySearch(arr, 0, n - 1, x); 41 | if (result == -1) 42 | System.out.println("Element not present"); 43 | else 44 | System.out.println("Element found at index " + result); 45 | } 46 | } 47 | 48 | -------------------------------------------------------------------------------- /heapsort.py: -------------------------------------------------------------------------------- 1 | # Python program for implementation of heap Sort 2 | 3 | # To heapify subtree rooted at index i. 4 | # n is size of heap 5 | def heapify(arr, n, i): 6 | largest = i # Initialize largest as root 7 | l = 2 * i + 1 # left = 2*i + 1 8 | r = 2 * i + 2 # right = 2*i + 2 9 | 10 | # See if left child of root exists and is 11 | # greater than root 12 | if l < n and arr[i] < arr[l]: 13 | largest = l 14 | 15 | # See if right child of root exists and is 16 | # greater than root 17 | if r < n and arr[largest] < arr[r]: 18 | largest = r 19 | 20 | # Change root, if needed 21 | if largest != i: 22 | arr[i],arr[largest] = arr[largest],arr[i] # swap 23 | 24 | # Heapify the root. 25 | heapify(arr, n, largest) 26 | 27 | # The main function to sort an array of given size 28 | def heapSort(arr): 29 | n = len(arr) 30 | 31 | # Build a maxheap. 32 | for i in range(n//2 - 1, -1, -1): 33 | heapify(arr, n, i) 34 | 35 | # One by one extract elements 36 | for i in range(n-1, 0, -1): 37 | arr[i], arr[0] = arr[0], arr[i] # swap 38 | heapify(arr, i, 0) 39 | 40 | # Driver code to test above 41 | arr = [ 12, 11, 13, 5, 6, 7] 42 | heapSort(arr) 43 | n = len(arr) 44 | print ("Sorted array is") 45 | for i in range(n): 46 | print ("%d" %arr[i]), 47 | # This code is contributed by Mohit Kumra 48 | -------------------------------------------------------------------------------- /insertInBST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node { 5 | public: 6 | int data; 7 | node* left, *right; 8 | 9 | node(int d) { 10 | this->data = d; 11 | left = NULL; 12 | right = NULL; 13 | } 14 | }; 15 | 16 | node* construct(node* root, int data) { 17 | if (root == NULL) { 18 | node* root = new node(data); 19 | return root; 20 | } 21 | 22 | if (data > root->data) { 23 | root->right = construct(root->right, data); 24 | } else { 25 | root->left = construct(root->left, data); 26 | } 27 | 28 | return root; 29 | } 30 | 31 | node* constructIteratively(node* root, int data) { 32 | node* q = new node(data); 33 | if (root == NULL) { 34 | return q; 35 | } 36 | node* p = root; 37 | while (true) { 38 | if (data < p->data) { 39 | if (p->left == NULL) { 40 | p->left = q; 41 | break; 42 | } 43 | p = p->left; 44 | } else { 45 | if (p->right == NULL) { 46 | p->right = q; 47 | break; 48 | } 49 | p = p->right; 50 | } 51 | } 52 | return root; 53 | } 54 | 55 | node* buildTree() { 56 | 57 | int data; 58 | cin >> data; 59 | 60 | node* root = NULL; 61 | 62 | while (data != -1) { 63 | root = constructIteratively(root, data); 64 | cin >> data; 65 | } 66 | 67 | return root; 68 | } 69 | 70 | int main() { 71 | 72 | node *root = buildTree(); 73 | 74 | return 0; 75 | } -------------------------------------------------------------------------------- /Count Good Triplets.java: -------------------------------------------------------------------------------- 1 | Ques: 2 | Given an array of integers arr, and three integers a, b and c. You need to find the number of good triplets. 3 | 4 | A triplet (arr[i], arr[j], arr[k]) is good if the following conditions are true: 5 | 6 | 0 <= i < j < k < arr.length 7 | |arr[i] - arr[j]| <= a 8 | |arr[j] - arr[k]| <= b 9 | |arr[i] - arr[k]| <= c 10 | Where |x| denotes the absolute value of x. 11 | 12 | Return the number of good triplets. 13 | 14 | 15 | 16 | Example 1: 17 | 18 | Input: arr = [3,0,1,1,9,7], a = 7, b = 2, c = 3 19 | Output: 4 20 | Explanation: There are 4 good triplets: [(3,0,1), (3,0,1), (3,1,1), (0,1,1)]. 21 | Example 2: 22 | 23 | Input: arr = [1,1,2,2,3], a = 0, b = 0, c = 1 24 | Output: 0 25 | Explanation: No triplet satisfies all conditions. 26 | 27 | Soln: 28 | class Solution { 29 | public int countGoodTriplets(int[] arr, int a, int b, int c) { 30 | int count=0; 31 | for(int i =0; ia) 34 | continue; 35 | for(int k=j+1;k 3 | 4 | void main(){ 5 | 6 | int sum,diff,mul,div; 7 | 8 | int a,b; 9 | printf("Enter the first number \n"); 10 | scanf("%d",&a); 11 | printf("Enter the second number \n"); 12 | scanf("%d",&b); 13 | printf("**************************\n"); 14 | printf("Operations\n"); 15 | printf("**************************\n"); 16 | printf("Addition +\"); 17 | printf("Subtraction -\n"); 18 | printf("Multiplication *\n"); 19 | printf("Division / \n"); 20 | 21 | printf("Please enter the character corresponding to the operation \n"); 22 | scanf("%c",&c); 23 | 24 | switch(c) 25 | { 26 | case ('+'): 27 | sum=a+b; 28 | printf("The sum of the numbers is %d\n",sum); 29 | break; 30 | case('-'): 31 | diff=a-b; 32 | printf("The difference of the numbers is %d\n",diff); 33 | break; 34 | printf("The sum of the numbers is %d\n",sum); > 35 | case('*'): 36 | mul=a*b; 37 | printf("The product of the numbers is %d\n",mul); 38 | break; 39 | 40 | case('/'): 41 | div=a/b; 42 | printf("The quotient of the division is %d \n",div); 43 | break; 44 | 45 | default : 46 | printf("Please enter the valid character from the list\n"); 47 | break; 48 | } 49 | 50 | } -------------------------------------------------------------------------------- /quicksort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | void swap(int* a, int* b) 6 | { 7 | int t = *a; 8 | *a = *b; 9 | *b = t; 10 | } 11 | 12 | 13 | int partition (int arr[], int low, int high) 14 | { 15 | int pivot = arr[high]; 16 | int i = (low - 1); 17 | 18 | for (int j = low; j <= high - 1; j++) 19 | { 20 | 21 | if (arr[j] < pivot) 22 | { 23 | i++; 24 | swap(&arr[i], &arr[j]); 25 | } 26 | } 27 | swap(&arr[i + 1], &arr[high]); 28 | return (i + 1); 29 | } 30 | 31 | 32 | void quickSort(int arr[], int low, int high) 33 | { 34 | if (low < high) 35 | { 36 | 37 | int pi = partition(arr, low, high); 38 | 39 | 40 | quickSort(arr, low, pi - 1); 41 | quickSort(arr, pi + 1, high); 42 | } 43 | } 44 | 45 | void printArray(int arr[], int size) 46 | { 47 | int i; 48 | for (i = 0; i < size; i++) 49 | cout << arr[i] << " "; 50 | cout << endl; 51 | } 52 | 53 | 54 | int main() 55 | { 56 | cout << "Enter the number of elements in the array" << endl; 57 | int n,i; 58 | cin >> n; 59 | int arr[n+1]; 60 | cout << "Enter the elements in the array" << endl; 61 | for(i=0;i> arr[i]; 63 | quickSort(arr, 0, n - 1); 64 | cout << "Sorted array: \n"; 65 | printArray(arr, n); 66 | return 0; 67 | } 68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /triangle patterns.cpp: -------------------------------------------------------------------------------- 1 | #include "iostream" 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | int menu,n,i,j,k,a; 7 | char repeat='y'; 8 | while(repeat=='y'){ 9 | cout<<"this program will create a triangular pattern with n as it's height"<>menu; 11 | 12 | switch(int(menu)){ 13 | case 1: { 14 | cout<<"give n: ";cin>>n; 15 | for (i=0;i=i;--k){ 17 | cout<<" "; 18 | } 19 | for (j=0;j<=i;++j){ 20 | if(j==0){ 21 | a=1; 22 | } 23 | else{ 24 | a=a*(i+1-j)/j; 25 | } 26 | cout<<" "; 27 | cout<>n; 36 | for(i=1;i<=n;++i){ 37 | for(k=n;k>=i;--k){ 38 | cout<<" "; 39 | } 40 | for (j=1;j<=i+(i-1);++j){ 41 | cout<<"^"; 42 | } 43 | cout<>n; 49 | for(i=1;i<=n;++i){ 50 | for(k=n;k>=i;--k){ 51 | cout<<" "; 52 | } 53 | for (j=1;j<=i+i;++j){ 54 | cout<<"^"; 55 | } 56 | cout<>repeat; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Area of Rectangle, Square and Circle using switch case.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void area_of_rectangle(void); 4 | void area_of_square(void); 5 | void area_of_circle(void); 6 | void main() 7 | { 8 | int ch; 9 | while(1) 10 | { 11 | printf("1.Area of Rectangle\n"); 12 | printf("2.Area of Square\n"); 13 | printf("3.Area of Circle\n"); 14 | printf("4.Quit\n"); 15 | printf("Enter your choice: "); 16 | scanf("%d",&ch); 17 | 18 | switch(ch) 19 | { 20 | case 1: area_of_rectangle(); 21 | break; 22 | case 2: area_of_square(); 23 | break; 24 | case 3: area_of_circle(); 25 | break; 26 | case 4: exit(1); 27 | break; 28 | default: printf("Enter choice is invalid!\n\n"); 29 | } 30 | } 31 | } 32 | void area_of_rectangle() 33 | { 34 | float l , b , a; 35 | printf("Enter length of the rectangle: "); 36 | scanf("%f",&l); 37 | printf("Enter breadth of the rectangle: "); 38 | scanf("%f",&b); 39 | a = l * b; 40 | printf("Area of the rectangle is : %f\n\n",a); 41 | 42 | } 43 | void area_of_square() 44 | { 45 | float l , a; 46 | printf("Enter the length of one side of square: "); 47 | scanf("%f",&l); 48 | a = l * l; 49 | printf("Area of the square is : %f\n\n",a); 50 | } 51 | void area_of_circle() 52 | { 53 | float r , a; 54 | printf("Enter the radius of the circle: "); 55 | scanf("%f",&r); 56 | a = 3.14 * r * r; 57 | printf("Area of the circle is : %f\n\n",a); 58 | } 59 | -------------------------------------------------------------------------------- /circular-linked-list.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /* 3 | #148 - Problem Domain: check if a linked list is a circular linked list 4 | using Floyd's Algorithm (tortoise and hare approach), return true if they meet, false if not 5 | */ 6 | 7 | function findCircular(linkedlist) { 8 | 9 | let tortoise = linkedlist.head; 10 | let hare = linkedlist.head; 11 | 12 | if (linkedlist.head.next === null) { 13 | return false; 14 | } 15 | while (hare.next && hare.next.next) { 16 | tortoise = tortoise.next; 17 | hare = hare.next.next; 18 | if (tortoise === hare) { 19 | return true; 20 | } 21 | } 22 | return false; 23 | 24 | } 25 | 26 | // Establishing LinkedList and Node Classes 27 | class Node { 28 | constructor(value, next = null) { 29 | this.value = value; 30 | this.next = next; 31 | } 32 | } 33 | 34 | class LinkedList { 35 | constructor() { 36 | this.head = null; 37 | } 38 | 39 | insert(val) { 40 | this.head = new Node(val, this.head); 41 | } 42 | 43 | append(val) { 44 | let current = this.head; 45 | while (current) { 46 | current = curent.next; 47 | } 48 | current.next = new Node(val) 49 | } 50 | 51 | // method to create a circular linked list 52 | makeCirc(val, existingNode) { 53 | let node = new Node(val, existingNode); 54 | let current = this.head; 55 | while (current.next) { 56 | current = current.next 57 | } 58 | current.next = node; 59 | } 60 | } 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /Java/Rotate90.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Rotate90 3 | { 4 | public static void main(String[]args) 5 | { 6 | Scanner sc=new Scanner(System.in); 7 | System.out.println("Input the number of rows or columns in the matrix"); 8 | int m=sc.nextInt(); 9 | if(m>=2&&m<=20) 10 | { 11 | int a[][]=new int[m][m]; 12 | System.out.println("Input the elements in the matrix"); 13 | for(int x=0;x 2 | using namespace std; 3 | int makeAnagram(string str1, string str2){ 4 | int n1 = str1.length(); 5 | int n2 = str2.length(); 6 | unordered_mapumap1; 7 | unordered_mapumap2; 8 | for(int i=0; i::iterator it; 17 | for(it=umap1.begin(); it!=umap1.end(); it++) 18 | { 19 | if(umap2.find(it->first)!=umap2.end()) 20 | { 21 | if(umap2[it->first]>=it->second) 22 | { 23 | umap2[it->first] = umap2[it->first]-it->second; 24 | umap1[it->first] = 0; 25 | } 26 | else 27 | { 28 | umap1[it->first] = it->second - umap2[it->first]; 29 | umap2[it->first] = 0; 30 | } 31 | } 32 | } 33 | int count=0; 34 | for(it=umap1.begin(); it!=umap1.end(); it++) 35 | { 36 | count+=it->second; 37 | } 38 | for(it=umap2.begin(); it!=umap2.end(); it++) 39 | { 40 | count+=it->second; 41 | } 42 | return count; 43 | } 44 | 45 | int main(){ 46 | string s1, s2; 47 | cout<<"Enter string 1 - "; 48 | cin>>s1; 49 | cout<<"Enter string 2 - "; 50 | cin>>s2; 51 | cout<<"Number of deletions required to make both strings anagram - "< 5 | #include 6 | 7 | #define MAX_SIZE 10 8 | 9 | typedef struct 10 | { 11 | int stack[MAX_SIZE]; 12 | int top; 13 | } opstack; 14 | 15 | void push(opstack *p, int num){ 16 | if(p -> top == MAX_SIZE){ 17 | printf("Stack full\n"); 18 | } 19 | (p -> top)++; 20 | p -> stack[p -> top] = num; 21 | } 22 | 23 | int pop(opstack *p){ 24 | return(p -> stack[p -> top--]); 25 | } 26 | 27 | int isDigit(char x){ 28 | if(x>='0' && x<='9') 29 | return 1; 30 | return 0; 31 | } 32 | 33 | int evaluate(char op, int op1, int op2){ 34 | switch(op){ 35 | case '+': 36 | return op1+op2; 37 | case '-': 38 | return op1-op2; 39 | case '*': 40 | return op1*op2; 41 | case '/': 42 | return op1/op2; 43 | case '%': 44 | return op1%op2; 45 | case '^': 46 | return pow(op1,op2); 47 | } 48 | 49 | } 50 | 51 | int main(int argc, char const *argv[]) 52 | { 53 | char exp[10]; 54 | char x; 55 | int i = 0; 56 | printf("Enter a prefix expression: \n"); 57 | while((x = getchar())!='\n'){ 58 | exp[i++] = x; 59 | } 60 | opstack s; 61 | s.top = -1; 62 | for(int j = i-1; j>=0; j--){ 63 | char ch = exp[j]; 64 | if(isDigit(ch)){ 65 | push(&s,ch-'0'); 66 | } 67 | else{ 68 | int op1 = pop(&s); 69 | int op2 = pop(&s); 70 | int value = evaluate(ch,op1,op2); 71 | push(&s,value); 72 | } 73 | } 74 | printf("The value is: %d\n",pop(&s)); 75 | return 0; 76 | } -------------------------------------------------------------------------------- /array_implementation_of_queue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int front=0, rear=-1, i, n; 4 | void enqueue(int a[100], int element) 5 | { 6 | if((rear!=-1 && ((rear+1)%n)==front)) 7 | { 8 | printf("-2\n"); 9 | return; 10 | } 11 | else 12 | { 13 | 14 | if(rear==n-1) 15 | { 16 | rear=-1; 17 | } 18 | rear++; 19 | a[rear]=element; 20 | return; 21 | } 22 | } 23 | int dequeue(int a[100]) 24 | { 25 | int num2; 26 | if(front==0 && rear==-1) 27 | { 28 | num2 = -1; 29 | return num2; 30 | } 31 | else if(front==rear) 32 | { 33 | num2 = a[front]; 34 | front=0; 35 | rear=-1; 36 | return num2; 37 | } 38 | else 39 | { 40 | num2 = a[front]; 41 | front++; 42 | if(front==n) 43 | front=0; 44 | return num2; 45 | } 46 | } 47 | void print_queue(int a[100]); 48 | int main() 49 | { 50 | int num, num2, a[100]; 51 | scanf("%d", &n); 52 | char ch; 53 | label:ch = getchar(); 54 | switch(ch) 55 | { 56 | case 'e' : scanf("%d", &num); 57 | enqueue(a, num); 58 | break; 59 | 60 | case 'd' : num2 = dequeue(a); 61 | printf("%d\n", num2); 62 | break; 63 | case 'p' : print_queue(a); 64 | printf("\n"); 65 | break; 66 | } 67 | if(ch!='s') 68 | goto label; 69 | return 0; 70 | } 71 | void print_queue(int a[100]) 72 | { 73 | if((front==0 && rear==-1)) 74 | { 75 | printf("-1"); 76 | return; 77 | } 78 | for(i=front;;i++) 79 | { 80 | if(i==n) 81 | i=0; 82 | printf("%d ", a[i]); 83 | if(i==rear) 84 | break; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /Linked List via C/Code to search an element in circular Linked List.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node{ 5 | int data; 6 | struct node *next; 7 | }; 8 | struct node *start, *current, *temp, *prev; 9 | 10 | //function to create the Linked List 11 | void create() 12 | { 13 | start=(struct node*)malloc(sizeof(struct node)); 14 | start->data=NULL; 15 | start->next=NULL; 16 | temp=start; 17 | } 18 | 19 | 20 | //function to add nodes to the Linked List 21 | void create_node() 22 | { 23 | current=(struct node*)malloc(sizeof(struct node)); 24 | printf("Enter the value of node to add:"); 25 | scanf("%d",¤t->data); 26 | temp->next=current; 27 | current->next=start; 28 | temp=current; 29 | } 30 | 31 | //function to search whether element present in Linked List or not 32 | void search() 33 | { 34 | int pos=0; 35 | int to_search; 36 | int found=0; 37 | printf("Enter the number to be searched: "); 38 | scanf("%d",&to_search); 39 | temp=start; 40 | while(temp->next!=start) 41 | { 42 | if(temp->data!=to_search){ 43 | temp=temp->next; 44 | pos++; 45 | } 46 | else 47 | { 48 | break;} 49 | } 50 | 51 | if(pos!=0) 52 | printf("Number found in the List at %d position",pos); 53 | else 54 | printf("Number not found in the List"); 55 | 56 | } 57 | 58 | int main() 59 | { 60 | create(); 61 | create_node(); 62 | create_node(); 63 | create_node(); 64 | create_node(); 65 | search(); 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /Linked List via C/Linked Stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node{ 5 | int data; 6 | struct node *next; 7 | }; 8 | struct node *start, *current, *temp, *prev,*top; 9 | 10 | //function to check if the List is empty or not 11 | bool isEmpty() 12 | { 13 | if(start->next==NULL) 14 | return true; 15 | else 16 | return false; 17 | } 18 | 19 | //function to create the Linked List 20 | void create() 21 | { 22 | start=(struct node*)malloc(sizeof(struct node)); 23 | start->data=NULL; 24 | start->next=NULL; 25 | temp=start; 26 | } 27 | 28 | 29 | //function to add nodes to the Linked List 30 | void push() 31 | { 32 | current=(struct node*)malloc(sizeof(struct node)); 33 | printf("Enter the value of node to add:"); 34 | scanf("%d",¤t->data); 35 | temp->next=current; 36 | current->next=NULL; 37 | temp=current; 38 | top=current; 39 | } 40 | 41 | //function to delete the last node 42 | void pop() 43 | { 44 | if(isEmpty()) 45 | printf("\nThe List is Empty\n"); 46 | else 47 | { 48 | prev=start; 49 | temp=start; 50 | while(temp->next!=NULL) 51 | { 52 | prev=temp; 53 | temp=temp->next; 54 | } 55 | prev->next=NULL; 56 | top=prev; 57 | printf("Poping Value %d\n",temp->data); 58 | } 59 | } 60 | 61 | int main() 62 | { 63 | create(); 64 | for(int i=0;i<10;i++) 65 | push(); 66 | for(int i=0;i<5;i++) 67 | pop(); 68 | printf("Top of Stack has the Value: %d",top->data); 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /Round_Robin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | class tll{ 6 | public: 7 | long P; 8 | long At; 9 | long Bt; 10 | tll(){ 11 | P=0; 12 | At=0; 13 | Bt=0; 14 | } 15 | }; 16 | class ll{ 17 | public: 18 | long p; 19 | long at; 20 | long bt; 21 | }; 22 | bool comp(ll a,ll b){ 23 | if(a.at>b.at){ 24 | return false; 25 | } 26 | return false; 27 | } 28 | int main(){ 29 | bool bbb=true; 30 | long n[1]; 31 | long times=1; 32 | long extremetime=99999; 33 | cin>>n[0]; 34 | ll *a=new ll[n[0]]; 35 | tll *tt=new tll[n[0]]; 36 | bool bb=false; 37 | long tsize=n[0],ct=0; 38 | ll temp2; 39 | temp2.bt=0; 40 | long i=0; 41 | while(i>a[i].p; 43 | cin>>a[i].at; 44 | cin>>a[i].bt; 45 | 46 | 47 | tt[i].Bt=a[i].bt; 48 | tt[i].At=a[i].at; 49 | tt[i].P=a[i].p; 50 | i=i+1; 51 | } 52 | sort(a,a+n[0],comp); 53 | long temp=0,count,time; 54 | long kk=0; 55 | while(kk0){ 67 | t--; 68 | } 69 | } 70 | time=a[i].at; 71 | if(time<=ct&&a[i].bt>0){ 72 | a[i].bt=a[i].bt-1; 73 | cout< 0 and mstSet[v] == False and key[v] > self.graph[u][v]: 44 | key[v] = self.graph[u][v] 45 | parent[v] = u 46 | 47 | self.printMST(parent) 48 | 49 | g = Graph(5) 50 | g.graph = [ [0, 2, 0, 6, 0], 51 | [2, 0, 3, 8, 5], 52 | [0, 3, 0, 0, 7], 53 | [6, 8, 0, 0, 9], 54 | [0, 5, 7, 9, 0]] 55 | 56 | g.primMST(); -------------------------------------------------------------------------------- /circular-linked-list.java: -------------------------------------------------------------------------------- 1 | // Java program to check if 2 | // linked list is circular 3 | import java.util.*; 4 | 5 | class CircularLinkedList 6 | { 7 | 8 | /* Link list Node */ 9 | static class Node 10 | { 11 | int data; 12 | Node next; 13 | } 14 | 15 | /*This function returns true if given linked 16 | list is circular, else false. */ 17 | static boolean isCircular( Node head) 18 | { 19 | // An empty linked list is circular 20 | if (head == null) 21 | return true; 22 | 23 | // Next of head 24 | Node node = head.next; 25 | 26 | // This loop would stop in both cases (1) If 27 | // Circular (2) Not circular 28 | while (node != null && node != head) 29 | node = node.next; 30 | 31 | // If loop stopped because of circular 32 | // condition 33 | return (node == head); 34 | } 35 | 36 | // Utility function to create a new node. 37 | static Node newNode(int data) 38 | { 39 | Node temp = new Node(); 40 | temp.data = data; 41 | temp.next = null; 42 | return temp; 43 | } 44 | 45 | /* Driver code*/ 46 | public static void main(String args[]) 47 | { 48 | /* Start with the empty list */ 49 | Node head = newNode(1); 50 | head.next = newNode(2); 51 | head.next.next = newNode(3); 52 | head.next.next.next = newNode(4); 53 | 54 | System.out.print(isCircular(head)? "Yes\n" : 55 | "No\n" ); 56 | 57 | // Making linked list circular 58 | head.next.next.next.next = head; 59 | 60 | System.out.print(isCircular(head)? "Yes\n" : 61 | "No\n" ); 62 | 63 | } 64 | } -------------------------------------------------------------------------------- /Fractional_knapack_greedy.py: -------------------------------------------------------------------------------- 1 | # Python3 program to solve fractional 2 | # Knapsack Problem 3 | class ItemValue: 4 | 5 | def __init__(self, wt, val, ind): 6 | self.wt = wt 7 | self.val = val 8 | self.ind = ind 9 | self.cost = val // wt 10 | 11 | def __lt__(self, other): 12 | return self.cost < other.cost 13 | 14 | # Greedy Approach 15 | 16 | class FractionalKnapSack: 17 | """Time Complexity O(n log n)""" 18 | 19 | @staticmethod 20 | def getMaxValue(wt, val, capacity): 21 | 22 | """function to get maximum value """ 23 | iVal = [] 24 | for i in range(len(wt)): 25 | iVal.append(ItemValue(wt[i], val[i], i)) 26 | 27 | # sorting items by value 28 | iVal.sort(reverse=True) 29 | 30 | totalValue = 0 31 | for i in iVal: 32 | curWt = int(i.wt) 33 | curVal = int(i.val) 34 | if capacity - curWt >= 0: 35 | capacity -= curWt 36 | totalValue += curVal 37 | else: 38 | fraction = capacity / curWt 39 | totalValue += curVal * fraction 40 | capacity = int(capacity - (curWt * fraction)) 41 | break 42 | return totalValue 43 | 44 | # Driver Code 45 | 46 | if __name__ == "__main__": 47 | wt = [10, 40, 20, 30] 48 | val = [60, 40, 100, 120] 49 | capacity = 50 50 | 51 | maxValue = FractionalKnapSack.getMaxValue(wt, val, capacity) 52 | print("Maximum value in Knapsack =", maxValue) 53 | 54 | -------------------------------------------------------------------------------- /Java/Count.java: -------------------------------------------------------------------------------- 1 | // Java prorgam to count frequencies of 2 | // characters in string using Hashmap 3 | import java.io.*; 4 | import java.util.*; 5 | class Count{ 6 | static void characterCount(String inputString) 7 | { 8 | // Creating a HashMap containing char 9 | // as a key and occurrences as a value 10 | HashMap charCountMap 11 | = new HashMap(); 12 | 13 | // Converting given string to char array 14 | 15 | char[] strArray = inputString.toCharArray(); 16 | 17 | // checking each char of strArray 18 | for (char c : strArray) { 19 | if (charCountMap.containsKey(c)) { 20 | 21 | // If char is present in charCountMap, 22 | // incrementing it's count by 1 23 | charCountMap.put(c, charCountMap.get(c) + 1); 24 | } 25 | else { 26 | 27 | // If char is not present in charCountMap, 28 | // putting this char to charCountMap with 1 as it's value 29 | charCountMap.put(c, 1); 30 | } 31 | } 32 | 33 | // Printing the charCountMap 34 | for (Map.Entry entry : charCountMap.entrySet()) { 35 | System.out.println(entry.getKey() + " " + entry.getValue()); 36 | } 37 | } 38 | 39 | // Driver Code 40 | public static void main(String[] args) 41 | { Scanner sc=new Scanner(System.in); 42 | 43 | String str=sc.nextLine();; 44 | characterCount(str); 45 | } 46 | } -------------------------------------------------------------------------------- /Java/identical-trees.java: -------------------------------------------------------------------------------- 1 | // Java program to see if two trees are identical 2 | 3 | // A binary tree node 4 | class Node 5 | { 6 | int data; 7 | Node left, right; 8 | 9 | Node(int item) 10 | { 11 | data = item; 12 | left = right = null; 13 | } 14 | } 15 | 16 | class BinaryTree 17 | { 18 | Node root1, root2; 19 | 20 | /* Given two trees, return true if they are 21 | structurally identical */ 22 | boolean identicalTrees(Node a, Node b) 23 | { 24 | /*1. both empty */ 25 | if (a == null && b == null) 26 | return true; 27 | 28 | /* 2. both non-empty -> compare them */ 29 | if (a != null && b != null) 30 | return (a.data == b.data 31 | && identicalTrees(a.left, b.left) 32 | && identicalTrees(a.right, b.right)); 33 | 34 | /* 3. one empty, one not -> false */ 35 | return false; 36 | } 37 | 38 | /* Driver program to test identicalTrees() function */ 39 | public static void main(String[] args) 40 | { 41 | BinaryTree tree = new BinaryTree(); 42 | 43 | tree.root1 = new Node(1); 44 | tree.root1.left = new Node(2); 45 | tree.root1.right = new Node(3); 46 | tree.root1.left.left = new Node(4); 47 | tree.root1.left.right = new Node(5); 48 | 49 | tree.root2 = new Node(1); 50 | tree.root2.left = new Node(2); 51 | tree.root2.right = new Node(3); 52 | tree.root2.left.left = new Node(4); 53 | tree.root2.left.right = new Node(5); 54 | 55 | if (tree.identicalTrees(tree.root1, tree.root2)) 56 | System.out.println("Both trees are identical"); 57 | else 58 | System.out.println("Trees are not identical"); 59 | 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /HouseRobber.md: -------------------------------------------------------------------------------- 1 | # Problem Statement 2 | 3 | You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night. 4 | 5 | Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police 6 | 7 | ``` 8 | Input: [1,2,3,1] 9 | Output: 4 10 | Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). 11 | Total amount you can rob = 1 + 3 = 4. 12 | ``` 13 | 14 | ``` 15 | Input: [2,7,9,3,1] 16 | Output: 12 17 | Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). 18 | Total amount you can rob = 2 + 9 + 1 = 12. 19 | ``` 20 | 21 | 22 | 23 | # Solution in Java 24 | ```Java 25 | class Solution { 26 | public int rob(int[] nums) { 27 | if(nums.length == 0) return 0; 28 | int[] dp = new int[nums.length]; 29 | dp[0] = nums[0]; //only one house 30 | if(nums.length == 1) return dp[0]; 31 | dp[1] = Math.max(nums[0], nums[1]); //two houses 32 | for(int i = 2; i < nums.length; i++){ 33 | dp[i] = Math.max(nums[i] + dp[i-2], dp[i-1]); 34 | //either rob or skip the current house. 35 | } 36 | return dp[nums.length - 1]; 37 | } 38 | } 39 | ``` 40 | 41 | [Link to Problem](https://leetcode.com/problems/house-robber/) 42 | -------------------------------------------------------------------------------- /TopologicalSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class Graph { 7 | int V; 8 | 9 | list* adj; 10 | 11 | 12 | void topologicalSortUtil(int v, bool visited[], stack& Stack); 13 | 14 | public: 15 | Graph(int V); 16 | 17 | 18 | void addEdge(int v, int w); 19 | 20 | 21 | void topologicalSort(); 22 | }; 23 | 24 | Graph::Graph(int V) 25 | { 26 | this->V = V; 27 | adj = new list[V]; 28 | } 29 | 30 | void Graph::addEdge(int v, int w) 31 | { 32 | adj[v].push_back(w); 33 | } 34 | 35 | void Graph::topologicalSortUtil(int v, bool visited[], 36 | stack& Stack) 37 | { 38 | 39 | visited[v] = true; 40 | 41 | 42 | list::iterator i; 43 | for (i = adj[v].begin(); i != adj[v].end(); ++i) 44 | if (!visited[*i]) 45 | topologicalSortUtil(*i, visited, Stack); 46 | 47 | 48 | Stack.push(v); 49 | } 50 | 51 | void Graph::topologicalSort() 52 | { 53 | stack Stack; 54 | 55 | 56 | bool* visited = new bool[V]; 57 | for (int i = 0; i < V; i++) 58 | visited[i] = false; 59 | 60 | 61 | for (int i = 0; i < V; i++) 62 | if (visited[i] == false) 63 | topologicalSortUtil(i, visited, Stack); 64 | 65 | while (Stack.empty() == false) { 66 | cout << Stack.top() << " "; 67 | Stack.pop(); 68 | } 69 | } 70 | int main() 71 | { 72 | Graph g(6); 73 | g.addEdge(5, 2); 74 | g.addEdge(5, 0); 75 | g.addEdge(4, 0); 76 | g.addEdge(4, 1); 77 | g.addEdge(2, 3); 78 | g.addEdge(3, 1); 79 | 80 | cout << "Topological Sort:"; 81 | g.topologicalSort(); 82 | 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /Beautiful_Matrix.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Beautiful_Matrix { 3 | static Scanner sr = new Scanner(System.in); 4 | public static void main(String args[]) 5 | { 6 | int i=0,j=0,row=0,col=0,count=0; 7 | int arr[][] = new int[5][5]; 8 | String str = null; 9 | String[] a = new String[5]; 10 | for(i=0;i<5;i++) 11 | { 12 | str = sr.nextLine(); 13 | str.trim(); 14 | a = str.split(" "); 15 | for(j=0;j<5;j++) 16 | { 17 | arr[i][j] = Integer.parseInt(a[j]); 18 | } 19 | } 20 | for(i=0;i<5;i++) 21 | { 22 | for(j=0;j<5;j++) 23 | { 24 | if(arr[i][j] == 1) 25 | { 26 | row = i+1; 27 | col = j+1; 28 | break; 29 | } 30 | } 31 | } 32 | while(row!=3) 33 | { 34 | if(row>3) 35 | { 36 | row--; 37 | count++; 38 | } 39 | else 40 | { 41 | row++; 42 | count++; 43 | } 44 | } 45 | while(col!=3) 46 | { 47 | if(col>3) 48 | { 49 | col--; 50 | count++; 51 | } 52 | else 53 | { 54 | col++; 55 | count++; 56 | } 57 | } 58 | System.out.println(count); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Java/Max-sum-of-hourglass.java: -------------------------------------------------------------------------------- 1 | // Java program to find maximum 2 | // sum of hour glass in matrix 3 | import java.io.*; 4 | 5 | class HourglassSum { 6 | 7 | static int R = 5; 8 | static int C = 5; 9 | 10 | 11 | static int findMaxSum(int [][]mat) 12 | { 13 | if (R < 3 || C < 3) 14 | return -1; 15 | 16 | int max_sum = Integer.MIN_VALUE; 17 | for (int i = 0; i < R - 2; i++) 18 | { 19 | for (int j = 0; j < C - 2; j++) 20 | { 21 | // Considering mat[i][j] as top 22 | // left cell of hour glass. 23 | int sum = (mat[i][j] + mat[i][j + 1] + 24 | mat[i][j + 2]) + (mat[i + 1][j + 1]) + 25 | (mat[i + 2][j] + mat[i + 2][j + 1] + 26 | mat[i + 2][j + 2]); 27 | 28 | // If previous sum is less then 29 | // current sum then update 30 | // new sum in max_sum 31 | max_sum = Math.max(max_sum, sum); 32 | } 33 | } 34 | return max_sum; 35 | } 36 | 37 | 38 | static public void main (String[] args) 39 | { 40 | int [][]mat = {{1, 2, 3, 0, 0}, 41 | {0, 0, 0, 0, 0}, 42 | {2, 1, 4, 0, 0}, 43 | {0, 0, 0, 0, 0}, 44 | {1, 1, 0, 1, 0}}; 45 | int res = findMaxSum(mat); 46 | if (res == -1) 47 | System.out.println("Not possible"); 48 | else 49 | System.out.println("Maximum sum of hour glass = " 50 | + res); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /PalindromePartition.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define int long long 5 | 6 | void cpc() 7 | { 8 | ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 9 | #ifndef ONLINE_JUDGE 10 | freopen("input.txt", "r", stdin); 11 | freopen("output.txt", "w", stdout); 12 | #endif 13 | } 14 | 15 | int palpart(string s) 16 | { 17 | int n = s.length(); 18 | 19 | bool ispal[n][n]; 20 | 21 | for (int i = 0; i < n; i++) 22 | {ispal[i][i] = 1;} 23 | 24 | for (int i = 0; i < n - 1; i++) 25 | { 26 | if (s[i] == s[i + 1]) 27 | {ispal[i][i + 1] = 1;} 28 | else 29 | {ispal[i][i + 1] = 0;} 30 | } 31 | 32 | for (int gap = 2; gap < n; gap++) 33 | { 34 | for (int i = 0; i < n - gap; i++) 35 | { 36 | if (s[i] == s[i + gap]) 37 | {ispal[i][i + gap] = ispal[i + 1][i + gap - 1];} 38 | else 39 | {ispal[i][i + gap] = 0;} 40 | } 41 | } 42 | 43 | int dp[n][n]; 44 | 45 | for (int i = 0; i < n; i++) 46 | dp[i][i] = 0; 47 | 48 | for (int i = 0; i < n - 1; i++) 49 | { 50 | if (s[i] == s[i + 1]) 51 | dp[i][i + 1] = 0; 52 | else 53 | dp[i][i + 1] = 1; 54 | } 55 | 56 | for (int gap = 2; gap < n; gap++) 57 | { 58 | 59 | for (int i = 0; i < n - gap; i++) 60 | { 61 | dp[i][i + gap] = gap; 62 | if (ispal[i][i + gap]) 63 | {dp[i][i + gap] = 0;} 64 | else { 65 | for (int k = i; k < i + gap; k++) 66 | dp[i][i + gap] = min(dp[i][i + gap], 1 + dp[i][k] + dp[k + 1][i + gap]); 67 | } 68 | } 69 | } 70 | 71 | return dp[0][n - 1]; 72 | 73 | } 74 | 75 | 76 | int32_t main() 77 | { 78 | cpc(); 79 | 80 | string s; 81 | cin >> s; 82 | 83 | cout << palpart(s); 84 | return 0; 85 | } 86 | -------------------------------------------------------------------------------- /NumberOfSquares.cpp: -------------------------------------------------------------------------------- 1 | // C++ program for the above approach 2 | 3 | #include 4 | using namespace std; 5 | 6 | // Function to count all the possible 7 | // squares with given lines parallel 8 | // to both the X and Y axis 9 | int numberOfSquares(int X[], int Y[], 10 | int N, int M) 11 | { 12 | // Stores the count of all possible 13 | // distances in X[] & Y[] respectively 14 | unordered_map m1, m2; 15 | int i, j, ans = 0; 16 | 17 | // Find distance between all 18 | // pairs in the array X[] 19 | for (i = 0; i < N; i++) { 20 | for (j = i + 1; j < N; j++) { 21 | 22 | int dist = abs(X[i] - X[j]); 23 | 24 | // Add the count to m1 25 | m1[dist]++; 26 | } 27 | } 28 | 29 | // Find distance between all 30 | // pairs in the array Y[] 31 | for (i = 0; i < M; i++) { 32 | for (j = i + 1; j < M; j++) { 33 | 34 | int dist = abs(Y[i] - Y[j]); 35 | 36 | // Add the count to m2 37 | m2[dist]++; 38 | } 39 | } 40 | 41 | // Find sum of m1[i] * m2[i] 42 | // for same distance 43 | for (auto i = m1.begin(); 44 | i != m1.end(); i++) { 45 | 46 | // Find current count in m2 47 | if (m2.find(i->first) 48 | != m2.end()) { 49 | 50 | // Add to the total count 51 | ans += (i->second 52 | * m2[i->first]); 53 | } 54 | } 55 | 56 | // Return the final count 57 | return ans; 58 | } 59 | 60 | // Driver Code 61 | int main() 62 | { 63 | // Given lines 64 | int X[] = { 1, 3, 7 }; 65 | int Y[] = { 2, 4, 6, 1 }; 66 | 67 | int N = sizeof(X) / sizeof(X[0]); 68 | 69 | int M = sizeof(Y) / sizeof(Y[0]); 70 | 71 | // Function Call 72 | cout << numberOfSquares(X, Y, N, M); 73 | 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /Convert to Strictly increasing integer array with minimum changes.cpp: -------------------------------------------------------------------------------- 1 | //Given an array of n integers. Write a program to find minimum number of changes in array so that //array is strictly increasing of integers. In strictly increasing array A[i] < A[i+1] for //0 <= i //< n 2 | 3 | //Examples: 4 | 5 | //Input : arr[] = { 1, 2, 6, 5, 4} 6 | //Output : 2 7 | //We can change a[2] to any value 8 | //between 2 and 5. 9 | //and a[4] to any value greater then 5. 10 | 11 | //Input : arr[] = { 1, 2, 3, 5, 7, 11 } 12 | //Output : 0 13 | //Array is already strictly increasing. 14 | 15 | // CPP program to find min elements to 16 | // change so array is strictly increasing 17 | #include 18 | using namespace std; 19 | 20 | // To find min elements to remove from array 21 | // to make it strictly increasing 22 | int minRemove(int arr[], int n) 23 | { 24 | int LIS[n], len = 0; 25 | 26 | // Mark all elements of LIS as 1 27 | for (int i = 0; i < n; i++) 28 | LIS[i] = 1; 29 | 30 | // Find LIS of array 31 | for (int i = 1; i < n; i++) { 32 | for (int j = 0; j < i; j++) { 33 | if (arr[i] > arr[j] && (i-j)<=(arr[i]-arr[j])){ 34 | LIS[i] = max(LIS[i], LIS[j] + 1); 35 | } 36 | } 37 | len = max(len, LIS[i]); 38 | } 39 | 40 | // Return min changes for array 41 | // to strictly increasing 42 | return n - len; 43 | } 44 | 45 | // Driver program to test minRemove() 46 | int main() 47 | { 48 | int arr[] = { 1, 2, 6, 5, 4 }; 49 | int n = sizeof(arr) / sizeof(arr[0]); 50 | 51 | cout << minRemove(arr, n); 52 | 53 | return 0; 54 | } -------------------------------------------------------------------------------- /LRU.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | // class Lru{ 6 | // int capacity; 7 | // list> lrlist; 8 | 9 | // unordered_map>:: iterator> hs; 10 | 11 | // void move_it(int key,int val){ 12 | // lrlist.erase(hs[key]); 13 | // lrlist.push_front(make_pair(key,val)); 14 | // hs[key]= lrlist.begin(); 15 | // } 16 | 17 | // public: 18 | 19 | // Lru(int capacity){ 20 | // this->capacity= capacity; 21 | // } 22 | 23 | // int get(int key){ 24 | // if(hs.find(key)== hs.end() ) 25 | // return -1; 26 | 27 | // int val= (*hs[key]).second; 28 | // move_it(key,val); 29 | 30 | // return val; 31 | // } 32 | 33 | 34 | // int put(int key,int value){ 35 | 36 | // if(hs.find(key)!= hs.end()){ 37 | // move_it(key,value);} 38 | 39 | // else{ 40 | 41 | // lrlist.push_front(make_pair(key,value)); 42 | // hs[key]= lrlist.begin(); 43 | // if(hs.size()> capacity){ 44 | // // erase 45 | 46 | // hs.erase(lrlist.back().first); 47 | // lrlist.pop_back(); 48 | // } 49 | // } 50 | // } 51 | // }; 52 | 53 | // int main(){ 54 | 55 | // Lru l(2); 56 | // l.put(1,1); 57 | // l.put(2,2); 58 | // l.get(1); 59 | // l.put(3,3); 60 | // l.get(2); 61 | // l.put(4,4); 62 | // l.get(1); 63 | // l.get(3); 64 | // l.get(4); 65 | // return 0; 66 | 67 | // } 68 | 69 | 70 | 71 | 72 | int main(){ 73 | char s; 74 | 75 | s='a'; 76 | 77 | int c= s-'a'; 78 | return c; 79 | 80 | } -------------------------------------------------------------------------------- /Linked List via C/Linked Queue in C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node{ 5 | int data; 6 | struct node *next; 7 | }; 8 | struct node *start, *current, *temp, *prev; 9 | 10 | //function to check if the List is empty or not 11 | bool isEmpty() 12 | { 13 | if(start->next==NULL) 14 | return true; 15 | else 16 | return false; 17 | } 18 | 19 | //function to create the Linked List 20 | void create() 21 | { 22 | start=(struct node*)malloc(sizeof(struct node)); 23 | start->data=NULL; 24 | start->next=NULL; 25 | temp=start; 26 | } 27 | 28 | //function to add nodes to the Linked List 29 | void enqueue() 30 | { 31 | current=(struct node*)malloc(sizeof(struct node)); 32 | printf("Enter the value of node to add:"); 33 | scanf("%d",¤t->data); 34 | temp->next=current; 35 | current->next=start; 36 | temp=current; 37 | } 38 | 39 | //function to delete nodes from the starting of the queue 40 | void dequeue() 41 | { 42 | if(isEmpty()) 43 | printf("\nThe List is Empty\n"); 44 | else 45 | { 46 | temp=start->next; 47 | printf("\nDeleted node data: %d\n",temp->data); 48 | start->next=temp->next; 49 | } 50 | } 51 | 52 | void display() 53 | { 54 | temp=start; 55 | while(temp->next!=start) 56 | { 57 | temp=temp->next; 58 | printf("%d ",temp->data); 59 | } 60 | } 61 | 62 | int main() 63 | { 64 | create(); 65 | for(int i=0;i<5;i++) 66 | enqueue(); 67 | printf("\nThe queue after enqueue operations is: "); 68 | display(); 69 | dequeue(); 70 | dequeue(); 71 | printf("\nThe queue after dequeue operations is: "); 72 | display(); 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /UniquePath.md: -------------------------------------------------------------------------------- 1 | # Problem Statement 2 | A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). 3 | 4 | The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below). 5 | 6 | How many possible unique paths are there? 7 | 8 | ``` 9 | Input: m = 3, n = 2 10 | Output: 3 11 | Explanation: 12 | From the top-left corner, there are a total of 3 ways to reach the bottom-right corner: 13 | 1. Right -> Right -> Down 14 | 2. Right -> Down -> Right 15 | 3. Down -> Right -> Right 16 | ``` 17 | 18 | ``` 19 | Input: m = 7, n = 3 20 | Output: 28 21 | ``` 22 | 23 | # Solution in Java 24 | 25 | ```Java 26 | 27 | class Solution { 28 | public int uniquePaths(int m, int n) { 29 | int[][] dp = new int[m][n]; 30 | 31 | dp[0][0] = 1; 32 | for(int i = 0; i < m; i++) 33 | dp[i][0] = 1; 34 | 35 | for(int j = 0; j < n; j++) 36 | dp[0][j] = 1; 37 | 38 | for(int i = 1; i < m; i++) 39 | for(int j = 1; j < n; j++) 40 | dp[i][j] = dp[i-1][j] + dp[i][j-1]; 41 | 42 | 43 | return dp[m-1][n-1]; 44 | } 45 | 46 | ``` 47 | 48 | 49 | ## BFS Approach -> Exceeds Time Limit 50 | 51 | ```Java 52 | class Solution { 53 | public int uniquePaths(int m, int n) { 54 | return bfs(m, n, 0, 0); 55 | } 56 | 57 | public int bfs(int m, int n, int i, int j){ 58 | if(i == m-1 && j == n-1){ 59 | return 1; 60 | } 61 | 62 | if(i >= m || j >= n){ 63 | return 0; 64 | } 65 | 66 | return bfs(m, n, i+1, j) + bfs(m, n, i, j+1); 67 | } 68 | } 69 | ``` 70 | -------------------------------------------------------------------------------- /Java/right-view-binary-tree.java: -------------------------------------------------------------------------------- 1 | // Java program to print right view of binary tree 2 | 3 | // A binary tree node 4 | class Node { 5 | 6 | int data; 7 | Node left, right; 8 | 9 | Node(int item) { 10 | data = item; 11 | left = right = null; 12 | } 13 | } 14 | 15 | // class to access maximum level by reference 16 | class Max_level { 17 | 18 | int max_level; 19 | } 20 | 21 | class BinaryTree { 22 | 23 | Node root; 24 | Max_level max = new Max_level(); 25 | 26 | // Recursive function to print right view of a binary tree. 27 | void rightViewUtil(Node node, int level, Max_level max_level) { 28 | 29 | // Base Case 30 | if (node == null) 31 | return; 32 | 33 | // If this is the last Node of its level 34 | if (max_level.max_level < level) { 35 | System.out.print(node.data + " "); 36 | max_level.max_level = level; 37 | } 38 | 39 | // Recur for right subtree first, then left subtree 40 | rightViewUtil(node.right, level + 1, max_level); 41 | rightViewUtil(node.left, level + 1, max_level); 42 | } 43 | 44 | void rightView() 45 | { 46 | rightView(root); 47 | } 48 | 49 | // A wrapper over rightViewUtil() 50 | void rightView(Node node) { 51 | 52 | rightViewUtil(node, 1, max); 53 | } 54 | 55 | // Driver program to test the above functions 56 | public static void main(String args[]) { 57 | BinaryTree tree = new BinaryTree(); 58 | tree.root = new Node(1); 59 | tree.root.left = new Node(2); 60 | tree.root.right = new Node(3); 61 | tree.root.left.left = new Node(4); 62 | tree.root.left.right = new Node(5); 63 | tree.root.right.left = new Node(6); 64 | tree.root.right.right = new Node(7); 65 | tree.root.right.left.right = new Node(8); 66 | 67 | tree.rightView(); 68 | 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /invertbst.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | struct node{ 6 | int val; 7 | node *left; 8 | node *right; 9 | 10 | 11 | }; 12 | node *root= NULL; 13 | 14 | node* getnode(int val){ 15 | 16 | node *temp= new node(); 17 | temp->val= val; 18 | temp->left= NULL; 19 | temp->right= NULL; 20 | return temp; 21 | 22 | } 23 | 24 | class BST{ 25 | public: 26 | node* insertbst(node *root, int data); 27 | 28 | void inorder(node *root); 29 | void invert(node *root); 30 | }; 31 | 32 | node *BST::insertbst(node *root,int data) 33 | //function to insert 34 | { 35 | 36 | //insert at root node 37 | if (root == NULL){ 38 | root= getnode(data); 39 | return root; 40 | } 41 | //left node insertion 42 | else if(data <= root->val){ 43 | root->left= insertbst(root->left,data); 44 | 45 | } 46 | 47 | //right node insertion 48 | else{ 49 | root->right= insertbst(root->right,data); 50 | } 51 | return root; 52 | } 53 | 54 | void BST:: inorder(node *root){ 55 | if(root== NULL){ 56 | return; 57 | } 58 | cout<val<<" " ; 59 | inorder(root->left); 60 | 61 | inorder(root->right); 62 | } 63 | 64 | void BST:: invert(node *root){ 65 | 66 | swap(root->left,root->right); 67 | 68 | 69 | invert(root->left); 70 | invert(root->right); 71 | inorder(root); 72 | 73 | 74 | } 75 | int main(){ 76 | BST b; 77 | root= b.insertbst(root,15); 78 | root= b.insertbst(root,10); 79 | root= b.insertbst(root,20); 80 | root= b.insertbst(root,25); 81 | root= b.insertbst(root,8); 82 | root= b.insertbst(root,12); 83 | b.inorder(root); 84 | cout<<"\n"; 85 | b.invert(root); 86 | 87 | 88 | return 0; 89 | } 90 | --------------------------------------------------------------------------------