├── LinkedList.py ├── substring.java ├── INFO.md ├── HelloWorld.rb ├── HelloWorld.ts ├── DynamicProgramming ├── inputf.in ├── JaniRamya.exe ├── PiggyBank.exe ├── Shakuntla.exe ├── frogJump2.exe ├── merge_sort.exe ├── quickSort.exe ├── FIleHandling.exe ├── FindComments.exe ├── Subsequences.exe ├── outputf.in ├── Chochlate_distributions.exe ├── Fibbonacci_using_recursion_Lect1.exe ├── PrintAllSubsequenceUsingRecursion.exe ├── binary_search_with_condition_on_not_find.exe ├── FIleHandling.cpp ├── JaniRamya.cpp ├── frogJump2.cpp ├── PiggyBank.cpp ├── Fibbonacci_using_recursion_Lect1.cpp ├── Shakuntla.cpp ├── Subsequences.cpp ├── Chochlate_distributions.cpp ├── PrintAllSubsequenceUsingRecursion.cpp ├── binary_search_with_condition_on_not_find.cpp ├── FindComments.cpp ├── quickSort.cpp ├── merge_sort.cpp ├── MyCode.cpp └── 3sum.cpp ├── HelloWorld.js ├── HelloWorld.py ├── HelloWorld.m ├── HelloWorld.rs ├── HelloWorld.swift ├── HelloWorld.dart ├── Factorial.exe ├── Fibonacci.exe ├── HelloWorld.sc ├── Primenumber.exe ├── HelloWorld.c ├── HelloWorld.go ├── rotate_array_by_Srishti.exe ├── HelloWorld.cpp ├── hello world.java ├── Sum.ts ├── HelloWorld.java ├── Sample.jsx ├── Factorial.c ├── happy number.cpp ├── hello.s ├── Primenumber.c ├── automative mailing.py ├── LCS.py ├── Reverse Linked List.cpp ├── contructor.cpp ├── LCS.cpp ├── LCS.js ├── ArrayLargest.py ├── hello world ├── LCS.java ├── TowerOfHanoi.cpp ├── BubbleSort.cpp ├── ArrayLargest.js ├── guessThenumberCPU.py ├── integer replacement.cpp ├── permutation.cpp ├── SubArraySum.java ├── Array_BestTimeToBuyAndSellStock1.java ├── guessTheNumberUser.py ├── ArrayLargest.cpp ├── pattern.c ├── Taylor.c ├── partition list.cpp ├── BubbleSort.py ├── CycleSort.cpp ├── ArrayLargest.java ├── leap year.c ├── Binary_Search.cpp ├── relative ranks .cpp ├── DepthFirstSearch.cpp ├── hello world in different language ├── SelectionSort.java ├── Kadane's.cpp ├── HeapSort.py ├── BubbleSort.java ├── EditDistance.java ├── Power_With_MOD.cpp ├── ReverseArray.cpp ├── HelloWorld.bf ├── rotate_array.c++ ├── Fibonacci.c ├── Calculator.c ├── BreadthFirstSearch.cpp ├── LinkedListReverse.cpp ├── FindLargestNumber.java ├── SelectionSort.cpp ├── GreedyBestFirstSearch.cpp ├── RadixSort.c ├── subsetsum.cpp ├── CountingSort.cpp ├── Gauss_Elimination.cpp ├── guess_number.py ├── linkedlist.cpp ├── Infix_to_Postfix.java ├── RomanToDecimal.java ├── linkedlist traversal.c ├── QuickSort.py ├── Search_in_a_rotated_array.cpp ├── finding-middle-element ├── Dijkstra’s Algo.cpp ├── FlatenABinaryTree.cpp ├── Trie.cpp ├── merge_sort.cpp ├── Pre_Post_Inorder_traversal_in_a_single_flow.cpp ├── Valid_Parentheses.java └── README.md /LinkedList.py: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /substring.java: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /INFO.md: -------------------------------------------------------------------------------- 1 | Hello everyone 2 | -------------------------------------------------------------------------------- /HelloWorld.rb: -------------------------------------------------------------------------------- 1 | puts "Hello, world!" 2 | -------------------------------------------------------------------------------- /HelloWorld.ts: -------------------------------------------------------------------------------- 1 | puts "Hello, world!" 2 | -------------------------------------------------------------------------------- /DynamicProgramming/inputf.in: -------------------------------------------------------------------------------- 1 | 5 2 | 3 2 1 4 5 -------------------------------------------------------------------------------- /HelloWorld.js: -------------------------------------------------------------------------------- 1 | console.log("Hello, world!") -------------------------------------------------------------------------------- /HelloWorld.py: -------------------------------------------------------------------------------- 1 | print "Hello, world!" 2 | -------------------------------------------------------------------------------- /HelloWorld.m: -------------------------------------------------------------------------------- 1 | % MATLAB 2 | disp('Hello, world!') 3 | -------------------------------------------------------------------------------- /HelloWorld.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | println!("Hello World!"); 3 | } 4 | -------------------------------------------------------------------------------- /HelloWorld.swift: -------------------------------------------------------------------------------- 1 | print("Hello, World!") 2 | print("Hola, World!") 3 | -------------------------------------------------------------------------------- /HelloWorld.dart: -------------------------------------------------------------------------------- 1 | void main() { 2 | 3 | print('hello world'); 4 | 5 | } 6 | -------------------------------------------------------------------------------- /Factorial.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/silicon-coder/INTRODUCTION/HEAD/Factorial.exe -------------------------------------------------------------------------------- /Fibonacci.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/silicon-coder/INTRODUCTION/HEAD/Fibonacci.exe -------------------------------------------------------------------------------- /HelloWorld.sc: -------------------------------------------------------------------------------- 1 | 2 | object HelloWorld extends App { 3 | printIn("Hello World") 4 | } 5 | -------------------------------------------------------------------------------- /Primenumber.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/silicon-coder/INTRODUCTION/HEAD/Primenumber.exe -------------------------------------------------------------------------------- /HelloWorld.c: -------------------------------------------------------------------------------- 1 | #include 'stdio.h' 2 | 3 | int main(void){ 4 | puts("Hello World!!!!!"); 5 | } 6 | -------------------------------------------------------------------------------- /HelloWorld.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Println("Hello World!") 7 | } 8 | -------------------------------------------------------------------------------- /rotate_array_by_Srishti.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/silicon-coder/INTRODUCTION/HEAD/rotate_array_by_Srishti.exe -------------------------------------------------------------------------------- /DynamicProgramming/JaniRamya.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/silicon-coder/INTRODUCTION/HEAD/DynamicProgramming/JaniRamya.exe -------------------------------------------------------------------------------- /DynamicProgramming/PiggyBank.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/silicon-coder/INTRODUCTION/HEAD/DynamicProgramming/PiggyBank.exe -------------------------------------------------------------------------------- /DynamicProgramming/Shakuntla.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/silicon-coder/INTRODUCTION/HEAD/DynamicProgramming/Shakuntla.exe -------------------------------------------------------------------------------- /DynamicProgramming/frogJump2.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/silicon-coder/INTRODUCTION/HEAD/DynamicProgramming/frogJump2.exe -------------------------------------------------------------------------------- /DynamicProgramming/merge_sort.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/silicon-coder/INTRODUCTION/HEAD/DynamicProgramming/merge_sort.exe -------------------------------------------------------------------------------- /DynamicProgramming/quickSort.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/silicon-coder/INTRODUCTION/HEAD/DynamicProgramming/quickSort.exe -------------------------------------------------------------------------------- /DynamicProgramming/FIleHandling.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/silicon-coder/INTRODUCTION/HEAD/DynamicProgramming/FIleHandling.exe -------------------------------------------------------------------------------- /DynamicProgramming/FindComments.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/silicon-coder/INTRODUCTION/HEAD/DynamicProgramming/FindComments.exe -------------------------------------------------------------------------------- /DynamicProgramming/Subsequences.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/silicon-coder/INTRODUCTION/HEAD/DynamicProgramming/Subsequences.exe -------------------------------------------------------------------------------- /HelloWorld.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | cout << "Hello world!!!"; 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /hello world.java: -------------------------------------------------------------------------------- 1 | class Simple{ 2 | public static void(String args[]){ 3 | system.out.println("helo java"); 4 | } 5 | } 6 | 7 | -------------------------------------------------------------------------------- /DynamicProgramming/outputf.in: -------------------------------------------------------------------------------- 1 | 3 1 5 2 | 3 1 3 | 3 4 4 | 3 5 5 | 3 6 | 2 4 7 | 2 5 8 | 2 9 | 1 5 10 | 1 11 | 4 12 | 5 13 | 14 | -------------------------------------------------------------------------------- /DynamicProgramming/Chochlate_distributions.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/silicon-coder/INTRODUCTION/HEAD/DynamicProgramming/Chochlate_distributions.exe -------------------------------------------------------------------------------- /DynamicProgramming/Fibbonacci_using_recursion_Lect1.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/silicon-coder/INTRODUCTION/HEAD/DynamicProgramming/Fibbonacci_using_recursion_Lect1.exe -------------------------------------------------------------------------------- /DynamicProgramming/PrintAllSubsequenceUsingRecursion.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/silicon-coder/INTRODUCTION/HEAD/DynamicProgramming/PrintAllSubsequenceUsingRecursion.exe -------------------------------------------------------------------------------- /DynamicProgramming/binary_search_with_condition_on_not_find.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/silicon-coder/INTRODUCTION/HEAD/DynamicProgramming/binary_search_with_condition_on_not_find.exe -------------------------------------------------------------------------------- /Sum.ts: -------------------------------------------------------------------------------- 1 | // Sum program written in TypeScript 2 | 3 | const sum = (a: number, b: number) => { 4 | console.log('Sum = ' + (a + b) + '\n'); 5 | }; 6 | 7 | sum(10, 2); 8 | sum(10, -10); 9 | -------------------------------------------------------------------------------- /HelloWorld.java: -------------------------------------------------------------------------------- 1 | // Program to print Hello World in Java 2 | 3 | class HelloWorld{ 4 | public static void main(String[] args) { 5 | System.out.println("Hello, world!!!"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Sample.jsx: -------------------------------------------------------------------------------- 1 | const myElement =

This is an h1 element written in JSX. Hello everyone.

; 2 | 3 | const root = ReactDOM.createRoot(document.getElementById('root')); 4 | root.render(myElement); 5 | -------------------------------------------------------------------------------- /Factorial.c: -------------------------------------------------------------------------------- 1 | # Factorial Program in C language 2 | #include 3 | int main(){ 4 | int i=1,f=1,num; 5 | 6 | printf("Enter a number: "); 7 | scanf("%d",&num); 8 | 9 | while(i<=num){ 10 | f=f*i; 11 | i++; 12 | } 13 | 14 | printf("Factorial of %d is: %d",num,f); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /happy number.cpp: -------------------------------------------------------------------------------- 1 | int digitSqSum(int n){ 2 | int sum = 0; 3 | while(n > 0){ 4 | sum = sum + ((n%10)*(n%10)); 5 | n=n/10; 6 | } 7 | return sum; 8 | } 9 | 10 | bool isHappy(int n) { 11 | unordered_map mp; 12 | while(n != 1) { 13 | if(mp[n] == 0)mp[n]++; 14 | else return false; 15 | 16 | n = digitSqSum(n); 17 | } 18 | return true; 19 | } -------------------------------------------------------------------------------- /hello.s: -------------------------------------------------------------------------------- 1 | global _start 2 | 3 | section .text 4 | 5 | _start: 6 | mov rax, 1 ; write( 7 | mov rdi, 1 ; STDOUT_FILENO, 8 | mov rsi, msg ; "Hello, world!\n", 9 | mov rdx, msglen ; sizeof("Hello, world!\n") 10 | syscall ; ); 11 | 12 | mov rax, 60 ; exit( 13 | mov rdi, 0 ; EXIT_SUCCESS 14 | syscall ; ); 15 | 16 | section .rodata 17 | msg: db "Hello, world!", 10 18 | msglen: equ $ - msg -------------------------------------------------------------------------------- /DynamicProgramming/FIleHandling.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | FILE *fp; 4 | fp = fopen("Student.txt","w"); 5 | fputs("Name Age Rollnumber \n",fp); 6 | fputs("Shivendu 19 2000290120153 \n",fp); 7 | fputs("Shiva 12 2000290120146 \n",fp); 8 | fputs("Yashasvi 62 2000290120199 \n",fp); 9 | fclose(fp); 10 | FILE *in=fopen("Student.txt","r"); 11 | char c; 12 | while((c=fgetc(in))!=EOF) 13 | putchar(c); 14 | fclose(in); 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Primenumber.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int n, i, count = 0; 4 | printf("Enter number to check prime number or not"); 5 | scanf("%d",&n); 6 | for(i=2; i<=n/2; ++i){ 7 | // check for non prime number 8 | if(n%i==0){ 9 | count=1; 10 | break; 11 | } 12 | } 13 | 14 | if (count==0) printf("%d is a prime number.",n); 15 | else printf("%d is not a prime number.",n); 16 | getch(); 17 | } 18 | -------------------------------------------------------------------------------- /automative mailing.py: -------------------------------------------------------------------------------- 1 | import smtplib as s 2 | from typing import List 3 | ob = s.SMTP("smtp.gmail.com",587) 4 | ob.starttls() 5 | ob.login("shubhamnag789@gmail.com","kdddpczcjwfjmqiz") 6 | subject = "Sending email using python" 7 | body = "Sending email using python script in simple" 8 | message = "Subject:{}\n\n{}".format(subject,body) 9 | listofAddress = ["shubhamng14@gmail.com"] 10 | ob.sendmail("shubhamnag789@gmail.com",listofAddress,message) 11 | print("send succesfully") 12 | ob.quit() -------------------------------------------------------------------------------- /LCS.py: -------------------------------------------------------------------------------- 1 | def max(x, y): 2 | if(x > y): 3 | return x 4 | return y 5 | 6 | def lps(seq, i, j): 7 | 8 | if (i == j): 9 | return 1 10 | 11 | if (seq[i] == seq[j] and i + 1 == j): 12 | return 2 13 | 14 | if (seq[i] == seq[j]): 15 | return lps(seq, i + 1, j - 1) + 2 16 | 17 | return max(lps(seq, i, j - 1), 18 | lps(seq, i + 1, j)) 19 | 20 | if __name__ == '__main__': 21 | seq = "GEEKSFORGEEKS" 22 | n = len(seq) 23 | print("The length of the LPS is", 24 | lps(seq, 0, n - 1)) 25 | -------------------------------------------------------------------------------- /Reverse Linked List.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode* reverseList(ListNode* head) { 4 | ListNode *cur,*prev,*next; 5 | prev=NULL; 6 | if(head==NULL) 7 | return head; 8 | cur=head; 9 | next=cur->next; 10 | cur->next=NULL; 11 | while(next!=NULL) 12 | { 13 | prev=cur; 14 | cur=next; 15 | next=cur->next; 16 | cur->next=prev; 17 | } 18 | head=cur; 19 | cur->next=prev; 20 | return head; 21 | } 22 | }; -------------------------------------------------------------------------------- /contructor.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | 5 | class student { 6 | int rno; 7 | char name[10]; 8 | double fee; 9 | 10 | public: 11 | student() 12 | { 13 | cout << "Enter the RollNo:"; 14 | cin >> rno; 15 | cout << "Enter the Name:"; 16 | cin >> name; 17 | cout << "Enter the Fee:"; 18 | cin >> fee; 19 | } 20 | 21 | void display() 22 | { 23 | cout << endl << rno << "\t" << name << "\t" << fee; 24 | } 25 | }; 26 | 27 | int main() 28 | { 29 | student s; 30 | s.display(); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /LCS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int max (int x, int y) { return (x > y)? x : y; } 5 | 6 | int lps(char *seq, int i, int j){ 7 | if (i == j) 8 | return 1; 9 | 10 | if (seq[i] == seq[j] && i + 1 == j) 11 | return 2; 12 | 13 | if (seq[i] == seq[j]) 14 | return lps (seq, i+1, j-1) + 2; 15 | 16 | return max( lps(seq, i, j-1), lps(seq, i+1, j) ); 17 | } 18 | 19 | int main(){ 20 | char seq[] = "GEEKSFORGEEKS"; 21 | int n = strlen(seq); 22 | cout << "The length of the LPS is "<< lps(seq, 0, n-1); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /LCS.js: -------------------------------------------------------------------------------- 1 | 25 | 26 | -------------------------------------------------------------------------------- /DynamicProgramming/JaniRamya.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void solve(){ 5 | int n,m; 6 | cin>>n>>m; 7 | 8 | vector vec(n); 9 | for(int i=0; i>vec[i]; 11 | } 12 | 13 | unordered_map mp; 14 | for(int i=0;i>t; 29 | while(t--){ 30 | solve() 31 | ; } 32 | 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /ArrayLargest.py: -------------------------------------------------------------------------------- 1 | # Python3 program to find maximum 2 | # in arr[] of size n 3 | 4 | # Function to find maximum 5 | # in arr[] of size n 6 | def largest(arr,n): 7 | # Initialize maximum element 8 | mx = arr[0] 9 | 10 | # Traverse array elements from second 11 | # and compare every element with 12 | # current max 13 | for i in range(1, n): 14 | if arr[i] > mx: 15 | mx = arr[i] 16 | 17 | return mx 18 | 19 | # Driver Code 20 | arr = [10, 324, 45, 90, 9808] 21 | n = len(arr) 22 | 23 | Ans = largest(arr,n) 24 | 25 | print ("Largest in given array is",Ans) 26 | -------------------------------------------------------------------------------- /DynamicProgramming/frogJump2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int cal(int n,int k,int idx, vector &h){ 5 | if(idx == 0){ 6 | return 0; 7 | } 8 | int mini = INT_MAX; 9 | for(int i=1;i<=k;i++){ 10 | if(idx-i>=0) 11 | mini = min(mini, cal(n,k,idx-i,h)+abs(h[idx]-h[idx-i])); 12 | } 13 | 14 | return mini; 15 | } 16 | int main(){ 17 | int n,k; 18 | cin>>n>>k; 19 | vector h; 20 | for(int i=0;i>a; 23 | h.push_back(a); 24 | } 25 | cout< y) ? x : y; 4 | } 5 | 6 | static int lps(char seq[], int i, int j) { 7 | if (i == j) return 1; 8 | 9 | if (seq[i] == seq[j] && i + 1 == j) return 2; 10 | 11 | if (seq[i] == seq[j]) return lps(seq, i + 1, j - 1) + 2; 12 | return max(lps(seq, i, j - 1), lps(seq, i + 1, j)); 13 | } 14 | 15 | public static void main(String[] args) { 16 | String seq = "GEEKSFORGEEKS"; 17 | int n = seq.length(); 18 | System.out.printf("The length of the LPS is %d", lps(seq.toCharArray(), 0, n - 1)); 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /DynamicProgramming/PiggyBank.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int maxCoins(vector &piggyBank){ 4 | sort(piggyBank.begin(),piggyBank.end()); 5 | int ans = 1*piggyBank[0]*piggyBank[1]; 6 | int n = piggyBank.size(); 7 | for(int i=1;i>n; 17 | vector vec(n); 18 | for(int i=0;i>vec[i]; 20 | } 21 | 22 | cout< 8 | using namespace std; 9 | void TOH(int n, char source, char help, char des){ 10 | 11 | if(n==1){ 12 | cout<<"Move 1 disc from "<>n; 25 | TOH(n, 's', 'h', 'd'); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /BubbleSort.cpp: -------------------------------------------------------------------------------- 1 | 2 | //C++ code for recursive bubble sort algorithm 3 | #include 4 | using namespace std; 5 | void bubblesort(int arr[], int n) 6 | { 7 | if (n == 0 || n == 1) 8 | { 9 | return; 10 | } 11 | for (int i = 0; i < n - 1; i++) 12 | { 13 | if (arr[i] > arr[i + 1]) 14 | { 15 | swap(arr[i], arr[i + 1]); 16 | } 17 | } 18 | bubblesort(arr, n - 1); 19 | } 20 | int main() 21 | { 22 | int arr[5] = {2, 5, 1, 6, 9}; 23 | bubblesort(arr, 5); 24 | for (int i = 0; i < 5; i++) 25 | { 26 | cout << arr[i] << " "; 27 | } 28 | return 0; 29 | } 30 | //code contributed by pragatikohli 31 | -------------------------------------------------------------------------------- /ArrayLargest.js: -------------------------------------------------------------------------------- 1 | 29 | -------------------------------------------------------------------------------- /guessThenumberCPU.py: -------------------------------------------------------------------------------- 1 | 2 | import random 3 | 4 | def guess(x):#defining a function 5 | random_number= random.randint(1,x) #using a object/method from random library/class random 6 | guess=0 7 | while(guess!=random_number): 8 | guess=int(input(f'guess the number between 1 to {x}:'))#we need output in int format and input text is also given 9 | if(guess>random_number): 10 | print('too high go a little low') 11 | elif(guess 2 | using namespace std; 3 | int fibbonacci(int n,vector &dp){ 4 | if(n<=1){ 5 | return n; 6 | } 7 | if(dp[n]!=-1){ 8 | return dp[n]; 9 | } 10 | dp[n] = fibbonacci(n-1,dp) + fibbonacci(n-2,dp); 11 | return dp[n]; 12 | } 13 | 14 | int main(){ 15 | int n; 16 | cin>>n; 17 | // vector dp(n+1,-1); 18 | // cout<&nums,vector>&ans,vector&res,int ind){ 4 | if(ind>=nums.size()){ 5 | ans.push_back(nums); 6 | return ; 7 | } 8 | for(int i=ind;i> permute(vector& nums) { 15 | vector>ans; 16 | vectorres; 17 | int index=0; 18 | solve(nums,ans,res,0); 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /SubArraySum.java: -------------------------------------------------------------------------------- 1 | public class SubArraySum { 2 | public static void main(String[] args) { 3 | int arr[] = {1,0,3,4,5,6,7,8,9,10,1,1}; 4 | int s = 2; 5 | int i=0,j=1; 6 | int sum=arr[i]; 7 | while(j<=arr.length){ 8 | if(sum==s){ 9 | System.out.println(sum); 10 | System.out.println((i+1)+" "+(j)); 11 | break; 12 | } 13 | if(sums){ 18 | sum-=arr[i++]; 19 | 20 | } 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Array_BestTimeToBuyAndSellStock1.java: -------------------------------------------------------------------------------- 1 | package DSA; 2 | 3 | public class Array_BestTimeToBuyAndSellStock1 { 4 | //here we buy and sell a stock onl;y one time 5 | public static int BTS(int a[]){ 6 | int minSofar=a[0]; 7 | int maxProfit=0; 8 | for(int i=0;i 4 | using namespace std; 5 | 6 | int largest(int arr[], int n) 7 | { 8 | int i; 9 | 10 | // Initialize maximum element 11 | int max = arr[0]; 12 | 13 | // Traverse array elements 14 | // from second and compare 15 | // every element with current max 16 | for (i = 1; i < n; i++) 17 | if (arr[i] > max) 18 | max = arr[i]; 19 | 20 | return max; 21 | } 22 | 23 | // Driver Code 24 | int main(){ 25 | int arr[] = {10, 324, 45, 90, 9808}; 26 | int n = sizeof(arr) / sizeof(arr[0]); 27 | cout << "Largest in given array is " 28 | << largest(arr, n); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /pattern.c: -------------------------------------------------------------------------------- 1 | //Github username: Mehnaz14 2 | // Aim: This is a pattern of half pyramid using c language 3 | // So in this case : 4 | //Time complexity:0(n^2). 5 | //Space complexity:0(1). 6 | 7 | // start coding 8 | #include 9 | int main() { 10 | int i, j, rows; 11 | printf("Enter the number of rows: "); 12 | scanf("%d", &rows); 13 | for (i = 1; i <= rows; ++i) { 14 | for (j = 1; j <= i; ++j) { 15 | printf("* "); 16 | } 17 | printf("\n"); 18 | } 19 | return 0; 20 | } 21 | 22 | //We are printing the no of'*' for each respective rows like n*(n-1)*(n-2)....,so Time complexity is 0(n^2). 23 | //We are using constant space,so Space complexity is 0(1). 24 | -------------------------------------------------------------------------------- /DynamicProgramming/Shakuntla.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int f(string &s){ 5 | 6 | int ans = -1; 7 | vector v; 8 | int m= 0,w= 0; 9 | for(int i=0; i>t; 33 | while(t--){ 34 | string s; 35 | cin>>s; 36 | cout< 2 | #include 3 | #define PI 3.142 4 | 5 | void main() 6 | { 7 | int i, degree; 8 | float nume, deno, x, sum=0, term; 9 | 10 | printf("Enter the value of degree\n"); 11 | scanf("%d",°ree); 12 | 13 | x = degree * (PI / 180); 14 | 15 | nume = x; 16 | deno = 1; 17 | i = 2; 18 | 19 | do{ 20 | term = nume/deno; 21 | nume = -nume * x * x; 22 | deno = deno * i * (i+1); 23 | sum = sum + term; 24 | i = i + 2; 25 | }while(fabs(term)>=0.000001); 26 | 27 | printf("The sine of %d is %.3f\n",degree,sum); 28 | printf("The sine using built-in function for %d is %.3f\n",degree,sin(x)); 29 | } 30 | -------------------------------------------------------------------------------- /partition list.cpp: -------------------------------------------------------------------------------- 1 | ListNode* partition(ListNode* head, int x) { 2 | if(head == NULL || head->next == NULL) return head; 3 | ListNode* temp = head, *cur = head; 4 | while(temp){ 5 | if(temp->val < x){ 6 | int value = temp->val; 7 | ListNode* dup = cur; 8 | while(dup){ 9 | int y = dup->val; 10 | dup->val = value; 11 | value = y; 12 | if(dup == temp) break; 13 | dup=dup->next; 14 | } 15 | cur = cur->next; 16 | } 17 | temp = temp->next; 18 | } 19 | return head; 20 | } -------------------------------------------------------------------------------- /BubbleSort.py: -------------------------------------------------------------------------------- 1 | # Python program for implementation of Bubble Sort 2 | 3 | 4 | def bubbleSort(arr): 5 | n = len(arr) 6 | 7 | # Traverse through all array elements 8 | for i in range(n): 9 | 10 | # Last i elements are already in place 11 | for j in range(0, n-i-1): 12 | 13 | # traverse the array from 0 to n-i-1 14 | # Swap if the element found is greater 15 | # than the next element 16 | if arr[j] > arr[j+1]: 17 | arr[j], arr[j+1] = arr[j+1], arr[j] 18 | 19 | 20 | # Driver code to test above 21 | if __name__ == "__main__": 22 | arr = [64, 34, 25, 12, 22, 11, 90] 23 | 24 | bubbleSort(arr) 25 | 26 | print("Sorted array is:") 27 | for i in range(len(arr)): 28 | print("%d" % arr[i], end=" ") 29 | -------------------------------------------------------------------------------- /CycleSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void cyclicSort(int arr[], int n){ 5 | int i = 0; 6 | while(i < n) 7 | { 8 | 9 | int correct = arr[i] - 1 ; 10 | if(arr[i] != arr[correct]){ 11 | swap(arr[i], arr[correct]) ; 12 | }else{ 13 | i++ ; 14 | } 15 | } 16 | 17 | } 18 | 19 | void printArray(int arr[], int size) 20 | { 21 | int i; 22 | for (i = 0; i < size; i++) 23 | cout << arr[i] << " "; 24 | cout << endl; 25 | } 26 | 27 | int main() { 28 | 29 | int arr[] = { 3, 2, 4, 5, 1}; 30 | int n = sizeof(arr) / sizeof(arr[0]); 31 | cout << "Before sorting array: \n"; 32 | printArray(arr, n); 33 | cyclicSort(arr, n); 34 | cout << "Sorted array: \n"; 35 | printArray(arr, n); 36 | return 0; 37 | 38 | } 39 | -------------------------------------------------------------------------------- /ArrayLargest.java: -------------------------------------------------------------------------------- 1 | // Java program to find maximum 2 | // in arr[] of size n 3 | import java.io.*; 4 | 5 | class GFG { 6 | 7 | static int largest(int arr[], int n, int i) 8 | { 9 | // last index 10 | // return the element 11 | if (i == n - 1) { 12 | return arr[i]; 13 | } 14 | 15 | // find the maximum from rest of the array 16 | int recMax = largest(arr, n, i + 1); 17 | 18 | // compare with i-th element and return 19 | return Math.max(recMax, arr[i]); 20 | } 21 | 22 | // Driver Code 23 | public static void main(String[] args) 24 | { 25 | int arr[] = { 10, 324, 45, 90, 9808 }; 26 | int n = arr.length; 27 | System.out.println("Largest in given array is " 28 | + largest(arr, n, 0)); 29 | } 30 | } 31 | 32 | -------------------------------------------------------------------------------- /DynamicProgramming/Subsequences.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector> vec; 5 | 6 | void printSubSeq(int idx,vector &arr, vector &v){ 7 | if(idx==arr.size()){ 8 | vec.push_back(v); 9 | return; 10 | } 11 | v.push_back(arr[idx]); 12 | printSubSeq(idx+1,arr,v); 13 | v.pop_back(); 14 | 15 | printSubSeq(idx+1,arr,v); 16 | } 17 | 18 | int main(){ 19 | vector arr; 20 | int siz; 21 | cin>>siz; 22 | for(int i=0;i>a; 25 | arr.push_back(a); 26 | } 27 | vector v; 28 | printSubSeq(0,arr,v); 29 | for(auto i :vec){ 30 | for(auto j:i){ 31 | cout< 2 | using namespace std; 3 | int mini = INT_MAX; 4 | void getMinimum(vector &v, vector &vm,int m, int idx){ 5 | if(idx>=v.size()){ 6 | return; 7 | } 8 | 9 | if(vm.size() == m){ 10 | sort(vm.begin(), vm.end()); 11 | mini = min(mini,abs(vm[m-1] - vm[0])); 12 | } 13 | 14 | vm.push_back(v[idx]); 15 | getMinimum(v,vm,m,idx+1); 16 | vm.pop_back(); 17 | getMinimum(v,vm,m,idx+1); 18 | 19 | } 20 | 21 | int main(){ 22 | int n; 23 | cin>>n ; 24 | vector v(n); 25 | for(int i =0 ;i>v[i]; 27 | } 28 | int m; 29 | cin>>m; 30 | vector vec; 31 | getMinimum(v,vec,m,0); 32 | cout< 2 | int main() { 3 | int year; 4 | printf("Enter a year: "); 5 | scanf("%d", &year); 6 | 7 | // leap year if perfectly divisible by 400 8 | if (year % 400 == 0) { 9 | printf("%d is a leap year.", year); 10 | } 11 | // not a leap year if divisible by 100 12 | // but not divisible by 400 13 | else if (year % 100 == 0) { 14 | printf("%d is not a leap year.", year); 15 | } 16 | // leap year if not divisible by 100 17 | // but divisible by 4 18 | else if (year % 4 == 0) { 19 | printf("%d is a leap year.", year); 20 | } 21 | // all other years are not leap years 22 | else { 23 | printf("%d is not a leap year.", year); 24 | } 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /DynamicProgramming/PrintAllSubsequenceUsingRecursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void getAllSubsequence(vector &vec,int idx, vector &v, vector> &vv){ 4 | if(idx>=vec.size()){ 5 | vv.push_back(v); 6 | return; 7 | } 8 | v.push_back(vec[idx]); 9 | getAllSubsequence(vec,idx+2,v,vv); 10 | v.pop_back(); 11 | getAllSubsequence(vec,idx+1,v,vv); 12 | } 13 | 14 | int main(){ 15 | int n ; 16 | cin>>n; 17 | vector vec(n); 18 | for(int i =0; i>vec[i]; 20 | } 21 | vector> vv; 22 | vector v; 23 | getAllSubsequence(vec,0,v,vv); 24 | for(auto i:vv){ 25 | for(int j:i){ 26 | cout< 2 | using namespace std; 3 | 4 | 5 | bool binary_search(int target, vector arr){ 6 | int start = 0, end= arr.size()-1; 7 | 8 | while(start <= end){ 9 | int mid = start + (end-start)/2; 10 | 11 | if(arr[mid] == target) 12 | return true; 13 | else if(target > arr[mid]) 14 | start = mid+1; 15 | else if(target < arr[mid]) 16 | end = mid-1; 17 | } 18 | 19 | return false; 20 | } 21 | 22 | 23 | int main(){ 24 | 25 | int n; 26 | cin>>n; 27 | 28 | vector arr(n); 29 | 30 | for(int i=0; i>arr[i]; 32 | } 33 | 34 | int target; 35 | cin>>target; 36 | 37 | cout<<"Is "< findRelativeRanks(vector& score) { 2 | unordered_map mp; 3 | int n=score.size(); 4 | vector ans(score.size()); 5 | for(int i=0;i()); 9 | for(int i=0;i using namespace std; 2 | class Graph { 3 | public: 4 | map visited; map > adj; void addEdge(int v, int w); 5 | void DFS(int v); 6 | }; 7 | void Graph::addEdge(int v, int w){ 8 | adj[v].push_back(w); 9 | } 10 | void Graph::DFS(int v){ 11 | visited[v] = true; 12 | cout << v << "; 13 | list::iterator i; 14 | for (i = adj[v].begin(); i != adj[v].end(); ++i) 15 | if (!visited[*i]) 16 | DFS(*i); 17 | } 18 | int main(){ 19 | Graph g; 20 | g.addEdge(0, 1); 21 | g.addEdge(0, 2); 22 | g.addEdge(1, 2); 23 | g.addEdge(2, 0); 24 | g.addEdge(2, 3); 25 | g.addEdge(3, 3); 26 | cout << "Following is Depth First Traversal (starting from vertex 2) \n"; 27 | g.DFS(2); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /DynamicProgramming/binary_search_with_condition_on_not_find.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int n; 5 | cin>>n; 6 | int target; 7 | cin>>target; 8 | vector v(n); 9 | for(int i=0;i>v[i]; 11 | } 12 | sort(v.begin(),v.end()); 13 | int lo = 0, hi = n-1; 14 | bool isFound= false; 15 | int correctPosition=-1; 16 | 17 | while(lo<=hi){ 18 | int mid = lo + (hi-lo)/2; 19 | if(v[mid] == target){ 20 | cout<<"Found"<target){ 29 | hi = mid-1; 30 | correctPosition = hi; 31 | } 32 | } 33 | if (!isFound){ 34 | cout< 2 | using namespace std; 3 | int main(){ 4 | string s; 5 | string input; 6 | string line; 7 | 8 | while (getline(cin, line)){ 9 | if (line == "\n"){ 10 | break; 11 | } 12 | input.push_back('+'); 13 | input += line; 14 | } 15 | 16 | s= input; 17 | for(int i=1; i 5 | 6 | int main(void) 7 | { 8 | printf("hello, world\n"); 9 | } 10 | 11 | 12 | 13 | hello world in c++ language 14 | 15 | 16 | #include 17 | 18 | int main() 19 | { 20 | std::cout << "Hello, world!\n"; 21 | return 0; 22 | } 23 | 24 | 25 | 26 | hello world in c# language 27 | 28 | 29 | 30 | using System; 31 | 32 | class Program 33 | { 34 | static void Main(string[] args) 35 | { 36 | Console.WriteLine("Hello, world!"); 37 | } 38 | } 39 | 40 | 41 | hello world in java language 42 | 43 | 44 | 45 | class HelloWorldApp { 46 | public static void main(String[] args) { 47 | System.out.println("Hello World!"); // Prints the string to the console. 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /DynamicProgramming/quickSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int partition(vector &vec, int lo, int hi ){ 5 | int pivot = vec[hi]; 6 | int i = lo -1; 7 | 8 | for(int j= lo; j &vec, int lo,int hi){ 19 | if(lo>n; 31 | vector vec(n); 32 | for(int i=0;i>vec[i]; 34 | } 35 | 36 | quicksort(vec,0,n-1); 37 | for(int i:vec){ 38 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int maxSubArraySum(int a[], int size) 6 | { 7 | int max_so_far = INT_MIN, max_ending_here = 0, 8 | start = 0, end = 0, s = 0; 9 | 10 | for (int i = 0; i < size; i++) { 11 | max_ending_here += a[i]; 12 | 13 | if (max_so_far < max_ending_here) { 14 | max_so_far = max_ending_here; 15 | start = s; 16 | end = i; 17 | } 18 | 19 | if (max_ending_here < 0) { 20 | max_ending_here = 0; 21 | s = i + 1; 22 | } 23 | } 24 | cout << "Maximum contiguous sum is " << max_so_far 25 | << endl; 26 | cout << "Starting index " << start << endl 27 | << "Ending index " << end << endl; 28 | } 29 | 30 | int main() 31 | { 32 | int a[] = { -2, -3, 4, -1, -2, 1, 5, -3 }; 33 | int n = sizeof(a) / sizeof(a[0]); 34 | int max_sum = maxSubArraySum(a, n); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /HeapSort.py: -------------------------------------------------------------------------------- 1 | # Python program for implementation of heap Sort 2 | 3 | def heapify(arr, N, i): 4 | largest = i # Initialize largest as root 5 | l = 2 * i + 1 # left = 2*i + 1 6 | r = 2 * i + 2 # right = 2*i + 2 7 | 8 | if l < N and arr[largest] < arr[l]: 9 | largest = l 10 | if r < N and arr[largest] < arr[r]: 11 | largest = r 12 | if largest != i: 13 | arr[i], arr[largest] = arr[largest], arr[i] 14 | heapify(arr, N, largest) 15 | def heapSort(arr): 16 | N = len(arr) 17 | for i in range(N//2 - 1, -1, -1): 18 | heapify(arr, N, i) 19 | for i in range(N-1, 0, -1): 20 | arr[i], arr[0] = arr[0], arr[i] # swap 21 | heapify(arr, i, 0) 22 | 23 | # Driver's code 24 | if __name__ == '__main__': 25 | arr = [12, 11, 13, 5, 6, 7] 26 | heapSort(arr) 27 | N = len(arr) 28 | 29 | print("Sorted array is") 30 | for i in range(N): 31 | print("%d" % arr[i], end=" ") 32 | -------------------------------------------------------------------------------- /BubbleSort.java: -------------------------------------------------------------------------------- 1 | // Java program for implementation of Bubble Sort 2 | class BubbleSort { 3 | void bubbleSort(int arr[]) 4 | { 5 | int n = arr.length; 6 | for (int i = 0; i < n - 1; i++) 7 | for (int j = 0; j < n - i - 1; j++) 8 | if (arr[j] > arr[j + 1]) { 9 | // swap arr[j+1] and arr[j] 10 | int temp = arr[j]; 11 | arr[j] = arr[j + 1]; 12 | arr[j + 1] = temp; 13 | } 14 | } 15 | 16 | /* Prints the array */ 17 | void printArray(int arr[]) 18 | { 19 | int n = arr.length; 20 | for (int i = 0; i < n; ++i) 21 | System.out.print(arr[i] + " "); 22 | System.out.println(); 23 | } 24 | 25 | // Driver method to test above 26 | public static void main(String args[]) 27 | { 28 | BubbleSort ob = new BubbleSort(); 29 | int arr[] = { 64, 34, 25, 12, 22, 11, 90 }; 30 | ob.bubbleSort(arr); 31 | System.out.println("Sorted array"); 32 | ob.printArray(arr); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /EditDistance.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import static java.lang.System.out; 4 | 5 | class EditDistance { 6 | 7 | 8 | static int eD(String s1, String s2, int m, int n) 9 | { 10 | if(m==0) 11 | return n; 12 | if(n==0) 13 | return m; 14 | 15 | if(s1.charAt(m-1)==s2.charAt(n-1)) 16 | return eD(s1,s2,m-1,n-1); 17 | else 18 | { 19 | return 1 + Math.min(eD(s1,s2,m,n-1), Math.min(eD(s1,s2,m-1,n), 20 | eD(s1,s2,m-1,n-1))); 21 | } 22 | 23 | } 24 | 25 | 26 | public static void main (String[] args) { 27 | 28 | String s1="CAT", s2="CUT"; 29 | int n=3, m=3; 30 | 31 | 32 | System.out.println(eD(s1,s2,m,n)); 33 | 34 | } 35 | } -------------------------------------------------------------------------------- /Power_With_MOD.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | 5 | string converting_to_binary(ll b); 6 | 7 | ll power (ll a,string s1,ll m) 8 | { 9 | ll ans=1; 10 | for(ll i=s1.size()-1;i>=0;i--) 11 | { 12 | if(s1[i]=='1') 13 | ans=((ans%m)*(a%m))%m; 14 | //cout<>a>>b>>m; 25 | string s; 26 | s=converting_to_binary(b); 27 | ans=power(a,s,m); 28 | cout<0) 39 | { 40 | char p=(k%2)+48; 41 | s+=p; 42 | k/=2; 43 | } 44 | 45 | for(ll i=s.size()-1;i>=0;i--) 46 | s1+=s[i]; 47 | 48 | return s1; 49 | } -------------------------------------------------------------------------------- /ReverseArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void reverseArr(int arr[], int start, int end) 5 | { 6 | while (start < end) 7 | { 8 | int temp = arr[start]; 9 | arr[start] = arr[end]; 10 | arr[end] = temp; 11 | start++; 12 | end--; 13 | } 14 | } 15 | 16 | void printArr(int arr[], int size) 17 | { 18 | for (int i = 0; i < size; i++) 19 | { 20 | cout << arr[i] << "\t"; 21 | } 22 | } 23 | 24 | int main() 25 | { 26 | int arr[5]; 27 | for (int i = 0; i < 5; i++) 28 | { 29 | cout << "Enter " << i+1 << " element : "; 30 | cin >> arr[i]; 31 | } 32 | int n = sizeof(arr) / sizeof(arr[0]); 33 | cout << "\nInput array is : " << endl; 34 | printArr(arr, n); // To print original array 35 | reverseArr(arr, 0, n-1); 36 | cout << "\n\nReversed array is : " << endl; 37 | printArr(arr, n); // To print the Reversed array 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /HelloWorld.bf: -------------------------------------------------------------------------------- 1 | +++++ +++++ initialize counter (cell #0) to 10 2 | [ use loop to set the next four cells to 70/100/30/10 3 | > +++++ ++ add 7 to cell #1 4 | > +++++ +++++ add 10 to cell #2 5 | > +++ add 3 to cell #3 6 | > + add 1 to cell #4 7 | <<<< - decrement counter (cell #0) 8 | ] 9 | > ++ . print 'H' 10 | > + . print 'e' 11 | +++++ ++ . print 'l' 12 | . print 'l' 13 | +++ . print 'o' 14 | > ++ . print ' ' 15 | << +++++ +++++ +++++ . print 'W' 16 | > . print 'o' 17 | +++ . print 'r' 18 | ----- - . print 'l' 19 | ----- --- . print 'd' 20 | > + . print '!' 21 | > . print '\n' 22 | -------------------------------------------------------------------------------- /rotate_array.c++: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void Rotate(int arr[], int d, int n){ 4 | int temp[10]; 5 | int k = 0; 6 | for (int i = d; i < n; i++) { 7 | temp[k] = arr[i]; 8 | k++; 9 | } 10 | for (int i = 0; i < d; i++) { 11 | temp[k] = arr[i]; 12 | k++; 13 | } 14 | for (int i = 0; i < n; i++) { 15 | arr[i] = temp[i]; 16 | } 17 | } 18 | 19 | void PrintTheArray(int arr[], int n){ 20 | for (int i = 0; i < n; i++) { 21 | cout << arr[i] << " "; 22 | } 23 | } 24 | 25 | int main(){ 26 | int arr[10],N,d; 27 | cout<<"Enter the size of an array: "; 28 | cin>>N; 29 | cout<<"Enter the array items:"; 30 | for(int i=0; i>arr[i]; 32 | } 33 | cout<<"Enter the times to be rotated: "; 34 | cin>>d; 35 | Rotate(arr, d, N); 36 | PrintTheArray(arr, N); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | int fib(int n) 3 | { 4 | if (n <= 1) 5 | return n; 6 | return fib(n-1) + fib(n-2); 7 | } 8 | 9 | int main () 10 | { 11 | int n = 9; 12 | printf("%d", fib(n)); 13 | getchar(); 14 | return 0; 15 | } 16 | 17 | // Recursive code for fibonacci 18 | #include 19 | 20 | int factorial(int n) { 21 | //base case 22 | if(n == 0) { 23 | return 1; 24 | } else { 25 | return n * factorial(n-1); 26 | } 27 | } 28 | 29 | int fibbonacci(int n) { 30 | if(n == 0){ 31 | return 0; 32 | } else if(n == 1) { 33 | return 1; 34 | } else { 35 | return (fibbonacci(n-1) + fibbonacci(n-2)); 36 | } 37 | } 38 | 39 | int main() { 40 | int n = 5; 41 | int i; 42 | 43 | printf("Factorial of %d: %d\n" , n , factorial(n)); 44 | printf("Fibbonacci of %d: " , n); 45 | 46 | for(i = 0;i 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | float valueOne; 10 | float valueTwo; 11 | char operator; 12 | float answer; 13 | 14 | printf("Enter calculation:\n\n"); 15 | scanf("%f %c %f", &valueOne, &operator, & valueTwo); 16 | 17 | switch(operator) 18 | { 19 | case '/': answer = valueOne/valueTwo; 20 | break; 21 | case '*': answer = valueOne*valueTwo; 22 | break; 23 | case '+': answer = valueOne+valueTwo; 24 | break; 25 | case '-': answer = valueOne-valueTwo; 26 | break; 27 | case '^': answer = pow(valueOne,valueTwo); 28 | break; 29 | case ' ': answer = sqrt(valueTwo); 30 | break; 31 | default: goto fail; 32 | } 33 | printf("%.9g%c%.9g = %.6g\n\n",valueOne,operator, valueTwo, answer); 34 | goto exit; 35 | fail: 36 | printf("Fail.\n"); 37 | exit: 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /BreadthFirstSearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | class Graph{ 5 | int V; list *adj; 6 | public: 7 | Graph(int V); void addEdge(int v, int w); 8 | 9 | void BFS(int s); 10 | }; 11 | Graph::Graph(int V){ 12 | this->V = V; adj = new list[V]; 13 | } 14 | 15 | void Graph::addEdge(int v, int w){ 16 | adj[v].push_back(w); 17 | } 18 | 19 | void Graph::BFS(int s){ 20 | bool *visited = new bool[V]; for(int i = 0; i < V; i++) 21 | visited[i] = false; 22 | list queue; 23 | visited[s] = true; queue.push_back(s); 24 | list::iterator i; 25 | while(!queue.empty()){ 26 | s = queue.front(); cout << s << " "; queue.pop_front(); 27 | for (i = adj[s].begin(); i != adj[s].end(); ++i){ 28 | if (!visited[*i]){ 29 | visited[*i] = true; queue.push_back(*i); 30 | } 31 | } 32 | } 33 | } 34 | int main(){ 35 | Graph g(4); 36 | g.addEdge(0, 1); 37 | g.addEdge(0, 2); 38 | g.addEdge(1, 2); 39 | g.addEdge(2, 0); 40 | g.addEdge(2, 3); 41 | g.addEdge(3, 3); 42 | cout << "Following is Breadth First Traversal "<<"(starting from vertex 2) \n"; 43 | g.BFS(2); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /LinkedListReverse.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node { 5 | int data; 6 | struct Node* next; 7 | Node(int data) 8 | { 9 | this->data = data; 10 | next = NULL; 11 | } 12 | }; 13 | 14 | struct LinkedList { 15 | Node* head; 16 | LinkedList() { head = NULL; } 17 | void reverse(){ 18 | Node* current = head; 19 | Node *prev = NULL, *next = NULL; 20 | 21 | while (current != NULL) { 22 | next = current->next; 23 | current->next = prev; 24 | prev = current; 25 | current = next; 26 | } 27 | head = prev; 28 | } 29 | 30 | void print(){ 31 | struct Node* temp = head; 32 | while (temp != NULL) { 33 | cout << temp->data << " "; 34 | temp = temp->next; 35 | } 36 | } 37 | 38 | void push(int data){ 39 | Node* temp = new Node(data); 40 | temp->next = head; 41 | head = temp; 42 | } 43 | }; 44 | 45 | int main(){ 46 | LinkedList ll; 47 | ll.push(20); 48 | ll.push(4); 49 | ll.push(15); 50 | ll.push(85); 51 | ll.push(105); 52 | cout << "Given linked list\n"; 53 | ll.print(); 54 | ll.reverse(); 55 | 56 | cout << "\nReversed Linked list \n"; 57 | ll.print(); 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /FindLargestNumber.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.InputStreamReader; 3 | import java.io.IOException; 4 | 5 | public class FindLargestNumber { 6 | static Integer findMax(String str, int i,int max){ 7 | String temp = str.substring(0,i)+str.substring(i+1, str.length()); 8 | 9 | if(Integer.parseInt(temp)>max){ 10 | return Integer.parseInt(temp); 11 | } 12 | else{ 13 | return max; 14 | } 15 | 16 | } 17 | static Integer solve(String str,String num){ 18 | int max=0; 19 | int i=0; 20 | while(i 2 | using namespace std; 3 | 4 | void mergesort(vector &vec, int lo, int mid, int hi){ 5 | int s1 = mid-lo+1; 6 | int s2 = hi-mid; 7 | vector lvec,rvec; 8 | for(int i=0;i &vec, int lo, int hi) { 42 | if(lo>n ; 53 | vector vec(n); 54 | for(int i=0 ;i>vec[i]; 56 | } 57 | 58 | divide(vec,0,n); 59 | 60 | for(int i:vec){ 61 | cout< 4 | using namespace std; 5 | 6 | //Swap function 7 | void swap(int *xp, int *yp) 8 | { 9 | int temp = *xp; 10 | *xp = *yp; 11 | *yp = temp; 12 | } 13 | 14 | void selectionSort(int arr[], int n) 15 | { 16 | int i, j, min_idx; 17 | 18 | // One by one move boundary of 19 | // unsorted subarray 20 | for (i = 0; i < n-1; i++) 21 | { 22 | 23 | // Find the minimum element in 24 | // unsorted array 25 | min_idx = i; 26 | for (j = i+1; j < n; j++) 27 | if (arr[j] < arr[min_idx]) 28 | min_idx = j; 29 | 30 | // Swap the found minimum element 31 | // with the first element 32 | if(min_idx!=i) 33 | swap(&arr[min_idx], &arr[i]); 34 | } 35 | } 36 | 37 | //Function to print an array 38 | void printArray(int arr[], int size) 39 | { 40 | int i; 41 | for (i=0; i < size; i++) 42 | cout << arr[i] << " "; 43 | cout << endl; 44 | } 45 | 46 | // Driver program to test above functions 47 | int main() 48 | { 49 | int arr[] = {64, 25, 12, 22, 11}; 50 | int n = sizeof(arr)/sizeof(arr[0]); 51 | selectionSort(arr, n); 52 | cout << "Sorted array: \n"; 53 | printArray(arr, n); 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /GreedyBestFirstSearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | typedef pair pi; 5 | vector > graph; 6 | void addedge(int x, int y, int cost) 7 | { 8 | graph[x].push_back(make_pair(cost, y)); 9 | graph[y].push_back(make_pair(cost, x)); 10 | } 11 | void best_first_search(int source, int target, int n) 12 | { 13 | vector visited(n, false); 14 | priority_queue, greater > pq; 15 | pq.push(make_pair(0, source)); 16 | int s = source; 17 | visited[s] = true; while (!pq.empty()) 18 | { 19 | int x = pq.top().second; 20 | cout << x << " "; pq.pop(); 21 | if (x == target) 22 | break; 23 | for (int i = 0; i < graph[x].size(); i++) 24 | { 25 | if (!visited[graph[x][i].second]) 26 | { 27 | visited[graph[x][i].second] = true; 28 | pq.push(make_pair(graph[x][i].first,graph[x][i].second)); 29 | } 30 | } 31 | } 32 | } 33 | int main() 34 | { 35 | int v = 4; graph.resize(v); 36 | addedge(0, 1, 3); 37 | addedge(0, 2, 6); 38 | addedge(1, 2, 12); 39 | addedge(2, 0, 9); 40 | addedge(2, 3, 8); 41 | addedge(3, 3, 5); 42 | int source = 0; 43 | int target = 3; 44 | best_first_search(source, target, v); 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /DynamicProgramming/MyCode.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector nextGreaterElement(vector& nums1, vector& nums2) { 4 | int m=nums2.size(); 5 | stack s; 6 | unordered_map mp; 7 | for(int i=m-1;i>=0;i--) 8 | { 9 | if(s.size()==0) 10 | { 11 | mp[nums2[i]]= -1; 12 | } 13 | else if(s.size()>0 && s.top()>nums2[i]) 14 | { 15 | mp[nums2[i]]= s.top(); 16 | } 17 | else if(s.size()>0 && s.top()<=nums2[i]) 18 | { 19 | while(s.size()>0 && s.top()<=nums2[i]) 20 | { 21 | s.pop(); 22 | } 23 | if(s.size()==0) 24 | { 25 | mp[nums2[i]]= -1; 26 | } 27 | if(s.size()>0 && s.top()>nums2[i]) 28 | { 29 | mp[nums2[i]]= s.top(); 30 | } 31 | } 32 | s.push(nums2[i]); 33 | } 34 | vectorans; 35 | for(auto it: nums1) 36 | { 37 | ans.push_back(mp[it]); 38 | } 39 | return ans; 40 | 41 | } 42 | }; -------------------------------------------------------------------------------- /DynamicProgramming/3sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> threeSum(vector& nums) { 4 | //sort the given func 5 | sort(nums.begin(),nums.end()); 6 | vector>ans; 7 | vectortemp; 8 | //for traversing a 9 | for(int i=0;i0 && nums[i]!=nums[i-1]){ 12 | int lo=i+1,hi=nums.size()-1,sum=0-nums[i];//target-a 13 | while(lo 6 | 7 | void countSort(int arr[], int n, int exp) { 8 | int output[n]; 9 | int i, count[10] = { 0 }; 10 | 11 | for (i = 0; i < n; i++){ 12 | count[(arr[i] / exp) % 10]++; 13 | } 14 | 15 | for (i = 1; i < 10; i++){ 16 | count[i] += count[i - 1]; 17 | } 18 | 19 | for (i = n - 1; i >= 0; i--) { 20 | output[count[(arr[i] / exp) % 10] - 1] = arr[i]; 21 | count[(arr[i] / exp) % 10]--; 22 | } 23 | 24 | for (i = 0; i < n; i++){ 25 | arr[i] = output[i]; 26 | } 27 | } 28 | 29 | void radixsort(int arr[], int n) { 30 | int m = arr[0]; 31 | for(int i=0;im) 33 | m=arr[i]; 34 | } 35 | 36 | int exp; 37 | for (exp = 1; m / exp > 0; exp *= 10){ 38 | countSort(arr, n, exp); 39 | } 40 | } 41 | 42 | void print(int arr[], int n) { 43 | int i; 44 | for (i = 0; i < n; i++) 45 | printf("%d ", arr[i]); 46 | } 47 | 48 | int main() { 49 | int arr[] = { 170, 45, 75, 90, 802, 24, 2, 66 }; 50 | int n = sizeof(arr) / sizeof(arr[0]); 51 | radixsort(arr, n); 52 | print(arr, n); 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /subsetsum.cpp: -------------------------------------------------------------------------------- 1 | // because of the complexity of the size of the array or list we are going to make the tc as 2^n 2 | // so to avoid it we will use dp 3 | 4 | #include 5 | using namespace std; 6 | bool SubsetSum(int arr[], int n, int sum) 7 | { 8 | bool subset[n + 1][sum + 1]; 9 | for (int i = 0; i <= n; i++) 10 | { 11 | subset[i][0] = true; 12 | } 13 | 14 | for (int i = 1; i <= sum; i++) 15 | { 16 | subset[0][i] = false; 17 | } 18 | 19 | for (int i = 1; i <= n; i++) 20 | { 21 | for (int j = 1; j <= sum; j++) 22 | { 23 | if (j < arr[i - 1]) 24 | { 25 | subset[i][j] = subset[i - 1][j]; 26 | } 27 | if (j >= arr[i - 1]) 28 | { 29 | subset[i][j] = subset[i - 1][j] || subset[i - 1][j - arr[i - 1]]; 30 | } 31 | } 32 | } 33 | 34 | for (int i = 0; i <= n; i++) 35 | { 36 | for (int j = 0; j <= sum; j++) 37 | printf("%4d", subset[i][j]); 38 | cout << "\n"; 39 | } 40 | 41 | return subset[n][sum]; 42 | } 43 | int main() 44 | { 45 | int n; 46 | cin >> n; 47 | int sum; 48 | cin >> sum; 49 | int *arr = new int[n]; 50 | for (int i = 0; i < n; i++) 51 | { 52 | cin >> arr[i]; 53 | } 54 | if (SubsetSum(arr, n, sum) == true) 55 | { 56 | cout << "Found a subset :"; 57 | } 58 | else 59 | { 60 | cout << "No subset found :"; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /CountingSort.cpp: -------------------------------------------------------------------------------- 1 | // C++ Program for counting sort 2 | #include 3 | #include 4 | using namespace std; 5 | #define RANGE 255 6 | 7 | // The main function that sort 8 | // the given string arr[] in 9 | // alphabetical order 10 | void countSort(char arr[]) 11 | { 12 | // The output character array 13 | // that will have sorted arr 14 | char output[strlen(arr)]; 15 | 16 | // Create a count array to store count of individual 17 | // characters and initialize count array as 0 18 | int count[RANGE + 1], i; 19 | memset(count, 0, sizeof(count)); 20 | 21 | // Store count of each character 22 | for (i = 0; arr[i]; ++i) 23 | ++count[arr[i]]; 24 | 25 | // Change count[i] so that count[i] now contains actual 26 | // position of this character in output array 27 | for (i = 1; i <= RANGE; ++i) 28 | count[i] += count[i - 1]; 29 | 30 | // Build the output character array 31 | for (i = 0; arr[i]; ++i) { 32 | output[count[arr[i]] - 1] = arr[i]; 33 | --count[arr[i]]; 34 | } 35 | 36 | /* 37 | For Stable algorithm 38 | for (i = sizeof(arr)-1; i>=0; --i) 39 | { 40 | output[count[arr[i]]-1] = arr[i]; 41 | --count[arr[i]]; 42 | } 43 | 44 | For Logic : See implementation 45 | */ 46 | 47 | // Copy the output array to arr, so that arr now 48 | // contains sorted characters 49 | for (i = 0; arr[i]; ++i) 50 | arr[i] = output[i]; 51 | } 52 | 53 | // Driver code 54 | int main() 55 | { 56 | char arr[] = "geeksforgeeks"; 57 | 58 | countSort(arr); 59 | 60 | cout << "Sorted character array is " << arr; 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /Gauss_Elimination.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int n; 6 | cout<<"how many variables "; 7 | cin>>n; 8 | float arr[n][n+1]; 9 | float x[n]; 10 | float sum=0; 11 | for(int i=0;i>arr[i][j]; 16 | } 17 | } 18 | for(int i=0;i=0; i--) 47 | { 48 | s=0; 49 | for(int j=i+1; j answer: 10 | print("Too high.") 11 | return turns - 1 12 | elif guess < answer: 13 | print("Too low.") 14 | return turns - 1 15 | else: 16 | print(f"You got it! The answer was {answer}.") 17 | 18 | #Make function to set difficulty. 19 | def set_difficulty(): 20 | level = input("Choose a difficulty. Type 'easy' or 'hard': ") 21 | if level == "easy": 22 | return EASY_LEVEL_TURNS 23 | else: 24 | return HARD_LEVEL_TURNS 25 | 26 | def game(): 27 | #Choosing a random number between 1 and 100. 28 | print("Welcome to the Number Guessing Game!") 29 | print("I'm thinking of a number between 1 and 100.") 30 | answer = randint(1, 100) 31 | print(f"Pssst, the correct answer is {answer}") 32 | 33 | turns = set_difficulty() 34 | #Repeat the guessing functionality if they get it wrong. 35 | guess = 0 36 | while guess != answer: 37 | print(f"You have {turns} attempts remaining to guess the number.") 38 | 39 | #Let the user guess a number. 40 | guess = int(input("Make a guess: ")) 41 | 42 | #Track the number of turns and reduce by 1 if they get it wrong. 43 | turns = check_answer(guess, answer, turns) 44 | if turns == 0: 45 | print("You've run out of guesses, you lose.") 46 | return 47 | elif guess != answer: 48 | print("Guess again.") 49 | 50 | 51 | game() 52 | -------------------------------------------------------------------------------- /linkedlist.cpp: -------------------------------------------------------------------------------- 1 | //Program to detect loop in a linked list using fast and slow pointers 2 | 3 | #include 4 | using namespace std; 5 | 6 | /* Link list node */ 7 | struct Node { 8 | int data; 9 | struct Node* next; 10 | }; 11 | 12 | void push(struct Node** head_ref, int new_data) 13 | { 14 | /* allocate node */ 15 | struct Node* new_node = new Node; 16 | 17 | /* put in the data */ 18 | new_node->data = new_data; 19 | 20 | /* link the old list off the new node */ 21 | new_node->next = (*head_ref); 22 | 23 | /* move the head to point to the new node */ 24 | (*head_ref) = new_node; 25 | } 26 | 27 | // Returns true if there is a loop in linked list 28 | // else returns false. 29 | bool detectLoop(struct Node* h) 30 | { 31 | unordered_set s; 32 | while (h != NULL) { 33 | // If this node is already present 34 | // in hashmap it means there is a cycle 35 | // (Because you will be encountering the 36 | // node for the second time). 37 | if (s.find(h) != s.end()) 38 | return true; 39 | 40 | // If we are seeing the node for 41 | // the first time, insert it in hash 42 | s.insert(h); 43 | 44 | h = h->next; 45 | } 46 | 47 | return false; 48 | } 49 | 50 | /* Driver program to test above function*/ 51 | int main() 52 | { 53 | /* Start with the empty list */ 54 | struct Node* head = NULL; 55 | 56 | push(&head, 20); 57 | push(&head, 4); 58 | push(&head, 15); 59 | push(&head, 10); 60 | 61 | /* Create a loop for testing */ 62 | head->next->next->next->next = head; 63 | 64 | if (detectLoop(head)) 65 | cout << "Loop found"; 66 | else 67 | cout << "No Loop"; 68 | 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /Infix_to_Postfix.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | import java.util.Stack; 3 | public class Infix_to_Postfix { 4 | static int Precedence(char ch){ 5 | switch (ch){ 6 | case '+': 7 | case '-': 8 | return 1; 9 | case '*': 10 | case '/': 11 | return 2; 12 | case '^': 13 | return 3; 14 | } 15 | return -1; 16 | } 17 | static String infixToPostfix(String exp){ 18 | String result = new String(""); 19 | Stack stack = new Stack<>(); 20 | for (int i = 0; i=value2){ 39 | total+=value1; 40 | i++; 41 | } 42 | else{ 43 | total+=(value2-value1); 44 | i=i+2; 45 | } 46 | 47 | } 48 | return total; 49 | } 50 | 51 | public static void main(String[] args) throws IOException{ 52 | BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); 53 | String str = bf.readLine(); 54 | int res=romanToDecimal(str); 55 | System.out.println("res-:"+res); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /linkedlist traversal.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node * f=NULL; 4 | struct node * r=NULL; 5 | struct node { 6 | int size; 7 | struct node *link; 8 | int data; 9 | }; 10 | void linkedtraversal(struct node * ptr) 11 | { 12 | while(ptr!=NULL) 13 | { 14 | printf("elements in the linked list is%d\n",ptr->data); 15 | ptr=ptr->link; 16 | } 17 | } 18 | int isFull() 19 | { 20 | struct node * n=(struct node *)malloc(sizeof(struct node)); 21 | if(n==NULL) 22 | { 23 | return 1; 24 | } 25 | else 26 | { 27 | return 0; 28 | } 29 | 30 | 31 | } 32 | int isEmpty(struct node * f) 33 | { 34 | if(f==NULL) 35 | { 36 | return 1; 37 | } 38 | else 39 | { 40 | return 0; 41 | } 42 | } 43 | void equene(int val) 44 | { 45 | struct node * n=(struct node *)malloc(sizeof(struct node)); 46 | if(n==NULL) 47 | { 48 | printf("quene is FULl"); 49 | } 50 | 51 | else 52 | { 53 | 54 | n->data=val; 55 | n->link=NULL; 56 | if(f==NULL) 57 | { 58 | f=r=n; 59 | } 60 | else 61 | { 62 | r->link=n; 63 | r=n; 64 | } 65 | } 66 | } 67 | int dequene() 68 | { 69 | int a=-1; 70 | struct node * ptr=f; 71 | if(isEmpty(f)) 72 | { 73 | printf("quene underflow\n"); 74 | } 75 | else 76 | { 77 | f=f->link; 78 | a=ptr->data; 79 | free(ptr); 80 | } 81 | return a; 82 | } 83 | 84 | 85 | int main() 86 | { 87 | linkedtraversal(f); 88 | equene(45); 89 | equene(70); 90 | equene(80); 91 | 92 | linkedtraversal(f); 93 | printf("dequqeing elements %d\n",dequene()); 94 | printf("dequqeing elements %d\n",dequene()); 95 | 96 | return 0; 97 | } 98 | -------------------------------------------------------------------------------- /QuickSort.py: -------------------------------------------------------------------------------- 1 | # Python program for implementation of Quicksort Sort 2 | 3 | # This implementation utilizes pivot as the last element in the nums list 4 | # It has a pointer to keep track of the elements smaller than the pivot 5 | # At the very end of partition() function, the pointer is swapped with the pivot 6 | # to come up with a "sorted" nums relative to the pivot 7 | 8 | 9 | # Function to find the partition position 10 | def partition(array, low, high): 11 | 12 | # choose the rightmost element as pivot 13 | pivot = array[high] 14 | 15 | # pointer for greater element 16 | i = low - 1 17 | 18 | # traverse through all elements 19 | # compare each element with pivot 20 | for j in range(low, high): 21 | if array[j] <= pivot: 22 | 23 | # If element smaller than pivot is found 24 | # swap it with the greater element pointed by i 25 | i = i + 1 26 | 27 | # Swapping element at i with element at j 28 | (array[i], array[j]) = (array[j], array[i]) 29 | 30 | # Swap the pivot element with the greater element specified by i 31 | (array[i + 1], array[high]) = (array[high], array[i + 1]) 32 | 33 | # Return the position from where partition is done 34 | return i + 1 35 | 36 | # function to perform quicksort 37 | 38 | 39 | def quickSort(array, low, high): 40 | if low < high: 41 | 42 | # Find pivot element such that 43 | # element smaller than pivot are on the left 44 | # element greater than pivot are on the right 45 | pi = partition(array, low, high) 46 | 47 | # Recursive call on the left of pivot 48 | quickSort(array, low, pi - 1) 49 | 50 | # Recursive call on the right of pivot 51 | quickSort(array, pi + 1, high) 52 | 53 | 54 | data = [1, 7, 4, 1, 10, 9, -2] 55 | print("Unsorted Array") 56 | print(data) 57 | 58 | size = len(data) 59 | 60 | quickSort(data, 0, size - 1) 61 | 62 | print('Sorted Array in Ascending Order:') 63 | print(data) 64 | -------------------------------------------------------------------------------- /Search_in_a_rotated_array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Submitted by: Sneha Jaiswal 3 | GitHub link: https://github.com/Sneha-jais 4 | PROBLEM STATEMENT: 5 | Given a sorted and rotated array A of N distinct elements which is rotated at some point, and 6 | given an element key. The task is to find the index of the given element key in the array A. 7 | ***************Expected Time Complexity: O(log N).****************** 8 | Expected Auxiliary Space: O(1) 9 | Input: n=9 10 | arr[]={5,6,7,8,9,10,1,2,3} 11 | key=10 12 | Output: 5 13 | 14 | */ 15 | 16 | 17 | #include 18 | using namespace std; 19 | 20 | int search_ind(int arr[],int low,int high,int key){ 21 | 22 | if(low<=high){ 23 | 24 | int mid=(low+high)/2; 25 | 26 | if(arr[mid]==key) 27 | return mid; 28 | 29 | //if first half array is sorted 30 | if(arr[low]<=arr[mid]){ 31 | if(key>=arr[low] && key<=arr[mid]) 32 | return search_ind(arr,low,mid-1,key); 33 | else 34 | return search_ind(arr,mid+1,high,key); 35 | } 36 | 37 | //if first is not sorted then second part definetly will be sorted 38 | else{ 39 | if (key >= arr[mid] && key <= arr[high]) 40 | return search_ind(arr, mid + 1, high, key); 41 | else 42 | return search_ind(arr, low, mid - 1, key); 43 | } 44 | 45 | 46 | } 47 | return -1; 48 | 49 | } 50 | 51 | int main() 52 | { 53 | int n; 54 | cout<<"Enter number of elements:\n"; 55 | cin>>n; 56 | int arr[n]; 57 | cout<<"Enter elements:\n"; 58 | for(int i=0;i>arr[i]; 60 | } 61 | cout<<"Enter element you want to search:\n"; 62 | int key; 63 | cin>>key; 64 | 65 | int temp = search_ind(arr, 0, n - 1, key); 66 | 67 | if (temp!= -1) 68 | cout << "Index of "<"); 30 | temp = temp.next; 31 | } 32 | System.out.print("Null"+"\n"); 33 | } 34 | 35 | /*Finding the length of the list.*/ 36 | public int getLen() 37 | { 38 | int length = 0; 39 | Node temp = head; 40 | while (temp != null) { 41 | length++; 42 | temp = temp.next; 43 | } 44 | return length; 45 | } 46 | 47 | /*Printing the middle element of the list.*/ 48 | public void printMiddle() 49 | { 50 | if (head != null) { 51 | int length = getLen(); 52 | Node temp = head; 53 | int middleLength = length / 2; 54 | while (middleLength != 0) { 55 | temp = temp.next; 56 | middleLength--; 57 | } 58 | System.out.print("The middle element is [" 59 | + temp.data + "]"); 60 | System.out.println(); 61 | } 62 | } 63 | 64 | public static void main(String[] args) 65 | { 66 | GFG list = new GFG(); 67 | for (int i = 5; i >= 1; i--) { 68 | list.pushNode(i); 69 | list.printNode(); 70 | list.printMiddle(); 71 | } 72 | } 73 | } -------------------------------------------------------------------------------- /Dijkstra’s Algo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define INF 0x3f3f3f3f 4 | 5 | 6 | typedef pair iPair; 7 | 8 | 9 | class Graph { 10 | int V; 11 | list >* adj; 12 | 13 | public: 14 | Graph(int V); 15 | 16 | 17 | void addEdge(int u, int v, int w); 18 | 19 | 20 | void shortestPath(int s); 21 | }; 22 | 23 | 24 | Graph::Graph(int V) 25 | { 26 | this->V = V; 27 | adj = new list[V]; 28 | } 29 | 30 | void Graph::addEdge(int u, int v, int w) 31 | { 32 | adj[u].push_back(make_pair(v, w)); 33 | adj[v].push_back(make_pair(u, w)); 34 | } 35 | 36 | 37 | void Graph::shortestPath(int src) 38 | { 39 | priority_queue, greater > 40 | pq; 41 | 42 | vector dist(V, INF); 43 | 44 | pq.push(make_pair(0, src)); 45 | dist[src] = 0; 46 | 47 | while (!pq.empty()) { 48 | 49 | int u = pq.top().second; 50 | pq.pop(); 51 | 52 | 53 | list >::iterator i; 54 | for (i = adj[u].begin(); i != adj[u].end(); ++i) { 55 | 56 | int v = (*i).first; 57 | int weight = (*i).second; 58 | 59 | 60 | if (dist[v] > dist[u] + weight) { 61 | 62 | dist[v] = dist[u] + weight; 63 | pq.push(make_pair(dist[v], v)); 64 | } 65 | } 66 | } 67 | 68 | 69 | printf("Vertex Distance from Source\n"); 70 | for (int i = 0; i < V; ++i) 71 | printf("%d \t\t %d\n", i, dist[i]); 72 | } 73 | 74 | 75 | int main() 76 | { 77 | 78 | int V = 9; 79 | Graph g(V); 80 | 81 | 82 | g.addEdge(0, 1, 4); 83 | g.addEdge(0, 7, 8); 84 | g.addEdge(1, 2, 8); 85 | g.addEdge(1, 7, 11); 86 | g.addEdge(2, 3, 7); 87 | g.addEdge(2, 8, 2); 88 | g.addEdge(2, 5, 4); 89 | g.addEdge(3, 4, 9); 90 | g.addEdge(3, 5, 14); 91 | g.addEdge(4, 5, 10); 92 | g.addEdge(5, 6, 2); 93 | g.addEdge(6, 7, 1); 94 | g.addEdge(6, 8, 6); 95 | g.addEdge(7, 8, 7); 96 | 97 | 98 | g.shortestPath(0); 99 | 100 | return 0; 101 | } 102 | -------------------------------------------------------------------------------- /FlatenABinaryTree.cpp: -------------------------------------------------------------------------------- 1 | //Flaten a Binary Tree 2 | //left of each node will be null 3 | #include 4 | using namespace std; 5 | struct Node{ 6 | int data; 7 | Node* left; 8 | Node* right; 9 | Node(int val){ 10 | data = val; 11 | left = NULL; 12 | right = NULL; 13 | } 14 | }; 15 | 16 | void printTree(Node* node) 17 | { 18 | if(node == NULL) 19 | return; 20 | 21 | cout<data<<" "; 22 | printTree(node->left); 23 | printTree(node->right); 24 | } 25 | void printInOrder(Node* root) 26 | { 27 | if(root==NULL) 28 | return; 29 | 30 | printInOrder(root->left); 31 | 32 | cout<data<<" "; 33 | 34 | printInOrder(root->right); 35 | 36 | } 37 | 38 | int searchIn(int in[],int start,int end,int curr) 39 | { 40 | for(int i=start;i<=end;i++) 41 | { 42 | if(in[i]==curr) 43 | return i; 44 | } 45 | return -1; 46 | } 47 | 48 | Node* treePreIn(int pre[], int in[], int start, int end) 49 | { 50 | if(start>end) 51 | return NULL; 52 | 53 | static int idx = 0; 54 | int curr = pre[idx]; 55 | idx++; 56 | Node* node = new Node(curr); 57 | 58 | if(start == end) 59 | return node; 60 | 61 | int pos = searchIn(in,start,end,curr); 62 | node->left = treePreIn(pre,in,start,pos-1); 63 | node->right = treePreIn(pre,in,pos+1,end); 64 | 65 | return node; 66 | } 67 | 68 | void treeFlatten(Node* root) 69 | { 70 | if(root == NULL || (root->left == NULL && root->right == NULL)) 71 | return; 72 | 73 | if(root->left != NULL) 74 | { 75 | treeFlatten(root->left); 76 | Node* temp = root->right; 77 | root->right = root->left; 78 | root->left = NULL; 79 | Node* t = root->right; 80 | while(t->right!=NULL) 81 | t = t->right; 82 | 83 | t->right = temp; 84 | } 85 | treeFlatten(root->right); 86 | } 87 | // 4 88 | // 9 5 89 | // 1 3 6 90 | 91 | int main () 92 | { 93 | int preOrder[] = {4,9,1,3,5,6}; 94 | int inOrder[] = {1,9,3,4,5,6}; 95 | 96 | Node* root = treePreIn(preOrder,inOrder,0,5); 97 | // cout<right->right->data<data; 103 | printInOrder(root); 104 | } -------------------------------------------------------------------------------- /Trie.cpp: -------------------------------------------------------------------------------- 1 | const int SZ = 26; 2 | struct Trie { 3 | struct TrieNode { 4 | TrieNode* children[SZ]; 5 | int cnt; 6 | 7 | TrieNode() { 8 | for(int i = 0; i < SZ; i++) 9 | { 10 | children[i] = nullptr; 11 | } 12 | cnt = 0; 13 | } 14 | }; 15 | 16 | TrieNode* root = new TrieNode(); // "" 17 | 18 | void insert(const string& word) 19 | { 20 | TrieNode* cur = root; 21 | for(int i = 0; i < word.size(); i++) 22 | { 23 | int idx = word[i] - 'a'; 24 | cur->cnt++; 25 | if(!cur->children[idx]) 26 | { 27 | cur->children[idx] = new TrieNode(); 28 | } 29 | cur = cur->children[idx]; 30 | } 31 | cur->cnt++; 32 | } 33 | 34 | int get_ans(const string& word) 35 | { 36 | TrieNode* cur = root; 37 | for(int i = 0; i < word.size(); i++) 38 | { 39 | int idx = word[i] - 'a'; 40 | if(!cur->children[idx]) return 0; 41 | cur = cur->children[idx]; 42 | } 43 | return cur->cnt; 44 | } 45 | 46 | bool is_empty(TrieNode* cur) 47 | { 48 | if(!cur) return true; 49 | for(int i = 0; i < SZ; i++) 50 | { 51 | if(cur->children[i]) return false; 52 | } 53 | return true; 54 | } 55 | 56 | void remove(const string& word) 57 | { 58 | remove(root, word); 59 | } 60 | 61 | // void remove(TrieNode* cur, const string& word) 62 | // { 63 | // for(int i = 0; i < word.size(); i++) 64 | // { 65 | // cur->cnt--; 66 | // int idx = word[i] - 'a'; 67 | // cur = cur->children[idx]; 68 | // } 69 | // cur->cnt--; 70 | // } 71 | TrieNode* remove(TrieNode* cur, const string& word, int idx = 0) 72 | { 73 | cur->cnt--; 74 | if(idx == word.size()) 75 | { 76 | if(cur->cnt == 0) 77 | { 78 | delete cur; 79 | cur = nullptr; 80 | } 81 | return cur; 82 | } 83 | 84 | int key = word[idx] - 'a'; 85 | cur->children[key] = remove(cur->children[key], word, idx+1); 86 | 87 | if(is_empty(cur) && cur->cnt == 0) 88 | { 89 | delete cur; 90 | cur = nullptr; 91 | } 92 | return cur; 93 | } 94 | }; -------------------------------------------------------------------------------- /merge_sort.cpp: -------------------------------------------------------------------------------- 1 | #Merge Sort in C++ 2 | 3 | 4 | #include 5 | using namespace std; 6 | typedef long long int ll; 7 | typedef long double ld; 8 | typedef vector vll; 9 | typedef vector> vp; 10 | typedef map ml; 11 | typedef unordered_map uml; 12 | #define pb push_back 13 | #define ff first 14 | #define ss second 15 | const ll inf = INT_MAX; 16 | const ll mod = 1000000007; 17 | ll gcd(ll a, ll b) 18 | { 19 | if (b == 0) 20 | return a; 21 | return gcd(b, a % b); 22 | } 23 | ll lcm(ll a, ll b) 24 | { 25 | return (a / gcd(a, b)) * b; 26 | } 27 | int factorial(ll n) 28 | { 29 | return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); 30 | } 31 | ll sm(ll n) 32 | { 33 | ll ans = n * (n + 1) / 2; 34 | return ans; 35 | } 36 | bool isprime(ll x) 37 | { 38 | for (ll i = 2; i * i <= x; i++) 39 | { 40 | if (x % i == 0) 41 | return 0; 42 | } 43 | return 1; 44 | } 45 | bool perfectSquare(ld x) 46 | { 47 | ld sr = sqrt(x); 48 | return ((sr - floor(sr)) == 0); 49 | } 50 | //function to join two sorted parts of array 51 | void merge(ll *arr, ll p, ll q, ll r) 52 | { 53 | ll n1 = q - p + 1; 54 | ll n2 = r - q; 55 | ll left[n1 + 1]; 56 | ll right[n2 + 1]; 57 | ll i, j; 58 | for (i = 0; i < n1; i++) 59 | { 60 | left[i] = arr[i + p]; 61 | } 62 | for (i = 0; i < n2; i++) 63 | { 64 | right[i] = arr[i + q + 1]; 65 | } 66 | left[n1] = INT_MAX; 67 | right[n2] = INT_MAX; 68 | i = 0, j = 0; 69 | for (ll k = p; k < r + 1; k++) 70 | { 71 | if (left[i] < right[j]) 72 | { 73 | arr[k] = left[i]; 74 | i++; 75 | } 76 | else if (left[i] > right[j]) 77 | { 78 | arr[k] = right[j]; 79 | j++; 80 | } 81 | } 82 | } 83 | // recursive function which calls the merge sort 84 | void merge_sort(ll *arr, ll p, ll r) 85 | { 86 | if (p < r) 87 | { 88 | ll q = ceil((p + r) / 2); 89 | merge_sort(arr, p, q); 90 | merge_sort(arr, q + 1, r); 91 | merge(arr, p, q, r); 92 | } 93 | } 94 | 95 | int main(){ 96 | ios_base::sync_with_stdio(0); 97 | cin.tie(0); 98 | ll test_case = 1; 99 | // cin >> test_case; 100 | while (test_case--){ 101 | ll n, i; 102 | cin >> n; 103 | ll arr[n]; 104 | ll ans[n]; 105 | for (i = 0; i < n; i++) 106 | cin >> arr[i]; 107 | merge_sort(arr, 0, n - 1); 108 | for (i = 0; i < n; i++) 109 | cout << arr[i] << " "; 110 | cout << endl; 111 | } 112 | return 0; 113 | } 114 | -------------------------------------------------------------------------------- /Pre_Post_Inorder_traversal_in_a_single_flow.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | #define null NULL 7 | 8 | struct Node 9 | { 10 | int data; 11 | struct Node *left; 12 | struct Node *right; 13 | 14 | Node(int val) 15 | { 16 | data = val; 17 | left = right = null; 18 | } 19 | }; 20 | 21 | class io 22 | { 23 | public: 24 | vector takeVectorInput(vector vec, int vectorSize) 25 | { 26 | for (int index = 0; index < vectorSize; index++) 27 | { 28 | int num; 29 | cin >> num; 30 | vec.push_back(num); 31 | } 32 | return vec; 33 | } 34 | 35 | void printVector(vector vec) 36 | { 37 | for (auto index = vec.begin(); index != vec.end(); index++) 38 | cout << *index << " "; 39 | 40 | cout << endl; 41 | } 42 | }; 43 | 44 | class Solution 45 | { 46 | public: 47 | void preInPostTraversal(Node *root) 48 | { 49 | stack> st; 50 | st.push({root, 1}); 51 | vector pre, post, in; 52 | 53 | while (!st.empty()) 54 | { 55 | auto it = st.top(); 56 | st.pop(); 57 | if (it.second == 1) 58 | { 59 | pre.push_back(it.first->data); 60 | it.second++; 61 | st.push(it); 62 | if (it.first->left != null) 63 | st.push({it.first->left, 1}); 64 | } 65 | else if (it.second == 2) 66 | { 67 | in.push_back(it.first->data); 68 | it.second++; 69 | st.push(it); 70 | if (it.first->right != null) 71 | st.push({it.first->right, 1}); 72 | } 73 | else 74 | post.push_back(it.first->data); 75 | } 76 | 77 | io obIo; 78 | 79 | cout << "Preorder: "; 80 | obIo.printVector(pre); 81 | 82 | cout << "Postorder: "; 83 | obIo.printVector(post); 84 | 85 | cout << "Inorder: "; 86 | obIo.printVector(in); 87 | } 88 | }; 89 | 90 | int main() 91 | { 92 | struct Node *root = new Node(1); 93 | 94 | // left 95 | root->left = new Node(2); 96 | root->left->left = new Node(3); 97 | root->left->right = new Node(4); 98 | 99 | // right 100 | root->right = new Node(5); 101 | root->right->left = new Node(6); 102 | root->right->right = new Node(7); 103 | 104 | Solution obSol; 105 | obSol.preInPostTraversal(root); 106 | 107 | /* 108 | The Tree will look like 109 | 1 110 | 2 5 111 | 3 4 6 7 112 | */ 113 | return 0; 114 | } -------------------------------------------------------------------------------- /Valid_Parentheses.java: -------------------------------------------------------------------------------- 1 | // Github username: Priyangshu1711 2 | // Aim: To check if any the given String with parentheses, has valid parentheses or not . 3 | //Data Structure Used: ~STACK~ 4 | //Time complexity: O(n) 5 | //Space complexity: O(n) 6 | // Date: 08/10/2022 7 | 8 | // **************************************************************** // 9 | 10 | import java.util.Stack;// to use the Stack class from the util package. 11 | import java.util.Scanner; 12 | 13 | class Valid_Parentheses { 14 | public boolean isValid(String s) { 15 | // creating object for the Stack class. here st. 16 | Stack st = new Stack(); 17 | // making a character array of the string. 18 | for (char c : s.toCharArray()){ 19 | String cc = String.valueOf(c); 20 | // to check if the parantheis begun. 21 | if(c=='{'|| c=='['|| c =='('){ 22 | st.push(cc); 23 | } 24 | else{ 25 | // if the input is empty then returns false. 26 | if(st.empty())return false; 27 | char top = st.peek().toCharArray()[0]; 28 | //if the input has any other chatacters except parantheses then ignores and continues. 29 | if(c!='{' && c!='[' && c !='(' && c != '}' && c!= ']' && c!= ')' ){ 30 | continue; 31 | } 32 | // to check if the parentheses is closed respectively * 33 | if((top == '{' && c == '}') || 34 | (top == '[' && c== ']') || 35 | (top == '(' && c== ')')){ 36 | // * if yes then we will go on popping it from stack until its empty. 37 | st.pop() ; 38 | } 39 | // if the algorithm couldn't remove all the parentheses from the stack then returns false * 40 | else return false; 41 | } 42 | } 43 | // * but if it could remove all the parentheses from the stack then returns true. 44 | return st.empty(); 45 | 46 | } 47 | // the main function 48 | public static void main(String ...args ){ 49 | //creating object for the Valid_Parentheses class 50 | 51 | Valid_Parentheses obj = new Valid_Parentheses(); 52 | System.out.println("enter a valid string with parentheses eg-[{[helloworld])}] : "); 53 | try (Scanner sc = new Scanner (System.in)) { 54 | // taking the returned boolean value in flag. 55 | boolean flag = obj.isValid(sc.nextLine()); 56 | // if the flag was flase then prints false * 57 | if(flag==true){ 58 | System.out.println("True"); 59 | } 60 | // *else true. 61 | else{ 62 | System.out.println("False");} 63 | } 64 | } 65 | 66 | } 67 | // **************************************************************** // 68 | 69 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Instructions: 2 | 3 | *Make sure you have a GitHub account. In case you don't have one, you can create your account by visiting https://github.com/ and clicking on ``Sign up`` option at the top right corner.* 4 | 5 | ### 1. Register yourself for Hacktoberfest 2022 6 | ###### Link to register: (https://hacktoberfest.com/) 7 | Click on "Start Hacking" and add your GitHub account. 8 | 9 | ### 2. Star and Fork this Repository 10 | ###### You can star and fork this repository on GitHub by navigating at the top of this repository. 11 | 12 | GitHub repository URLs will reference both the username associated with the owner of the repository, as well as the repository name. For example, acmbvp is the owner of the Hacktoberfest repository, so the GitHub URL for this project is: 13 | 14 | https://github.com/acmbvp/Hacktoberfest/ 15 | 16 | 17 | ![screenshot-star](https://user-images.githubusercontent.com/37223446/47229499-79d87300-d3e5-11e8-9f2f-84e0d49e0439.png) 18 | 19 | ![screenshot-fork](https://user-images.githubusercontent.com/37223446/47229495-77761900-d3e5-11e8-9de0-1891f5f7b245.png) 20 | 21 | When you’re on the main page of a repository, you’ll see a button to "Star" and “Fork” the repository on the upper right-hand side of the page, underneath your user icon. 22 | 23 | ### 3. Clone the Repository 24 | 25 | To make your own local copy of the repository you would like to contribute to, let’s first open up a terminal window. 26 | 27 | We’ll use the `git clone` command along with the URL that points to your fork of the repository. 28 | 29 | This URL will be similar to the URL above, except now it will end with `.git.` In the cloud_haiku example above, the URL will look like this: 30 | https://github.com/your-username/Hacktoberfest.git 31 | 32 | You can alternatively copy the URL by using the green “Clone or download” button from your repository page that you just forked from the original repository page. Once you click the button, you’ll be able to copy the URL by clicking the binder button next to the URL: 33 | 34 | ![screenshot 73](https://user-images.githubusercontent.com/37223446/47234826-b495d780-d3f4-11e8-9077-859ed9078677.png) 35 | 36 | Once we have the URL, we’re ready to clone the repository. To do this, we’ll combine the git clone command with the repository URL from the command line in a terminal window: 37 | 38 | `git clone https://github.com/your-username/Hacktoberfest.git` 39 | 40 | ![screenshot 153](https://user-images.githubusercontent.com/37223446/47234624-0f7aff00-d3f4-11e8-81c4-9198e3e83860.png) 41 | 42 | ### 4. Create a New Branch 43 | 44 | To create your branch, from your terminal window, change your directory so that you are working in the directory of the repository. Be sure to use the actual name of the repository (i.e. Hacktoberfest) to switch into that directory. 45 | 46 | ##### `cd Hacktoberfest` 47 | 48 | Now, we’ll create our new branch with the git branch command. Make sure you name it descriptively so that others working on the project understand what you are working on. 49 | 50 | ![screenshot 154](https://user-images.githubusercontent.com/37223446/47234964-240bc700-d3f5-11e8-9837-50685ad44382.png) 51 | 52 | ##### `git branch new-branch` 53 | 54 | Now that our new branch is created, we can switch to make sure that we are working on that branch by using the git checkout command: 55 | 56 | ##### ` git checkout new-branch ` 57 | 58 | Once you enter the git `checkout` command, you will receive the following output: 59 | 60 | ###### `Output:` 61 | ##### `Switched to branch 'new-branch' ` 62 | 63 | 64 | At this point, you can now modify existing files or add new files to the project on your own branch. 65 | 66 | #### Make Changes Locally 67 | 68 | Once you have modified existing files or added new files to the project, you can add them to your local repository, which you can do with the git add command. Let’s add the -A flag to add all changes that we have made: 69 | 70 | ##### ` git add -A ` or ` git add . ` 71 | 72 | ![screenshot 155](https://user-images.githubusercontent.com/37223446/47253493-417e7680-d471-11e8-83cf-a4f969da5131.png) 73 | 74 | 75 | Next, we’ll want to record the changes that we made to the repository with the git commit command. 76 | 77 | *The commit message is an important aspect of your code contribution; it helps the other contributors fully understand the change you have made, why you made it, and how significant it is. Additionally, commit messages provide a historical record of the changes for the project at large, helping future contributors along the way.* 78 | 79 | 80 | ![screenshot 155](https://user-images.githubusercontent.com/37223446/47235390-87e2bf80-d3f6-11e8-81c0-e01b7463d038.png) 81 | 82 | If you have a very short message, you can record that with the -m flag and the message in quotes: 83 | 84 | ###### ` Example: ` 85 | ##### ` git commit -m "Updated Readme.md" ` 86 | 87 | ![screenshot 156](https://user-images.githubusercontent.com/37223446/47235961-3fc49c80-d3f8-11e8-906f-a1cfa7a15726.png) 88 | 89 | 90 | ###### At this point you can use the git push command to push the changes to the current branch of your forked repository: 91 | ###### ` Example:` 92 | ###### ` git push --set-upstream origin new-branch ` 93 | 94 | ### 5. Update Local Repository 95 | 96 | *While working on a project alongside other contributors, it is important for you to keep your local repository up-to-date with the project as you don’t want to make a pull request for code that will cause conflicts. To keep your local copy of the code base updated, you’ll need to sync changes.* 97 | 98 | We’ll first go over configuring a remote for the fork, then syncing the fork. 99 | 100 | ### 6. Configure a Remote for the Fork 101 | 102 | Next up, you’ll have to specify a new remote upstream repository for us to sync with the fork. This will be the original repository that you forked from. you’ll have to do this with the git remote add command. 103 | 104 | ###### ` git remote add upstream https://github.com/acmbvp/Hacktoberfest.git ` 105 | 106 | 107 | ![screenshot 74](https://user-images.githubusercontent.com/37223446/47235672-5f0efa00-d3f7-11e8-9e45-1af02d8b969b.png) 108 | 109 | In this example, // upstream // is the shortname we have supplied for the remote repository since in terms of Git, “upstream” refers to the repository that you cloned from. If you want to add a remote pointer to the repository of a collaborator, you may want to provide that collaborator’s username or a shortened nickname for the shortname. 110 | 111 | ### 7. Sync the Fork 112 | 113 | Once you have configured a remote that references the upstream and original repository on GitHub, you are ready to sync your fork of the repository to keep it up-to-date. 114 | To sync your fork, from the directory of your local repository in a terminal window, you’ll have to use the // git fetch // command to fetch the branches along with their respective commits from the upstream repository. Since you used the shortname “upstream” to refer to the upstream repository, you’ll have to pass that to the command: 115 | 116 | ##### ` git fetch upstream ` 117 | 118 | Switch to the local master branch of our repository: 119 | 120 | ##### ` git checkout master ` 121 | 122 | Now merge any changes that were made in the original repository’s master branch, that you will access through your local upstream/master branch, with your local master branch: 123 | 124 | ###### ` git merge upstream/master ` 125 | 126 | ### 8. Create Pull Request 127 | 128 | At this point, you are ready to make a pull request to the original repository. 129 | 130 | Navigate to your forked repository, and press the “New pull request” button on your left-hand side of your Repo page. 131 | 132 | # Keep contributing!!!! 133 | --------------------------------------------------------------------------------