diff --git a/AffirmationsInPython.py b/AffirmationsInPython.py new file mode 100644 index 0000000..02d4e9f --- /dev/null +++ b/AffirmationsInPython.py @@ -0,0 +1,77 @@ +# -*- coding: utf-8 -*- +""" +Created on Tue Oct 4 15:32:00 2022 + +@author: aarus +""" + +# Import turtle package +import turtle + +# Creating a turtle object(pen) +pen = turtle.Turtle() + +# Defining a method to draw curve +def curve(): + for i in range(200): + + # Defining step by step curve motion + pen.right(1) + pen.forward(1) + +# Defining method to draw a full heart +def heart(): + + # Set the fill color to red + pen.fillcolor('red') + + # Start filling the color + pen.begin_fill() + + # Draw the left line + pen.left(140) + pen.forward(113) + + # Draw the left curve + curve() + pen.left(120) + + # Draw the right curve + curve() + + # Draw the right line + pen.forward(112) + + # Ending the filling of the color + pen.end_fill() + +# Defining method to write text +def txt(): + + # Move turtle to air + pen.up() + + # Move turtle to a given position + pen.setpos(-68, 95) + + # Move the turtle to the ground + pen.down() + + # Set the text color to lightgreen + pen.color('black') + + # Write the specified text in + # specified font style and size + pen.write("YOU MATTER!!", font=( + "Andada Pro", 15, "bold")) + + + +# Draw a heart +heart() + +# Write text +txt() + +# To hide turtle +pen.ht() \ No newline at end of file diff --git a/AlternateSwap.cpp b/AlternateSwap.cpp new file mode 100644 index 0000000..52a3211 --- /dev/null +++ b/AlternateSwap.cpp @@ -0,0 +1,35 @@ +#include +using namespace std; + +void swapAlternate(int arr[], int size) +{ + for (int i = 0; i < size; i += 2) + { + if (i + 1 < size) + { + swap(arr[i], arr[i + 1]); + } + } +} + +void printArray(int arr[], int n) +{ + for (int i = 0; i < n; i++) + { + cout << arr[i] << " "; + } + cout << endl; +} + +int main() +{ + int arr[8] = {1, 3, 5, 7, 9, 2, 6, 0}; + int brr[5] = {1, 3, 5, 7, 9}; + + swapAlternate(arr, 8); + printArray(arr, 8); + + swapAlternate(brr, 5); + printArray(brr, 5); + return 0; +} \ No newline at end of file diff --git a/BellmanFord.cpp b/BellmanFord.cpp new file mode 100644 index 0000000..047de33 --- /dev/null +++ b/BellmanFord.cpp @@ -0,0 +1,56 @@ +#include "bits/stdc++.h" +using namespace std; + +const int INF = 1e9; + +int main(){ + int n,m;cin>>n>>m; + vector> edges; + cout<<"u v w"<>u>>v>>w; + edges.push_back({u,v,w}); + } + cout<>src; + vector dist(n,INF); + dist[src] = 0; + for (int iter = 0; iter < n-1; iter++) + { + bool change = false; + + for(auto e : edges){ + int u = e[0]; + int v = e[1]; + int w = e[2]; + + //for detection of cycle + if(dist[v] > w + dist[u]){ + change = true; + } + dist[v] = min(dist[v], w + dist[u]); + } + } + cout<<"VERTEX DISTANCE FROM SOURCE"< +#include +using namespace std; + +int main() +{ + int n; + cin >> n; + int ans=0; + int i=0; + while(n!=0) + { + int bit = n%10; + n/=10; + ans = (bit * pow(2,i)) + ans; + i++; + } + cout<< "Answer is " << ans < + +int binarySearch(int arr[], int l, int r, int x) +{ + if (r >= l) { + int mid = l + (r - l) / 2; + + if (arr[mid] == x) + return mid; + + if (arr[mid] > x) + return binarySearch(arr, l, mid - 1, x); + + return binarySearch(arr, mid + 1, r, x); + } + return -1; +} + +int main(void) +{ + int arr[] = { 2, 3, 4, 10, 40 }; + int n = sizeof(arr) / sizeof(arr[0]); + int x = 10; + int result = binarySearch(arr, 0, n - 1, x); + (result == -1) + ? printf("Element is not present in array") + : printf("Element is present at index %d", result); + return 0; +} diff --git a/BreadthFirstSearch.c b/BreadthFirstSearch.c new file mode 100644 index 0000000..cdd3f72 --- /dev/null +++ b/BreadthFirstSearch.c @@ -0,0 +1,37 @@ +#include +void BFS(int a[10][10], int n, int src, int s[10]) +{ + int f = 0, r = -1, q[20], i, v; + printf("Source Node: %d\n", src); + s[src] = 1; + q[++r] = src; + printf("Visited Nodes using BFS:\n"); + while (f <= r) + { + v = q[f++]; + for (i = 1; i <= n; i++) + if (s[i] == 0 && a[v][i]) + { + q[++r] = i; + printf("%d\t", i); + s[i] = 1; + } + } +} +int main() +{ + int a[10][10], n, i, j, s[20], src; + printf("Enter no. of Nodes in Graph\n"); + scanf("%d", &n); + printf("Enter the connections of Graph in Adjacency Matrix\n"); + for (i = 1; i <= n; i++) + for (j = 1; j <= n; j++) + scanf("%d", &a[i][j]); + printf("Enter the Source Node\n"); + scanf("%d", &src); + for (i = 1; i <= n; i++) + s[i] = 0; + BFS(a, n, src, s); + printf("\n"); + return 0; +} diff --git a/BubbleSort_OptimisedCode.cpp b/BubbleSort_OptimisedCode.cpp new file mode 100644 index 0000000..ce4b3e5 --- /dev/null +++ b/BubbleSort_OptimisedCode.cpp @@ -0,0 +1,63 @@ +// Optimized implementation of Bubble sort to avoid extra spaces. + +#include +#include +using namespace std; + + +void bubbleSort(int arr[], int n) +{ + int i, j, flag, temp; + + for (i = 0; i < n-1; i++) + { + flag=0; + for (j = 0; j < n-1-i; j++) + { + if (arr[j] > arr[j+1]) + { + temp=arr[j]; + arr[j]=arr[j+1]; + arr[j+1]=temp; + flag=1; + } + } + + // IF no two elements were swapped + // by inner loop, then break + if (flag==0) + break; + } +} + +// Function to print an array +void printArray(int arr[], int size) +{ + for (int i = 0; i < size; i++) + { + cout <<" "<< arr[i]; + } +} + +// Driver program to test above functions +int main() +{ + int n;// size of the array + cout<<"Enter the size of Array"<>n; + + int arr[n]; + cout<<"Enter the elements of the Array"<>arr[i]; + } + + bubbleSort(arr, n); + cout <<"Sorted array: \n"; + printArray(arr, n); + return 0; +} +// Time Complexity: +// Best Case: O(n) +// Worst case: O(n^2) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a375309..0417e12 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,2 +1,5 @@ A repo for beginner contributors in hacktober 2022 , you can create a file and upload a new program and pull. MAke sure the program is error free. Multiple languages allowed . Happy coding. + + +[Abhishek Kevin Gomes.txt](https://github.com/Anurag2622002/Codefornewccoder/files/9703528/Abhishek.Kevin.Gomes.txt) diff --git a/Cocktail Sort.cpp b/Cocktail Sort.cpp new file mode 100644 index 0000000..5e39ace --- /dev/null +++ b/Cocktail Sort.cpp @@ -0,0 +1,72 @@ +// C++ implementation of Cocktail Sort +#include +using namespace std; + +// Sorts array a[0..n-1] using Cocktail sort +void CocktailSort(int a[], int n) +{ +bool swapped = true; +int start = 0; +int end = n - 1; + +while (swapped) { +// reset the swapped flag on entering +// the loop, because it might be true from +// a previous iteration. +swapped = false; + +// loop from left to right same as +// the bubble sort +for (int i = start; i < end; ++i) { +if (a[i] > a[i + 1]) { + swap(a[i], a[i + 1]); + swapped = true; +} +} + +// if nothing moved, then array is sorted. +if (!swapped) +break; + +// otherwise, reset the swapped flag so that it +// can be used in the next stage +swapped = false; + +// move the end point back by one, because +// item at the end is in its rightful spot +--end; + +// from right to left, doing the +// same comparison as in the previous stage +for (int i = end - 1; i >= start; --i) { +if (a[i] > a[i + 1]) { + swap(a[i], a[i + 1]); + swapped = true; +} +} + +// increase the starting point, because +// the last stage would have moved the next +// smallest number to its rightful spot. +++start; +} +} + +/* Prints the array */ +void printArray(int a[], int n) +{ +for (int i = 0; i < n; i++) +printf("%d ", a[i]); +printf("\n"); +} + +// Driver code +int main() +{ +int arr[] = { 5, 1, 4, 2, 8, 0, 2 }; +int n = sizeof(arr) / sizeof(arr[0]); +CocktailSort(arr, n); +printf("Sorted array :\n"); +printArray(arr, n); +return 0; +} diff --git a/DecToBin.cpp b/DecToBin.cpp new file mode 100644 index 0000000..81f6798 --- /dev/null +++ b/DecToBin.cpp @@ -0,0 +1,19 @@ +#include +#include +using namespace std; + +int main() +{ + int n; + cin >> n; + int ans=1; //0 to run in online compiler + int i=0; + while(n!=0) + { + int bit = n & 1; + ans = (bit * pow(10,i)) + ans; + n = n >> 1; + i++; + } + cout<< "Answer is " << ans < +void DFS(int a[10][10], int n, int src, int s[10]) +{ + int i; + s[src] = 1; + printf("%d\t", src); + for (i = 1; i <= n; i++) + if (s[i] == 0 && a[src][i]) + DFS(a, n, i, s); +} +int main() +{ + int a[10][10], n, i, j, s[20], src; + printf("Enter no. of Nodes in Graph\n"); + scanf("%d", &n); + printf("Enter the connections of Graph in Adjacency Matrix\n"); + for (i = 1; i <= n; i++) + for (j = 1; j <= n; j++) + scanf("%d", &a[i][j]); + printf("Enter the Source Node\n"); + scanf("%d", &src); + printf("Source Node = %d\n", src); + for (i = 1; i <= n; i++) + s[i] = 0; + printf("Visited Nodes using DFS:\n"); + DFS(a, n, src, s); + printf("\n"); + return 0; +} \ No newline at end of file diff --git a/Diamond Pattern in Python b/Diamond Pattern in Python new file mode 100644 index 0000000..6be8698 --- /dev/null +++ b/Diamond Pattern in Python @@ -0,0 +1,12 @@ +#Diamond Pattern in Python by Kevin + +n = 5 +print("Pattern 1") +for a1 in range (0,n): + for a2 in range (a1): + print("*", end="") + print() +for a1 in range (n,0,-1): + for a2 in range (a1): + print("*", end="") + print() diff --git a/Fibonnaci.py b/Fibonnaci.py new file mode 100644 index 0000000..fe666f3 --- /dev/null +++ b/Fibonnaci.py @@ -0,0 +1,17 @@ +#Fibonacci series in Python + +def recur_fibo(n): + if n <= 1: + return n + else: + return(recur_fibo(n-1) + recur_fibo(n-2)) + +nterms = 10 + +# checking number is negative or positive if negative then enter positive number and if positive then print series +if nterms <= 0: + print("Plese enter a positive integer") +else: + print("Fibonacci sequence:") + for i in range(nterms): + print(recur_fibo(i)) diff --git a/FloydWarshallAlgo.cpp b/FloydWarshallAlgo.cpp new file mode 100644 index 0000000..f05856f --- /dev/null +++ b/FloydWarshallAlgo.cpp @@ -0,0 +1,47 @@ +#include "bits/stdc++.h" +using namespace std; + +const int INF = 1e9; + +int main(){ + vector> graph = + { {0,5,INF,10}, + {INF,0,3,INF}, + {INF,INF,0,1}, + {INF,INF,INF,0} + }; + + int n = graph.size(); + vector> dist = graph; + for(int k=0; k dist[i][k] + dist[k][j]){ + dist[i][j] = dist[i][k] + dist[k][j]; + } + } + } + } + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if(dist[i][j] == INF){ + cout<<"INF "; + } + else{ + cout< +using namespace std; + +int main(){ + + int rows, cols; + // Getting dimensions of rectangle + cout << "Enter the number of rows in rectangle - "; + cin >> rows; + + cout << "Enter the number of columns in rectangle - "; + cin >> cols; + + + cout << "Rectangle of dimensions " << rows << "*" << cols << endl; + + // Main logic to print hollow rectangle. + for( int i = 0; i < rows; i++ ) { + for( int j = 0; j < cols; j++ ){ + + // If the index is at the border, then + // print *. + if( i == 0 || i == rows - 1 || j == 0 || j == cols -1 ) + cout<<"* "; + else + cout<<" "; + } + cout< +#include + +void insertionSort(int arr[], int n) +{ + int i, key, j; + for (i = 1; i < n; i++) { + key = arr[i]; + j = i - 1; + + while (j >= 0 && arr[j] > key) { + arr[j + 1] = arr[j]; + j = j - 1; + } + arr[j + 1] = key; + } +} + +void printArray(int arr[], int n) +{ + int i; + for (i = 0; i < n; i++) + printf("%d ", arr[i]); + printf("\n"); +} + +int main() +{ + int arr[] = { 12, 11, 13, 5, 6 }; + int n = sizeof(arr) / sizeof(arr[0]); + + insertionSort(arr, n); + printArray(arr, n); + + return 0; +} diff --git a/Median_of_TwoSortedArrays_OptimisedCode.cpp b/Median_of_TwoSortedArrays_OptimisedCode.cpp new file mode 100644 index 0000000..a3c6967 --- /dev/null +++ b/Median_of_TwoSortedArrays_OptimisedCode.cpp @@ -0,0 +1,83 @@ +// Most Optimised and Efficient approach to find the median of Two sorted Arrays of different sizes + +#include +using namespace std; + +// Binary Search Method to find median +double MedianOfSortedArrays(vector& nums1, vector& nums2) +{ + if(nums2.size() < nums1.size()) return MedianOfSortedArrays(nums2,nums1); + // Swapping to make first array i.e. nums1 smaller + + int n1 = nums1.size(); + int n2 = nums2.size(); + + int low = 0; + int high= n1; + + while (low <= high) + { + int cut1 = (low+high) >> 1; // (low+high)/2 + int cut2 = (n1+n2+1) / 2 - cut1; + + int left1=cut1==0 ? INT_MIN : nums1[cut1-1]; + int left2=cut2==0 ? INT_MIN : nums2[cut2-1]; + + int right1=cut1==n1 ? INT_MAX : nums1[cut1]; + int right2=cut2==n2 ? INT_MAX : nums2[cut2]; + + + // if correct partition is done + if (left1<= right2 and left2<=right1) + { + if((n1+n2)%2 ==0 ) return (max(left1,left2)+ min(right1,right2))/2.0; + else return max(left1,left2); + } + else if (left1 > right2) + { + high = cut1-1; + } + else + { + low = cut1 + 1; + } + } + return 0.0; +} + +// Driver code +int main() +{ + cout<<"Enter the size of first array"<>n1; + vector arr1 ; + int a; + cout<<"Enter the elements of First array"<>a; + arr1.push_back(a); + } + + cout<<"Enter the size of second array"<>n2; + vector arr2; + + cout<<"Enter the elements of Second array"<>a; + arr2.push_back(a); + } + + cout << "Median of the two arrays are" << endl; + cout << MedianOfSortedArrays(arr1, arr2); + return 0; + + // Time Complexity: O(min(log n1, log n2)) [Since binary search is being applied on the smaller of the 2 arrays] + // Auxiliary Space: O(1) [ As no extra space is used ] + + +} diff --git a/PeakElement_of_an_Array.cpp b/PeakElement_of_an_Array.cpp new file mode 100644 index 0000000..83cf0d9 --- /dev/null +++ b/PeakElement_of_an_Array.cpp @@ -0,0 +1,52 @@ +// C++ program to find a peak element in an array using iterative Binary Search + +#include +using namespace std; + +// A binary search based function that returns index of a peak element in an array + +int PeakElement(int arr[], int size) +{ + int start = 0; + int end = size-1; + while(start <= end) + { + // finding mid element + int mid= start + (end - start)/2; + + if(mid>0 and mid arr[mid-1] and arr[mid] >arr[mid+1]) return mid; + else if(arr[mid-1] > arr[mid]) end=mid-1; + else start=mid+1; + } + else if(mid==0) + { + if(arr[0]>arr[1]) return 0; + else return 1; + } + else if(mid == size-1) + { + if(arr[size-1]>arr[size-2]) return size-1; + else return size -2; + } + + } + + return -1; +} + +// Driver Code +int main() +{ + int arr[] = { 10, 20, 15, 2, 23, 90, 67 }; + int N = sizeof(arr) / sizeof(arr[0]); + cout << "Index of a peak point is " << PeakElement(arr, N); + return 0; +} + +/* Time Complexity: O(log n), Where n is the number of elements in the input array +[ In each step our search becomes half,hence we can compare it to Binary search.So, The time complexity is O(log n) ] + + Space Complexity: O(1) [ Since no extra space is required ] + */ diff --git a/Quicksort.java b/Quicksort.java index d2da1fa..4e11a35 100644 --- a/Quicksort.java +++ b/Quicksort.java @@ -1,47 +1,45 @@ import java.io.*; -class GFG { - static void swap(int[] arr, int i, int j) - { - int temp = arr[i]; - arr[i] = arr[j]; - arr[j] = temp; - }static int partition(int[] arr, int low, int high) - { - int pivot = arr[high]; - int i = (low - 1); +import java.util.Arrays; - for (int j = low; j <= high - 1; j++) { - if (arr[j] < pivot) { - i++; - swap(arr, i, j); - } - } - swap(arr, i + 1, high); - return (i + 1); - } - static void quickSort(int[] arr, int low, int high) - { - if (low < high) { - int pi = partition(arr, low, high); - quickSort(arr, low, pi - 1); - quickSort(arr, pi + 1, high); - } - } -static void printArray(int[] arr, int size) - { - for (int i = 0; i < size; i++) - System.out.print(arr[i] + " "); +public class QuickSort { + public static void main(String[] args) { + int[] arr = {5, 4, 3, 2, 1}; +// sort(arr, 0, arr.length - 1); +// System.out.println(Arrays.toString(arr)); + Arrays.sort(arr); + } - System.out.println(); - } + static void sort(int[] nums, int low, int hi) { + if (low >= hi) { + return; + } - public static void main(String[] args) - { - int[] arr = { 10, 7, 8, 9, 1, 5 }; - int n = arr.length; + int s = low; + int e = hi; + int m = s + (e - s) / 2; + int pivot = nums[m]; - quickSort(arr, 0, n - 1); - System.out.println("Sorted array: "); - printArray(arr, n); - } + while (s <= e) { + + // also a reason why if its already sorted it will not swap + while (nums[s] < pivot) { + s++; + } + while (nums[e] > pivot) { + e--; + } + + if (s <= e) { + int temp = nums[s]; + nums[s] = nums[e]; + nums[e] = temp; + s++; + e--; + } + } + + // now my pivot is at correct index, please sort two halves now + sort(nums, low, e); + sort(nums, s, hi); + } } diff --git a/Rat in Maze b/Rat in Maze new file mode 100644 index 0000000..963c73d --- /dev/null +++ b/Rat in Maze @@ -0,0 +1,81 @@ +#include + +using namespace std; + +bool isSafe(int** arr,int x,int y,int n) + { + if(x> n; + + int** arr = new int*[n]; + for(int i=0;i>arr[i][j]; + } + } + + int** solArr = new int*[n]; + for(int i=0;i +using namespace std; + +// shell sort implementation +int shellSort(int arr[], int N) +{ + for (int gap = N/2; gap > 0; gap /= 2) + { + for (int i = gap; i < N; i += 1) + { + //sort sub lists created by applying gap +int temp = arr[i]; + +int j; +for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) +arr[j] = arr[j - gap]; + +arr[j] = temp; + } + } + return 0; +} + +int main() +{ + int arr[] = {45,23,53,43,18,24,8,95,101}, i; + //Calculate size of array + int N = sizeof(arr)/sizeof(arr[0]); + + cout << "Array to be sorted: \n"; + for (int i=0; i +using namespace std; + +// This function will return the K_th smallest element in the array +int kth_Smallest(int arr[], int k, int size) +{ + priority_queue maxh; // max heap initialisation + for(int i=0; ik) // if the size of the max heap exceeds k , it will remove the top element of the max heap + { + maxh.pop(); + } + } + return maxh.top(); // top element of the max heap will be the k_th smallest element of the array +} + + +// Driver Code +int main() +{ + int k1,k2; + cin>>k1>>k2; + + int n; + cin>>n; + int arr[n]; + for(int i=0; i>arr[i]; + } + + int first = kth_Smallest(arr,k1,n); // k1 smallest element in the array + int second = kth_Smallest(arr,k2,n);// k2 smallest element in the array + int sum=0; + + for(int i=0; ifirst and arr[i] +using namespace std; + +void Top_K_FrequentNumbers(int arr[], int n, int k) +{ + priority_queue , vector>, greater< pair> > minh; + unordered_map mp; + for(int i=0; isecond, i->first}); + + if(minh.size()>k) + { + minh.pop(); + } + } + + while(minh.size()>0) + { + cout<>n; + + int k; + cin>>k; + + int arr[n]; + cout<<"Enter the elements of the Array"<>arr[i]; + } + + Top_K_FrequentNumbers(arr,n,k); + + return 0; +} + +/* Time Complexity: O(K logd + d logd) + [ To remove the top of the priority queue O(log d) time is required, so if k elements are removed then O(k log d) time is required, and + To construct a priority queue with d elements, O(d logd) time is required] + + Space Complexity: O(d), where d is the count of distinct elements in the array. + */ diff --git a/TopologicalSort.cpp b/TopologicalSort.cpp new file mode 100644 index 0000000..1ab6248 --- /dev/null +++ b/TopologicalSort.cpp @@ -0,0 +1,75 @@ +#include +using namespace std; + +class Solution +{ + public: + void dfs(int node, int vis[], stack &st, vector adj[]) { + vis[node] = 1; + + for(auto x : adj[node]) { + if(!vis[x]) { + dfs(x, vis, st, adj); + } + } + + st.push(node); + } + + vector topoSort(int V, vector adj[]) + { + int vis[V] = {0}; + stack st; + + for(int i = 0; i < V; i++) { + if(!vis[i]) { + dfs(i, vis, st, adj); + } + } + + vector ans; + while(!st.empty()) { + ans.push_back(st.top()); + st.pop(); + } + + return ans; + } +}; + +int check(int V, vector &res, vector adj[]) { + vector map(V, -1); + for (int i = 0; i < V; i++) { + map[res[i]] = i; + } + for (int i = 0; i < V; i++) { + for (int v : adj[i]) { + if (map[i] > map[v]) return 0; + } + } + return 1; +} + +int main() { + int T; + cin >> T; + while (T--) { + int N, E; + cin >> E >> N; + int u, v; + + vector adj[N]; + + for (int i = 0; i < E; i++) { + cin >> u >> v; + adj[u].push_back(v); + } + + Solution obj; + vector res = obj.topoSort(N, adj); + + cout << check(N, res, adj) << endl; + } + + return 0; +} \ No newline at end of file diff --git a/Tower of Hanoi.py b/Tower of Hanoi.py new file mode 100644 index 0000000..cbf4246 --- /dev/null +++ b/Tower of Hanoi.py @@ -0,0 +1,14 @@ +#Tower of Hanoi +def tower_of_hanoi(disks, source, auxiliary, target): + if(disks == 1): + print('Move disk 1 from rod {} to rod {}.'.format(source, target)) + return + # function call itself + tower_of_hanoi(disks - 1, source, target, auxiliary) + print('Move disk {} from rod {} to rod {}.'.format(disks, source, target)) + tower_of_hanoi(disks - 1, auxiliary, source, target) + + +disks = int(input('Enter the number of disks: ')) +# We are referring source as A, auxiliary as B, and target as C +tower_of_hanoi(disks, 'A', 'B', 'C') diff --git a/TowerOfHanoi.c b/TowerOfHanoi.c new file mode 100644 index 0000000..35359a0 --- /dev/null +++ b/TowerOfHanoi.c @@ -0,0 +1,22 @@ +#include +#include + +void move(int n, char source, char dest, char spare) +{ + if (n == 1) + printf("\n Move from %c to %c", source, dest); + else + { + move(n - 1, source, spare, dest); + move(1, source, dest, spare); + move(n - 1, spare, dest, source); + } +} +int main() +{ + int n; + printf("\n Enter the number of rings: "); + scanf("%d", &n); + move(n, 'a', 'c', 'b'); + return 0; +} diff --git a/TowerogHanoi b/TowerogHanoi new file mode 100644 index 0000000..d123643 --- /dev/null +++ b/TowerogHanoi @@ -0,0 +1,24 @@ +#include +using namespace std; + +// Tower of Hanoi + + void TowerOfHanoi(int n, char source, char destiny, char helper) +{ + if(n==0) + { + return ; + } + + TowerOfHanoi(n-1,source,helper,destiny); + cout << "Moving from " << source << " to " << destiny << endl ; + TowerOfHanoi(n-1,helper,destiny,source); +} + +int main() +{ + int n; + cin >> n; + TowerOfHanoi(n,'A','C','B'); + return 0; +} diff --git a/atm.c b/atm.c new file mode 100644 index 0000000..9fbf149 --- /dev/null +++ b/atm.c @@ -0,0 +1,84 @@ +#include +#include + +int currentbal,initialbal,withdraw,deposit,choice,i,pin; +float principal, t, rate, SI; +char transaction ='y'; + +int main() +{ + while (pin != 3579) +{ + printf("ENTER YOUR PIN NUMBER: "); + scanf("%d", &pin); + if (pin != 3579) + printf("PLEASE ENTER VALID PASSWORD\n"); +} + +printf("Enter initial balance:\n"); +scanf("%d",&initialbal); +do{ + printf("Select your choice:"); + printf("*********Welcome to ATM Service*********\n"); + printf("1. Check Balance\n"); + printf("2. Deposit Cash\n"); + printf("3. Withdraw Cash\n"); + printf("4. View previous transaction\n"); + printf("5. Calculate interest accrued in the account\n"); + printf("6. Exit the application\n"); + printf("Enter your choice: \n"); + scanf("%d", &choice); + +switch(choice) +{ + case 1: + printf("The current balance is Rs.%d", currentbal ); + break; + + case 2: + printf("Enter the amount to deposit in the account: \n"); + scanf("%d", &deposit); + currentbal = initialbal + deposit; + break; + + case 3: + printf("Enter the amount to withdraw from the account: \n"); + scanf("%d", &withdraw); + currentbal = currentbal - withdraw; + break; + + case 4: + if(currentbal>initialbal) + printf("The overall deposit is made of Rs. %d\n", currentbal - initialbal); + else + printf("The overall withdrawal is made of Rs. %d\n", initialbal - currentbal); + break; + + case 5: + printf("Enter the principal amount: "); + scanf("%f", &principal); + printf("Enter the time period: "); + scanf("%f", &t); + printf("Enter the rate of interest: "); + scanf("%f", &rate); + SI = (principal * t * rate) / 100; + printf("Simple Interest for Principal Amount %.2f is %.2f", principal, SI); + break; + + case 6: + printf("Exiting Program\n"); + break; + + default: + printf("\n INVALID CHOICE"); +} + +printf("\n\n\n DO U WISH TO HAVE ANOTHER TRANSCATION?(y/n): "); +fflush(stdin); +scanf("%c", &transaction); +if (transaction == 'n'|| transaction == 'N') +i = 0; +} while (!i); + +printf("\n\n THANKS FOR USING OUR ATM SERVICE"); +} \ No newline at end of file diff --git a/balancedParenthesis.cpp b/balancedParenthesis.cpp new file mode 100644 index 0000000..2ef3cd6 --- /dev/null +++ b/balancedParenthesis.cpp @@ -0,0 +1,28 @@ +#include +using namespace std; +int main() +{ +int st[16], i, j, k, temp; +cout<<"enter 5 elements only to sort n:-"; +for (i = 0; i < 5; i++) +{ +cin>>st[i]; +} +for (i = 1; i < 5; i++) +{ +for (j = i; j >= 1; j--) +{ +if (st[j] < st[j-1]) +{ +temp = st[j]; +st[j] = st[j-1]; +st[j-1] = temp; +} +else +break; +} +} +cout<<"sorted array n "< +using namespace std; + +int BinarySearch(int n, int search, int arr[]) +{ + int s = 0; + int e = n; + int mid = (s + e) / 2; + while (s <= e) + { + if (arr[mid] == search) + { + return mid; + } + else if (arr[mid] < search) + { + s = mid + 1; + } + else{ + e=mid-1; + } + } + return -1; +} + +int main() +{ + int n; + cin >> n; + int arr[n]; + for (int i = 0; i < n; i++) + { + cin >> arr[i]; + } + + int number; + cin >> number; + cout << BinarySearch(n, number, arr); + + return 0; +} \ No newline at end of file diff --git a/c b/c new file mode 100644 index 0000000..d0af7d7 --- /dev/null +++ b/c @@ -0,0 +1,65 @@ +#include +int removerepeated(int size,int a[]); +void sort(int size,int a[]); +main(){ + int i,size1,size2,size,j=0,k; + printf("Enter size of an array1\n"); + scanf("%d",&size1); + printf("Enter size of an array2\n"); + scanf("%d",&size2); + int a[size1],b[size2],uni[size1+size2]; + printf("Enter numbers for array 1\n"); + for(i=0;ia[j]){ + temp=a[i]; + a[i]=a[j]; + a[j]=temp; + } + } + } +} diff --git a/count_frequency_of_each_element_in_array.cpp b/count_frequency_of_each_element_in_array.cpp new file mode 100644 index 0000000..7b2f640 --- /dev/null +++ b/count_frequency_of_each_element_in_array.cpp @@ -0,0 +1,32 @@ +#include +using namespace std; + +void countFreq(int arr[], int n) +{ + vector visited(n, false); + + for (int i = 0; i < n; i++) { + + // Skip this element if already processed + if (visited[i] == true) + continue; + + // Count frequency + int count = 1; + for (int j = i + 1; j < n; j++) { + if (arr[i] == arr[j]) { + visited[j] = true; + count++; + } + } + cout << arr[i] << " " << count << endl; + } +} + +int main() +{ + int arr[] = {10,5,10,15,10,5}; + int n = sizeof(arr) / sizeof(arr[0]); + countFreq(arr, n); + return 0; +} \ No newline at end of file diff --git a/create cc b/create cc new file mode 100644 index 0000000..d0af7d7 --- /dev/null +++ b/create cc @@ -0,0 +1,65 @@ +#include +int removerepeated(int size,int a[]); +void sort(int size,int a[]); +main(){ + int i,size1,size2,size,j=0,k; + printf("Enter size of an array1\n"); + scanf("%d",&size1); + printf("Enter size of an array2\n"); + scanf("%d",&size2); + int a[size1],b[size2],uni[size1+size2]; + printf("Enter numbers for array 1\n"); + for(i=0;ia[j]){ + temp=a[i]; + a[i]=a[j]; + a[j]=temp; + } + } + } +} diff --git a/decimaltobinary.c b/decimaltobinary.c new file mode 100644 index 0000000..e1c082f --- /dev/null +++ b/decimaltobinary.c @@ -0,0 +1,29 @@ +#include +#include + +int main(){ + + int decimal_num,remainder,binary_num=0; + int i = 0; + + + + printf("\n Enter The Decimal No: "); + scanf("%d", &decimal_num); + + + while (decimal_num != 0) + { + remainder= decimal_num%2; + binary_num += remainder*pow(10,i); + decimal_num = decimal_num/2; + i++; +} + + +printf("\n The Binary Equivalent = %d",binary_num); + +return 0; + +} + diff --git a/decimaltobinary.exe b/decimaltobinary.exe new file mode 100644 index 0000000..5b794e6 Binary files /dev/null and b/decimaltobinary.exe differ diff --git a/fibonacci.java b/fibonacci.java new file mode 100644 index 0000000..c05978e --- /dev/null +++ b/fibonacci.java @@ -0,0 +1,44 @@ +// Program to print fibonacci series followed by giver number by user... + +import java.util.Scanner; + +public class fibonacci { + + static int fib(int no) + { + if(no <= 1) { + return no; + } + else { + return fib(no - 1) + fib(no - 2); + } + + } + + //-------------------MAIN------------------------ + + public static void main(String[] args) { + + Scanner inp = new Scanner(System.in); + + while(true){ + + System.out.print("Enter Number : "); + int no = inp.nextInt(); + int stop = 000; + if(no == stop) { // If user enter 000 process will stop.. + break; + } + else { + for (int i = 0; i < no; i++) { + System.out.print(fib(i) + " "); + } + System.out.println(" "); + } + } + + } +} + + +//exit is a string , but we are taking input as integer ( int no = inp.nextInt(); ) , how to break while loop ? diff --git a/generateparantheses.cpp b/generateparantheses.cpp new file mode 100644 index 0000000..b0af342 --- /dev/null +++ b/generateparantheses.cpp @@ -0,0 +1,42 @@ +/* + +22. Generate Parentheses +Medium +15.4K +588 +Companies +Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. +*/ + +class Solution { +public: + vectorresult; + + void helper(int open,int close,int n,string current) + { + if(current.length()==n*2) + { + result.push_back(current); + return; + } + if(open generateParenthesis(int n) { + helper(0,0,n,""); + return result; + } +}; +// Intuition: Since we are asked to calculate all the possible permutations of brackets , hence we have to use backtracking + +// Concept: In every backtracking problem , there are two things to keep in mind , which we will explore here as well : + +// Base Case: Every problem of backtracking has some base case which tells us at which point we have to stop with the recursion process. In our case, when the length of our string has reached the maximum length(n*2), we stop with the recursion for that case and that is our base case. + +// Conditions: On observing carefully we find that there are two conditions present: + +// For adding (: If number of opening brackets(open) is less than the the given length(n) i.e. +// if max + +using namespace std; + +// A function to heapify the array. +void MaxHeapify(int a[], int i, int n) +{ + int j, temp; + temp = a[i]; + j = 2*i; + + while (j <= n) + { + if (j < n && a[j+1] > a[j]) + j = j+1; + // Break if parent value is already greater than child value. + if (temp > a[j]) + break; + // Switching value with the parent node if temp < a[j]. + else if (temp <= a[j]) + { + a[j/2] = a[j]; + j = 2*j; + } + } + a[j/2] = temp; + return; +} +void HeapSort(int a[], int n) +{ + int i, temp; + for (i = n; i >= 2; i--) + { + // Storing maximum value at the end. + temp = a[i]; + a[i] = a[1]; + a[1] = temp; + // Building max heap of remaining element. + MaxHeapify(a, 1, i - 1); + } +} +void Build_MaxHeap(int a[], int n) +{ + int i; + for(i = n/2; i >= 1; i--) + MaxHeapify(a, i, n); +} +int main() +{ + int n, i; + cout<<"\nEnter the number of data element to be sorted: "; + cin>>n; + n++; + int arr[n]; + for(i = 1; i < n; i++) + { + cout<<"Enter element "<>arr[i]; + } + // Building max heap. + Build_MaxHeap(arr, n-1); + HeapSort(arr, n-1); + + // Printing the sorted data. + cout<<"\nSorted Data "; + + for (i = 1; i < n; i++) + cout<<"->"< +int a[20],n; + +void display() +{ +int i; +for(i=0;i0) +{ +parent=(loc-1)/2; +if (num<=a[parent]) +{ +a[loc]=num; +return; +} +a[loc]=a[parent]; +loc=parent; +} +a[0]=num; +} + +void create_heap() +{ +int i; +for(i=0;i=a[left] && a[i]>=a[right] ) +return; +if ( a[right]<=a[left] ) +{ +temp=a[i]; +a[i]=a[left]; +a[left]=temp; +i=left; +} +else +{ +temp=a[i]; +a[i]=a[right]; +a[right]=temp; +i=right; +} +left=2*i+1; +right=2*i+2; +} +if ( left==last-1 && a[i]0; last--) +del_root(last); +} + +void main() +{ +int i; +printf ("\nEnter number of elements :"); +scanf ("%d",&n); +for(i=0;i= 0 and value < list1[j]: + list1[j + 1] = list1[j] + j -= 1 + list1[j + 1] = value + return list1 + # Driver code to test above + +list1 = [10, 5, 13, 8, 2] +print("The unsorted list is:", list1) + +print("The sorted list1 is:", insertion_sort(list1)) \ No newline at end of file diff --git a/lengthOfLongestSubstring.java b/lengthOfLongestSubstring.java new file mode 100644 index 0000000..9376bb9 --- /dev/null +++ b/lengthOfLongestSubstring.java @@ -0,0 +1,58 @@ +// Java program to find the length of the +// longest substring without repeating +// characters +import java.io.*; +import java.util.*; + +class sk{ + +// This function returns true if all characters in +// str[i..j] are distinct, otherwise returns false +public static Boolean areDistinct(String str, + int i, int j) +{ + + // Note : Default values in visited are false + boolean[] visited = new boolean[26]; + + for(int k = i; k <= j; k++) + { + if (visited[str.charAt(k) - 'a'] == true) + return false; + + visited[str.charAt(k) - 'a'] = true; + } + return true; +} + +// Returns length of the longest substring +// with all distinct characters. +public static int longestUniqueSubsttr(String str) +{ + int n = str.length(); + + // Result + int res = 0; + + for(int i = 0; i < n; i++) + for(int j = i; j < n; j++) + if (areDistinct(str, i, j)) + res = Math.max(res, j - i + 1); + + return res; +} + +// Driver code +public static void main(String[] args) +{ + String str = "geeksforgeeks"; + System.out.println("The input string is " + str); + + int len = longestUniqueSubsttr(str); + + System.out.println("The length of the longest " + + "non-repeating character " + + "substring is " + len); +} +} + \ No newline at end of file diff --git a/longest_increasing_subsequence.cpp b/longest_increasing_subsequence.cpp new file mode 100644 index 0000000..0cbd974 --- /dev/null +++ b/longest_increasing_subsequence.cpp @@ -0,0 +1,56 @@ +/* C++ implementation of LIS problem */ +#include +using namespace std; + +/* lis prints the length and the longest + increasing subsequence in arr[] of size n */ +void lis_and_print(int arr[], int n) +{ + int lis[n]; + + lis[0] = 1; + + /* Compute optimized LIS values in + bottom up manner */ + for (int i = 1; i < n; i++) + { + lis[i] = 1; + for (int j = 0; j < i; j++) + if (arr[i] > arr[j] && lis[i] < lis[j] + 1) + lis[i] = lis[j] + 1; + } + + // maximum value in lis + int length = *max_element(lis, lis + n); + + cout << "Length of LIS is " << length << "\n"; + + // Prints Longest increasing subsequence in arr[] + + cout << "LIS is "; + + vector longest_inc_sub; + for (int i = n - 1; i >= 0; i--) + { + if (lis[i] == length) + { + longest_inc_sub.push_back(arr[i]); + length--; + } + } + + reverse(longest_inc_sub.begin(), longest_inc_sub.end()); + + for (auto elem : longest_inc_sub) + cout << elem << " "; + cout << "\n"; +} + +/* Driver program to test above function */ +int main() +{ + int arr[] = {20, 32, 19, 43, 31, 60, 51, 70}; + int n = sizeof(arr) / sizeof(arr[0]); + lis_and_print(arr, n); + return 0; +} \ No newline at end of file diff --git a/longest_palindromic_subsequence.cpp b/longest_palindromic_subsequence.cpp new file mode 100644 index 0000000..2ecc7b4 --- /dev/null +++ b/longest_palindromic_subsequence.cpp @@ -0,0 +1,44 @@ +// A Dynamic Programming based C++ program for LPS problem +// Returns the length of the longest palindromic subsequence in seq +#include +#include + +int max (int x, int y) { return (x > y)? x : y; } + +int lps(char *str) +{ +int n = strlen(str); +int i, j, cl; +int L[n][n]; + + +// Strings of length 1 are palindrome of length 1 +for (i = 0; i < n; i++) + L[i][i] = 1; + + for (cl=2; cl<=n; cl++) + { + for (i=0; i +using namespace std; + +int factorial(int n) +{ + int fact=1; + for(int i=1; i<=n; i++) + { + fact = fact * i; + } + return fact; +} + +int nCr(int n, int r) +{ + int num=factorial(n); + int den= factorial(r) * factorial(n-r); + int ans = num/den; + return ans; + +} + +int main() +{ + int n,r; + cin >> n >> r; + cout<< nCr(n,r) << endl; + + return 0; +} \ No newline at end of file diff --git a/print_rax.asm b/print_rax.asm new file mode 100644 index 0000000..719e4f3 --- /dev/null +++ b/print_rax.asm @@ -0,0 +1,44 @@ +section .data +codes: + db '0123456789ABCDEF' + + +section .text +global _start +_start: + ; number 1122... in hexadecimal format + mov rax, 0x1122334455667788 + + mov rdi, 1 + mov rdx, 1 + mov rcx, 64 + ; Each 4 bits should be output as one hexadecimal digit + ; Use shift and bitwise AND to isolate them + ; the result is the offset in 'codes' array +.loop: + push rax + sub rcx, 4 + + ; cl is a register, smallest part of rcx + ; rax -- eax -- ax -- ah + al + ; rcx -- ecx -- cx -- ch + cl + sar rax, cl + + and rax, 0xf + lea rsi, [codes + rax] + mov rax, 1 + + ; syscall leaves rcx and r11 changed + push rcx + syscall + pop rcx + + pop rax + ; test can be used for the fastest 'is it a zero?' check + ; see docs for 'test' command + test rcx, rcx + jnz .loop + + mov rax, 60 ; invoke 'exit' system call + xor rdi, rdi + syscall \ No newline at end of file diff --git a/reverseKgrpLinkedList.cpp b/reverseKgrpLinkedList.cpp new file mode 100644 index 0000000..d7a5d38 --- /dev/null +++ b/reverseKgrpLinkedList.cpp @@ -0,0 +1,86 @@ +#include +using namespace std ; + +class Node{ + public: + int data; + Node* next; + +// CONSTRUCTOR +Node(int data){ + this -> data = data ; + this -> next = NULL; +} +}; + +void insertAtHead( Node* &head,int data){ + Node* New = new Node(data); + New -> next = head ; + head = New ; +}; + +void print(Node* head){ + Node* New = head ; + while (New != NULL) + { + cout << New -> data << " "; + New = New -> next ; + } + cout << endl ; +}; + +Node* reversekGroup(Node* head,int k){ + + if (head == NULL ) + { + return NULL ; + } + + + Node* forward = NULL ; + Node* prev = NULL ; + Node* current = head ; + int count = 0; + + while (current != NULL && count < k) + { + forward = current -> next ; + current -> next = prev ; + prev = current ; + current = forward ; + count++; + } + + if (forward != NULL ) + { + head -> next = reversekGroup(forward,k); + } + + return prev ; + +}; + +int main(){ + + Node* head = NULL ; + + insertAtHead(head,10); + print(head); + + insertAtHead(head,20); + print(head); + + insertAtHead(head,30); + print(head); + + insertAtHead(head,40); + print(head); + + insertAtHead(head,50); + print(head); +cout << " REVERSE "<> k ; +head = reversekGroup(head,k); +print(head); + return 0; +} \ No newline at end of file diff --git a/selection_sort.cpp b/selection_sort.cpp new file mode 100644 index 0000000..4e5eba9 --- /dev/null +++ b/selection_sort.cpp @@ -0,0 +1,76 @@ +/* +Algorithm +Step 1 − Set MIN to location 0 +Step 2 − Search the minimum element in the list +Step 3 − Swap with value at location MIN +Step 4 − Increment MIN to point to next element +Step 5 − Repeat until list is sorted + +Pseudocode +procedure selection sort + list : array of items + n : size of list + + for i = 1 to n - 1 + set current element as minimum + min = i + + check the element to be minimum + + for j = i+1 to n + if list[j] < list[min] then + min = j; + end if + end for + + swap the minimum element with the current element + if indexMin != i then + swap list[min] and list[i] + end if + end for + +end procedure + +Time Complexity = O(n^2); +Space Complexity = O(1); +*/ + +#include +using namespace std; + +void readArray(int array[], int size){ + for(int i = 0 ; i < size; i++){ + cin >> array[i]; + } +} + +void printArray(int array[], int size){ + for(int i = 0 ; i < size; i++){ + cout << array[i] << " "; + } +} + +void selectionSort(int array[], int size){ + for(int i = 0; i < size - 1; i++){ + int minIndex = i; + + for(int j = i + 1; j < size; j++){ + if(array[j] < array[minIndex]){ + minIndex = j; + } + } + swap(array[minIndex], array[i]); + } +} + +int main(){ + int size; + cin >> size; + int array[size]; + cout << "Enter the Elements of the Array. " << endl; + readArray(array, size); + selectionSort(array, size); + printArray(array, size); + + +} \ No newline at end of file diff --git a/sort_array_waveform.cpp b/sort_array_waveform.cpp new file mode 100644 index 0000000..3179464 --- /dev/null +++ b/sort_array_waveform.cpp @@ -0,0 +1,35 @@ +// program to sort an input array in wave form + +#include +using namespace std; + +void swap(int *x, int *y) +{ + int temp = *x; + *x = *y; + *y = temp; +} + +void sortInWave(int arr[], int n) +{ + + for (int i = 0; i < n; i += 2) + { + + if (i > 0 && arr[i - 1] > arr[i]) + swap(&arr[i], &arr[i - 1]); + + if (i < n - 1 && arr[i] < arr[i + 1]) + swap(&arr[i], &arr[i + 1]); + } +} + +int main() +{ + int arr[] = {10, 90, 49, 2, 1, 5, 23}; + int n = sizeof(arr) / sizeof(arr[0]); + sortInWave(arr, n); + for (int i = 0; i < n; i++) + cout << arr[i] << " "; + return 0; +}