├── Android
└── CameraCentricApp
│ ├── .gitignore
│ ├── .idea
│ ├── .gitignore
│ ├── compiler.xml
│ ├── gradle.xml
│ ├── jarRepositories.xml
│ ├── misc.xml
│ └── vcs.xml
│ ├── app
│ ├── .gitignore
│ ├── build.gradle
│ ├── proguard-rules.pro
│ └── src
│ │ ├── androidTest
│ │ └── java
│ │ │ └── com
│ │ │ └── example
│ │ │ └── cameracentricapp
│ │ │ └── ExampleInstrumentedTest.java
│ │ ├── main
│ │ ├── AndroidManifest.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── example
│ │ │ │ └── cameracentricapp
│ │ │ │ └── MainActivity.java
│ │ └── res
│ │ │ ├── drawable-v24
│ │ │ └── ic_launcher_foreground.xml
│ │ │ ├── drawable
│ │ │ └── ic_launcher_background.xml
│ │ │ ├── layout
│ │ │ └── activity_main.xml
│ │ │ ├── mipmap-anydpi-v26
│ │ │ ├── ic_launcher.xml
│ │ │ └── ic_launcher_round.xml
│ │ │ ├── mipmap-hdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ │ ├── values-night
│ │ │ └── themes.xml
│ │ │ ├── values
│ │ │ ├── colors.xml
│ │ │ ├── strings.xml
│ │ │ └── themes.xml
│ │ │ └── xml
│ │ │ └── provider_paths.xml
│ │ └── test
│ │ └── java
│ │ └── com
│ │ └── example
│ │ └── cameracentricapp
│ │ └── ExampleUnitTest.java
│ ├── build.gradle
│ ├── gradle.properties
│ ├── gradle
│ └── wrapper
│ │ ├── gradle-wrapper.jar
│ │ └── gradle-wrapper.properties
│ ├── gradlew
│ ├── gradlew.bat
│ └── settings.gradle
├── C
├── BFS
│ └── BFS.c
├── DFS
│ └── DFS.c
├── LinkedList
│ ├── Circular_Doubly_Linked_List
│ │ ├── .gitignore
│ │ ├── Deleting.c
│ │ ├── Inserting.c
│ │ ├── README.md
│ │ └── Traversing.c
│ ├── Circular_Linked_List
│ │ ├── .gitignore
│ │ ├── Deleting.c
│ │ ├── Inserting.c
│ │ ├── README.md
│ │ └── Traversing.c
│ ├── Cycle creation and detection in Linked List
│ ├── Deque Implementation using Linked List
│ │ ├── input restricted deque.h
│ │ ├── output restricted deque.h
│ │ └── readme.md
│ ├── Doubly_Linked_List
│ │ ├── .gitignore
│ │ ├── Deleting.c
│ │ ├── Inserting.c
│ │ ├── README.md
│ │ └── Traversing.c
│ ├── Singly_Linked_List
│ │ ├── .gitignore
│ │ ├── Deleting.c
│ │ ├── Inserting.c
│ │ ├── README.md
│ │ └── Traversing.c
│ └── linkedlist.c
├── Printing Pattern.c
├── Searching
│ ├── .gitignore
│ ├── BinarySearch.c
│ ├── InterSearch.c
│ ├── JumpSearch.c
│ ├── LinearSearch.c
│ └── README.md
├── SortingC
│ ├── .gitignore
│ ├── BubbleSort.c
│ ├── MergeSort.c
│ ├── QuickSort.c
│ ├── README.md
│ ├── RadixSort.c
│ ├── SelectionSort.c
│ ├── Shell-Sort.c
│ ├── bubblesortMethod2.c
│ └── insertionSort.c
├── gcd_using_recursion.c
├── queuearray.c
├── reverse_number.c
└── stack.c
├── CPP
├── BFS
│ ├── BFS_Adj_List.cpp
│ ├── BFS_graph_traversal.cpp
│ └── GRAPHBFS.cpp
├── Catalan
│ ├── Catalan using DP.cpp
│ └── Catalan using Inverse Modulo.cpp
├── DFS
│ ├── Bridges.cpp
│ ├── Building_teams.cpp
│ ├── Iterative Deepening Depth First Search.cpp
│ └── dfsalgo.cpp
├── DP
│ ├── Bellman Ford Algorithm.cpp
│ ├── knapsack.cpp
│ └── matrix_chain_multiplication.cpp
├── Dijikstra
│ ├── Dijkshtra Adjaceny List and Heap (ELogV).cpp
│ ├── Dijkshtra Adjaceny List and Heap.cpp
│ ├── Dijkshtra using operator overloading Priority Queue.cpp
│ ├── Dijstra_with_negative_edge.cpp
│ └── dijikstra.cpp
├── Floyd-Warshall
│ ├── Floyd–Warshall Algorithm.cpp
│ └── flyod_warshall.cpp
├── LinkedList
│ ├── Deque Using Linked List.cpp
│ ├── Doubly Linked List Full Implementation.cpp
│ ├── Linkedlist.cpp
│ ├── ReverseDoublyLinkedList.cpp
│ ├── linked_list_all_functions.cpp
│ ├── polynomialSumByLinkedList.cpp
│ └── swap_nodes_linked_list.cpp
├── Matrix
│ ├── SpiralMatrixSort.cpp
│ ├── ananya998_rotate_a_matrix.cpp
│ └── spiral_matrix.cpp
├── Numbers
│ ├── Avg_Marks.cpp
│ ├── Nth Fibonacci Number.cpp
│ ├── Nth_Term_of_Fibonacci_series.cpp
│ ├── alternate positive and negative numbers.cpp
│ └── numberPattern.cpp
├── OOPS
│ ├── DeepCopy and ShallowCopy.cpp
│ └── Polynomial.cpp
├── Prims Algorithm
│ └── prims.cpp
├── PrimsAlgoCPP
│ ├── Prims.cpp
│ └── prims_ALgo.cpp
├── Queue
│ ├── Improved_Bubble_Sort.cpp
│ ├── insert_delete_circular_queue.cpp
│ ├── queueUsingArray.cpp
│ └── queueUsingLinkedList.cpp
├── STACK
│ ├── Parenthesischeck.cpp
│ ├── Stack.cpp
│ ├── Valid Parenthesis.cpp
│ ├── stack_using_STL.cpp
│ └── two_stack.cpp
├── STL_Vector
│ ├── clear().cpp
│ ├── erase().cpp
│ ├── front() and back().cpp
│ ├── init
│ ├── initialization().cpp
│ ├── insert().cpp
│ ├── pop_back().cpp
│ ├── push_back().cpp
│ ├── resize().cpp
│ └── size().cpp
├── SegmentTree
│ ├── MaxElementInRange.cpp
│ ├── kthOne_SegmentTree.cpp
│ └── numberOfInverstions_segmentTree.cpp
├── Sorting
│ ├── BubbleSort.cpp
│ ├── Bucket Sort.cpp
│ ├── HeapSort.cpp
│ ├── Insertionsort4.cpp
│ ├── Radix_sort.cpp
│ ├── Selection sort
│ │ ├── Selection sort.cpp
│ │ ├── selection_sort.cpp
│ │ └── selectionsort.cpp
│ ├── Sorting_Algorithms_cpp
│ │ ├── BubbleSort.cpp
│ │ ├── HeapSort.cpp
│ │ ├── InsertionSort.cpp
│ │ ├── MergeSort.cpp
│ │ ├── QuickSort.cpp
│ │ ├── RadixSort.cpp
│ │ ├── ReadMe.md
│ │ └── SelectionSort.cpp
│ ├── bubblesort2.cpp
│ ├── heap_sort_dsa.cpp
│ ├── inser_sort3.cpp
│ ├── insertion.cpp.txt
│ ├── insertionSort.cpp
│ ├── insertionsort_code.cpp
│ ├── mergesort.cpp
│ ├── quicksort2.cpp
│ └── quicksort_algo.cpp
├── Trees
│ ├── Tree_with_N_nodes.cpp
│ ├── brian_kerningam_algo.cpp
│ └── kruskalsMinimumSpanningTreeAlgorithm.cpp
├── arrays
│ ├── Queue_using_array.cpp
│ └── SQRT_Decomposition.cpp
├── binarysearch.cpp
├── caesar_cipher.cpp
├── calculator_using_templatefnc.cpp
├── insertion.cpp
└── string algorithms
│ └── z_function.cpp
├── Java
├── Addition
│ ├── addNumberusingfunction.java
│ └── addition.java
├── BFS
│ └── BFS.java
├── Binary Trees
│ └── BinaryTree.java
├── GRAM_SCHMIDT_PROCESS.java
├── KthLargestElement.java
├── Largestsum.java
├── LinkedList.java
├── LongestSubstringWithNonRepeatingCharacters.java
├── Min_Element_In_Stack.java
├── Minimum bracket Reversal.java
├── Numbers
│ ├── Factorial.java
│ ├── Fascinating Numbers.java
│ ├── FibonacciBigNumbers.java
│ ├── Neon Number.java
│ ├── PrimeNumber.java
│ ├── Subtract.java
│ └── SumOfPrices.java
├── OrderAgnosticBinary.java
├── Program for Tower of Hanoi.java
├── Program to Format Time in AM-PM.java
├── RotateArray.java
├── Sorting
│ ├── InsertionSort.java
│ ├── InsertionSort2.java
│ ├── MergeSort.java
│ ├── Quick.java
│ ├── RadixSort2.java
│ ├── Randomized_Quick_Sort.java
│ ├── SelectionSort.java
│ ├── Sortarray.java
│ ├── circle_sort.java
│ ├── quickSort.java
│ └── radixSort.java
├── Stack_Using_LinkedList.java
├── SubArray.java
└── insertion.java
├── KadaneAlgorithm
├── MDFiles
├── Addition.md
├── CONTRIBUTING.md
└── cheatsheet.md
├── Priority_Queue
├── Product Of Array Except Self
├── Python
├── BinarySearch.py
├── Create S3_To_DynamoDB.py
├── Hackerankpattern.py
├── Job _scheduling-Shouvik-99.py
├── Lapindromes.py
├── PYTHONic_SIRI.py
├── Program to Solve Quadratic Equation.py
├── Sorting
│ └── Bubblesort.py
├── Valid Sudoku.py
├── calculator.py
├── fibonacci_golden_ratio.py
├── generador de contraseñas.py
├── list.py
├── towerOfHanoi.py
└── wifi_info.py
├── README.md
├── TicTacToe
├── tic_tac_toe.cpp
├── tic_tac_toe.py
└── tictactoe.py
├── _design
├── Awesome Cheatsheets.sketch
├── awesome_cheatsheets_logo.png
├── awesome_cheatsheets_logo.psd
├── awesome_cheatsheets_logo@2x.png
├── awesome_cheatsheets_logo@4x.png
└── cover_github@2x.png
├── backend
├── adonis.js
├── django.py
├── express.js
├── feathers.js
├── laravel.php
├── moleculer.js
├── node.js
└── sails.js
├── c++
├── Count set bits in an integer Solution.cpp
├── Duplicate String.cpp
├── Dutch_National_Flag_Algorithm.cpp
├── MTT.cpp
├── Magical_Doors.cpp
├── MatrixExponentiation.cpp
├── Modular_Equation_codechef.cpp
├── Mountain-Points.cpp
├── N-Queens.cpp
├── Reverse word.cpp
├── Shortest_Job_First.cpp
├── Sudoku.cpp
├── Trapping Rain Water.cpp
├── TravellingSalesman.cpp
├── countOfSubset.cpp
├── fibonacci_series_code.cpp
├── first_n_primes.cpp
├── kmp.cpp
├── longestpathsum.cpp
├── main.cpp
├── maximum subarray sun.cpp
├── multiplymatrix.cpp
├── nextGreaterToLeft.cpp
├── nim_Game.cpp
├── sieve.cpp
├── stocks_Buy_And_Sell_Problem.cpp
├── subsetsum.cpp
├── suffix_array_n_logn.cpp
├── swaping.cpp
└── two-sum.cpp
├── databases
├── mysql.sh
└── redis.sh
├── division of numbers using GUI
├── frontend
├── angular.js
├── angularjs.js
├── ember.js
├── html5.html
├── react.js
├── tailwind.css
└── vue.js
├── javascript
├── LinearSearch.js
└── sortstack.js
├── javascriptalert().html
├── languages
├── C#.txt
├── C.txt
├── XML.md
├── bash.sh
├── golang.md
├── java.md
├── javascript.js
├── php.php
└── python.md
├── logistic_regression_from__scratch.ipynb
├── tools
├── aws.sh
├── curl.sh
├── docker.sh
├── drush.sh
├── elasticsearch.js
├── emmet.md
├── gcp.md
├── git.sh
├── heroku.sh
├── kubernetes.md
├── macos.sh
├── nanobox_boxfile.yml
├── nanobox_cli.sh
├── nginx.sh
├── pm2.sh
├── puppeteer.js
├── sublime_text.md
├── ubuntu.sh
├── vim.txt
├── vscode.md
└── xcode.txt
└── topo sort.cp
/Android/CameraCentricApp/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | /.idea/navEditor.xml
9 | /.idea/assetWizardSettings.xml
10 | .DS_Store
11 | /build
12 | /captures
13 | .externalNativeBuild
14 | .cxx
15 | local.properties
16 |
--------------------------------------------------------------------------------
/Android/CameraCentricApp/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 |
--------------------------------------------------------------------------------
/Android/CameraCentricApp/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Android/CameraCentricApp/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
20 |
21 |
--------------------------------------------------------------------------------
/Android/CameraCentricApp/.idea/jarRepositories.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/Android/CameraCentricApp/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Android/CameraCentricApp/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Android/CameraCentricApp/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/Android/CameraCentricApp/app/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.application'
3 | }
4 |
5 | android {
6 | compileSdkVersion 30
7 | buildToolsVersion "30.0.2"
8 |
9 | defaultConfig {
10 | applicationId "com.example.cameracentricapp"
11 | minSdkVersion 25
12 | targetSdkVersion 30
13 | versionCode 1
14 | versionName "1.0"
15 |
16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17 | }
18 |
19 | buildTypes {
20 | release {
21 | minifyEnabled false
22 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23 | }
24 | }
25 | compileOptions {
26 | sourceCompatibility JavaVersion.VERSION_1_8
27 | targetCompatibility JavaVersion.VERSION_1_8
28 | }
29 | }
30 |
31 | dependencies {
32 |
33 | implementation 'androidx.appcompat:appcompat:1.3.0'
34 | implementation 'com.google.android.material:material:1.3.0'
35 | implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
36 | testImplementation 'junit:junit:4.+'
37 | androidTestImplementation 'androidx.test.ext:junit:1.1.2'
38 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
39 | }
--------------------------------------------------------------------------------
/Android/CameraCentricApp/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/Android/CameraCentricApp/app/src/androidTest/java/com/example/cameracentricapp/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.example.cameracentricapp;
2 |
3 | import android.content.Context;
4 |
5 | import androidx.test.platform.app.InstrumentationRegistry;
6 | import androidx.test.ext.junit.runners.AndroidJUnit4;
7 |
8 | import org.junit.Test;
9 | import org.junit.runner.RunWith;
10 |
11 | import static org.junit.Assert.*;
12 |
13 | /**
14 | * Instrumented test, which will execute on an Android device.
15 | *
16 | * @see Testing documentation
17 | */
18 | @RunWith(AndroidJUnit4.class)
19 | public class ExampleInstrumentedTest {
20 | @Test
21 | public void useAppContext() {
22 | // Context of the app under test.
23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
24 | assertEquals("com.example.cameracentricapp", appContext.getPackageName());
25 | }
26 | }
--------------------------------------------------------------------------------
/Android/CameraCentricApp/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
16 |
21 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/Android/CameraCentricApp/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
15 |
18 |
21 |
22 |
23 |
24 |
30 |
--------------------------------------------------------------------------------
/Android/CameraCentricApp/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/Android/CameraCentricApp/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/Android/CameraCentricApp/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coder-forfun/Hactoberfest-accepted/134a579948482348af9684ad8b351ae254b6fe00/Android/CameraCentricApp/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/Android/CameraCentricApp/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coder-forfun/Hactoberfest-accepted/134a579948482348af9684ad8b351ae254b6fe00/Android/CameraCentricApp/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/Android/CameraCentricApp/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coder-forfun/Hactoberfest-accepted/134a579948482348af9684ad8b351ae254b6fe00/Android/CameraCentricApp/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/Android/CameraCentricApp/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coder-forfun/Hactoberfest-accepted/134a579948482348af9684ad8b351ae254b6fe00/Android/CameraCentricApp/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/Android/CameraCentricApp/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coder-forfun/Hactoberfest-accepted/134a579948482348af9684ad8b351ae254b6fe00/Android/CameraCentricApp/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/Android/CameraCentricApp/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coder-forfun/Hactoberfest-accepted/134a579948482348af9684ad8b351ae254b6fe00/Android/CameraCentricApp/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/Android/CameraCentricApp/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coder-forfun/Hactoberfest-accepted/134a579948482348af9684ad8b351ae254b6fe00/Android/CameraCentricApp/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/Android/CameraCentricApp/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coder-forfun/Hactoberfest-accepted/134a579948482348af9684ad8b351ae254b6fe00/Android/CameraCentricApp/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/Android/CameraCentricApp/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coder-forfun/Hactoberfest-accepted/134a579948482348af9684ad8b351ae254b6fe00/Android/CameraCentricApp/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/Android/CameraCentricApp/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coder-forfun/Hactoberfest-accepted/134a579948482348af9684ad8b351ae254b6fe00/Android/CameraCentricApp/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/Android/CameraCentricApp/app/src/main/res/values-night/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
--------------------------------------------------------------------------------
/Android/CameraCentricApp/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #FFBB86FC
4 | #FF6200EE
5 | #FF3700B3
6 | #FF03DAC5
7 | #FF018786
8 | #FF000000
9 | #FFFFFFFF
10 |
--------------------------------------------------------------------------------
/Android/CameraCentricApp/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | CameraCentricApp
3 |
--------------------------------------------------------------------------------
/Android/CameraCentricApp/app/src/main/res/values/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
--------------------------------------------------------------------------------
/Android/CameraCentricApp/app/src/main/res/xml/provider_paths.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Android/CameraCentricApp/app/src/test/java/com/example/cameracentricapp/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.example.cameracentricapp;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/Android/CameraCentricApp/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 | buildscript {
3 | repositories {
4 | google()
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath "com.android.tools.build:gradle:4.1.0"
9 |
10 | // NOTE: Do not place your application dependencies here; they belong
11 | // in the individual module build.gradle files
12 | }
13 | }
14 |
15 | allprojects {
16 | repositories {
17 | google()
18 | jcenter()
19 | }
20 | }
21 |
22 | task clean(type: Delete) {
23 | delete rootProject.buildDir
24 | }
--------------------------------------------------------------------------------
/Android/CameraCentricApp/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app"s APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
--------------------------------------------------------------------------------
/Android/CameraCentricApp/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coder-forfun/Hactoberfest-accepted/134a579948482348af9684ad8b351ae254b6fe00/Android/CameraCentricApp/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/Android/CameraCentricApp/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Jun 21 22:42:46 IST 2021
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
7 |
--------------------------------------------------------------------------------
/Android/CameraCentricApp/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 | rootProject.name = "CameraCentricApp"
--------------------------------------------------------------------------------
/C/LinkedList/Circular_Doubly_Linked_List/.gitignore:
--------------------------------------------------------------------------------
1 | a.out
--------------------------------------------------------------------------------
/C/LinkedList/Circular_Doubly_Linked_List/README.md:
--------------------------------------------------------------------------------
1 | # Circular Doubly Linked List
2 |
3 | 1. Adding & Traversing.
4 | 2. Adding, Inserting & Traversing.
5 | 3. Adding, Deleting & Traversing.
6 |
--------------------------------------------------------------------------------
/C/LinkedList/Circular_Linked_List/.gitignore:
--------------------------------------------------------------------------------
1 | a.out
--------------------------------------------------------------------------------
/C/LinkedList/Circular_Linked_List/README.md:
--------------------------------------------------------------------------------
1 | # Circular Linked List
2 |
3 | 1. Adding & Traversing.
4 | 2. Adding, Inserting & Traversing.
5 | 3. Adding, Deleting & Traversing.
--------------------------------------------------------------------------------
/C/LinkedList/Circular_Linked_List/Traversing.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | typedef struct node{
5 | int data;
6 | struct node *nextAddress;
7 | }Node;
8 |
9 | Node *start = NULL;
10 |
11 | void add(int no)
12 | {
13 | Node *newNode, *ptr;
14 | newNode = (Node *)malloc(sizeof(Node));
15 | newNode->data = no;
16 | newNode->nextAddress = NULL;
17 | if(start == NULL)
18 | {
19 | start = newNode;
20 | newNode->nextAddress = start;
21 | }
22 | else
23 | {
24 | ptr = start;
25 | while(ptr->nextAddress != start)
26 | {
27 | ptr = ptr->nextAddress;
28 | }
29 | ptr->nextAddress = newNode;
30 | newNode->nextAddress = start;
31 | }
32 | }
33 | void display()
34 | {
35 | Node *ptr;
36 | ptr = start;
37 | printf("START->");
38 | while(ptr->nextAddress != start)
39 | {
40 | printf("%d->",ptr->data);
41 | ptr = ptr->nextAddress;
42 | }
43 | printf("%d->",ptr->data);
44 | printf("END");
45 | }
46 | int main()
47 | {
48 | printf("Adding & Traversing\n");
49 | printf("*******************\n\n");
50 | printf("1: Add\n2: Display\n0: Exit\n\n");
51 | int choice,no;
52 | while(1)
53 | {
54 | printf("Enter Choice : ");
55 | scanf("%d",&choice);
56 | if(choice==1)
57 | {
58 | printf("Enter Number : ");
59 | scanf("%d",&no);
60 | printf("\n");
61 | add(no);
62 | }
63 | else if(choice==2)
64 | {
65 | display();
66 | printf("\n\n");
67 | }
68 | else if(choice==0)
69 | {
70 | break;
71 | }
72 | }
73 | }
--------------------------------------------------------------------------------
/C/LinkedList/Cycle creation and detection in Linked List:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | class node{
5 | public:
6 | int data;
7 | node *next;
8 |
9 | node(int data){
10 | this->data=data;
11 | next=NULL;
12 |
13 | }
14 |
15 |
16 | };
17 | node *take_input(){
18 | int data;
19 | cout<<"Enter Linked list Elements"<>data;
22 | node* head=NULL;
23 | node* tail=NULL;
24 | while(data!=-1){
25 | node *n=new node(data);
26 | if(head==NULL){
27 | head=n;
28 | tail=n;
29 |
30 | }
31 | else{
32 | tail->next=n;
33 | tail=tail->next;
34 | }
35 |
36 | cin>>data;
37 | }
38 |
39 | return head;
40 | }
41 | void display(node *head){
42 |
43 | while(head!=NULL){
44 | cout<data<<"->";
45 | head=head->next;
46 | }
47 | cout<<"NULL";
48 | }
49 |
50 | void make_cycle(node* head,int pos){
51 | int c=1;
52 | node* temp=head;
53 | node* startnode;
54 | while(temp->next!=NULL){
55 | if(pos==c){
56 | startnode=temp;
57 | }
58 |
59 | temp=temp->next;
60 | c++;
61 | }
62 |
63 | temp->next=startnode;
64 |
65 | }
66 | bool detection_cycle(node* head){
67 | node* slow=head;
68 | node* fast=head;
69 | while(fast!=NULL && fast->next!=NULL)
70 | {
71 |
72 | slow=slow->next;
73 | fast=fast->next->next;
74 | if(slow==fast)
75 | {
76 | return true;
77 | }
78 |
79 | }
80 | return false;
81 | }
82 |
83 | int main(){
84 | node* head=take_input();
85 | int n;
86 | cout<<"Enter the position to Make cycle"<>n;
88 | make_cycle(head,n);
89 | //display(head);
90 | cout<
2 | #include
3 | typedef struct node{
4 | int data;
5 | struct node *leftAddress;
6 | struct node *rightAddress;
7 | }Node;
8 |
9 | Node *start = NULL, *end = NULL;
10 |
11 | void add(int no)
12 | {
13 | Node *newNode, *ptr;
14 | newNode = (Node *)malloc(sizeof(Node));
15 | newNode->leftAddress = NULL;
16 | newNode->data = no;
17 | newNode->rightAddress = NULL;
18 | if(start == NULL)
19 | {
20 | start = newNode;
21 | }
22 | else
23 | {
24 | ptr = start;
25 | while(ptr->rightAddress != NULL)
26 | {
27 | ptr = ptr->rightAddress;
28 | }
29 | ptr->rightAddress = newNode;
30 | newNode->leftAddress = ptr;
31 | end = newNode;
32 | }
33 | }
34 | void display()
35 | {
36 | Node *ptr;
37 | ptr = start;
38 | printf("START<->");
39 | while(ptr!=NULL)
40 | {
41 | printf("%d<->",ptr->data);
42 | ptr = ptr->rightAddress;
43 | }
44 | printf("END");
45 | }
46 | void revDisplay()
47 | {
48 | Node *ptr;
49 | ptr = end;
50 | printf("END<->");
51 | while(ptr!=NULL)
52 | {
53 | printf("%d<->",ptr->data);
54 | ptr = ptr->leftAddress;
55 | }
56 | printf("START");
57 | }
58 | int main()
59 | {
60 | printf("Adding & Traversing\n");
61 | printf("*******************\n\n");
62 | printf("1: Add\n2: Display\n3: Reverse Display\n0: Exit\n\n");
63 | int choice,no;
64 | while(1)
65 | {
66 | printf("Enter Choice : ");
67 | scanf("%d",&choice);
68 | if(choice==1)
69 | {
70 | printf("Enter Number : ");
71 | scanf("%d",&no);
72 | printf("\n");
73 | add(no);
74 | }
75 | else if(choice==2)
76 | {
77 | display();
78 | printf("\n\n");
79 | }
80 | else if(choice==3)
81 | {
82 | revDisplay();
83 | printf("\n\n");
84 | }
85 | else if(choice==0)
86 | {
87 | break;
88 | }
89 | }
90 | }
--------------------------------------------------------------------------------
/C/LinkedList/Singly_Linked_List/.gitignore:
--------------------------------------------------------------------------------
1 | a.out
--------------------------------------------------------------------------------
/C/LinkedList/Singly_Linked_List/README.md:
--------------------------------------------------------------------------------
1 | # Singly Linked List
2 |
3 | 1. Adding & Traversing.
4 | 2. Adding, Inserting & Traversing.
5 | 3. Adding, Deleting & Traversing.
--------------------------------------------------------------------------------
/C/LinkedList/Singly_Linked_List/Traversing.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | typedef struct node{
4 | int data;
5 | struct node *nextAddress;
6 | }Node;
7 | Node *start = NULL;
8 | void insert(int no)
9 | {
10 | Node *newNode, *ptr;
11 | newNode = (Node *)malloc(sizeof(Node));
12 | newNode->data = no;
13 | newNode->nextAddress = NULL;
14 | if(start == NULL)
15 | {
16 | start = newNode;
17 | }
18 | else
19 | {
20 | ptr = start;
21 | while(ptr->nextAddress!=NULL)
22 | {
23 | ptr = ptr->nextAddress;
24 | }
25 | ptr->nextAddress = newNode;
26 | }
27 | }
28 | void display()
29 | {
30 | Node *ptr;
31 | ptr = start;
32 | printf("START->");
33 | while(ptr != NULL)
34 | {
35 | printf("%d->",ptr->data);
36 | ptr = ptr->nextAddress;
37 | }
38 | printf("END");
39 | }
40 | int main()
41 | {
42 | printf("Traversing a Singly Linked List\n");
43 | printf("*******************************\n\n");
44 | printf("1: Insert\n2: Display\n0: No\n\n");
45 | int choice, no;
46 | while(1)
47 | {
48 | printf("Enter your Choice : ");
49 | scanf("%d",&choice);
50 | if(choice==0)
51 | {
52 | printf("\n");
53 | break;
54 | }
55 | else if(choice==1)
56 | {
57 | printf("Enter Number : ");
58 | scanf("%d",&no);
59 | printf("\n");
60 | insert(no);
61 | }
62 | else if(choice==2)
63 | {
64 | display();
65 | printf("\n\n");
66 | }
67 | }
68 | }
--------------------------------------------------------------------------------
/C/LinkedList/linkedlist.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | int main()
4 | {
5 | struct node{
6 | int data;
7 | struct node *next;
8 | };
9 | struct node *head,*newnode,*temp;
10 | head=0;
11 | int choice=1;
12 | while(choice)
13 | {
14 | newnode=(struct node*)malloc(sizeof(struct node));
15 | printf("Enter Data :");
16 | scanf("%d",&newnode->data);
17 | newnode->next=0;
18 | if(head==0)
19 | {
20 | head=temp=newnode;
21 | }
22 | else
23 | {
24 | temp->next=newnode;
25 | temp=newnode;
26 | }
27 | printf("Do you want to add data(0/1)?? ");
28 | scanf("%d",&choice);
29 | }
30 | temp=head;
31 | while(temp!=0)
32 | {
33 | printf("%d ",temp->data);
34 | temp=temp->next;
35 | }
36 | return 0;
37 | }
38 | /*
39 | linkedlist:
40 | linkedlist are linear data struture similar to array but values in linkedlist are stored using pointers.
41 | Example
42 | INPUT
43 | Enter Data :3
44 | Do you want to add data(0/1)?? 1
45 | Enter Data :4
46 | Do you want to add data(0/1)?? 1
47 | Enter Data :5
48 | Do you want to add data(0/1)?? 1
49 | Enter Data :7
50 | Do you want to add data(0/1)?? 0
51 |
52 | OUTPUT
53 | 3 4 5 7
54 | *
--------------------------------------------------------------------------------
/C/Printing Pattern.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 |
6 | int main()
7 | {
8 |
9 | int n,len,i,j;
10 | scanf("%d", &n);
11 | // Complete the code to print the pattern.
12 | int size=2*n-1;
13 | int a[size][size], start=0, end=size-1;
14 | while(n!=0)
15 | {
16 | for(i=start;i<=end;i++)
17 | for(j=start;j<=end;j++)
18 | {
19 | if(i==start || j==start || i==end || j==end)
20 | a[i][j]=n;
21 | }
22 | ++start;
23 | --end;
24 | --n;
25 | }
26 |
27 | for(i=0;i
2 | int search(int *arr, int n, int no)
3 | {
4 | int beg=0;
5 | int end=n-1;
6 | int pos=-1;
7 | while(beg<=end)
8 | {
9 | int mid = (beg+end)/2;
10 | if(no == *(arr+mid))
11 | {
12 | pos = mid;
13 | break;
14 | }
15 | else if(no > *(arr+mid))
16 | {
17 | beg = mid + 1;
18 | }
19 | else if(no < *(arr+mid))
20 | {
21 | end = mid - 1;
22 | }
23 | }
24 | return pos;
25 | }
26 | int main()
27 | {
28 | printf("Binary Search\n");
29 | printf("*************\n\n");
30 | int arr[100];
31 | int n,i,no;
32 | printf("Enter Size of Array : ");
33 | scanf("%d",&n);
34 | printf("\n");
35 | printf("Enter Numbers in Sorted Form : \n");
36 | for(i=0;i
2 | int search(int *arr, int n, int no)
3 | {
4 | int beg = 0;
5 | int end = n-1;
6 | int pos = -1;
7 | while(beg <= end)
8 | {
9 | int mid = beg + (end-beg) * ((no-*(arr+beg)) / (*(arr+end) - *(arr+beg)));
10 | if(no == *(arr+mid))
11 | {
12 | pos=mid;
13 | break;
14 | }
15 | else if(no < *(arr+mid))
16 | {
17 | end = mid - 1;
18 | }
19 | else if(no > *(arr+mid))
20 | {
21 | beg = mid + 1;
22 | }
23 | }
24 | return pos;
25 | }
26 | int main()
27 | {
28 | printf("Interpolation Search\n");
29 | printf("********************\n\n");
30 | int arr[100];
31 | int n,i,no;
32 | printf("Enter Size of Array : ");
33 | scanf("%d",&n);
34 | printf("\n");
35 | printf("Enter Numbers : \n");
36 | for(i=0;i
2 | #include
3 | void search(int *arr, int n, int no)
4 | {
5 | int jump = sqrt(n);
6 | int low = 0;
7 | int high = jump;
8 | int flag = 0;
9 | int i;
10 | for(i=1;i<=jump;i++)
11 | {
12 | if(no>=*(arr+low) && no<=*(arr+high))
13 | {
14 | flag = 1;
15 | break;
16 | }
17 | else
18 | {
19 | low = high;
20 | high = high + jump;
21 | }
22 | }
23 | if(flag==0)
24 | {
25 | printf("Number is Not Present in Array\n");
26 | }
27 | else
28 | {
29 | for(i=low;i
2 | int search(int *arr, int n, int no)
3 | {
4 | int i,pos=0;
5 | for(i=0;i
2 | int bubble(int *arr, int n)
3 | {
4 | int i,j,temp=0,flag=0;
5 | for(i=0;i *(arr+j+1))
10 | {
11 | flag=1;
12 | temp = *(arr+j);
13 | *(arr+j) = *(arr+j+1);
14 | *(arr+j+1) = temp;
15 | }
16 | }
17 | if(flag==0)
18 | {
19 | return flag;
20 | }
21 | }
22 | }
23 | int main()
24 | {
25 | printf("Bubble Sort\n");
26 | printf("***********\n\n");
27 | int arr[100];
28 | int n,i;
29 | printf("Enter Size of Array : ");
30 | scanf("%d",&n);
31 | printf("\n");
32 | printf("Enter Numbers : \n");
33 | for(i=0;i
2 | void merge(int *arr, int beg, int mid, int end)
3 | {
4 | int i=beg, j=mid+1, index=beg, temp[100], k;
5 | while(i<=mid && j<=end)
6 | {
7 | if(*(arr+i) < *(arr+j))
8 | {
9 | temp[index] = *(arr+i);
10 | i++;
11 | }
12 | else
13 | {
14 | temp[index] = *(arr+j);
15 | j++;
16 | }
17 | index++;
18 | }
19 | if(i>mid)
20 | {
21 | while(j<=end)
22 | {
23 | temp[index] = *(arr+j);
24 | j++;
25 | index++;
26 | }
27 | }
28 | else
29 | {
30 | while(i<=mid)
31 | {
32 | temp[index] = *(arr+i);
33 | i++;
34 | index++;
35 | }
36 | }
37 | for(k=beg;k
2 | int partition(int *arr, int beg, int end)
3 | {
4 | int left = beg;
5 | int loc = beg;
6 | int right = end;
7 | int flag = 0;
8 | int temp=0;
9 | while(flag != 1)
10 | {
11 | if(*(arr+loc) <= *(arr+right) && loc != right)
12 | {
13 | right = right - 1;
14 | if(loc==right)
15 | {
16 | flag=1;
17 | continue;
18 | }
19 | }
20 | else if(*(arr+loc) > *(arr+right))
21 | {
22 | temp = *(arr+loc);
23 | *(arr+loc) = *(arr+right);
24 | *(arr+right) = temp;
25 | loc = right;
26 | }
27 | if(*(arr+loc) >= *(arr+left) && loc != left)
28 | {
29 | left = left + 1;
30 | if(loc==left)
31 | {
32 | flag=1;
33 | continue;
34 | }
35 | }
36 | else if(*(arr+loc) < *(arr+left))
37 | {
38 | temp = *(arr+loc);
39 | *(arr+loc) = *(arr+left);
40 | *(arr+left) = temp;
41 | loc = left;
42 | }
43 | }
44 | return loc;
45 | }
46 | void sort(int *arr, int beg, int end)
47 | {
48 | if(beg
2 | int findPlaceValue(int *arr, int j, int k)
3 | {
4 | int r;
5 | int no = *(arr+j);
6 | while(k > 0)
7 | {
8 | r = no%10;
9 | no = no/10;;
10 | k--;
11 | }
12 | return r;
13 | }
14 | void sort(int *arr, int n, int k)
15 | {
16 | int count[10] = {0,0,0,0,0,0,0,0,0,0};
17 | int updated_count[10];
18 | int i,j;
19 | int temp_arr[100];
20 | for(i=0;i<=9;i=i+1)
21 | {
22 | for(j=0;j=0;i--)
41 | {
42 | int placeValue = findPlaceValue(temp_arr,i,k);
43 | updated_count[placeValue] = updated_count[placeValue] - 1;
44 | *(arr+updated_count[placeValue]) = temp_arr[i];
45 | }
46 | }
47 | int digitCount(int no)
48 | {
49 | int count=0;
50 | while(no>0)
51 | {
52 | count = count + 1;
53 | no = no/10;
54 | }
55 | return count;
56 | }
57 | int largest(int *arr, int n)
58 | {
59 | int max=0,i;
60 | for(i=0;i
2 | int smallestPos(int *arr, int n, int i)
3 | {
4 | int small = *(arr+i);
5 | int pos = i;
6 | int k;
7 | for(k=i;k *(arr+k))
10 | {
11 | small = *(arr+k);
12 | pos = k;
13 | }
14 | }
15 | return pos;
16 | }
17 | void selection(int *arr, int n)
18 | {
19 | int temp = 0;
20 | int i;
21 | for(i=0;i
3 | #include
4 | #include
5 |
6 | void print_array(int arr[], int n) {
7 | for(int i=0;i1) {
22 | flag=0;
23 | gap=(gap+1)/2;
24 | for(int i=0;i<(n-gap);i++) {
25 | if(arr[i+gap]
2 |
3 | int main(){
4 | int array[100], n, c, d, swap;
5 |
6 | printf("Enter number of elements\n");
7 | scanf("%d", &n);
8 | printf("Enter %d integers\n", n);
9 |
10 | for (c = 0; c < n; c++)
11 | scanf("%d", &array[c]);
12 | for (c = 0 ; c < n - 1; c++){
13 | for (d = 0 ; d < n - c - 1; d++){
14 | if (array[d] > array[d+1]){
15 | swap = array[d];
16 | array[d] = array[d+1];
17 | array[d+1] = swap;
18 | }
19 | }
20 | }
21 |
22 | printf("Sorted list in ascending order:\n");
23 |
24 | for (c = 0; c < n; c++)
25 | printf("%d\n", array[c]);
26 | return 0;
27 | }
28 |
--------------------------------------------------------------------------------
/C/SortingC/insertionSort.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | void printArray(int* A, int n){
4 | for (int i = 0; i < n; i++)
5 | {
6 | printf("%d ", A[i]);
7 | }
8 | printf("\n");
9 | }
10 |
11 | void insertionSort(int *A, int n){
12 | int key, j;
13 | // Loop for passes
14 | for (int i = 1; i <= n-1; i++)
15 | {
16 | key = A[i];
17 | j = i-1;
18 | // Loop for each pass
19 | while(j>=0 && A[j] > key){
20 | A[j+1] = A[j];
21 | j--;
22 | }
23 | A[j+1] = key;
24 | }
25 | }
26 |
27 | int main(){
28 |
29 | int A[] = {12, 54, 65, 7, 23, 9};
30 | int n = 6;
31 | printf("Before Sorting: ");
32 | printArray(A, n);
33 | insertionSort(A, n);
34 | printf("After Sorting: ");
35 | printArray(A, n);
36 | return 0;
37 | }
38 |
--------------------------------------------------------------------------------
/C/gcd_using_recursion.c:
--------------------------------------------------------------------------------
1 | #include
2 | void main()
3 | {
4 | int x,y,a;
5 | int gcd(int x,int y);
6 | printf("Enter any two numbers");
7 | scanf("%d\t%d",&x,&y);
8 | a=gcd(x,y);
9 | printf("The GCD of %d and %d is %d",x,y,a);
10 | }
11 | int gcd(int x,int y)
12 | {
13 | if(y!=0)
14 | {
15 | return gcd(y,x%y);
16 | }
17 | else
18 | return x;
19 | }
20 |
--------------------------------------------------------------------------------
/C/queuearray.c:
--------------------------------------------------------------------------------
1 | // Simulation of Queue using Array
2 | #include
3 | #include
4 | #define maxsize 5
5 | void enqueue();
6 | void dequeue();
7 | void display();
8 | int front = -1, rear = -1;
9 | int queue[maxsize];
10 |
11 | void main ()
12 | {
13 |
14 | int choice;
15 | while(choice != 4)
16 | {
17 |
18 | printf("\nChoose any option from below\n");
19 | printf("\n1.Insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
20 | printf("\nEnter your choice:\n");
21 |
22 | scanf("%d",&choice);
23 |
24 | switch(choice)
25 | {
26 | case 1:
27 | enqueue();
28 | break;
29 |
30 | case 2:
31 | dequeue();
32 | break;
33 |
34 | case 3:
35 | display();
36 | break;
37 |
38 | case 4:
39 | exit(0);
40 | break;
41 | default:
42 | printf("\nEnter valid choice??\n");
43 | }
44 | }
45 | }
46 |
47 | void enqueue()
48 | {
49 | int item;
50 | printf("\nEnter the element\n");
51 | scanf("\n%d",&item);
52 | if(rear == maxsize-1)
53 | {
54 | printf("\nOVERFLOW\n");
55 | return;
56 | }
57 | if(front == -1 && rear == -1)
58 | {
59 | front = 0;
60 | rear = 0;
61 | queue[rear]= item;
62 | }
63 | else
64 | {
65 | rear = rear+1;
66 | }
67 | queue[rear] = item;
68 | printf("\nValue enqueueed ");
69 | }
70 |
71 | void dequeue()
72 | {
73 | int item;
74 | if (front == -1 || front > rear)
75 | {
76 | printf("\nUNDERFLOW\n");
77 | return;
78 | }
79 | else
80 | {
81 | item = queue[front];
82 | if(front == rear)
83 | {
84 | front = -1;
85 | rear = -1 ;
86 | }
87 | else
88 | {
89 | front = front + 1;
90 | }
91 | printf("\nvalue dequeued ");
92 | }
93 | }
94 |
95 | void display()
96 | {
97 | int i;
98 | if(rear == -1)
99 | {
100 | printf("\nEmpty queue\n");
101 | }
102 | else
103 | { printf("\n.............printing values ..............\n");
104 | for(i=front;i<=rear;i++)
105 | {
106 | printf("\n%d\n",queue[i]);
107 | }
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/C/reverse_number.c:
--------------------------------------------------------------------------------
1 |
2 |
3 | #include
4 |
5 | int main()
6 | {
7 | int n,nn,rem,sum=0;
8 | printf("Enter a number\n");
9 | scanf("%d",&n);
10 | nn=n;
11 | while(nn!=0)
12 | {
13 | rem=nn%10;
14 | sum=sum*10+rem;
15 | nn=nn/10;
16 | }
17 | printf("The reversed number is : %d",sum);
18 | return 0;
19 | }
20 |
--------------------------------------------------------------------------------
/C/stack.c:
--------------------------------------------------------------------------------
1 | //implementation of stack in c
2 |
3 | #include
4 | #define MAX 5
5 | int stack[MAX]; //Global Data
6 | int top=-1;
7 |
8 | int item,i;
9 |
10 | void main() //main function
11 | {
12 | int ch;
13 | do //display menu
14 | {
15 | printf("\n________________________________________________________________\n");
16 | printf("\n@@@@@... SELECT ...@@@@@\n");
17 | printf("\n-->1 PUSH\n-->2 POP\n-->3 DISPLAY\n-->4 EXIT");
18 | printf("\n________________________________________________________________\n");
19 | printf("\n->YOUR CHOICE.....? ");
20 | scanf("%d",&ch);
21 | switch(ch)
22 | {
23 |
24 | case 1: //case 1 for push
25 |
26 | if(top==MAX-1) //check condition
27 | printf("\n STACK OVERFLOW");
28 | else
29 | {
30 | printf("\nEnter the number you want to PUSH ");
31 | scanf("%d",&item);
32 | top=top+1; //increase the top
33 | stack[top]=item;
34 | printf("\n%d is pushed onto stack",item);
35 | }
36 |
37 | break;
38 |
39 |
40 | case 2: //case 2 for pop
41 |
42 | if(top==-1) //check condition
43 | printf("\n STACK UNDERFLOW");
44 | else
45 | {
46 | item = stack[top];
47 | top=top-1; //decrease the top
48 | printf("\n%d is popped from stack ",item);
49 | }
50 |
51 | break;
52 |
53 |
54 | case 3: //case 3 for display
55 |
56 | if(top==-1) //check condition
57 | printf("\nStack is empty ");
58 | else
59 | {
60 | printf("\nStack is-->\n\t");
61 | for(i=top;i>=0;i--)
62 | {
63 | printf("%d\n\t",stack[i]);
64 | }
65 | }
66 |
67 | break;
68 |
69 |
70 | case 4: //Exit case
71 |
72 | break;
73 |
74 | default : printf("\n ...SORRY...\nWrong Choice ");
75 | }
76 |
77 | }while(ch!=4);
78 |
79 | printf("\nThank You....!");
80 | } // end of main
81 | //END OF PROGRAM
82 |
--------------------------------------------------------------------------------
/CPP/BFS/BFS_Adj_List.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using namespace std;
4 | #define int long long
5 | #define pb push_back
6 |
7 | vector bfs(int n, vector> adj[]){
8 | vector bfs;
9 | vector vis(n+1, 0);
10 |
11 | for(int i=1; i<=n; i++){
12 | if(!vis[i]){
13 | queue q;
14 | cout<<"i-"<>n>>m>>bidir;
39 |
40 | vector> adj[n+1];
41 | for(int i=0; i>u>>v>>wt;
44 | adj[u].pb({v, wt});
45 | if(bidir)
46 | adj[v].pb({u, wt});
47 | }
48 | //call bfs here
49 | // int src;
50 | // cin>>src;
51 | vector bfs_res=bfs(n, adj);
52 | for(int i=0; i> t;
67 | while (t--) {
68 | solve();
69 | }
70 |
71 | cerr << "time taken : " << (float) clock() / CLOCKS_PER_SEC << " secs" << endl;
72 | return 0;
73 | }
74 |
--------------------------------------------------------------------------------
/CPP/BFS/BFS_graph_traversal.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | using namespace std;
4 | int cost[10][10],i,j,k,n,queue[10],front=0,rear=0,v,visit[10],visited[10]; //INITIALIZATION
5 | int main()
6 | {
7 | int m;
8 | cout <<"Enter no. of vertices: "; // TAKING INPUT VALUE FOR VERICES
9 | cin >> n;
10 | cout <<"Enter no. of edges: "; // TAKING INPUT VALUE FOR EDGES
11 | cin >> m;
12 | cout <<"\nEnter the EDGES: \n";
13 | for(k=1;k<=m;k++)
14 | {
15 | cin >>i>>j; // GIVING THE EDGES VALUE (Eg:1 2)
16 | cost[i][j]=1;
17 | }
18 | cout <<"\nEnter the initial vertex: "; // GIVING THE INITIAL VERTEX TO START
19 | cin >>v; // BFS GRAPH TRAVERSAL
20 | cout <<"Visitied vertices (BFS) are: ";
21 | cout <
2 | #include
3 | #include
4 | using namespace std;
5 | #define MAX 1000
6 |
7 | /*DP Solution for Catalan Numbers.TLE for Large value of MAX*/
8 | long long dp[MAX];
9 | void catalan(){
10 | dp[0]=dp[1]=1;
11 | for(int i=2;i<10000;i++){
12 | dp[i]=0;
13 | for(int j=0;j>t;
26 |
27 | while(t--){
28 | scanf("%d",&n);
29 | printf("%lld\n",dp[n]);
30 | }
31 | return 0;
32 | }
33 |
--------------------------------------------------------------------------------
/CPP/Catalan/Catalan using Inverse Modulo.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | using namespace std;
5 | #define MOD 1000000007
6 | #define ll long long
7 | ll fact[2000005];
8 |
9 |
10 | void factorial(){
11 | fact[0]=fact[1]=1;
12 | for(int i=2;i<=2000000;i++)
13 | {fact[i]=i*fact[i-1];
14 | fact[i]%=MOD;
15 | }
16 | }
17 |
18 | ll pow(ll a,ll b)
19 | {
20 | ll res=1;
21 | while(b){
22 |
23 | if(b&1)
24 | res=(res*a)%MOD;
25 |
26 | a=(a*a)%MOD;
27 | b=b>>1;
28 | }
29 | return res;
30 | }
31 |
32 | ll catalan(ll n){
33 | return ((((fact[2*n]*pow(fact[n],MOD-2))%MOD)*pow(fact[n+1],MOD-2))%MOD)%MOD;
34 | }
35 |
36 |
37 |
38 | int main(){
39 | ll t,n;
40 | factorial();
41 | cin>>t;
42 | while(t--){
43 | scanf("%lld",&n);
44 | printf("%lld\n",catalan(n));
45 | }
46 |
47 | return 0;
48 | }
49 |
--------------------------------------------------------------------------------
/CPP/DFS/Building_teams.cpp:
--------------------------------------------------------------------------------
1 | // https://cses.fi/problemset/task/1668 Problem link
2 | // This is a nice problem to understand the concept of Bipartite graph
3 |
4 |
5 | #include
6 | #include
7 | using namespace std ;
8 |
9 | vector adj[100001];
10 | int visited[100001];
11 | int team[100001] ;
12 |
13 | bool DFS_Team(int start , int t){
14 |
15 | visited[start] = 1 ;
16 | team[start] = t ;
17 |
18 | for(int x : adj[start]){
19 | if(visited[x] == 0){
20 | if(t == 1){
21 | if(DFS_Team(x , 2) == false) return false ;
22 | }
23 | else if(t == 2)
24 | if(DFS_Team(x , 1) == false) return false ;
25 | }
26 | else {
27 | if(team[x] == team[start]) return false ;
28 | }
29 | }
30 |
31 | return true ;
32 |
33 | }
34 |
35 | int main(){
36 |
37 | int v , e ;
38 | cin >> v >> e ;
39 |
40 | for(int i = 1 ; i <= e ; i++){
41 | int start , end;
42 | cin >> start >> end ;
43 | adj[start].push_back(end);
44 | adj[end].push_back(start);
45 | }
46 |
47 | bool ans = true;
48 | for(int i = 1; i <= v ; i++){
49 | if(visited[i] == 0){
50 | int result = DFS_Team(i ,1) ;
51 | ans = result ;
52 | if(result == false){
53 | break ;
54 | }
55 |
56 | }
57 | }
58 | if(ans == false) cout<<"IMPOSSIBLE";
59 | else {
60 | for(int i = 1 ; i <= v ; i++){
61 | cout<
2 | #include
3 | using namespace std;
4 |
5 |
6 | class Graph
7 | {
8 | int V;
9 | list *adj;
10 |
11 | public:
12 |
13 | bool DLS(int src, int target, int limit){
14 | if (src == target)
15 | return true;
16 |
17 | // If reached the maximum depth, stop recursing.
18 | if (limit <= 0)
19 | return false;
20 |
21 | for (auto i = adj[src].begin(); i != adj[src].end(); ++i)
22 | if (DLS(*i, target, limit-1) == true)
23 | return true;
24 |
25 | return false;
26 | }
27 |
28 | Graph(int V){
29 | this->V = V;
30 | adj = new list[V];
31 | }
32 |
33 | void addEdge(int v, int w){
34 | adj[v].push_back(w); // Add w to v’s list.
35 | }
36 |
37 | bool IDDFS(int src, int target, int max_depth){ // Repeatedly depth-limit search till the maximum depth.
38 | for (int i = 0; i <= max_depth; i++){
39 | if (DLS(src, target, i) == true)
40 | return true;
41 | }
42 | return false;
43 | }
44 | };
45 |
46 |
47 |
48 |
49 | int main()
50 | {
51 | Graph g(10);
52 | g.addEdge(0, 1);
53 | g.addEdge(0, 2);
54 | g.addEdge(1, 5);
55 | g.addEdge(1, 8);
56 | g.addEdge(2, 9);
57 | g.addEdge(5, 16);
58 | g.addEdge(5, 5);
59 | g.addEdge(8, 2);
60 |
61 | int target = 16, limit = 3, src = 0;
62 | if (g.IDDFS(src, target, limit) == true)
63 | cout << "Target reached within Maxdepth: "<< limit << endl;
64 | else
65 | cout << "Target cannot be reached within Maxdepth: "<< limit << endl;
66 | return 0;
67 | }
68 |
--------------------------------------------------------------------------------
/CPP/DFS/dfsalgo.cpp:
--------------------------------------------------------------------------------
1 | // DFS algorithm in C++
2 | #include
3 | #include
4 | using namespace std;
5 |
6 | class Graph {
7 | int numVertices;
8 | list *adjLists;
9 | bool *visited;
10 |
11 | public:
12 | Graph(int V);
13 | void addEdge(int src, int dest);
14 | void DFS(int vertex);
15 | };
16 |
17 | // Initialize graph
18 | Graph::Graph(int vertices) {
19 | numVertices = vertices;
20 | adjLists = new list[vertices];
21 | visited = new bool[vertices];
22 | }
23 |
24 | // Add edges
25 | void Graph::addEdge(int src, int dest) {
26 | adjLists[src].push_front(dest);
27 | }
28 |
29 | // DFS algorithm
30 | void Graph::DFS(int vertex) {
31 | visited[vertex] = true;
32 | list adjList = adjLists[vertex];
33 |
34 | cout << vertex << " ";
35 |
36 | list::iterator i;
37 | for (i = adjList.begin(); i != adjList.end(); ++i)
38 | if (!visited[*i])
39 | DFS(*i);
40 | }
41 |
42 | int main() {
43 | Graph g(4);
44 | g.addEdge(0, 1);
45 | g.addEdge(0, 2);
46 | g.addEdge(1, 2);
47 | g.addEdge(2, 3);
48 |
49 | g.DFS(2);
50 |
51 | return 0;
52 | }
--------------------------------------------------------------------------------
/CPP/DP/Bellman Ford Algorithm.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coder-forfun/Hactoberfest-accepted/134a579948482348af9684ad8b351ae254b6fe00/CPP/DP/Bellman Ford Algorithm.cpp
--------------------------------------------------------------------------------
/CPP/DP/knapsack.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #define MAX 10
3 | using namespace std;
4 | struct product
5 | {
6 | int product_num;
7 | int profit;
8 | int weight;
9 | float ratio;
10 | float take_quantity;
11 | };
12 | int main()
13 | {
14 | product P[MAX],temp;
15 | int i,j,total_product,capacity;
16 | float value=0;
17 | cout<<"ENTER NUMBER OF ITEMS : ";
18 | cin>>total_product;
19 | cout<<"ENTER CAPACITY OF SACK : ";
20 | cin>>capacity;
21 | cout<<"\n";
22 | for(i=0;i>P[i].profit>>P[i].weight;
27 |
28 | P[i].ratio=(float)P[i].profit/P[i].weight;
29 | P[i].take_quantity=0;
30 | }
31 |
32 | //HIGHEST RATIO BASED SORTING
33 | for(i=0;icapacity)
55 | {
56 | P[i].take_quantity=(float)capacity/P[i].weight;
57 | capacity=0;
58 | }
59 | }
60 |
61 | cout<<"\n\nPRODUCTS TO BE TAKEN -";
62 | for(i=0;i
3 | using namespace std;
4 | int dp[100][100];
5 |
6 | // Function for matrix chain multiplication
7 | int matrixChainMemoised(int* p, int i, int j)
8 | {
9 | if (i == j)
10 | {
11 | return 0;
12 | }
13 | if (dp[i][j] != -1)
14 | {
15 | return dp[i][j];
16 | }
17 | dp[i][j] = INT_MAX;
18 | for (int k = i; k < j; k++)
19 | {
20 | dp[i][j] = min(
21 | dp[i][j], matrixChainMemoised(p, i, k)
22 | + matrixChainMemoised(p, k + 1, j)
23 | + p[i - 1] * p[k] * p[j]);
24 | }
25 | return dp[i][j];
26 | }
27 | int MatrixChainOrder(int* p, int n)
28 | {
29 | int i = 1, j = n - 1;
30 | return matrixChainMemoised(p, i, j);
31 | }
32 |
33 | // Driver Code
34 | int main()
35 | {
36 | int arr[] = { 1, 2, 3, 4 };
37 | int n = sizeof(arr) / sizeof(arr[0]);
38 | memset(dp, -1, sizeof dp);
39 |
40 | cout << "Minimum number of multiplications is "
41 | << MatrixChainOrder(arr, n);
42 | }
43 |
--------------------------------------------------------------------------------
/CPP/Dijikstra/dijikstra.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coder-forfun/Hactoberfest-accepted/134a579948482348af9684ad8b351ae254b6fe00/CPP/Dijikstra/dijikstra.cpp
--------------------------------------------------------------------------------
/CPP/Floyd-Warshall/Floyd–Warshall Algorithm.cpp:
--------------------------------------------------------------------------------
1 | //https://cses.fi/problemset/task/1672/
2 | #include
3 | using namespace std;
4 |
5 | typedef long long ll;
6 | const ll inf=1e18;
7 | int main()
8 | {
9 | ios_base::sync_with_stdio(false);
10 | cin.tie(NULL);cout.tie(NULL);
11 |
12 | ll n,m,q;
13 | cin >> n >> m >> q;
14 | ll dp[n+1][n+1];
15 | for(int i=1;i<=n;i++)
16 | {
17 | for(int j=1;j<=n;j++)
18 | {
19 | dp[i][j]=inf;
20 | }
21 | }
22 | for(int i=0;i> x >> y >> l;
26 | dp[x][y] =min(dp[x][y],l);
27 | dp[y][x] = min(dp[y][x],l);
28 | }
29 | //floyd-warshall
30 | for(int i=1;i<=n;i++)
31 | {
32 | for(int j=1;j<=n;j++)
33 | {
34 | for(int k=1;k<=n;k++)
35 | {
36 | if(j==i || k==i)
37 | continue;
38 | else if(j==k)
39 | dp[j][k]=0;
40 | else
41 | {
42 | ll d1 = dp[j][i]+dp[i][k];
43 | ll d2 = dp[k][i]+dp[i][j];
44 | ll d = min(d1,d2);
45 | if(dp[j][k]>d)
46 | dp[j][k]=d;
47 | }
48 | }
49 | }
50 | }
51 | while(q--)
52 | {
53 | ll x,y;
54 | cin >> x >> y;
55 | if(dp[x][y]==inf)
56 | cout << -1 << "\n";
57 | else
58 | cout << dp[x][y] << "\n";
59 | }
60 | return 0;
61 | }
62 |
63 |
64 |
--------------------------------------------------------------------------------
/CPP/Floyd-Warshall/flyod_warshall.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | using namespace std;
4 | void floyds(int b[][7])
5 | {
6 | int i, j, k;
7 | for (k = 0; k < 7; k++)
8 | {
9 | for (i = 0; i < 7; i++)
10 | {
11 | for (j = 0; j < 7; j++)
12 | {
13 | if ((b[i][k] * b[k][j] != 0) && (i != j))
14 | {
15 | if ((b[i][k] + b[k][j] < b[i][j]) || (b[i][j] == 0))
16 | {
17 | b[i][j] = b[i][k] + b[k][j];
18 | }
19 | }
20 | }
21 | }
22 | }
23 | for (i = 0; i < 7; i++)
24 | {
25 | cout<<"\nMinimum Cost With Respect to Node:"<>b[i][j];
43 | }
44 | }
45 | floyds(b);
46 | getch();
47 | }
48 |
--------------------------------------------------------------------------------
/CPP/LinkedList/ReverseDoublyLinkedList.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | struct Node{
5 | int data;
6 | Node* next;
7 | Node* prev;
8 | };
9 |
10 | typedef struct Node* node;
11 |
12 |
13 | Node* createDLL(int n){
14 | node curr,head = NULL;
15 | for(int i=0;inext=NULL;
19 | head->prev = NULL;
20 | curr = head;
21 | }else{
22 | node newnode = new Node();
23 | newnode->next=NULL;
24 | curr->next = newnode;
25 | newnode->prev = curr;
26 | curr = newnode;
27 | }
28 | }
29 | return head;
30 | }
31 |
32 | void display(Node* head){
33 | node temp = head;
34 | while(temp!=NULL){
35 | cout<data<<" ";
36 | temp = temp->next;
37 | }
38 | }
39 |
40 | Node* insertdata(Node* head){
41 | node temp = head;
42 | int k=0;
43 | while(temp!=NULL){
44 | cout<<"Enter the value for Node "<>temp->data;
46 | cout<<"\n";
47 | temp=temp->next;
48 | k=k+1;
49 | }
50 | }
51 |
52 | Node* reverseDLL(Node* head){
53 | node temp = head;
54 | if(temp == NULL){
55 | return NULL;
56 | }
57 | if(temp->next == NULL){
58 | return head;
59 | }
60 | else{
61 | node prevadd;
62 | while(temp!=NULL){
63 | prevadd = temp->next;
64 | temp->next = temp->prev;
65 | temp->prev = prevadd;
66 | head = temp;
67 | temp = temp->prev;
68 | }
69 | return head;
70 | }
71 | }
72 |
73 |
74 |
75 |
76 | int main(){
77 | int nNodes;
78 | cout<<"Enter the number of nodes : \n";
79 | cin>>nNodes;
80 | cout<<"\n";
81 | node head = createDLL(nNodes);
82 | insertdata(head);
83 | cout<<"Original Linked List : \n";
84 | display(head);
85 | head = reverseDLL(head);
86 | cout<<"\nReversed Linked List : \n";
87 | display(head);
88 | return 0;
89 |
90 | }
--------------------------------------------------------------------------------
/CPP/Matrix/ananya998_rotate_a_matrix.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | #define N 4
5 |
6 | void rotate90Clockwise(int a[N][N])
7 | {
8 |
9 |
10 | for (int i = 0; i < N / 2; i++) {
11 | for (int j = i; j < N - i - 1; j++) {
12 |
13 | int temp = a[i][j];
14 | a[i][j] = a[N - 1 - j][i];
15 | a[N - 1 - j][i] = a[N - 1 - i][N - 1 - j];
16 | a[N - 1 - i][N - 1 - j] = a[j][N - 1 - i];
17 | a[j][N - 1 - i] = temp;
18 | }
19 | }
20 | }
21 |
22 |
23 | void printMatrix(int arr[N][N])
24 | {
25 | for (int i = 0; i < N; i++) {
26 | for (int j = 0; j < N; j++)
27 | cout << arr[i][j] << " ";
28 | cout << '\n';
29 | }
30 | }
31 |
32 |
33 | int main()
34 | {
35 | int arr[N][N] = { { 1, 2, 3, 4 },
36 | { 5, 6, 7, 8 },
37 | { 9, 10, 11, 12 },
38 | { 13, 14, 15, 16 } };
39 | rotate90Clockwise(arr);
40 | printMatrix(arr);
41 | return 0;
42 | }
43 |
--------------------------------------------------------------------------------
/CPP/Matrix/spiral_matrix.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | vector spiralOrder(vector >& matrix)
5 | {
6 | vector ans;
7 |
8 | if (matrix.size() == 0)
9 | return ans;
10 |
11 | int R = matrix.size(), C = matrix[0].size();
12 | vector > seen(R, vector(C, false));
13 | int dr[] = { 0, 1, 0, -1 };
14 | int dc[] = { 1, 0, -1, 0 };
15 | int r = 0, c = 0, di = 0;
16 | for (int i = 0; i < R * C; i++) {
17 | ans.push_back(matrix[r]);
18 | seen[r] = true;
19 | int cr = r + dr[di];
20 | int cc = c + dc[di];
21 |
22 | if (0 <= cr && cr < R && 0 <= cc && cc < C
23 | && !seen[cr][cc]) {
24 | r = cr;
25 | c = cc;
26 | }
27 | else {
28 | di = (di + 1) % 4;
29 | r += dr[di];
30 | c += dc[di];
31 | }
32 | }
33 | return ans;
34 | }
35 |
36 | // Driver code
37 | int main()
38 | {
39 | vector > a{ { 1, 2, 3, 4 },
40 | { 5, 6, 7, 8 },
41 | { 9, 10, 11, 12 },
42 | { 13, 14, 15, 16 } };
43 |
44 | for (int x : spiralOrder(a)) {
45 | cout << x << " ";
46 | }
47 | return 0;
48 | }
49 |
--------------------------------------------------------------------------------
/CPP/Numbers/Avg_Marks.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | using namespace std;
4 |
5 | class Student
6 | {
7 | public:
8 | double m1, m2, m3, arr[3];
9 | string nameA,usn;
10 |
11 | void set(string u, string name, double n1, double n2, double n3);
12 | double avg(double, double, double);
13 | void display(int, Student *);
14 | ~Student()
15 | {
16 | cout << "Destructor is called!\n";
17 | }
18 | };
19 |
20 | void Student::set(string u, string name, double n1, double n2, double n3)
21 | {
22 | usn = u;
23 | nameA = name;
24 | m1 = n1;
25 | m2 = n2;
26 | m3 = n3;
27 | }
28 |
29 | double
30 | Student::avg(double m1, double m2, double m3)
31 | {
32 |
33 | return (m1+m2+m3) / 3;
34 | }
35 |
36 | void Student::display(int n, Student *s)
37 | {
38 | for (int i = 0; i < n; i++)
39 | {
40 | cout << "Name : " << s[i].nameA << endl;
41 | cout << "USN : " << s[i].usn << endl;
42 | cout << "Average : " << s[i].avg(s[i].m1, s[i].m2, s[i].m3);
43 | cout << "\n\n";
44 | }
45 | }
46 |
47 | int main()
48 | {
49 | int n;
50 | double m1, m2, m3;
51 | string name,usn;
52 | cout << "Enter the number of students\n";
53 | cin >> n;
54 | Student *s = new Student[n];
55 | for (int i = 0; i < n; i++)
56 | {
57 | cout << "Enter the name\n";
58 | cin >> name;
59 | cout << "Enter the usn\n";
60 | cin >> usn;
61 | cout << "Enter the marks of 3 tests\n";
62 | cin >> m1 >> m2 >> m3;
63 | s[i].set(usn, name, m1, m2, m3);
64 | }
65 | cout << "\n\nThe details are :\n\n";
66 | s->display(n, s);
67 | delete[] s;
68 | return 0;
69 | }
--------------------------------------------------------------------------------
/CPP/Numbers/Nth Fibonacci Number.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | int fib(int n)
5 | {
6 | if(n == 0)
7 | return 0;
8 | if(n == 1)
9 | return 1;
10 | return fib(n-1)+fib(n-2);
11 | }
12 |
13 | int main()
14 | {
15 | int n, num=0;
16 | cout<<"Enter the no. of term you want to search in fibonaci series: ";
17 | cin>>n;
18 | num = fib(n);
19 | cout<<"nth Term of Fibonacci series is: "<
2 | using namespace std;
3 | int main()
4 | {
5 | cout<<"Enter the term of fibonacci you want to print\n";
6 | int n;
7 | cin>>n;
8 | int dp[n+1]={0};
9 | dp[1]=1;
10 | for(int i=2;i<=n;i++)
11 | dp[i]=dp[i-1]+dp[i-2];
12 | cout<<"Nth term of Fibonacci is\n";
13 | cout<
2 | #include
3 | #include
4 | using namespace std;
5 |
6 | // Partitioning routine of Quicksort
7 | int partition(int A[], int n)
8 | {
9 | int j = 0;
10 | int pivot = 0; // consider 0 as a pivot
11 |
12 | // each time we find a negative number, `j` is incremented,
13 | // and a negative element would be placed before the pivot
14 | for (int i = 0; i < n; i++)
15 | {
16 | if (A[i] < pivot)
17 | {
18 | swap(A[i], A[j]);
19 | j++;
20 | }
21 | }
22 |
23 | // holds the index of the first positive element
24 | return j;
25 | }
26 |
27 | // Function to rearrange a given array such that it contains positive
28 | // and negative numbers at alternate positions
29 | int rearrange(int A[], int size)
30 | {
31 | // partition a given array such that all positive elements move
32 | // to the end of the array
33 | int p = partition(A, size);
34 |
35 | // swap alternate negative elements from the next available positive
36 | // element till the end of the array is reached, or all negative or
37 | // positive elements are exhausted.
38 |
39 | for (int n = 0; (p < size && n < p); p++, n += 2) {
40 | swap(A[n], A[p]);
41 | }
42 | }
43 |
44 | int main()
45 | {
46 | int A[] = { 6, 3, -5, 2, -8, -6, 1, 3 };
47 | int n = sizeof(A)/sizeof(A[0]);
48 |
49 | rearrange(A, n);
50 |
51 | // print the rearranged array
52 | for (int i = 0; i < n; i++) {
53 | cout << setw(3) << A[i];
54 | }
55 |
56 | return 0;
57 | }
--------------------------------------------------------------------------------
/CPP/Numbers/numberPattern.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Program prints pattern:
3 | eg. for n = 4:
4 | 4444444
5 | 4333334
6 | 4322234
7 | 4321234
8 | 4322234
9 | 4333334
10 | 4444444
11 | */
12 | #include
13 | using namespace std;
14 | #define ll long long int
15 |
16 | int main(){
17 |
18 | int n;
19 | cin >> n;
20 | if(n < 0) return -1;
21 | int ar[n][n];
22 | for(int i = 0; i < n; i++){
23 | int k = 0;
24 | for(int j = 0; j < n; j++){
25 | ar[i][j] = n -k;
26 | // cout << n - k;
27 | cout << ar[i][j];
28 | if(k < i) k++;
29 | }
30 | for(int m = n-2; m >= 0; m--){
31 | cout << ar[i][m];
32 | }
33 | cout << endl;
34 | }
35 | for(int i = n - 2; i >= 0; i--){
36 | for(int j = 0; j < n; j++){
37 | cout << ar[i][j];
38 | }
39 | for(int m = n-2; m >= 0; m--){
40 | cout << ar[i][m];
41 | }
42 | cout << endl;
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/CPP/OOPS/DeepCopy and ShallowCopy.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | using namespace std;
4 | class Student {
5 | int age;
6 | char *name;
7 |
8 | public :
9 |
10 | Student(int age, char *name) {
11 | this -> age = age;
12 | // Shallow copy
13 | // this -> name = name;
14 |
15 | // Deep copy
16 | this -> name = new char[strlen(name) + 1];
17 | strcpy(this -> name, name);
18 |
19 | }
20 |
21 | void display() {
22 | cout << name << " " << age << endl;
23 | }
24 | };
25 |
--------------------------------------------------------------------------------
/CPP/PrimsAlgoCPP/Prims.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 |
5 |
6 | int cost[10][10];
7 | int visited[10] = {0};
8 | int mn=0, min_cost=0;
9 |
10 | int n, ne=1, a=0, b=0, u=0, v=0, i, j;
11 | int main()
12 | {
13 |
14 | cout<<"Enter the number of nodes:"<>n;
16 | cout<<"Enter the cost matrix:"<>cost[i][j];
23 | if(cost[i][j]==0)
24 | {
25 | cost[i][j] = 999;
26 | }
27 | }
28 | }
29 | for(i=1; i<=n; i++)
30 | visited[i]=0;
31 | cout<<"Enter the root node:"<>i;
33 | visited[i]=1;
34 | cout<<"Minimum cost spanning tree:"< %d) cost = %d \n", ne++, a, b, mn);
57 | min_cost = min_cost + mn;
58 | visited[b]=1;
59 | }
60 | cost[a][b] = cost[b][a] = 999;
61 | }
62 | cout<<"\nMinimum cost: " <
2 | #include
3 | using namespace std;
4 |
5 | #define V 5
6 | int c = 0;
7 |
8 | int minKey(int *key, bool *mstSet)
9 | {
10 | int min = INT_MAX, min_index;
11 | for (int i = 0; i < V; i++)
12 | {
13 | if (mstSet[i] == false && key[i] < min)
14 | {
15 | min = key[i], min_index = i;
16 | }
17 | }
18 | return min_index;
19 | }
20 |
21 | void display(int *parent, int n, int graph[V][V])
22 | {
23 | cout << "Edge Weight\n";
24 | int cost=0;
25 | for (int i = 1; i < V; i++)
26 | {
27 | printf("%d - %d %d \n", parent[i], i, graph[i][parent[i]]);
28 | cost+=graph[i][parent[i]];
29 | }
30 | cout<<"Total cost = "<> graph[i][j];
70 | }
71 | }
72 | primMST(graph);
73 | return 0;
74 | }
--------------------------------------------------------------------------------
/CPP/Queue/Improved_Bubble_Sort.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 | int main(void){
4 | int n;
5 | cout<<"Enter Size of array: ";
6 | cin>>n;
7 | int arr[n];
8 | cout<<"Enter elements in the array: ";
9 | for(int i=0;i>arr[i];
11 | }
12 | bool flag=false;
13 | for(int x=0; xarr[y+1])
19 | {
20 | swap(arr[y],arr[y+1]);
21 | flag = true;
22 | }
23 | }
24 | if(!flag){
25 | break;
26 | }
27 | }
28 | cout<<"Elements in array after sorting: "<
2 | #define size 20
3 | using namespace std;
4 | class queue{
5 | public:
6 | int queue[size];
7 | int ele = -1;
8 | int len=0;
9 | void push(int value){
10 | ele++;
11 | queue[ele]=value;
12 | len++;
13 | }
14 | void pop(){
15 | for(int i=0;i";
24 | }
25 | cout<<"NULL";
26 | }
27 | void peek(){
28 | cout<<"\n first "<
2 | using namespace std;
3 | class node{
4 | public:
5 | int data;
6 | node* next;
7 | node(int value){
8 | data=value;
9 | next=NULL;
10 | }
11 | };
12 | class stack{
13 | public:
14 | node* top;
15 | node* bottom;
16 | int length = 0;
17 | stack(){
18 | top=NULL;
19 | bottom=NULL;
20 | length=0;
21 | }
22 | void push(node* &head,int value){
23 | node* temp=head;
24 | node* n = new node(value);
25 | if(length==0){
26 | head=n;
27 | top=n;
28 | }else{
29 | top->next=n;
30 | top=n;
31 |
32 | }
33 | length++;
34 | }
35 | void display(node* head){
36 | if(length==0){
37 | cout<<"NOTHING TO DISPLAY";
38 | }else{
39 | while(head!=NULL){
40 | cout<data<<"-->";
41 | head=head->next;
42 | }
43 | cout<<"NULL";
44 | }
45 | }
46 | void peek(){
47 | cout<<"\nTop Data: "<data;
48 | }
49 | void DELETE(node* &head){
50 | if(length==0){
51 | cout<<"Stack is UNDERFLOWN";
52 | }
53 | else{
54 | node* temp = top;
55 | node* prev=NULL;
56 | while(temp->next!=NULL){
57 | prev=temp;
58 | temp=temp->next;
59 | }
60 | prev->next=NULL;
61 | top=prev;
62 | delete(temp);
63 |
64 | }
65 | }
66 | };
67 | int main()
68 | {
69 | stack* s = new stack;
70 | node* head=NULL;
71 | s->push(head,2);
72 | s->push(head,3);
73 | s->push(head,12);
74 | s->push(head,13);
75 | s->push(head,18);
76 | s->display(head);
77 | cout<<"\n";
78 | // s->DELETE(head);
79 | // s->display(head);
80 | // s->DELETE(head);
81 | // s->display(head);
82 | s->peek();
83 | // s->peek();
84 | return 0;
85 | }
86 |
--------------------------------------------------------------------------------
/CPP/STACK/Stack.cpp:
--------------------------------------------------------------------------------
1 | /* C++ program to implement basic stack
2 | operations */
3 | #include
4 |
5 | using namespace std;
6 |
7 | #define MAX 1000
8 |
9 | class Stack {
10 | int top;
11 |
12 | public:
13 | int a[MAX]; // Maximum size of Stack
14 |
15 | Stack() { top = -1; }
16 | bool push(int x);
17 | int pop();
18 | int peek();
19 | bool isEmpty();
20 | };
21 |
22 | bool Stack::push(int x)
23 | {
24 | if (top >= (MAX - 1)) {
25 | cout << "Stack Overflow";
26 | return false;
27 | }
28 | else {
29 | a[++top] = x;
30 | cout << x << " pushed into stack\n";
31 | return true;
32 | }
33 | }
34 |
35 | int Stack::pop()
36 | {
37 | if (top < 0) {
38 | cout << "Stack Underflow";
39 | return 0;
40 | }
41 | else {
42 | int x = a[top--];
43 | return x;
44 | }
45 | }
46 | int Stack::peek()
47 | {
48 | if (top < 0) {
49 | cout << "Stack is Empty";
50 | return 0;
51 | }
52 | else {
53 | int x = a[top];
54 | return x;
55 | }
56 | }
57 |
58 | bool Stack::isEmpty()
59 | {
60 | return (top < 0);
61 | }
62 |
63 | // Driver program to test above functions
64 | int main()
65 | {
66 | class Stack s;
67 | s.push(10);
68 | s.push(20);
69 | s.push(30);
70 | cout << s.pop() << " Popped from stack\n";
71 | //print all elements in stack :
72 | cout<<"Elements present in stack : ";
73 | while(!s.isEmpty())
74 | {
75 | // print top element in stack
76 | cout<
3 | using namespace std;
4 | bool isBalanced(string st)
5 | {
6 | stack s;
7 | char x;
8 | for(int i=0;i>t;
44 | while(t>0)
45 | {
46 | string s;
47 | cin>>s;
48 | if(isBalanced(s))
49 | cout<<"Valid\n";
50 | else
51 | cout<<"Not Valid\n";
52 | t--;
53 | }
54 | return 0;
55 | }
56 |
--------------------------------------------------------------------------------
/CPP/STACK/stack_using_STL.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | using namespace std;
4 | void createStack(stack mystack)
5 | {
6 | stack ms = mystack;
7 | while (!ms.empty())
8 | {
9 | cout< st;
17 | st.push(32);
18 | st.push(21);
19 | st.push(39);
20 | st.push(89);
21 | st.push(25);
22 |
23 | cout << "The stack st is: ";
24 | createStack(st);
25 | cout << "st.size() : " << st.size();
26 | cout << "st.top() : " << st.top();
27 | cout << "st.pop() : ";
28 | st.pop();
29 | createStack(st);
30 | return 0;
31 | }
32 |
--------------------------------------------------------------------------------
/CPP/STL_Vector/clear().cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using namespace std;
4 |
5 | #include
6 |
7 | int main()
8 | {
9 | vector a ={1,2,3,4,5};
10 | vector::iterator i;
11 |
12 | a.clear();
13 | for(i=a.begin();i!=a.end();i++)
14 | {
15 | cout<< *i<<" ";
16 | }
17 |
18 | return 0;
19 | }
20 |
21 |
--------------------------------------------------------------------------------
/CPP/STL_Vector/erase().cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using namespace std;
4 |
5 | #include
6 |
7 | int main()
8 | {
9 | vector a ={1,2,3,4,5};
10 | vector::iterator i;
11 |
12 | i=a.begin()+2;
13 | a.erase(i);
14 | for(i=a.begin();i!=a.end();i++)
15 | {
16 | cout<< *i<<" ";
17 | }
18 |
19 | return 0;
20 | }
21 |
22 |
--------------------------------------------------------------------------------
/CPP/STL_Vector/front() and back().cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using namespace std;
4 |
5 | #include
6 |
7 | int main()
8 | {
9 | vector a ={0,1,2,3,4,5,6,7};
10 | vector::iterator i;
11 |
12 |
13 | /*for(i=a.begin();i!=a.end();i++)
14 | {
15 | cout<< *i<<" ";
16 | }*/
17 | cout<
2 |
3 | using namespace std;
4 |
5 | #include
6 |
7 | int main()
8 | {
9 | vector a = {1,2,3,4,5};
10 | vector s={"nikhil"};
11 |
12 | cout<
2 |
3 | using namespace std;
4 |
5 | #include
6 |
7 | int main()
8 | {
9 | vector v={1,2,3,4,5};
10 | vector:: iterator i=v.begin();
11 |
12 | v.insert(i,2,10);
13 |
14 | for(i = v.begin(); i != v.end(); i++)
15 | {
16 | cout << *i <<" ";
17 | }
18 | return 0;
19 | }
20 |
21 |
--------------------------------------------------------------------------------
/CPP/STL_Vector/pop_back().cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using namespace std;
4 |
5 | #include
6 |
7 | int main()
8 | {
9 | vector a ={1,2,3,4,5};
10 | vector::iterator i;
11 |
12 | a.pop_back();
13 | for(i=a.begin();i!=a.end();i++)
14 | {
15 | cout<< *i<<" ";
16 | }
17 |
18 | return 0;
19 | }
20 |
21 |
--------------------------------------------------------------------------------
/CPP/STL_Vector/push_back().cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using namespace std;
4 |
5 | #include
6 |
7 | int main()
8 | {
9 | vector a;
10 | int i;
11 |
12 | a.push_back(1);
13 | a.push_back(2);
14 | a.push_back(3);
15 |
16 | for(i=0;i
2 |
3 | using namespace std;
4 |
5 | #include
6 |
7 | int main()
8 | {
9 | vector a ={1,2,3,4,5};
10 | vector::iterator i;
11 |
12 | a.resize(3);
13 | a.resize(8);
14 | for(i=a.begin();i!=a.end();i++)
15 | {
16 | cout<< *i<<" ";
17 | }
18 |
19 | return 0;
20 | }
21 |
22 |
--------------------------------------------------------------------------------
/CPP/STL_Vector/size().cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using namespace std;
4 |
5 | #include
6 |
7 | int main()
8 | {
9 | vector a ={1,2,3,4,5};
10 | vector::iterator i;
11 |
12 |
13 | /*for(i=a.begin();i!=a.end();i++)
14 | {
15 | cout<< *i<<" ";
16 | }*/
17 | cout<
3 | using namespace std;
4 | #define int int64_t
5 |
6 | const int MAXN = 1e5 + 5;
7 | int n;
8 | int tree[ 4*MAXN ]; // segment Tree
9 | int a[ MAXN ]; // array
10 |
11 | // set a[idx] = val in O(logN)
12 | void update(int v , int tl, int tr , int idx , int val) {
13 | if(tl == tr)
14 | tree[v] = val;
15 | else {
16 | int tmid = (tl + tr)/2;
17 | if(idx > tmid )
18 | update(2*v + 1 , tmid + 1 , tr , idx , val);
19 | else
20 | update(2*v , tl , tmid , idx , val);
21 | tree[v] = tree[2*v] + tree[2*v + 1];
22 | }
23 | }
24 |
25 | // get a[l] + ... + a[r] in O(logN)
26 | int sum(int v , int tl, int tr, int l , int r ){
27 | if(tl > r || l > tr)
28 | return 0;
29 | if(tl >= l && tr <= r)
30 | return tree[v];
31 | int tmid = (tl + tr)/2;
32 | return sum(2*v , tl , tmid , l , r ) + sum(2*v + 1 , tmid + 1 , tr , l , r);
33 | }
34 | int32_t main() {
35 | cin >> n;
36 | for(int i = 0 ; i < n; ++i)
37 | cin >> a[i];
38 |
39 | // count number of inversions in array
40 | for(int i = 0 ; i < n; ++i) {
41 | cout << sum(1 , 0 , n-1 , a[i] , n ) << " ";
42 | update(1 , 0 , n-1 , a[i]-1 , 1 );
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/CPP/Sorting/BubbleSort.cpp:
--------------------------------------------------------------------------------
1 | // Bubble sort
2 |
3 | #include
4 | using namespace std;
5 |
6 | void swap(int *xp, int *yp)
7 | {
8 | int temp = *xp;
9 | *xp = *yp;
10 | *yp = temp;
11 | }
12 |
13 | // A function to implement bubble sort
14 | void bubbleSort(int arr[], int n)
15 | {
16 | int i, j;
17 | for (i = 0; i < n-1; i++)
18 |
19 | // Last i elements are already in place
20 | for (j = 0; j < n-i-1; j++)
21 | if (arr[j] > arr[j+1])
22 | swap(&arr[j], &arr[j+1]);
23 | }
24 |
25 | // Function to print an array
26 | void printArray(int arr[], int size)
27 | {
28 | int i;
29 | for (i = 0; i < size; i++)
30 | cout << arr[i] << " ";
31 | cout << endl;
32 | }
33 |
34 | // Entry Point
35 | int main()
36 | {
37 | int arr[] = {64, 34, 25, 12, 22, 11, 90};
38 | int n = sizeof(arr)/sizeof(arr[0]);
39 | bubbleSort(arr, n);
40 | cout<<"Sorted array: \n";
41 | printArray(arr, n);
42 | return 0;
43 | }
--------------------------------------------------------------------------------
/CPP/Sorting/Bucket Sort.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | using namespace std;
5 |
6 | //Bucket Sort Program when integers are floating point numbers in some range
7 |
8 | void bucketSort(float *arr,int n)
9 | {
10 | vector b[n];
11 | int i,j;
12 | for(i=0;i>n;
38 | int i;
39 | for(i=0;i>arr[i];
41 |
42 | bucketSort(arr,n);
43 | cout<<"Sorted Array"<
3 | using namespace std;
4 |
5 | // To heapify a subtree rooted with node i which is
6 | // an index in arr[]. n is size of heap
7 | void heapify(int arr[], int n, int i)
8 | {
9 | int largest = i; // Initialize largest as root
10 | int l = 2 * i + 1; // left = 2*i + 1
11 | int r = 2 * i + 2; // right = 2*i + 2
12 |
13 | // If left child is larger than root
14 | if (l < n && arr[l] > arr[largest])
15 | largest = l;
16 |
17 | // If right child is larger than largest so far
18 | if (r < n && arr[r] > arr[largest])
19 | largest = r;
20 |
21 | // If largest is not root
22 | if (largest != i) {
23 | swap(arr[i], arr[largest]);
24 |
25 | // Recursively heapify the affected sub-tree
26 | heapify(arr, n, largest);
27 | }
28 | }
29 |
30 | // main function to do heap sort
31 | void heapSort(int arr[], int n)
32 | {
33 | // Build heap (rearrange array)
34 | for (int i = n / 2 - 1; i >= 0; i--)
35 | heapify(arr, n, i);
36 |
37 | // One by one extract an element from heap
38 | for (int i = n - 1; i > 0; i--) {
39 | // Move current root to end
40 | swap(arr[0], arr[i]);
41 |
42 | // call max heapify on the reduced heap
43 | heapify(arr, i, 0);
44 | }
45 | }
46 |
47 | /* A utility function to print array of size n */
48 | void printArray(int arr[], int n)
49 | {
50 | for (int i = 0; i < n; ++i)
51 | cout << arr[i] << " ";
52 | cout << "\n";
53 | }
54 |
55 | // Driver code
56 | int main()
57 | {
58 | int arr[] = { 12, 11, 13, 5, 6, 7 };
59 | int n = sizeof(arr) / sizeof(arr[0]);
60 | heapSort(arr, n);
61 | cout << "Sorted array is \n";
62 | printArray(arr, n);
63 | }
64 |
--------------------------------------------------------------------------------
/CPP/Sorting/Insertionsort4.cpp:
--------------------------------------------------------------------------------
1 | // C++ program for insertion sort
2 | #include
3 | using namespace std;
4 |
5 | /* Function to sort an array using insertion sort*/
6 | void insertionSort(int arr[], int n)
7 | {
8 | int i, key, j;
9 | for (i = 1; i < n; i++)
10 | {
11 | key = arr[i];
12 | j = i - 1;
13 |
14 | /* Move elements of arr[0..i-1], that are
15 | greater than key, to one position ahead
16 | of their current position */
17 | while (j >= 0 && arr[j] > key)
18 | {
19 | arr[j + 1] = arr[j];
20 | j = j - 1;
21 | }
22 | arr[j + 1] = key;
23 | }
24 | }
25 |
26 | // A utility function to print an array of size n
27 | void printArray(int arr[], int n)
28 | {
29 | int i;
30 | for (i = 0; i < n; i++)
31 | cout << arr[i] << " ";
32 | cout << endl;
33 | }
34 |
35 | /* Driver code */
36 | int main()
37 | {
38 | int arr[] = { 12, 11, 13, 5, 6 };
39 | int n = sizeof(arr) / sizeof(arr[0]);
40 |
41 | insertionSort(arr, n);
42 | printArray(arr, n);
43 |
44 | return 0;
45 | }
46 |
47 |
--------------------------------------------------------------------------------
/CPP/Sorting/Radix_sort.cpp:
--------------------------------------------------------------------------------
1 | // radix sort
2 | #include
3 | using namespace std;
4 | int getMax(int arr[], int n)
5 | {
6 | int mx = arr[0];
7 | for (int i = 1; i < n; i++)
8 | if (arr[i] > mx)
9 | mx = arr[i];
10 | return mx;
11 | }
12 | void countSort(int arr[], int n, int exp)
13 | {
14 | int output[n];
15 | int i, count[10] = { 0 };
16 | for (i = 0; i < n; i++)
17 | count[(arr[i] / exp) % 10]++;
18 | for (i = 1; i < 10; i++)
19 | count[i] += count[i - 1];
20 | for (i = n - 1; i >= 0; i--) {
21 | output[count[(arr[i] / exp) % 10] - 1] = arr[i];
22 | count[(arr[i] / exp) % 10]--;
23 | }
24 | for (i = 0; i < n; i++)
25 | arr[i] = output[i];
26 | }
27 | void radixsort(int arr[], int n)
28 | {
29 | int m = getMax(arr, n);
30 | for (int exp = 1; m / exp > 0; exp *= 10)
31 | countSort(arr, n, exp);
32 | for (int i = 0; i < n; i++)
33 | cout << arr[i] << " ";
34 | }
35 | int main()
36 | {
37 | int arr[] = { 17, 4, 5, 9, 22, 24, 122, 66 };
38 | int n = sizeof(arr) / sizeof(arr[0]);
39 | radixsort(arr, n);
40 | return 0;
41 | }
42 |
--------------------------------------------------------------------------------
/CPP/Sorting/Selection sort/Selection sort.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | void swap(int *xp, int *yp)
5 | {
6 | int temp = *xp;
7 | *xp = *yp;
8 | *yp = temp;
9 | }
10 |
11 | void selectionSort(int arr[], int n)
12 | {
13 | int i, j, min_idx;
14 |
15 | for (i = 0; i < n-1; i++)
16 | {
17 | min_idx = i;
18 | for (j = i+1; j < n; j++)
19 | if (arr[j] < arr[min_idx])
20 | min_idx = j;
21 |
22 | swap(&arr[min_idx], &arr[i]);
23 | }
24 | }
25 |
26 | void printArray(int arr[], int size)
27 | {
28 | int i;
29 | for (i=0; i < size; i++)
30 | cout << arr[i] << " ";
31 | cout << endl;
32 | }
33 |
34 | int main()
35 | {
36 | int arr[] = {64, 25, 12, 22, 11};
37 | int n = sizeof(arr)/sizeof(arr[0]);
38 | selectionSort(arr, n);
39 | cout << "Sorted array: \n";
40 | printArray(arr, n);
41 | return 0;
42 | }
43 |
44 |
--------------------------------------------------------------------------------
/CPP/Sorting/Selection sort/selection_sort.cpp:
--------------------------------------------------------------------------------
1 | // C program for implementation of selection sort
2 | #include
3 |
4 | void swap(int *xp, int *yp)
5 | {
6 | int temp = *xp;
7 | *xp = *yp;
8 | *yp = temp;
9 | }
10 |
11 | void selectionSort(int arr[], int n)
12 | {
13 | int i, j, min_idx;
14 |
15 | // One by one move boundary of unsorted subarray
16 | for (i = 0; i < n-1; i++)
17 | {
18 | // Find the minimum element in unsorted array
19 | min_idx = i;
20 | for (j = i+1; j < n; j++)
21 | if (arr[j] < arr[min_idx])
22 | min_idx = j;
23 |
24 | // Swap the found minimum element with the first element
25 | swap(&arr[min_idx], &arr[i]);
26 | }
27 | }
28 |
29 | /* Function to print an array */
30 | void printArray(int arr[], int size)
31 | {
32 | int i;
33 | for (i=0; i < size; i++)
34 | printf("%d ", arr[i]);
35 | printf("\n");
36 | }
37 |
38 | // Driver program to test above functions
39 | int main()
40 | {
41 | int arr[] = {64, 25, 12, 22, 11};
42 | int n = sizeof(arr)/sizeof(arr[0]);
43 | selectionSort(arr, n);
44 | printf("Sorted array: \n");
45 | printArray(arr, n);
46 | return 0;
47 | }
48 |
--------------------------------------------------------------------------------
/CPP/Sorting/Selection sort/selectionsort.cpp:
--------------------------------------------------------------------------------
1 | // C++ program for implementation of selection sort
2 | #include
3 | using namespace std;
4 |
5 | void swap(int *xp, int *yp)
6 | {
7 | int temp = *xp;
8 | *xp = *yp;
9 | *yp = temp;
10 | }
11 |
12 | void selectionSort(int arr[], int n)
13 | {
14 | int i, j, min_idx;
15 |
16 | // One by one move boundary of unsorted subarray
17 | for (i = 0; i < n-1; i++)
18 | {
19 | // Find the minimum element in unsorted array
20 | min_idx = i;
21 | for (j = i+1; j < n; j++)
22 | if (arr[j] < arr[min_idx])
23 | min_idx = j;
24 |
25 | // Swap the found minimum element with the first element
26 | swap(&arr[min_idx], &arr[i]);
27 | }
28 | }
29 |
30 | /* Function to print an array */
31 | void printArray(int arr[], int size)
32 | {
33 | int i;
34 | for (i=0; i < size; i++)
35 | cout << arr[i] << " ";
36 | cout << endl;
37 | }
38 |
39 | // Driver program to test above functions
40 | int main()
41 | {
42 | int arr[] = {64, 25, 12, 22, 11};
43 | int n = sizeof(arr)/sizeof(arr[0]);
44 | selectionSort(arr, n);
45 | cout << "Sorted array: \n";
46 | printArray(arr, n);
47 | return 0;
48 | }
49 |
--------------------------------------------------------------------------------
/CPP/Sorting/Sorting_Algorithms_cpp/BubbleSort.cpp:
--------------------------------------------------------------------------------
1 | // Bubble Sort
2 |
3 | // Bubble sort is a simple sorting algorithm. This sorting algorithm is
4 | // comparison-based algorithm in which each pair of adjacent elements is
5 | // compared and the elements are swapped if they are not in order.
6 |
7 | // This algorithm is not suitable for large data sets as its
8 | // average and worst case complexity are of Ο(n^2) where n is the number of items.
9 |
10 | #include
11 | using namespace std;
12 | int main()
13 | {
14 | int n, i, arr[50], j, temp;
15 | cout<<"Enter the Size (max. 50): ";
16 | cin>>n;
17 | cout<<"Enter "<>arr[i];
20 | cout<<"\nSorting the Array using Bubble Sort Technique..\n";
21 | for(i=0; i<(n-1); i++)
22 | {
23 | for(j=0; j<(n-i-1); j++)
24 | {
25 | if(arr[j]>arr[j+1])
26 | {
27 | temp = arr[j];
28 | arr[j] = arr[j+1];
29 | arr[j+1] = temp;
30 | }
31 | }
32 | }
33 | cout<<"\nArray Sorted Successfully!\n";
34 | cout<<"\nThe New Array is: \n";
35 | for(i=0; i
8 |
9 | using namespace std;
10 |
11 |
12 | void MaxHeapify(int a[], int i, int n)
13 | {
14 | int j, temp;
15 | temp = a[i];
16 | j = 2*i;
17 |
18 | while (j <= n)
19 | {
20 | if (j < n && a[j+1] > a[j])
21 | j = j+1;
22 |
23 | if (temp > a[j])
24 | break;
25 |
26 | else if (temp <= a[j])
27 | {
28 | a[j/2] = a[j];
29 | j = 2*j;
30 | }
31 | }
32 | a[j/2] = temp;
33 | return;
34 | }
35 | void HeapSort(int a[], int n)
36 | {
37 | int i, temp;
38 | for (i = n; i >= 2; i--)
39 | {
40 |
41 | temp = a[i];
42 | a[i] = a[1];
43 | a[1] = temp;
44 |
45 | MaxHeapify(a, 1, i - 1);
46 | }
47 | }
48 | void Build_MaxHeap(int a[], int n)
49 | {
50 | int i;
51 | for(i = n/2; i >= 1; i--)
52 | MaxHeapify(a, i, n);
53 | }
54 | int main()
55 | {
56 | int n, i;
57 | cout<<"\nEnter the number of data element to be sorted: ";
58 | cin>>n;
59 | n++;
60 | int arr[n];
61 | for(i = 1; i < n; i++)
62 | {
63 | cin>>arr[i];
64 | }
65 |
66 | Build_MaxHeap(arr, n-1);
67 | HeapSort(arr, n-1);
68 |
69 |
70 | cout<<"\nSorted Data ";
71 |
72 | for (i = 1; i < n; i++)
73 | cout<
10 | using namespace std;
11 | int main()
12 | {
13 | int arr[50], tot, i, j, k, elem, index;
14 | cout<<"Enter the Size for Array: ";
15 | cin>>tot;
16 | cout<<"Enter "<>arr[i];
19 | for(i=1; ij; k--)
30 | arr[k] = arr[k-1];
31 | break;
32 | }
33 | }
34 | }
35 | else
36 | continue;
37 | arr[index] = elem;
38 | }
39 | cout<<"\nThe New Array (Sorted Array):\n";
40 | for(i=0; i
12 |
13 | using namespace std;
14 |
15 |
16 | void Merge(int *a, int low, int high, int mid)
17 | {
18 |
19 | int i, j, k, temp[high-low+1];
20 | i = low;
21 | k = 0;
22 | j = mid + 1;
23 |
24 |
25 | while (i <= mid && j <= high)
26 | {
27 | if (a[i] < a[j])
28 | {
29 | temp[k] = a[i];
30 | k++;
31 | i++;
32 | }
33 | else
34 | {
35 | temp[k] = a[j];
36 | k++;
37 | j++;
38 | }
39 | }
40 |
41 |
42 | while (i <= mid)
43 | {
44 | temp[k] = a[i];
45 | k++;
46 | i++;
47 | }
48 |
49 |
50 | while (j <= high)
51 | {
52 | temp[k] = a[j];
53 | k++;
54 | j++;
55 | }
56 |
57 |
58 |
59 | for (i = low; i <= high; i++)
60 | {
61 | a[i] = temp[i-low];
62 | }
63 | }
64 |
65 |
66 | void MergeSort(int *a, int low, int high)
67 | {
68 | int mid;
69 | if (low < high)
70 | {
71 | mid=(low+high)/2;
72 |
73 | MergeSort(a, low, mid);
74 | MergeSort(a, mid+1, high);
75 |
76 |
77 | Merge(a, low, high, mid);
78 | }
79 | }
80 |
81 | int main()
82 | {
83 | int n, i;
84 | cout<<"\nEnter the number of data element to be sorted: ";
85 | cin>>n;
86 |
87 | int arr[n];
88 | for(i = 0; i < n; i++)
89 | {
90 | cin>>arr[i];
91 | }
92 |
93 | MergeSort(arr, 0, n-1);
94 |
95 |
96 | cout<<"\nSorted Data ";
97 | for (i = 0; i < n; i++)
98 | cout<
11 | using namespace std;
12 | int partition(int *a,int start,int end)
13 | {
14 | int pivot=a[end];
15 |
16 | int P_index=start;
17 | int i,t;
18 |
19 |
20 |
21 | for(i=start;i>n;
53 | int a[n];
54 | cout<<"Enter the array elements:\n";
55 | for(int i=0;i>a[i];
58 | }
59 | Quicksort(a,0,n-1);
60 | cout<<"After Quick Sort the array is:\n";
61 | for(int i=0;i
12 |
13 | using namespace std;
14 |
15 |
16 | int getMax(int arr[], int n)
17 | {
18 | int max = arr[0];
19 | for (int i = 1; i < n; i++)
20 | if (arr[i] > max)
21 | max = arr[i];
22 | return max;
23 | }
24 |
25 |
26 | void countSort(int arr[], int n, int exp)
27 | {
28 |
29 | int output[n], i, count[10] = {0};
30 |
31 |
32 | for (i = 0; i < n; i++)
33 | count[(arr[i] / exp) % 10]++;
34 |
35 |
36 | for (i = 1; i < 10; i++)
37 | count[i] += count[i-1];
38 |
39 |
40 | for (i = n - 1; i >= 0; i--)
41 | {
42 | output[count[(arr[i] / exp) % 10] - 1] = arr[i];
43 | count[(arr[i] / exp) % 10]--;
44 | }
45 |
46 |
47 | for (i = 0; i < n; i++)
48 | arr[i] = output[i];
49 | }
50 |
51 |
52 | void radixsort(int arr[], int n)
53 | {
54 | int exp, m;
55 | m = getMax(arr, n);
56 |
57 |
58 | for (exp = 1; m/exp > 0; exp *= 10)
59 | countSort(arr, n, exp);
60 | }
61 |
62 | int main()
63 | {
64 | int n, i;
65 | cout<<"\nEnter the number of data element to be sorted: ";
66 | cin>>n;
67 |
68 | int arr[n];
69 | for(i = 0; i < n; i++)
70 | {
71 | cin>>arr[i];
72 | }
73 |
74 | radixsort(arr, n);
75 |
76 |
77 | cout<<"\nSorted Data ";
78 | for (i = 0; i < n; i++)
79 | cout<
11 | using namespace std;
12 | int main()
13 | {
14 | int tot, arr[50], i, j, temp, small, chk, index;
15 | cout<<"Enter the Size of Array: ";
16 | cin>>tot;
17 | cout<<"Enter "<>arr[i];
20 | for(i=0; i<(tot-1); i++)
21 | {
22 | chk=0;
23 | small = arr[i];
24 | for(j=(i+1); jarr[j])
27 | {
28 | small = arr[j];
29 | chk++;
30 | index = j;
31 | }
32 | }
33 | if(chk!=0)
34 | {
35 | temp = arr[i];
36 | arr[i] = small;
37 | arr[index] = temp;
38 | }
39 | }
40 | cout<<"\nSorted Array is:\n";
41 | for(i=0; i
3 | using namespace std;
4 | void swap(int *xp, int *yp)
5 | {
6 | int temp = *xp;
7 | *xp = *yp;
8 | *yp = temp;
9 | }
10 |
11 | // An optimized version of Bubble Sort
12 | void bubbleSort(int arr[], int n)
13 | {
14 | int i, j;
15 | bool swapped;
16 | for (i = 0; i < n-1; i++)
17 | {
18 | swapped = false;
19 | for (j = 0; j < n-i-1; j++)
20 | {
21 | if (arr[j] > arr[j+1])
22 | {
23 | swap(&arr[j], &arr[j+1]);
24 | swapped = true;
25 | }
26 | }
27 |
28 | // IF no two elements were swapped by inner loop, then break
29 | if (swapped == false)
30 | break;
31 | }
32 | }
33 |
34 | /* Function to print an array */
35 | void printArray(int arr[], int size)
36 | {
37 | int i;
38 | for (i = 0; i < size; i++)
39 | cout <<" "<< arr[i];
40 | cout <<" n";
41 | }
42 |
43 | // Driver program to test above functions
44 | int main()
45 | {
46 | int arr[] = {64, 34, 25, 12, 22, 11, 90};
47 | int n = sizeof(arr)/sizeof(arr[0]);
48 | bubbleSort(arr, n);
49 | cout <<"Sorted array: \n";
50 | printArray(arr, n);
51 | return 0;
52 | }
53 |
54 |
--------------------------------------------------------------------------------
/CPP/Sorting/heap_sort_dsa.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | // To heapify a subtree rooted with node i which is
5 | // an index in arr[]. n is size of heap
6 | void heapify(int arr[], int n, int i)
7 | {
8 | int largest = i; // Initialize largest as root
9 | int l = 2 * i + 1; // left = 2*i + 1
10 | int r = 2 * i + 2; // right = 2*i + 2
11 |
12 | // If left child is larger than root
13 | if (l < n && arr[l] > arr[largest])
14 | largest = l;
15 |
16 | // If right child is larger than largest so far
17 | if (r < n && arr[r] > arr[largest])
18 | largest = r;
19 |
20 | // If largest is not root
21 | if (largest != i) {
22 | swap(arr[i], arr[largest]);
23 |
24 | // Recursively heapify the affected sub-tree
25 | heapify(arr, n, largest);
26 | }
27 | }
28 |
29 | // main function to do heap sort
30 | void heapSort(int arr[], int n)
31 | {
32 | // Build heap (rearrange array)
33 | for (int i = n / 2 - 1; i >= 0; i--)
34 | heapify(arr, n, i);
35 |
36 | // One by one extract an element from heap
37 | for (int i = n - 1; i >= 0; i--) {
38 | // Move current root to end
39 | swap(arr[0], arr[i]);
40 |
41 | // call max heapify on the reduced heap
42 | heapify(arr, i, 0);
43 | }
44 | }
45 |
46 | /* A utility function to print array of size n */
47 | void printArray(int arr[], int n)
48 | {
49 | for (int i = 0; i < n; ++i)
50 | cout << arr[i] << " ";
51 | cout << "\n";
52 | }
53 |
54 | // Driver program
55 | int main()
56 | {
57 | int arr[] = { 12, 11, 13, 5, 6, 7 };
58 | int n = sizeof(arr) / sizeof(arr[0]);
59 |
60 | heapSort(arr, n);
61 |
62 | cout << "Sorted array is \n";
63 | printArray(arr, n);
64 | }
65 |
--------------------------------------------------------------------------------
/CPP/Sorting/inser_sort3.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | void insertionSort(int arr[], int n)
5 | {
6 | for (int pos = 1; pos < n; pos++)
7 | {
8 | int nextPos = pos;
9 | while (nextPos > 0 && (arr[nextPos] < arr[nextPos - 1]))
10 | {
11 | int temp = arr[nextPos];
12 | arr[nextPos] = arr[nextPos - 1];
13 | arr[nextPos - 1] = temp;
14 | nextPos = nextPos - 1;
15 | }
16 | }
17 | }
18 |
19 | int main()
20 | {
21 | int n;
22 | cin >> n;
23 | int arr[n];
24 | for (int i = 0; i < n; i++)
25 | {
26 | cin >> arr[i];
27 | }
28 | insertionSort(arr, n);
29 | for (int i = 0; i < n; i++)
30 | {
31 | cout << arr[i] << " ";
32 | }
33 | return 0;
34 | }
--------------------------------------------------------------------------------
/CPP/Sorting/insertion.cpp.txt:
--------------------------------------------------------------------------------
1 |
2 | #include
3 | using namespace std;
4 |
5 | void insertionSort(int arr[], int n)
6 | {
7 | int i, key, j;
8 | for (i = 1; i < n; i++)
9 | {
10 | key = arr[i];
11 | j = i - 1;
12 |
13 | while (j >= 0 && arr[j] > key)
14 | {
15 | arr[j + 1] = arr[j];
16 | j = j - 1;
17 | }
18 | arr[j + 1] = key;
19 | }
20 | }
21 |
22 | void printArray(int arr[], int n)
23 | {
24 | int i;
25 | for (i = 0; i < n; i++)
26 | cout << arr[i] << " ";
27 | cout << endl;
28 | }
29 |
30 | /* Driver code */
31 | int main()
32 | {
33 | int arr[] = { 12, 11, 13, 5, 6 };
34 | int n = sizeof(arr) / sizeof(arr[0]);
35 |
36 | insertionSort(arr, n);
37 | printArray(arr, n);
38 |
39 | return 0;
40 | }
41 |
42 |
--------------------------------------------------------------------------------
/CPP/Sorting/insertionSort.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 | int main() {
4 | // Insertion Sort
5 | int n,j;
6 | cin>>n;
7 | int a[n];
8 | for(int i=0;i>a[i];}
9 | for(int i=0;i=0 && a[j]>temp;j--){a[j+1]=a[j];}a[j+1]=temp;}
12 | for(int i=0;i
6 | using namespace std;
7 |
8 | int main(){
9 | int n;
10 | cout<<"enter the size of array: ";
11 | cin>>n;
12 | int arr[n];
13 | cout<<"enter the values of array: ";
14 | for(int i=0; i>arr[i];
16 | }
17 | for(int i=0;icurrent && j>=0){
22 | arr[j+1]=arr[j];
23 | j--;
24 | }
25 | arr[j+1]=current;
26 | }
27 | for(int i=0;i
4 | using namespace std;
5 |
6 | // Swap Two Elements
7 | void swap(int *a, int *b) {
8 | int t = *a;
9 | *a = *b;
10 | *b = t;
11 | }
12 |
13 | // Print the Array
14 | void printArray(int array[], int size) {
15 | int i;
16 | for (i = 0; i < size; i++)
17 | cout << array[i] << " ";
18 | cout << endl;
19 | }
20 |
21 | //rearrange array
22 | int partition(int array[], int low, int high) {
23 |
24 | // rightmost element as pivot
25 | int pivot = array[high];
26 |
27 | // pointer for greater element
28 | int i = (low - 1);
29 |
30 | /*traverse each element of the array compare them with the pivot */
31 | for (int j = low; j < high; j++) {
32 | if (array[j] <= pivot) {
33 | i++; // if element smaller than pivot is found
34 | // swap it with the greater element pointed by i
35 | swap(&array[i], &array[j]);
36 | }
37 | }
38 |
39 | // swap pivot with the greater element at i
40 | swap(&array[i + 1], &array[high]);
41 |
42 | // return the partition point
43 | return (i + 1);
44 | }
45 |
46 | void quickSort(int array[], int low, int high) {
47 | if (low < high) {
48 |
49 | int pi = partition(array, low, high);
50 |
51 | // recursive call on the left of pivot
52 | quickSort(array, low, pi - 1);
53 |
54 | // recursive call on the right of pivot
55 | quickSort(array, pi + 1, high);
56 | }
57 | }
58 |
59 | // Driver code
60 | int main() {
61 | int data[] = {2,4,6,1,8,20,5};
62 | int n = sizeof(data) / sizeof(data[0]);
63 |
64 | cout << "Unsorted Array: \n";
65 | printArray(data, n);
66 |
67 | quickSort(data, 0, n - 1);
68 |
69 | cout << "Sorted array in ascending order: \n";
70 | printArray(data, n);
71 | }
72 |
73 | //Done By Atishi Jain
--------------------------------------------------------------------------------
/CPP/Sorting/quicksort_algo.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | using namespace std;
4 |
5 | void swap(int arr[], int a, int b)
6 | {
7 | int temp=arr[a];
8 | arr[a]=arr[b];
9 | arr[b]=temp;
10 | }
11 |
12 | int partition(int arr[], int l, int r)
13 | {
14 | int pi=arr[r];
15 | int i=l-1;
16 | for(int j=l;j>n;
54 | for(int i=0;i>arr[i];
57 | }
58 | int l=0;
59 | int r=n-1;
60 | quicksort(arr, l, r);
61 |
62 | display(arr, n);
63 | }
--------------------------------------------------------------------------------
/CPP/Trees/brian_kerningam_algo.cpp:
--------------------------------------------------------------------------------
1 | // BRIAN KERNINGAM ALGORITHM (COUNT SET BITS)
2 |
3 | #include
4 | using namespace std;
5 |
6 | int main()
7 | {
8 | int n;
9 | cin>>n;
10 | int res=0;
11 | while(n>0)
12 | {
13 | n = (n&(n-1));
14 | res++;
15 | }
16 | cout<
6 | using namespace std;
7 | #define int long long
8 | const int N = 1e5+2, MOD = 1e9+7;
9 |
10 | signed main()
11 | {
12 | int n;
13 | cin>>n;
14 | vector a(n);
15 | for(int i=0;i>a[i];
17 | }
18 |
19 | int len = sqrtl(n)+1;
20 | vector b(len);
21 |
22 | ////pre computation of blocks
23 | for(int i=0;i>q;
29 | while(q--){
30 | int l,r;
31 | //input in 1 based indexing
32 | cin>>l>>r;
33 | //converting to 0 based indexing
34 | l--;r--;
35 | int sum =0;
36 | for(int i=l;i<=r;){
37 | if(i%len == 0 && i+len-1 <=r){
38 | sum += b[i/len];
39 | i += len;
40 | }
41 | else{
42 | sum+=a[i];
43 | i++;
44 | }
45 | }
46 | cout<
9 | using namespace std;
10 |
11 | // A shift value of 3
12 | int key = 3;
13 |
14 | // function takes in plain text, and shifts and returns
15 | // the encrpyted text.
16 | string encrypt(string &plainText) {
17 | string enc_msg;
18 | for(auto x:plainText) {
19 | char add = ( x<='Z' && x>='A' ? 'A' : 'a' );
20 | // applying transformation to each character
21 | enc_msg += (x-add+key)%26 + add;
22 | }
23 | // return the encoded message
24 | return enc_msg;
25 | }
26 |
27 | // function to input the string
28 | void input(string &s)
29 | {
30 | while(1==1) {
31 | char x = getchar();
32 | if(x=='\n') break;
33 | s += x;
34 | }
35 | return;
36 | }
37 |
38 | int main()
39 | {
40 | string s;
41 | cout << "\nEnter plain text : "; // A string of alphabets
42 | input(s);
43 | string cipherText = encrypt(s);
44 | cout << "\nCipher Text : " << cipherText;
45 | return 0;
46 | }
47 |
--------------------------------------------------------------------------------
/CPP/calculator_using_templatefnc.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 | template < typename T>
4 | void calculator(T a,T b){
5 | cout<<"Sum of input numbers: "<(2,3);
12 |
13 | calculator (2.2,3.3);
14 |
15 | calculator (2.2222,3.333);
16 | }
17 |
18 |
--------------------------------------------------------------------------------
/CPP/string algorithms/z_function.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 | typedef long long ll;
4 |
5 | ll z[2000005];
6 |
7 | int main() {
8 | string s;
9 | cin>>s;
10 | ll n=s.size();
11 |
12 | for (ll i = 1, l = 0, r = 0; i < n; ++i) {
13 | if (i <= r)
14 | z[i] = min (r - i + 1, z[i - l]);
15 | while (i + z[i] < n && s[z[i]] == s[i + z[i]])
16 | ++z[i];
17 | if (i + z[i] - 1 > r)
18 | l = i, r = i + z[i] - 1;
19 | }
20 |
21 | for(ll i=1;i ll
10 | = new LinkedList();
11 |
12 | // Adding elements to the linked list
13 | ll.add("A");
14 | ll.add("B");
15 | ll.addLast("C");
16 | ll.addFirst("D");
17 | ll.add(2, "E");
18 |
19 | System.out.println(ll);
20 |
21 | ll.remove("B");
22 | ll.remove(3);
23 | ll.removeFirst();
24 | ll.removeLast();
25 |
26 | System.out.println(ll);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/Java/LongestSubstringWithNonRepeatingCharacters.java:
--------------------------------------------------------------------------------
1 | import java.io.*;
2 | import java.util.*;
3 | class LongestSubstring {
4 | public static void main(String args[]) throws IOException {
5 | DataInputStream ins = new DataInputStream(System.in);
6 | String s = ins.readLine();
7 | System.out.println("Length of longest substring without repeating characters:-"+lengthOfLongestSubstring(s));
8 | }
9 | public static int lengthOfLongestSubstring(String s) {
10 | int n = s.length(), ans = 0;
11 | Map map = new HashMap<>(); // current index of character
12 | // try to extend the range [i, j]
13 | for (int j = 0, i = 0; j < n; j++) {
14 | if (map.containsKey(s.charAt(j))) {
15 | i = Math.max(map.get(s.charAt(j)), i);
16 | }
17 | ans = Math.max(ans, j - i + 1);
18 | map.put(s.charAt(j), j + 1);
19 | }
20 | return ans;
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/Java/Min_Element_In_Stack.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 | import java.util.Stack;
3 |
4 | class Min_Element_In_Stack{
5 |
6 | //s1 will act as primary stack and s2 as temporary stack
7 | Stack s1 = new Stack();
8 | Stack s2 = new Stack();
9 |
10 | //Push data to the top of the stack
11 | void insertData(int data){
12 | //If both the stacks are empty add the item to both the stack
13 | if(s1.isEmpty() && s2.isEmpty()){
14 | s1.push(data);
15 | s2.push(data);
16 | }
17 | else{
18 | s1.push(data);
19 | if(s1.peek()<=s2.peek()){
20 | s2.push(data);
21 | }
22 | }
23 |
24 | }
25 |
26 | //Find the minimum from top of the stack
27 | int findMinimum(){
28 | return s2.peek();
29 | }
30 |
31 | public static void main(String args[]){
32 | Scanner inp = new Scanner(System.in);
33 |
34 | Min_Element_In_Stack mObj = new Min_Element_In_Stack();
35 | int n;
36 | System.out.print("Enter the no of Elements in the Stack: ");
37 | n = inp.nextInt();
38 |
39 | System.out.println("Enter the values of the stack:");
40 | for(int i = 0; i < n; i++){
41 | mObj.insertData(inp.nextInt());
42 | }
43 |
44 | //Find Minimun
45 | System.out.println("Minimum element in the above stack is : " + mObj.findMinimum());
46 |
47 | }
48 | }
49 |
50 | /*Test Cases-
51 | Enter the no of Elements in the Stack: 4
52 | Enter the values of the stack:
53 | 100 50 1 -100
54 | Minimum element in the above stack is : -100
55 | */
56 |
--------------------------------------------------------------------------------
/Java/Minimum bracket Reversal.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | class Solution {
3 | public static int countBracketReversals(String input) {
4 | //Your code goes here
5 | int len = input.length();
6 | if(len%2!=0)
7 | return -1;
8 | int open=0,close=0,rev=0;
9 | for(int i=0;i 1 || count == 0)
30 | {
31 | found = false;
32 | break;
33 | }
34 | }
35 | if(found)
36 | System.out.println(num + " is a fascinating number.");
37 | else
38 | System.out.println(num + " is not a fascinating number.");
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/Java/Numbers/FibonacciBigNumbers.java:
--------------------------------------------------------------------------------
1 | //This program uses "BigInteger" to get Fibonacci numbers bigger than 64bit limit (the only limit is your RAM capacity)
2 | import java.util.*;
3 | import java.math.*;
4 | public class Fibonacci_Numbeers_V2_NO_ARRAY
5 | {
6 | static Scanner reader = new Scanner(System.in);
7 |
8 | public static void main ( String[] args)
9 | {
10 | //long[] Fibonacci=new long[100];
11 | BigInteger FibonacciN_1 = BigInteger.ONE;
12 | BigInteger FibonacciN_2 = BigInteger.ONE;
13 | BigInteger FibonacciN;
14 | int count =1;
15 | for(int i=1;ib)
18 | c=a-b;
19 | else
20 | c=b-a;
21 | System.out.println("Subtraction of two numbers is : "+c);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/Java/Numbers/SumOfPrices.java:
--------------------------------------------------------------------------------
1 | import java.io.*;
2 | import java.util.*;
3 | public class test {
4 | public static void main(String args[])throws IOException{
5 | Scanner sc = new Scanner(System.in);
6 | int n = sc.nextInt();
7 | List show = new ArrayList();
8 | for(int i=0;i prices){
16 | PriorityQueue min = new PriorityQueue();
17 | if(prices.size() == 1){
18 | return prices.get(0);
19 | }
20 | else{
21 | int sum = prices.get(0);
22 | min.add(prices.get(0));
23 | for(int i=1;i arr[mid]) {
49 | end = mid - 1 ;
50 | }
51 | else {
52 | start = mid + 1;
53 | }
54 | }
55 | }
56 | return -1;
57 | }
58 | }
--------------------------------------------------------------------------------
/Java/Program for Tower of Hanoi.java:
--------------------------------------------------------------------------------
1 | // JAVA recursive function to
2 | // solve tower of hanoi puzzle
3 | import java.util.*;
4 | import java.io.*;
5 | import java.math.*;
6 | class GFG
7 | {
8 | static void towerOfHanoi(int n, char from_rod,
9 | char to_rod, char aux_rod)
10 | {
11 | if (n == 1)
12 | {
13 | System.out.println("Move disk 1 from rod "+
14 | from_rod+" to rod "+to_rod);
15 | return;
16 | }
17 | towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);
18 | System.out.println("Move disk "+ n + " from rod " +
19 | from_rod +" to rod " + to_rod );
20 | towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);
21 | }
22 |
23 | // Driver code
24 | public static void main(String args[])
25 | {
26 | int n = 4; // Number of disks
27 | towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/Java/Program to Format Time in AM-PM.java:
--------------------------------------------------------------------------------
1 | // Java program to convert 24 hour
2 | // time format to 12 hour format
3 |
4 | // Importing generic java libraries
5 | import java.util.*;
6 |
7 | class GFG {
8 |
9 | // Function converting entered values to String type
10 | static void convertTime(String time)
11 | {
12 | String format;
13 |
14 | // Parsing hours, minutes and seconds in array
15 | String[] arr = time.split(":");
16 |
17 | // Converting hours into integer
18 | int hh = Integer.parseInt(arr[0]);
19 |
20 | if (hh > 12) {
21 | hh = hh - 12;
22 | format = "PM";
23 | }
24 | else if (hh == 00) {
25 | hh = 12;
26 | format = "AM";
27 | }
28 | else if (hh == 12) {
29 | hh = 12;
30 | format = "PM";
31 | }
32 | else {
33 | format = "AM";
34 | }
35 |
36 | // Converting hh to String and
37 | // padding it with 0 on left side
38 | String hour = String.format("%02d", hh);
39 | String minute = arr[1];
40 | String second = arr[2];
41 |
42 | // Printing formatted time
43 | System.out.print("Time in 12-hour format is : ");
44 | System.out.print(hour + ":" + minute + ":" + second
45 | + " " + format);
46 | }
47 |
48 | // Main driver code
49 | public static void main(String[] args)
50 | {
51 | // Taking input from the user via Scanner class
52 | Scanner sc = new Scanner(System.in);
53 |
54 | // Asking from user to enter
55 | // time in 24 format
56 | System.out.println(
57 | "Enter the time in 24-hour format : ");
58 |
59 | /* User: Remember to enter time in below format
60 | Enter in this format - '14:02:45' */
61 | String time = sc.nextLine();
62 |
63 | // Passing time as entered above as parameter
64 | // wher efunrion isn
65 | convertTime(time);
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/Java/RotateArray.java:
--------------------------------------------------------------------------------
1 | public class RotateArray {
2 | public static void main(String[] args) {
3 | int[] arr = {1,4,3,6,7,9};
4 | int n = arr.length;
5 | printArray(arr,n);
6 | System.out.println();
7 | rightRotateByOne(arr,n);
8 | printArray(arr,n);
9 | System.out.println();
10 | leftRotateByOne(arr,n);
11 | printArray(arr,n);
12 | }
13 | private static void rightRotateByOne(int[] arr, int n) {
14 | int temp = arr[n-1];
15 | int i;
16 | for(i = n-1; i > 0; i--){
17 | arr[i] = arr[i-1];
18 | }
19 | arr[i] = temp;
20 | }
21 | private static void leftRotateByOne(int[] arr, int n) {
22 | int temp = arr[0];
23 | int i;
24 | for(i = 0; i < n-1; i++){
25 | arr[i] = arr[i+1];
26 | }
27 | arr[i] = temp;
28 | }
29 | private static void printArray(int[] arr, int n) {
30 | for(int i = 0; i < n; i++){
31 | System.out.print(arr[i] + " ");
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/Java/Sorting/InsertionSort.java:
--------------------------------------------------------------------------------
1 | class InsertionSort {
2 | /*Function to sort array using insertion sort*/
3 | void sort(int arr[])
4 | {
5 | int n = arr.length;
6 | for (int i = 1; i < n; ++i) {
7 | int key = arr[i];
8 | int j = i - 1;
9 |
10 | /* Move elements of arr[0..i-1], that are
11 | greater than key, to one position ahead
12 | of their current position */
13 | while (j >= 0 && arr[j] > key) {
14 | arr[j + 1] = arr[j];
15 | j = j - 1;
16 | }
17 | arr[j + 1] = key;
18 | }
19 | }
20 |
21 | /* A utility function to print array of size n*/
22 | static void printArray(int arr[])
23 | {
24 | int n = arr.length;
25 | for (int i = 0; i < n; ++i)
26 | System.out.print(arr[i] + " ");
27 |
28 | System.out.println();
29 | }
30 |
31 | // Driver method
32 | public static void main(String args[])
33 | {
34 | int arr[] = { 12, 11, 13, 5, 6 };
35 |
36 | InsertionSort ob = new InsertionSort();
37 | ob.sort(arr);
38 |
39 | printArray(arr);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/Java/Sorting/InsertionSort2.java:
--------------------------------------------------------------------------------
1 | class InsertionSort {
2 | /*Function to sort array using insertion sort*/
3 | void sort(int arr[])
4 | {
5 | int n = arr.length;
6 | for (int i = 1; i < n; ++i) {
7 | int key = arr[i];
8 | int j = i - 1;
9 |
10 | while (j >= 0 && arr[j] > key) {
11 | arr[j + 1] = arr[j];
12 | j = j - 1;
13 | }
14 | arr[j + 1] = key;
15 | }
16 | }
17 |
18 | public static void main(String args[])
19 | {
20 | int arr[] = { 12, 11, 13, 5, 6 };
21 |
22 | InsertionSort ob = new InsertionSort();
23 | ob.sort(arr);
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/Java/Sorting/Quick.java:
--------------------------------------------------------------------------------
1 | public class Quick
2 | {
3 | /* function that consider last element as pivot,
4 | place the pivot at its exact position, and place
5 | smaller elements to left of pivot and greater
6 | elements to right of pivot. */
7 | int partition (int a[], int start, int end)
8 | {
9 | int pivot = a[end]; // pivot element
10 | int i = (start - 1);
11 |
12 | for (int j = start; j <= end - 1; j++)
13 | {
14 | // If current element is smaller than the pivot
15 | if (a[j] < pivot)
16 | {
17 | i++; // increment index of smaller element
18 | int t = a[i];
19 | a[i] = a[j];
20 | a[j] = t;
21 | }
22 | }
23 | int t = a[i+1];
24 | a[i+1] = a[end];
25 | a[end] = t;
26 | return (i + 1);
27 | }
28 |
29 | /* function to implement quick sort */
30 | void quick(int a[], int start, int end) /* a[] = array to be sorted, start = Starting index, end = Ending index */
31 | {
32 | if (start < end)
33 | {
34 | int p = partition(a, start, end); //p is partitioning index
35 | quick(a, start, p - 1);
36 | quick(a, p + 1, end);
37 | }
38 | }
39 |
40 | /* function to print an array */
41 | void printArr(int a[], int n)
42 | {
43 | int i;
44 | for (i = 0; i < n; i++)
45 | System.out.print(a[i] + " ");
46 | }
47 | public static void main(String[] args) {
48 | int a[] = { 13, 18, 27, 2, 19, 25 };
49 | int n = a.length;
50 | System.out.println("\nBefore sorting array elements are - ");
51 | Quick q1 = new Quick();
52 | q1.printArr(a, n);
53 | q1.quick(a, 0, n - 1);
54 | System.out.println("\nAfter sorting array elements are - ");
55 | q1.printArr(a, n);
56 | System.out.println();
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/Java/Sorting/RadixSort2.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 |
3 | /**
4 | * @author Ayan1089
5 | * @since 1.0.0
6 | */
7 | class RadixSort {
8 |
9 | // Method to counting sort w.r.t to the digit in exponent
10 | static void countingSort(int array[], int n, int exponent)
11 | {
12 | int[] output = new int[n]; // output array
13 | int i;
14 | int[] count = new int[10];
15 | Arrays.fill(count, 0);
16 |
17 | for (i = 0; i < n; i++)
18 | count[(array[i] / exponent) % 10]++;
19 |
20 | for (i = 1; i < 10; i++)
21 | count[i] += count[i - 1];
22 |
23 | // Creating the output array
24 | for (i = n - 1; i >= 0; i--) {
25 | output[count[(array[i] / exponent) % 10] - 1] = array[i];
26 | count[(array[i] / exponent) % 10]--;
27 | }
28 |
29 | for (i = 0; i < n; i++)
30 | array[i] = output[i];
31 | }
32 |
33 | // Getting the maximum value of the array
34 | static int getMaxValue(int max_arr[], int n)
35 | {
36 | int max = max_arr[0];
37 | for (int i = 1; i < n; i++)
38 | if (max_arr[i] > max)
39 | max = max_arr[i];
40 | return max;
41 | }
42 |
43 | public static void main(String[] args)
44 | {
45 | int[] array = { 19, 0, 77, 219, 69, 21 , 18 };
46 | int n = array.length;
47 |
48 | // Find the maximum number to know number of digits
49 | int m = getMaxValue(array, n);
50 |
51 |
52 | // exponent is passed here for counting sort
53 | for (int exp = 1; m / exp > 0; exp *= 10)
54 | countingSort(array, n, exp);
55 |
56 | //Print the output
57 | for (int j : array) System.out.print(j + " ");
58 | }
59 | }
--------------------------------------------------------------------------------
/Java/Sorting/SelectionSort.java:
--------------------------------------------------------------------------------
1 | // we do not need util package
2 | // import java.util.*;
3 | class selectionSort
4 | {
5 | public static void main(String args[])
6 | {
7 | int arr[] = {2,3,1,5,4};
8 |
9 | for(int i = 0; i < arr.length-1; i++)
10 | {
11 | int d = i;
12 | for(int j = i+1; j < arr.length; j++)
13 | {
14 | if(arr[j] < arr[d])
15 | {
16 | d = j;
17 | }
18 | }
19 | int t = arr[i];
20 | arr[i] = arr[d];
21 | arr[d] = t;
22 | }
23 | for(int i = 0; i < arr.length; i++)
24 | {
25 | System.out.print(arr[i]+" ");
26 | }
27 | System.out.println("");
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/Java/Sorting/circle_sort.java:
--------------------------------------------------------------------------------
1 | // importing libraries for array
2 | import java.util.Arrays;
3 | // create public class
4 | public class cyclicsort {
5 | public static void main(String[] args){
6 | int[] arr = {1,3,2,5,4};
7 | sort(arr);
8 | System.out.println(Arrays.toString(arr));
9 | }
10 | static void sort(int[] arr){
11 | int i=0;
12 | while(i= 0 && arr[j] > key) {
15 | arr[j + 1] = arr[j];
16 | j = j - 1;
17 | }
18 | arr[j + 1] = key;
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/KadaneAlgorithm:
--------------------------------------------------------------------------------
1 | /*package whatever //do not write package name here */
2 | import java.util.*;
3 | import java.lang.*;
4 | import java.io.*;
5 | class KadaneAlgo {
6 | static int maxSubArraySum(int a[], int size)
7 | {
8 | int sum = a[0];
9 | int curr_max = a[0];
10 | for (int i = 1; i < size; i++)
11 | {
12 | curr_max = Math.max(a[i], curr_max+a[i]);
13 | sum = Math.max(sum, curr_max);
14 | }
15 | return sum;
16 | }
17 | public static void main (String[] args)
18 | {
19 | Scanner sc = new Scanner(System.in);
20 | int t = sc.nextInt();
21 | while(t-->0)
22 | {
23 | int n = sc.nextInt();
24 | int a[]=new int[n];
25 | for (int i = 0; i < n; i++)
26 | {
27 | a[i]=sc.nextInt();
28 | }
29 | System.out.println(maxSubArraySum(a,n));
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/MDFiles/Addition.md:
--------------------------------------------------------------------------------
1 | class Sub
2 |
3 | {
4 |
5 | public static void main(String[] arg)
6 |
7 | {
8 |
9 | int a,b,c;
10 |
11 | Scanner s=new Scanner(System.in);
12 |
13 | System.out.println("Enter first number");
14 |
15 | a=s.nextInt();
16 |
17 | System.out.println("Enter second number");
18 |
19 | b=s.nextInt();
20 |
21 | if(a>b)
22 |
23 | c=a+b;
24 |
25 | else
26 |
27 | c=b+a;
28 |
29 | System.out.println("Addition of two numbers is : "+c);
30 |
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/MDFiles/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # Contributing Guides
2 |
3 | - Fork the project
4 |
5 | - Clone to your local machine
6 |
7 | - Find an issue to solve
8 |
9 | - Or Implement any DSA
10 |
11 | - Comment your changes and commits
12 |
13 | - Push your changes
14 |
15 | - Click the Compare & pull request button
16 |
17 | - Create new Pull Request to open new PR
18 |
--------------------------------------------------------------------------------
/Product Of Array Except Self:
--------------------------------------------------------------------------------
1 | /*
2 | Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].
3 | The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
4 | You must write an algorithm that runs in O(n) time and without using the division operation.
5 | */
6 |
7 | /*
8 | Input: nums = [1,2,3,4]
9 | Output: [24,12,8,6]
10 | */
11 |
12 | /*
13 | Input: nums = [-1,1,0,-3,3]
14 | Output: [0,0,9,0,0]
15 | */
16 |
17 | #include
18 | using namespace std;
19 | int main()
20 | {
21 | int a[100],b[100],n,mult=1;
22 | cout<<"Enter the number of elements in the array: ";
23 | cin>>n;
24 | cout<<"Enter the elements of the array: ";
25 | for(int i=0;i>a[i];
28 | mult = mult * a[i];
29 | }
30 | for(int i=0;i n:
17 | return binary_search(list1, low, mid - 1, n)
18 |
19 | # Else the search moves to the right sublist1
20 | else:
21 | return binary_search(list1, mid + 1, high, n)
22 |
23 | else:
24 | # Element is not available in the list1
25 | return -1
26 |
27 | # Test list1ay
28 | list1 = [12, 24, 32, 39, 45, 50, 54]
29 | n = 32
30 |
31 | # Function call
32 | res = binary_search(list1, 0, len(list1)-1, n)
33 |
34 | if res != -1:
35 | print("Element is present at index", str(res))
36 | else:
37 | print("Element is not present in list1")
--------------------------------------------------------------------------------
/Python/Create S3_To_DynamoDB.py:
--------------------------------------------------------------------------------
1 | """
2 | This code has been designed by - Amrit Raj
3 | The code uses the boto3 to read the contents of a .txt file stored in a S3 bucket in AWS
4 | and transfer the contents to a DynamoDB table at AWS.
5 | This code is for example purpose and can be suitably modified for any .txt or other file format
6 | and for any table.
7 | For this example, we have assumed an employee table with three attributes - id, name, and salary
8 | """
9 | import boto3
10 | client = boto3.client("s3")
11 |
12 | dynamodb = boto3.resource('dynamodb')
13 | table = dynamodb.Table('employee')
14 |
15 |
16 | def lambda_handler(event, context):
17 | bucket_name = event['Records'][0]['s3']['bucket']['name']
18 | file_name = event['Records'][0]['s3']['object']['key']
19 | repo = client.get_object(Bucket = bucket_name, Key = file_name)
20 | data = repo['Body'].read().decode("utf-8")
21 | employees = data.split("\n")
22 | for emp in employees:
23 | print(emp)
24 | emp_data = emp.split(", ")
25 |
26 | table.put_item(
27 | Item = {
28 | "id" : int(emp_data[0]),
29 | "name" : emp_data[1],
30 | "salary" : emp_data[2]
31 | }
32 | )
33 |
--------------------------------------------------------------------------------
/Python/Hackerankpattern.py:
--------------------------------------------------------------------------------
1 | thickness = int(input("Enter Number: ")) #This must be an odd number
2 | c = 'H'
3 |
4 | #Top Cone
5 | for i in range(thickness):
6 | print((c*i).rjust(thickness-1)+c+(c*i).ljust(thickness-1))
7 |
8 | #Top Pillars
9 | for i in range(thickness+1):
10 | print((c*thickness).center(thickness*2)+(c*thickness).center(thickness*6))
11 |
12 | #Middle Belt
13 | for i in range((thickness+1)//2):
14 | print((c*thickness*5).center(thickness*6))
15 |
16 | #Bottom Pillars
17 | for i in range(thickness+1):
18 | print((c*thickness).center(thickness*2)+(c*thickness).center(thickness*6))
19 |
20 | #Bottom Cone
21 | for i in range(thickness):
22 | print(((c*(thickness-i-1)).rjust(thickness)+c+(c*(thickness-i-1)).ljust(thickness)).rjust(thickness*6))
--------------------------------------------------------------------------------
/Python/Job _scheduling-Shouvik-99.py:
--------------------------------------------------------------------------------
1 | # Python3 program for weighted job scheduling using
2 | # Naive Recursive Method
3 |
4 | # Importing the following module to sort array
5 | # based on our custom comparison function
6 | from functools import cmp_to_key
7 |
8 | # A job has start time, finish time and profit
9 | class Job:
10 |
11 | def __init__(self, start, finish, profit):
12 |
13 | self.start = start
14 | self.finish = finish
15 | self.profit = profit
16 |
17 | # A utility function that is used for
18 | # sorting events according to finish time
19 | def jobComparataor(s1, s2):
20 |
21 | return s1.finish < s2.finish
22 |
23 | # Find the latest job (in sorted array) that
24 | # doesn't conflict with the job[i]. If there
25 | # is no compatible job, then it returns -1
26 | def latestNonConflict(arr, i):
27 |
28 | for j in range(i - 1, -1, -1):
29 | if arr[j].finish <= arr[i - 1].start:
30 | return j
31 |
32 | return -1
33 |
34 | # A recursive function that returns the
35 | # maximum possible profit from given
36 | # array of jobs. The array of jobs must
37 | # be sorted according to finish time
38 | def findMaxProfitRec(arr, n):
39 |
40 | # Base case
41 | if n == 1:
42 | return arr[n - 1].profit
43 |
44 | # Find profit when current job is included
45 | inclProf = arr[n - 1].profit
46 | i = latestNonConflict(arr, n)
47 |
48 | if i != -1:
49 | inclProf += findMaxProfitRec(arr, i + 1)
50 |
51 | # Find profit when current job is excluded
52 | exclProf = findMaxProfitRec(arr, n - 1)
53 | return max(inclProf, exclProf)
54 |
55 | # The main function that returns the maximum
56 | # possible profit from given array of jobs
57 | def findMaxProfit(arr, n):
58 |
59 | # Sort jobs according to finish time
60 | arr = sorted(arr, key = cmp_to_key(jobComparataor))
61 | return findMaxProfitRec(arr, n)
62 |
63 | # Driver code
64 | values = [ (3, 10, 20), (1, 2, 50),
65 | (6, 19, 100), (2, 100, 200) ]
66 | arr = []
67 | for i in values:
68 | arr.append(Job(i[0], i[1], i[2]))
69 |
70 | n = len(arr)
71 |
72 | print("The optimal profit is", findMaxProfit(arr, n))
73 |
--------------------------------------------------------------------------------
/Python/Lapindromes.py:
--------------------------------------------------------------------------------
1 | n=int(input("Enter Number of test cases: "))
2 | for i in range(0,n,1):
3 | x=input("Enter String: ")
4 | firsthalf=[]
5 | secondhalf=[]
6 | f=[]
7 | s=[]
8 | l=len(x)//2
9 | if len(x)%2!=0:
10 | for i in range(0,len(x)):
11 | if il):
14 | secondhalf.append(x[i])
15 | else:
16 | for i in range(0,len(x)):
17 | if i=l):
20 | secondhalf.append(x[i])
21 | dict1=dict()
22 | dict2=dict()
23 | # Try
24 | # for i in firsthalf:
25 | # if i not in f:
26 | # f.append(i)
27 | # for i in secondhalf:
28 | # if i not in s:
29 | # s.append(i)
30 | # firsthalf=sorted(f)
31 | # secondhalf=sorted(s)
32 | # if(firsthalf==secondhalf):
33 | # print("YES")
34 | # else:
35 | # print("NO")
36 |
37 |
38 |
39 |
40 |
41 | # testCases = int(input())
42 | # while testCases:
43 | # testCases -= 1
44 | # string = input() # input is already in string format so no need to type-cast
45 | # halve = len(string) // 2
46 | # if len(string) % 2 == 0:
47 | # if sorted(string[:halve]) == sorted(string[halve:]):
48 | # print('YES')
49 | # else:
50 | # print('NO')
51 | # else:
52 | # if sorted(string[:halve]) == sorted(string[halve + 1:]):
53 | # print('YES')
54 | # else:
55 | # print('NO')
56 |
--------------------------------------------------------------------------------
/Python/Program to Solve Quadratic Equation.py:
--------------------------------------------------------------------------------
1 | # Solve the quadratic equation ax**2 + bx + c = 0
2 |
3 | # import complex math module
4 | import cmath
5 |
6 | a = 1
7 | b = 5
8 | c = 6
9 |
10 | # calculate the discriminant
11 | d = (b**2) - (4*a*c)
12 |
13 | # find two solutions
14 | sol1 = (-b-cmath.sqrt(d))/(2*a)
15 | sol2 = (-b+cmath.sqrt(d))/(2*a)
16 |
17 | print('The solution are {0} and {1}'.format(sol1,sol2))
18 |
--------------------------------------------------------------------------------
/Python/Sorting/Bubblesort.py:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | void swap(int *xp, int *yp)
5 | {
6 | int temp = *xp;
7 | *xp = *yp;
8 | *yp = temp;
9 | }
10 |
11 | // A function to implement bubble sort
12 | void bubbleSort(int arr[], int n)
13 | {
14 | int i, j;
15 | for (i = 0; i < n-1; i++)
16 |
17 | // Last i elements are already in place
18 | for (j = 0; j < n-i-1; j++)
19 | if (arr[j] > arr[j+1])
20 | swap(&arr[j], &arr[j+1]);
21 | }
22 |
23 | /* Function to print an array */
24 | void printArray(int arr[], int size)
25 | {
26 | int i;
27 | for (i = 0; i < size; i++)
28 | cout << arr[i] << " ";
29 | cout << endl;
30 | }
31 |
32 | // Driver code
33 | int main()
34 | {
35 | int arr[] = {64, 34, 25, 12, 22, 11, 90};
36 | int n = sizeof(arr)/sizeof(arr[0]);
37 | bubbleSort(arr, n);
38 | cout<<"Sorted array: \n";
39 | printArray(arr, n);
40 | return 0;
41 | }
--------------------------------------------------------------------------------
/Python/Valid Sudoku.py:
--------------------------------------------------------------------------------
1 | class Solution(object):
2 | def isValidSudoku(self, board):
3 | """
4 | :type board: List[List[str]]
5 | :rtype: bool
6 | """
7 | big = set()
8 | for i in range(9):
9 | for j in range(9):
10 | if board[i][j] != '.':
11 | cur = board[i][j]
12 | if (i, cur) in big or (cur, j) in big or (i // 3, j // 3, cur) in big:
13 | return False
14 | big.add((i, cur))
15 | big.add((cur, j))
16 | big.add((i // 3, j // 3, cur))
17 | print(big)
18 | return True
19 |
--------------------------------------------------------------------------------
/Python/calculator.py:
--------------------------------------------------------------------------------
1 | a, b = map(int, input().split())
2 | print('''Choose the operation
3 | 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Exit''')
4 | op = int(input())
5 | if op == 1:
6 | print(a+b)
7 | elif op == 2:
8 | print(a-b)
9 | elif op == 3:
10 | print(a*b)
11 | elif op == 4:
12 | try:
13 | print(a/b)
14 | except ZeroDivisionError:
15 | print("Divide by zero error!")
16 | else:
17 | exit
18 |
--------------------------------------------------------------------------------
/Python/fibonacci_golden_ratio.py:
--------------------------------------------------------------------------------
1 | import math
2 |
3 |
4 | def fib(n):
5 | phi = (1 + math.sqrt(5)) / 2
6 | return round(pow(phi, n) / math.sqrt(5))
7 |
8 |
9 | if __name__ == '__main__':
10 | n = int(input())
11 | print(fib(n))
12 |
--------------------------------------------------------------------------------
/Python/list.py:
--------------------------------------------------------------------------------
1 | list1=['a','b','c']
2 |
3 | #list methods
4 | list1.append('d')
5 | print(list1)
6 |
7 | list1.insert(1,'e')
8 | print(list1)
9 |
10 | #reverse the list
11 | list1.reverse()
12 |
--------------------------------------------------------------------------------
/Python/towerOfHanoi.py:
--------------------------------------------------------------------------------
1 | # Recursive Python function to solve the tower of hanoi
2 |
3 | def TowerOfHanoi(n , source, destination, auxiliary):
4 | if n==1:
5 | print "Move disk 1 from source",source,"to destination",destination
6 | return
7 | TowerOfHanoi(n-1, source, auxiliary, destination)
8 | print "Move disk",n,"from source",source,"to destination",destination
9 | TowerOfHanoi(n-1, auxiliary, destination, source)
10 |
11 | # Driver code
12 | n = 4
13 | TowerOfHanoi(n,'A','B','C')
14 | # A, C, B are the name of rods
15 |
--------------------------------------------------------------------------------
/Python/wifi_info.py:
--------------------------------------------------------------------------------
1 | import subprocess
2 |
3 | data = subprocess.check_output(['netsh', 'wlan', 'show', 'profiles']).decode('utf-8').split('\n')
4 | profiles = [i.split(":")[1][1:-1] for i in data if "All User Profile" in i]
5 | for i in profiles:
6 | results = subprocess.check_output(['netsh', 'wlan', 'show', 'profile', i, 'key=clear']).decode('utf-8').split('\n')
7 | results = [b.split(":")[1][1:-1] for b in results if "Key Content" in b]
8 | try:
9 | print ("{:<30}| {:<}".format(i, results[0]))
10 | except IndexError:
11 | print ("{:<30}| {:<}".format(i, ""))
12 | input("")
13 |
--------------------------------------------------------------------------------
/_design/Awesome Cheatsheets.sketch:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coder-forfun/Hactoberfest-accepted/134a579948482348af9684ad8b351ae254b6fe00/_design/Awesome Cheatsheets.sketch
--------------------------------------------------------------------------------
/_design/awesome_cheatsheets_logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coder-forfun/Hactoberfest-accepted/134a579948482348af9684ad8b351ae254b6fe00/_design/awesome_cheatsheets_logo.png
--------------------------------------------------------------------------------
/_design/awesome_cheatsheets_logo.psd:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coder-forfun/Hactoberfest-accepted/134a579948482348af9684ad8b351ae254b6fe00/_design/awesome_cheatsheets_logo.psd
--------------------------------------------------------------------------------
/_design/awesome_cheatsheets_logo@2x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coder-forfun/Hactoberfest-accepted/134a579948482348af9684ad8b351ae254b6fe00/_design/awesome_cheatsheets_logo@2x.png
--------------------------------------------------------------------------------
/_design/awesome_cheatsheets_logo@4x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coder-forfun/Hactoberfest-accepted/134a579948482348af9684ad8b351ae254b6fe00/_design/awesome_cheatsheets_logo@4x.png
--------------------------------------------------------------------------------
/_design/cover_github@2x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coder-forfun/Hactoberfest-accepted/134a579948482348af9684ad8b351ae254b6fe00/_design/cover_github@2x.png
--------------------------------------------------------------------------------
/c++/Count set bits in an integer Solution.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | int Countbits(int n)
5 | {
6 | int c = 0;
7 |
8 | while (n > 0)
9 | {
10 | c = c + (n & 1);
11 | n = n >> 1;
12 | }
13 |
14 | return c;
15 | }
16 |
17 | int CountbitsFast(int n)
18 | {
19 | int c = 0;
20 |
21 | while (n > 0)
22 | {
23 | n = n & (n - 1);
24 | c++;
25 | }
26 |
27 | return c;
28 | }
29 |
30 | int main()
31 | {
32 | int n;
33 | cout << "Enter the number: ";
34 | cin >> n;
35 |
36 | // Method-1 (Inbuilt function in STL)
37 | cout << __builtin_popcount(n) << endl;
38 |
39 | // Method-2
40 | cout << Countbits(n) << endl;
41 |
42 | // Method- 3 (Brian Kernighan’s Algorithm)
43 | cout << CountbitsFast(n) << endl;
44 | return 0;
45 | }
46 |
--------------------------------------------------------------------------------
/c++/Duplicate String.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 | void printDups(string str)
4 | {
5 | map count;
6 | for (int i = 0; i < str.length(); i++)
7 | count[str[i]]++;
8 |
9 | for (auto it : count)
10 | if (it.second > 1)
11 | cout << it.first << ", count = " << it.second<
2 | using namespace std;
3 | void swap(int &a,int &b)
4 | {
5 | int temp = a;
6 | a = b;
7 | b = temp;
8 | }
9 | int main()
10 | {
11 | int n;
12 | cin>>n;
13 | int a[n];
14 | for(int i=0;i>a[i];
16 | int low = 0, mid = 0, high = n-1; // initializing the pointers
17 | while(mid <= high)
18 | {
19 | switch(a[mid])
20 | {
21 | case 0: swap(a[low],a[mid]);
22 | low++,mid++;
23 | break; // it is required to prevent jumping onto next case
24 | case 1: mid++;
25 | break;
26 | case 2: swap(a[high],a[mid]);
27 | high--;
28 | break;
29 | }
30 | }
31 | // printing the contents of the array
32 | for(int i=0;i
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include