add snippets
This commit is contained in:
parent
dfa4d03042
commit
70e5b4c1cc
89
init.lua
89
init.lua
|
|
@ -98,6 +98,8 @@ vim.opt.foldlevel = 5
|
||||||
vim.opt.foldmethod = "expr"
|
vim.opt.foldmethod = "expr"
|
||||||
vim.opt.foldexpr = "nvim_treesitter#foldexpr()"
|
vim.opt.foldexpr = "nvim_treesitter#foldexpr()"
|
||||||
|
|
||||||
|
vim.opt.diffopt="internal,filler,closeoff,iwhite,linematch:60"
|
||||||
|
|
||||||
-- [[ Basic Keymaps ]]
|
-- [[ Basic Keymaps ]]
|
||||||
-- See `:help vim.keymap.set()`
|
-- See `:help vim.keymap.set()`
|
||||||
|
|
||||||
|
|
@ -165,6 +167,22 @@ vim.api.nvim_create_autocmd('TextYankPost', {
|
||||||
callback = function() vim.hl.on_yank() end,
|
callback = function() vim.hl.on_yank() end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
vim.api.nvim_create_autocmd('BufReadPost', {
|
||||||
|
pattern = { '*' },
|
||||||
|
desc = 'When editing a file, always jump to the last known cursor position',
|
||||||
|
callback = function()
|
||||||
|
local line = vim.fn.line '\'"'
|
||||||
|
if
|
||||||
|
line >= 1
|
||||||
|
and line <= vim.fn.line '$'
|
||||||
|
and vim.bo.filetype ~= 'commit'
|
||||||
|
and vim.fn.index({ 'xxd', 'gitrebase' }, vim.bo.filetype) == -1
|
||||||
|
then
|
||||||
|
vim.cmd 'normal! g`"'
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
|
||||||
--vim.api.nvim_set_hl(0, '@lsp.type.comment.cpp', {})
|
--vim.api.nvim_set_hl(0, '@lsp.type.comment.cpp', {})
|
||||||
|
|
||||||
-- [[ Install `lazy.nvim` plugin manager ]]
|
-- [[ Install `lazy.nvim` plugin manager ]]
|
||||||
|
|
@ -584,8 +602,21 @@ require('lazy').setup({
|
||||||
cmd = {'/home/utils/llvm-17.0.6/bin/clangd',
|
cmd = {'/home/utils/llvm-17.0.6/bin/clangd',
|
||||||
"--background-index",
|
"--background-index",
|
||||||
"--clang-tidy",
|
"--clang-tidy",
|
||||||
"--header-insertion=iwyu",
|
"--header-insertion=never",
|
||||||
"--j=2"},
|
"--j=2"},
|
||||||
|
init_options = {
|
||||||
|
clangdFileStatus = true,
|
||||||
|
inlayHints= {
|
||||||
|
enabled = true,
|
||||||
|
parameterNames = true,
|
||||||
|
autoDeducedTypes = true, -- Enable auto type hints
|
||||||
|
typeHints = true,
|
||||||
|
typeNameLimit = 24,
|
||||||
|
lambdaReturnType = true,
|
||||||
|
blockEnd = true,
|
||||||
|
designators = true,
|
||||||
|
},
|
||||||
|
},
|
||||||
filetypes = {"cpp"},
|
filetypes = {"cpp"},
|
||||||
single_file_support = true,
|
single_file_support = true,
|
||||||
capabilities = capabilities,
|
capabilities = capabilities,
|
||||||
|
|
@ -711,6 +742,7 @@ require('lazy').setup({
|
||||||
'L3MON4D3/LuaSnip',
|
'L3MON4D3/LuaSnip',
|
||||||
version = '2.*',
|
version = '2.*',
|
||||||
build = (function()
|
build = (function()
|
||||||
|
CC={"/home/utils/gcc-14.1.0/bin/gcc"}
|
||||||
-- Build Step is needed for regex support in snippets.
|
-- Build Step is needed for regex support in snippets.
|
||||||
-- This step is not supported in many windows environments.
|
-- This step is not supported in many windows environments.
|
||||||
-- Remove the below condition to re-enable on windows.
|
-- Remove the below condition to re-enable on windows.
|
||||||
|
|
@ -725,8 +757,7 @@ require('lazy').setup({
|
||||||
'rafamadriz/friendly-snippets',
|
'rafamadriz/friendly-snippets',
|
||||||
config = function()
|
config = function()
|
||||||
require('luasnip.loaders.from_vscode').lazy_load({ include={"cpp", "python", "lua"} })
|
require('luasnip.loaders.from_vscode').lazy_load({ include={"cpp", "python", "lua"} })
|
||||||
local snip_loader = require('luasnip.loaders.from_vscode')
|
require('luasnip.loaders.from_vscode').lazy_load({ paths = vim.fn.stdpath("config").."/snippets/" })
|
||||||
snip_loader.lazy_load({ paths = vim.fn.stdpath("config").."/snippets/" })
|
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
@ -801,8 +832,8 @@ require('lazy').setup({
|
||||||
-- change the command in the config to whatever the name of that colorscheme is.
|
-- change the command in the config to whatever the name of that colorscheme is.
|
||||||
--
|
--
|
||||||
-- If you want to see what colorschemes are already installed, you can use `:Telescope colorscheme`.
|
-- If you want to see what colorschemes are already installed, you can use `:Telescope colorscheme`.
|
||||||
--'folke/tokyonight.nvim',
|
'folke/tokyonight.nvim',
|
||||||
'catppuccin/nvim',
|
--'catppuccin/nvim',
|
||||||
priority = 1000, -- Make sure to load this before all the other start plugins.
|
priority = 1000, -- Make sure to load this before all the other start plugins.
|
||||||
config = function()
|
config = function()
|
||||||
---@diagnostic disable-next-line: missing-fields
|
---@diagnostic disable-next-line: missing-fields
|
||||||
|
|
@ -819,6 +850,7 @@ require('lazy').setup({
|
||||||
|
|
||||||
-- You can configure highlights by doing something like:
|
-- You can configure highlights by doing something like:
|
||||||
vim.cmd.hi 'Comment gui=none'
|
vim.cmd.hi 'Comment gui=none'
|
||||||
|
vim.api.nvim_set_hl(0, "LspInlayHint", {fg = "#9DA9A0", bg="#000000" })
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -903,45 +935,6 @@ require('lazy').setup({
|
||||||
})
|
})
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
{ 'ngemily/vim-vp4',
|
|
||||||
lazy = true,
|
|
||||||
event = "BufModifiedSet",
|
|
||||||
cmd = {"Vp4Diff", "Vp4Annotate"},
|
|
||||||
},
|
|
||||||
{ 'AndrewRadev/linediff.vim',
|
|
||||||
lazy = true,
|
|
||||||
cmd = "Linediff",
|
|
||||||
},
|
|
||||||
{ 'junegunn/vim-easy-align',
|
|
||||||
lazy = true,
|
|
||||||
cmd="EasyAlign"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
'pteroctopus/faster.nvim',
|
|
||||||
opts = {
|
|
||||||
behaviors = {
|
|
||||||
bigfile = {
|
|
||||||
features_disabled = {
|
|
||||||
"illuminate", "matchparen", "lsp", "treesitter", "indent_blankline",
|
|
||||||
"vimopts", "syntax", "filetype", "linediff", "vim-vp4", "vim-easy-align", "telescope"
|
|
||||||
},
|
|
||||||
filesize = 3,
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
--{
|
|
||||||
-- "m4xshen/hardtime.nvim",
|
|
||||||
-- dependencies = { "MunifTanjim/nui.nvim", "nvim-lua/plenary.nvim" },
|
|
||||||
-- opts = {
|
|
||||||
-- max_count = 10,
|
|
||||||
-- restriction_mode = "hint",
|
|
||||||
-- }
|
|
||||||
--},
|
|
||||||
{
|
|
||||||
'ckipp01/nvim-jenkinsfile-linter',
|
|
||||||
requires = { "nvim-lua/plenary.nvim" }
|
|
||||||
},
|
|
||||||
|
|
||||||
-- The following comments only work if you have downloaded the kickstart repo, not just copy pasted the
|
-- The following comments only work if you have downloaded the kickstart repo, not just copy pasted the
|
||||||
-- init.lua. If you want these files, they are in the repository, so you can just download them and
|
-- init.lua. If you want these files, they are in the repository, so you can just download them and
|
||||||
|
|
@ -963,12 +956,8 @@ require('lazy').setup({
|
||||||
-- This is the easiest way to modularize your config.
|
-- This is the easiest way to modularize your config.
|
||||||
--
|
--
|
||||||
-- Uncomment the following line and add your plugins to `lua/custom/plugins/*.lua` to get going.
|
-- Uncomment the following line and add your plugins to `lua/custom/plugins/*.lua` to get going.
|
||||||
-- { import = 'custom.plugins' },
|
-- For additional information, see `:help lazy.nvim-lazy.nvim-structuring-your-plugins`
|
||||||
--
|
{ import = 'custom.plugins' },
|
||||||
-- For additional information with loading, sourcing and examples see `:help lazy.nvim-🔌-plugin-spec`
|
|
||||||
-- Or use telescope!
|
|
||||||
-- In normal mode type `<space>sh` then write `lazy.nvim-plugin`
|
|
||||||
-- you can continue same window with `<space>sr` which resumes last telescope search
|
|
||||||
}, { ---@diagnostic disable-line: missing-fields
|
}, { ---@diagnostic disable-line: missing-fields
|
||||||
ui = {
|
ui = {
|
||||||
-- If you are using a Nerd Font: set icons to an empty table which will use the
|
-- If you are using a Nerd Font: set icons to an empty table which will use the
|
||||||
|
|
|
||||||
|
|
@ -5,4 +5,50 @@
|
||||||
|
|
||||||
---@module 'lazy'
|
---@module 'lazy'
|
||||||
---@type LazySpec
|
---@type LazySpec
|
||||||
return {}
|
return {
|
||||||
|
{ 'ngemily/vim-vp4',
|
||||||
|
lazy = true,
|
||||||
|
event = "BufModifiedSet",
|
||||||
|
cmd = {"Vp4Diff", "Vp4Annotate"},
|
||||||
|
},
|
||||||
|
{ 'AndrewRadev/linediff.vim',
|
||||||
|
lazy = true,
|
||||||
|
cmd = "Linediff",
|
||||||
|
},
|
||||||
|
{ 'junegunn/vim-easy-align',
|
||||||
|
lazy = true,
|
||||||
|
cmd="EasyAlign"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'pteroctopus/faster.nvim',
|
||||||
|
opts = {
|
||||||
|
behaviors = {
|
||||||
|
bigfile = {
|
||||||
|
features_disabled = {
|
||||||
|
"illuminate", "matchparen", "lsp", "treesitter", "indent_blankline",
|
||||||
|
"vimopts", "syntax", "filetype", "linediff", "vim-vp4", "vim-easy-align", "telescope"
|
||||||
|
},
|
||||||
|
filesize = 3,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
--{
|
||||||
|
-- "m4xshen/hardtime.nvim",
|
||||||
|
-- dependencies = { "MunifTanjim/nui.nvim", "nvim-lua/plenary.nvim" },
|
||||||
|
-- opts = {
|
||||||
|
-- max_count = 10,
|
||||||
|
-- restriction_mode = "hint",
|
||||||
|
-- }
|
||||||
|
--},
|
||||||
|
{
|
||||||
|
"gbprod/substitute.nvim",
|
||||||
|
config = function()
|
||||||
|
vim.keymap.set("n", "s", require('substitute').operator, { noremap = true })
|
||||||
|
vim.keymap.set("n", "ss", require('substitute').line, { noremap = true })
|
||||||
|
vim.keymap.set("n", "S", require('substitute').eol, { noremap = true })
|
||||||
|
vim.keymap.set("x", "s", require('substitute').visual, { noremap = true })
|
||||||
|
end,
|
||||||
|
opts = {},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
|
||||||
50
package.json
50
package.json
|
|
@ -3,10 +3,52 @@
|
||||||
"contributes": {
|
"contributes": {
|
||||||
"snippets": [
|
"snippets": [
|
||||||
{
|
{
|
||||||
"language": [
|
"language": "cpp",
|
||||||
"cpp"
|
"path": "./snippets/cpp.json"
|
||||||
],
|
},
|
||||||
"path": "snippets/cpp.json"
|
{
|
||||||
|
"language": "cpp",
|
||||||
|
"path": "./snippets/fmod.json"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"language": "cpp",
|
||||||
|
"path": "./snippets/starters.json"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"language": "cpp",
|
||||||
|
"path": "./snippets/basicAlgo.json"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"language": "cpp",
|
||||||
|
"path": "./snippets/sorting.json"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"language": "cpp",
|
||||||
|
"path": "./snippets/searching.json"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"language": "cpp",
|
||||||
|
"path": "./snippets/input.json"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"language": "cpp",
|
||||||
|
"path": "./snippets/commonLang.json"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"language": "cpp",
|
||||||
|
"path": "./snippets/tree.json"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"language": "cpp",
|
||||||
|
"path": "./snippets/bst.json"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"language": "cpp",
|
||||||
|
"path": "./snippets/graph.json"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"language": "cpp",
|
||||||
|
"path": "./snippets/strings.json"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,282 @@
|
||||||
|
{
|
||||||
|
"Greatest Common Divisor": {
|
||||||
|
"prefix": "gcd",
|
||||||
|
"body": [
|
||||||
|
"int gcd(int a, int b)",
|
||||||
|
"{",
|
||||||
|
" if (b == 0)",
|
||||||
|
" return a;",
|
||||||
|
" return gcd(b, a % b);",
|
||||||
|
" ",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "GCD using Euclidean Algorithm"
|
||||||
|
},
|
||||||
|
"Maximum Function with long long int": {
|
||||||
|
"prefix": "maxll",
|
||||||
|
"body": [
|
||||||
|
"long long int max(long long int a, long long int b)",
|
||||||
|
"{",
|
||||||
|
" if (a >= b)",
|
||||||
|
" {",
|
||||||
|
" return a;",
|
||||||
|
" }",
|
||||||
|
" else",
|
||||||
|
" {",
|
||||||
|
" return b;",
|
||||||
|
" }",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Maximum Function with long long int"
|
||||||
|
},
|
||||||
|
"Minimum Function with long long int": {
|
||||||
|
"prefix": "minll",
|
||||||
|
"body": [
|
||||||
|
"long long int min(long long int a, long long int b)",
|
||||||
|
"{",
|
||||||
|
" if (a >= b)",
|
||||||
|
" {",
|
||||||
|
" return b;",
|
||||||
|
" }",
|
||||||
|
" else",
|
||||||
|
" {",
|
||||||
|
" return a;",
|
||||||
|
" }",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Minimum Function with long long int"
|
||||||
|
},
|
||||||
|
"Check number is power of two": {
|
||||||
|
"prefix": "isPowerOfTwo",
|
||||||
|
"body": [
|
||||||
|
"bool isPowerOfTwo(int n) {",
|
||||||
|
" return n && (!(n & (n - 1)));",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Check number is power of two"
|
||||||
|
},
|
||||||
|
"Check number is prime": {
|
||||||
|
"prefix": "isPrime",
|
||||||
|
"body": [
|
||||||
|
"bool isPrime(int n)",
|
||||||
|
"{",
|
||||||
|
" for (int i = 2; i * i <= n; i++) {",
|
||||||
|
" if (n % i == 0)return false;",
|
||||||
|
" }",
|
||||||
|
" return true;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Check number is prime"
|
||||||
|
},
|
||||||
|
"Return all prime factors": {
|
||||||
|
"prefix": "primeFactors",
|
||||||
|
"body": [
|
||||||
|
"vector<int> primeFactors(int n)",
|
||||||
|
"{",
|
||||||
|
" vector<int> v;",
|
||||||
|
" if (n % 2 == 0) {",
|
||||||
|
" v.push_back(2);",
|
||||||
|
" while (n % 2 == 0)n /= 2;",
|
||||||
|
" }",
|
||||||
|
" for (int i = 3; i * i <= n; i += 2) {",
|
||||||
|
" if (n % i == 0) {",
|
||||||
|
" v.push_back(i);",
|
||||||
|
" while (n % i == 0)n /= i;",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" if (n > 2)v.push_back(n);",
|
||||||
|
" return v;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "return all prime factors"
|
||||||
|
},
|
||||||
|
"Sum of all divisors": {
|
||||||
|
"prefix": "divisorFunction",
|
||||||
|
"body": [
|
||||||
|
"//return sum all divisors",
|
||||||
|
"int divisorFunction(int n)",
|
||||||
|
"{",
|
||||||
|
" int ans = 1;",
|
||||||
|
" int power = 0;",
|
||||||
|
" while (n % 2 == 0)power++, n /= 2;",
|
||||||
|
" //sum of GP",
|
||||||
|
" ans *= (pow(2, power + 1) - 1);",
|
||||||
|
" for (int i = 3; i * i <= n; i += 2) {",
|
||||||
|
" power = 0;",
|
||||||
|
" while (n % i == 0) {",
|
||||||
|
" power++;",
|
||||||
|
" n /= i;",
|
||||||
|
" }",
|
||||||
|
" ans *= (pow(i, power + 1) - 1) / (i - 1);",
|
||||||
|
" }",
|
||||||
|
" return ans;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "sum of all divisors"
|
||||||
|
},
|
||||||
|
"Return n!": {
|
||||||
|
"prefix": "nFactorial",
|
||||||
|
"body": [
|
||||||
|
"//when n! <1e18 and you need raw",
|
||||||
|
"long long int nFactorial(int n)",
|
||||||
|
"{",
|
||||||
|
" long long int ans = 1;",
|
||||||
|
" for (int i = 2; i <= n; i++)ans *= i;",
|
||||||
|
" return ans;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "return n!"
|
||||||
|
},
|
||||||
|
"Return n!%mod": {
|
||||||
|
"prefix": "nFactorialMOD",
|
||||||
|
"body": [
|
||||||
|
"// n!%mod ,for ex mod=1e9+7",
|
||||||
|
"int nFactorialMOD(int n, int mod)",
|
||||||
|
"{",
|
||||||
|
" int ans = 1;",
|
||||||
|
" for (int i = 2; i <= n; i++)(ans *= i) %= mod;",
|
||||||
|
" return ans;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "return n!%mod"
|
||||||
|
},
|
||||||
|
"Next power of two": {
|
||||||
|
"prefix": "nextPowerOfTwo",
|
||||||
|
"body": [
|
||||||
|
"int nextPowerOfTwo(int n)",
|
||||||
|
"{",
|
||||||
|
" //if n is power of two",
|
||||||
|
" if (n && (!n & (n - 1)))return n;",
|
||||||
|
" return 1 << ((int)ceil(log2(n)));",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "next power of two"
|
||||||
|
},
|
||||||
|
"Prev power of two": {
|
||||||
|
"prefix": "prevPowerOfTwo",
|
||||||
|
"body": [
|
||||||
|
"int prevPowerOfTwo(int n)",
|
||||||
|
"{",
|
||||||
|
" //if n is power of two",
|
||||||
|
" if (n && (!n & (n - 1)))return n;",
|
||||||
|
" return 1 << ((int)ceil(log2(n) - 1));",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "prev power of two"
|
||||||
|
},
|
||||||
|
"x^y": {
|
||||||
|
"prefix": "xpowery",
|
||||||
|
"body": [
|
||||||
|
"long long int xpowery(long long int x,long long int y) {",
|
||||||
|
" long long int res = 1;",
|
||||||
|
" /*",
|
||||||
|
" it depends sometimes on questions too,",
|
||||||
|
" whether pow(0,0) is 1 or 0",
|
||||||
|
" So change accordingly",
|
||||||
|
" */",
|
||||||
|
" if (x == 0)return 0LL;",
|
||||||
|
" while (y)",
|
||||||
|
" {",
|
||||||
|
" if (y & 1)res *= x;",
|
||||||
|
" y >>= 1;",
|
||||||
|
" x *= x;",
|
||||||
|
" }",
|
||||||
|
" return res;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "x^y"
|
||||||
|
},
|
||||||
|
"(x^y)%mod": {
|
||||||
|
"prefix": "xpoweryMOD",
|
||||||
|
"body": [
|
||||||
|
"long long int xpoweryMOD(long long int x,long long int y, int mod)",
|
||||||
|
"{",
|
||||||
|
" long long int res = 1;",
|
||||||
|
" /*",
|
||||||
|
" it depends sometimes on questions too,",
|
||||||
|
" whether pow(0,0) is 1 or 0",
|
||||||
|
" So change accordingly",
|
||||||
|
" */",
|
||||||
|
" if (x == 0)return 0;",
|
||||||
|
" while (y)",
|
||||||
|
" {",
|
||||||
|
" if (y & 1)(res *= x) %= mod;",
|
||||||
|
" y >>= 1;",
|
||||||
|
" (x *= x) %= mod;",
|
||||||
|
" }",
|
||||||
|
" return res;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "(x^y)%mod"
|
||||||
|
},
|
||||||
|
"Sieve of eratosthenes": {
|
||||||
|
"prefix": "sieve",
|
||||||
|
"body": [
|
||||||
|
"//return all primes b/w 1 to n",
|
||||||
|
"vector<int> sieve(int n)",
|
||||||
|
"{",
|
||||||
|
" vector<bool> is(n + 1, true);",
|
||||||
|
" vector<int> primes;",
|
||||||
|
" for (int i = 2; i * i <= n; i++) {",
|
||||||
|
" if (is[i]) {",
|
||||||
|
" for (int p = i * i; p <= n; p += i)is[p] = false;",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" for (int i = 2; i <= n; i++)",
|
||||||
|
" if (is[i])primes.push_back(i);",
|
||||||
|
" return primes;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "sieve of eratosthenes"
|
||||||
|
},
|
||||||
|
"LCM between two number": {
|
||||||
|
"prefix": "lcm",
|
||||||
|
"body": [
|
||||||
|
"long long int gcd(long long int a, long long int b)",
|
||||||
|
"{",
|
||||||
|
" if (b == 0)",
|
||||||
|
" return a;",
|
||||||
|
" return gcd(b, a % b);",
|
||||||
|
"}",
|
||||||
|
"long long int lcm(int a, int b)",
|
||||||
|
"{",
|
||||||
|
" return (a / gcd(a, b)) * b;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Least Common Multiple"
|
||||||
|
},
|
||||||
|
"Combinatorics": {
|
||||||
|
"prefix": "combinatorics",
|
||||||
|
"body": [
|
||||||
|
"int SIZE = 2000005;",
|
||||||
|
"vector<int> fact(SIZE, 1);",
|
||||||
|
"vector<int> inv(SIZE, 1);",
|
||||||
|
"int inverse(int x, int y)",
|
||||||
|
"{",
|
||||||
|
" int res = 1;",
|
||||||
|
" while (y) {",
|
||||||
|
" if (y & 1)res = (1LL * res * x) % MOD;",
|
||||||
|
" y >>= 1;",
|
||||||
|
" x = (1LL * x * x) % MOD;",
|
||||||
|
" }",
|
||||||
|
" return res;",
|
||||||
|
"}",
|
||||||
|
"void initCombinatorics()",
|
||||||
|
"{",
|
||||||
|
" for (int i = 2; i < SIZE; i++) {",
|
||||||
|
" fact[i] = (1LL * fact[i - 1] * i) % MOD;",
|
||||||
|
" inv[i] = inverse(fact[i], MOD - 2);",
|
||||||
|
" }",
|
||||||
|
"}",
|
||||||
|
"int nCr(int n, int r)",
|
||||||
|
"{",
|
||||||
|
" int res = 1;",
|
||||||
|
" if (r > n)return 0;",
|
||||||
|
" res = (1LL * fact[n] * inv[r]) % MOD;",
|
||||||
|
" res = (1LL * res * inv[n - r]) % MOD;",
|
||||||
|
" return res;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Combinatorics"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,253 @@
|
||||||
|
{
|
||||||
|
"Insert in BST": {
|
||||||
|
"prefix": "insBst",
|
||||||
|
"body": [
|
||||||
|
"Node *insertBST(Node *root, int val)",
|
||||||
|
"{",
|
||||||
|
" if (root == NULL)",
|
||||||
|
" {",
|
||||||
|
" return new Node(val);",
|
||||||
|
" }",
|
||||||
|
" if (val < root->data)",
|
||||||
|
" {",
|
||||||
|
" root->left = insertBST(root->left, val);",
|
||||||
|
" }",
|
||||||
|
" else",
|
||||||
|
" {",
|
||||||
|
" root->right = insertBST(root->right, val);",
|
||||||
|
" }",
|
||||||
|
" return root;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Insert in BST"
|
||||||
|
},
|
||||||
|
"Delete in BST": {
|
||||||
|
"prefix": "delBst",
|
||||||
|
"body": [
|
||||||
|
"Node *inorderSucc(Node *root)",
|
||||||
|
"{",
|
||||||
|
" Node *curr = root;",
|
||||||
|
" while (curr && curr->left != NULL)",
|
||||||
|
" {",
|
||||||
|
" curr = curr->left;",
|
||||||
|
" }",
|
||||||
|
" return curr;",
|
||||||
|
"}",
|
||||||
|
"Node *deleteInBST(Node *root, int key)",
|
||||||
|
"{",
|
||||||
|
" if (key < root->data)",
|
||||||
|
" {",
|
||||||
|
" root->left = deleteInBST(root->left, key);",
|
||||||
|
" }",
|
||||||
|
" else if (key > root->data)",
|
||||||
|
" {",
|
||||||
|
" root->right = deleteInBST(root->right, key);",
|
||||||
|
" }",
|
||||||
|
" else",
|
||||||
|
" {",
|
||||||
|
" if (root->left == NULL)",
|
||||||
|
" {",
|
||||||
|
" Node *temp = root->right;",
|
||||||
|
" free(root);",
|
||||||
|
" return temp;",
|
||||||
|
" }",
|
||||||
|
" else if (root->right == NULL)",
|
||||||
|
" {",
|
||||||
|
" Node *temp = root->left;",
|
||||||
|
" free(root);",
|
||||||
|
" return temp;",
|
||||||
|
" }",
|
||||||
|
" Node *temp = inorderSucc(root->right);",
|
||||||
|
" root->data = temp->data;",
|
||||||
|
" root->right = deleteInBST(root->right, temp->data);",
|
||||||
|
" }",
|
||||||
|
" return root;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Delete in BST"
|
||||||
|
},
|
||||||
|
"Search in BST": {
|
||||||
|
"prefix": "searchBst",
|
||||||
|
"body": [
|
||||||
|
"Node *searchInBST(Node *root, int key)",
|
||||||
|
"{",
|
||||||
|
" if (root == NULL)",
|
||||||
|
" {",
|
||||||
|
" return NULL;",
|
||||||
|
" }",
|
||||||
|
" if (root->data == key)",
|
||||||
|
" {",
|
||||||
|
" return root;",
|
||||||
|
" }",
|
||||||
|
" if (root->data > key)",
|
||||||
|
" {",
|
||||||
|
" return searchInBST(root->left, key);",
|
||||||
|
" }",
|
||||||
|
" return searchInBST(root->right, key);",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Search in BST"
|
||||||
|
},
|
||||||
|
"Construct BST": {
|
||||||
|
"prefix": "constructBst",
|
||||||
|
"body": [
|
||||||
|
"Node *constructBST(int preorder[], int *preorderIdx, int key, int min, int max, int n)",
|
||||||
|
"{",
|
||||||
|
" if (*preorderIdx >= n)",
|
||||||
|
" {",
|
||||||
|
" return NULL;",
|
||||||
|
" }",
|
||||||
|
" Node *root = NULL;",
|
||||||
|
" if (key > min && key < max)",
|
||||||
|
" {",
|
||||||
|
" root = new Node(key);",
|
||||||
|
" *preorderIdx = *preorderIdx + 1;",
|
||||||
|
" if (*preorderIdx < n)",
|
||||||
|
" {",
|
||||||
|
" root->left = constructBST(preorder, preorderIdx, preorder[*preorderIdx], min, key, n);",
|
||||||
|
" }",
|
||||||
|
" if (*preorderIdx < n)",
|
||||||
|
" {",
|
||||||
|
" root->right = constructBST(preorder, preorderIdx, preorder[*preorderIdx], key, max, n);",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" return root;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Construct BST"
|
||||||
|
},
|
||||||
|
"Check the given tree is BST or not": {
|
||||||
|
"prefix": "isBst",
|
||||||
|
"body": [
|
||||||
|
"bool isBST(Node *root, Node *min = NULL, Node *max = NULL)",
|
||||||
|
"{",
|
||||||
|
" if (root == NULL)",
|
||||||
|
" {",
|
||||||
|
" return true;",
|
||||||
|
" }",
|
||||||
|
" if (min != NULL && root->data <= min->data)",
|
||||||
|
" {",
|
||||||
|
" return false;",
|
||||||
|
" }",
|
||||||
|
" if (max != NULL && root->data >= max->data)",
|
||||||
|
" {",
|
||||||
|
" return false;",
|
||||||
|
" }",
|
||||||
|
" bool leftValid = isBST(root->left, min, root);",
|
||||||
|
" bool rightValid = isBST(root->right, root, max);",
|
||||||
|
"",
|
||||||
|
" return leftValid and rightValid;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Check the given tree is BST or not"
|
||||||
|
},
|
||||||
|
"Sorted array to Bst": {
|
||||||
|
"prefix": "sortArrToBst",
|
||||||
|
"body": [
|
||||||
|
"Node *sortedArrToBST(int a[], int start, int end)",
|
||||||
|
"{",
|
||||||
|
" if (start > end)",
|
||||||
|
" {",
|
||||||
|
" return NULL;",
|
||||||
|
" }",
|
||||||
|
"",
|
||||||
|
" int mid = (start + end) / 2;",
|
||||||
|
" Node *root = new Node(a[mid]);",
|
||||||
|
"",
|
||||||
|
" root->left = sortedArrToBST(a, start, mid - 1);",
|
||||||
|
" root->right = sortedArrToBST(a, mid + 1, end);",
|
||||||
|
"",
|
||||||
|
" return root;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Sorted array to Bst"
|
||||||
|
},
|
||||||
|
"Is Bst identical or nor": {
|
||||||
|
"prefix": "isIdentical",
|
||||||
|
"body": [
|
||||||
|
"bool isIdentical(Node *root1, Node *root2)",
|
||||||
|
"{",
|
||||||
|
" if (root1 == NULL && root2 == NULL)",
|
||||||
|
" {",
|
||||||
|
" return true;",
|
||||||
|
" }",
|
||||||
|
" else if (root1 == NULL || root2 == NULL)",
|
||||||
|
" {",
|
||||||
|
" return false;",
|
||||||
|
" }",
|
||||||
|
" else",
|
||||||
|
" {",
|
||||||
|
" bool c1 = root1->data == root2->data;",
|
||||||
|
" bool c2 = isIdentical(root1->left, root2->left);",
|
||||||
|
" bool c3 = isIdentical(root1->right, root2->right);",
|
||||||
|
"",
|
||||||
|
" if (c1 && c2 && c3)",
|
||||||
|
" {",
|
||||||
|
" return true;",
|
||||||
|
" }",
|
||||||
|
" else",
|
||||||
|
" {",
|
||||||
|
" return false;",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Is Bst identical or nor"
|
||||||
|
},
|
||||||
|
"zigzagTraversal Bst": {
|
||||||
|
"prefix": "zigzagTraversal",
|
||||||
|
"body": [
|
||||||
|
"void zigzagTraversal(Node *root)",
|
||||||
|
"{",
|
||||||
|
" if (root == NULL)",
|
||||||
|
" {",
|
||||||
|
" return;",
|
||||||
|
" }",
|
||||||
|
" stack<Node *> currLevel;",
|
||||||
|
" stack<Node *> nextLevel;",
|
||||||
|
"",
|
||||||
|
" bool leftToRight = true;",
|
||||||
|
"",
|
||||||
|
" currLevel.push(root);",
|
||||||
|
" while (!currLevel.empty())",
|
||||||
|
" {",
|
||||||
|
" Node *temp = currLevel.top();",
|
||||||
|
" currLevel.pop();",
|
||||||
|
"",
|
||||||
|
" if (temp)",
|
||||||
|
" {",
|
||||||
|
" cout << temp->data << \" \";",
|
||||||
|
" if (leftToRight)",
|
||||||
|
" {",
|
||||||
|
" if (temp->left)",
|
||||||
|
" {",
|
||||||
|
" nextLevel.push(temp->left);",
|
||||||
|
" }",
|
||||||
|
" if (temp->right)",
|
||||||
|
" {",
|
||||||
|
" nextLevel.push(temp->right);",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" else",
|
||||||
|
" {",
|
||||||
|
" if (temp->right)",
|
||||||
|
" {",
|
||||||
|
" nextLevel.push(temp->right);",
|
||||||
|
" }",
|
||||||
|
" if (temp->left)",
|
||||||
|
" {",
|
||||||
|
" nextLevel.push(temp->left);",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" if (currLevel.empty())",
|
||||||
|
" {",
|
||||||
|
" leftToRight = !leftToRight;",
|
||||||
|
" swap(currLevel, nextLevel);",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "zigzagTraversal Bst"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,42 @@
|
||||||
|
{
|
||||||
|
"For loop with n": {
|
||||||
|
"prefix": "forn",
|
||||||
|
"body": ["for(int i=0;i<n;i++)", "{", "${1:cursor}", "}"],
|
||||||
|
"description": "For loop with n"
|
||||||
|
},
|
||||||
|
"While loop with n": {
|
||||||
|
"prefix": "whilen",
|
||||||
|
"body": ["int i=0;", " while()", " {", " i++;", " }"],
|
||||||
|
"description": "While loop with n"
|
||||||
|
},
|
||||||
|
"Print element of array of n size": {
|
||||||
|
"prefix": "couta",
|
||||||
|
"body": [
|
||||||
|
"for(int i=0;i<n;i++)",
|
||||||
|
" {",
|
||||||
|
" cout<<a[i]<<\" \";",
|
||||||
|
" }cout<<endl;"
|
||||||
|
],
|
||||||
|
"description": "Print element of array of n size"
|
||||||
|
},
|
||||||
|
"Print element of vector": {
|
||||||
|
"prefix": "coutv",
|
||||||
|
"body": [
|
||||||
|
"for(int i=0;i<v.size();i++)",
|
||||||
|
" {",
|
||||||
|
" cout<<v[i]<<\" \";",
|
||||||
|
" }cout<<endl;"
|
||||||
|
],
|
||||||
|
"description": "Print element of vector"
|
||||||
|
},
|
||||||
|
"Print element of vector with auto": {
|
||||||
|
"prefix": "autov",
|
||||||
|
"body": [
|
||||||
|
"for(auto it:v)",
|
||||||
|
" {",
|
||||||
|
" cout<<it<<\" \";",
|
||||||
|
" }cout<<endl;"
|
||||||
|
],
|
||||||
|
"description": "Print element of vector with auto"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -4,8 +4,29 @@
|
||||||
"body": [
|
"body": [
|
||||||
"class Iterator {",
|
"class Iterator {",
|
||||||
"}",
|
"}",
|
||||||
""
|
" "
|
||||||
],
|
],
|
||||||
"description": "iterator class"
|
"description": "iterator class"
|
||||||
|
},
|
||||||
|
"cout using ostream_iterator": {
|
||||||
|
"prefix": "vector_cout",
|
||||||
|
"body": [
|
||||||
|
"std::ranges::copy(vec, std::ostream_iterator<int>(std::cout, \" \"));",
|
||||||
|
" "
|
||||||
|
],
|
||||||
|
"description": "cout << vec"
|
||||||
|
},
|
||||||
|
"Greatest Common Divisor": {
|
||||||
|
"prefix": "gcd",
|
||||||
|
"body": [
|
||||||
|
"int gcd(int a, int b)",
|
||||||
|
"{",
|
||||||
|
" if (b == 0)",
|
||||||
|
" return a;",
|
||||||
|
" return gcd(b, a % b);",
|
||||||
|
" ",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "GCD using Euclidean Algorithm"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
{
|
||||||
|
"cslDebugGroup": {
|
||||||
|
"prefix": "csld",
|
||||||
|
"body": [
|
||||||
|
"cslDebugGroup((\"MPC\", 50, \"MPC%d:%d: \\n\", GPCid, TPCid));"
|
||||||
|
],
|
||||||
|
"description": "iterator class"
|
||||||
|
},
|
||||||
|
"cslAssertMsg": {
|
||||||
|
"prefix": "csla",
|
||||||
|
"body": [
|
||||||
|
"cslAssertMsg( (), (\"\\n\", sm_mask));"
|
||||||
|
],
|
||||||
|
"description": "iterator class"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,520 @@
|
||||||
|
{
|
||||||
|
"Adjacency Matrix in Graph": {
|
||||||
|
"prefix": "adjMatrix",
|
||||||
|
"body": [
|
||||||
|
"void adjMatrix()",
|
||||||
|
"{",
|
||||||
|
" int n, m;",
|
||||||
|
" cin >> n >> m;",
|
||||||
|
" int adj[n + 1][n + 1];",
|
||||||
|
" for (int i = 0; i <= n; i++)",
|
||||||
|
" {",
|
||||||
|
" for (int j = 0; j <= n; j++)",
|
||||||
|
" {",
|
||||||
|
" adj[i][j] = 0;",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" for (int i = 0; i < m; i++)",
|
||||||
|
" {",
|
||||||
|
" int u, v;",
|
||||||
|
" cin >> u >> v;",
|
||||||
|
" adj[u][v] = 1;",
|
||||||
|
" adj[v][u] = 1;",
|
||||||
|
" }",
|
||||||
|
" for (int i = 0; i <= n; i++)",
|
||||||
|
" {",
|
||||||
|
" for (int j = 0; j <= n; j++)",
|
||||||
|
" {",
|
||||||
|
" cout << adj[i][j] << \" \";",
|
||||||
|
" }",
|
||||||
|
" cout << endl;",
|
||||||
|
" }",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Adjacency Matrix in Graph"
|
||||||
|
},
|
||||||
|
"Adjacency List in Graph": {
|
||||||
|
"prefix": "adjList",
|
||||||
|
"body": [
|
||||||
|
"void adjList()",
|
||||||
|
"{",
|
||||||
|
" int n, m;",
|
||||||
|
" cin >> n >> m;",
|
||||||
|
" vector<vector<int>> adj(n);",
|
||||||
|
" for (int i = 0; i < m; i++)",
|
||||||
|
" {",
|
||||||
|
" int u, v;",
|
||||||
|
" cin >> u >> v;",
|
||||||
|
" adj[u].pb(v);",
|
||||||
|
" adj[v].pb(u);",
|
||||||
|
" }",
|
||||||
|
" for (int i = 0; i < adj.size(); i++)",
|
||||||
|
" {",
|
||||||
|
" cout << i << \"->\";",
|
||||||
|
" for (auto x : adj[i])",
|
||||||
|
" {",
|
||||||
|
" cout << x << \" \";",
|
||||||
|
" }",
|
||||||
|
" cout << endl;",
|
||||||
|
" }",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Adjacency List in Graph"
|
||||||
|
},
|
||||||
|
"BFS in Graph": {
|
||||||
|
"prefix": "bfs",
|
||||||
|
"body": [
|
||||||
|
"void BFS()",
|
||||||
|
"{",
|
||||||
|
" int n, m;",
|
||||||
|
" cin >> n >> m;",
|
||||||
|
" vector<bool> vis(n + 1, false);",
|
||||||
|
" vector<vector<int>> adj(n + 1);",
|
||||||
|
" for (int i = 0; i < m; i++)",
|
||||||
|
" {",
|
||||||
|
" int u, v;",
|
||||||
|
" cin >> u >> v;",
|
||||||
|
" adj[u].pb(v);",
|
||||||
|
" adj[v].pb(u);",
|
||||||
|
" }",
|
||||||
|
" queue<int> q;",
|
||||||
|
" for (int i = 0; i < n; i++)",
|
||||||
|
" {",
|
||||||
|
" if (!vis[i])",
|
||||||
|
" {",
|
||||||
|
"",
|
||||||
|
" q.push(i);",
|
||||||
|
" vis[i] = true;",
|
||||||
|
" while (!q.empty())",
|
||||||
|
" {",
|
||||||
|
" int node = q.front();",
|
||||||
|
"",
|
||||||
|
" q.pop();",
|
||||||
|
" cout << node << \" \";",
|
||||||
|
" for (auto x : adj[node])",
|
||||||
|
" {",
|
||||||
|
" if (!vis[x])",
|
||||||
|
" {",
|
||||||
|
" vis[x] = true;",
|
||||||
|
" q.push(x);",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "BFS in Graph"
|
||||||
|
},
|
||||||
|
"DFS in Graph": {
|
||||||
|
"prefix": "dfs",
|
||||||
|
"body": [
|
||||||
|
"void dfs(int node, vector<vector<int>> adj, vector<bool> &vis)",
|
||||||
|
"{",
|
||||||
|
" vis[node] = true;",
|
||||||
|
" cout << node << \" \";",
|
||||||
|
" for (auto x : adj[node])",
|
||||||
|
" {",
|
||||||
|
" if (vis[x])",
|
||||||
|
" {",
|
||||||
|
" ;",
|
||||||
|
" }",
|
||||||
|
" else",
|
||||||
|
" {",
|
||||||
|
" dfs(x, adj, vis);",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
"}",
|
||||||
|
"void DFS()",
|
||||||
|
"{",
|
||||||
|
" int n, m;",
|
||||||
|
" cin >> n >> m;",
|
||||||
|
" vector<bool> vis(n + 1, false);",
|
||||||
|
" vector<vector<int>> adj(n + 1);",
|
||||||
|
" for (int i = 0; i < m; i++)",
|
||||||
|
" {",
|
||||||
|
" int u, v;",
|
||||||
|
" cin >> u >> v;",
|
||||||
|
" adj[u].pb(v);",
|
||||||
|
" adj[v].pb(u);",
|
||||||
|
" }",
|
||||||
|
"",
|
||||||
|
" for (int i = 0; i < n; i++)",
|
||||||
|
" {",
|
||||||
|
" if (!vis[i])",
|
||||||
|
" {",
|
||||||
|
"",
|
||||||
|
" dfs(i, adj, vis);",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "DFS in Graph"
|
||||||
|
},
|
||||||
|
"Topological BFS in Graph": {
|
||||||
|
"prefix": "topoBfs",
|
||||||
|
"body": [
|
||||||
|
"void TopoBFS()",
|
||||||
|
"{",
|
||||||
|
" int n, m;",
|
||||||
|
" cin >> n >> m;",
|
||||||
|
" vector<vector<int>> adj(n);",
|
||||||
|
" vector<int> indeg(n, 0);",
|
||||||
|
" for (int i = 0; i < m; i++)",
|
||||||
|
" {",
|
||||||
|
" int u, v;",
|
||||||
|
" cin >> u >> v;",
|
||||||
|
" adj[u].pb(v);",
|
||||||
|
" indeg[v]++;",
|
||||||
|
" }",
|
||||||
|
" queue<int> q;",
|
||||||
|
" for (int i = 0; i < indeg.size(); i++)",
|
||||||
|
" {",
|
||||||
|
" if (indeg[i] == 0)",
|
||||||
|
" {",
|
||||||
|
" q.push(i);",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
"",
|
||||||
|
" while (!q.empty())",
|
||||||
|
" {",
|
||||||
|
" int node = q.front();",
|
||||||
|
"",
|
||||||
|
" q.pop();",
|
||||||
|
" cout << node << \" \";",
|
||||||
|
" for (auto x : adj[node])",
|
||||||
|
" {",
|
||||||
|
" indeg[x]--;",
|
||||||
|
" if (indeg[x] == 0)",
|
||||||
|
" {",
|
||||||
|
" q.push(x);",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Topological BFS in Graph"
|
||||||
|
},
|
||||||
|
"Topological DFS in Graph": {
|
||||||
|
"prefix": "topoDfs",
|
||||||
|
"body": [
|
||||||
|
"void Topodfs(int node, vector<vector<int>> adj, vector<bool> &vis, stack<int> &st)",
|
||||||
|
"{",
|
||||||
|
" vis[node] = true;",
|
||||||
|
" for (auto x : adj[node])",
|
||||||
|
" {",
|
||||||
|
" if (vis[x])",
|
||||||
|
" {",
|
||||||
|
" ;",
|
||||||
|
" }",
|
||||||
|
" else",
|
||||||
|
" {",
|
||||||
|
" Topodfs(x, adj, vis, st);",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" st.push(node);",
|
||||||
|
"}",
|
||||||
|
"void TopoDFS()",
|
||||||
|
"{",
|
||||||
|
" int n, m;",
|
||||||
|
" cin >> n >> m;",
|
||||||
|
" vector<bool> vis(n + 1, false);",
|
||||||
|
" vector<vector<int>> adj(n + 1);",
|
||||||
|
" for (int i = 0; i < m; i++)",
|
||||||
|
" {",
|
||||||
|
" int u, v;",
|
||||||
|
" cin >> u >> v;",
|
||||||
|
" adj[u].pb(v);",
|
||||||
|
" }",
|
||||||
|
" stack<int> st;",
|
||||||
|
" for (int i = 0; i < n; i++)",
|
||||||
|
" {",
|
||||||
|
" if (!vis[i])",
|
||||||
|
" {",
|
||||||
|
"",
|
||||||
|
" Topodfs(i, adj, vis, st);",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" while (!st.empty())",
|
||||||
|
" {",
|
||||||
|
" int ans = st.top();",
|
||||||
|
" cout << ans << \" \";",
|
||||||
|
" st.pop();",
|
||||||
|
" }",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Topological DFS in Graph"
|
||||||
|
},
|
||||||
|
"Is Cycle is Present in Graph using DFS": {
|
||||||
|
"prefix": "isCycleDfs",
|
||||||
|
"body": [
|
||||||
|
"bool isCycle(int s, vector<vector<int>> &adj, vector<bool> &vis, int parent)",
|
||||||
|
"{",
|
||||||
|
" vis[s] = true;",
|
||||||
|
" for (auto x : adj[s])",
|
||||||
|
" {",
|
||||||
|
" if (x != parent)",
|
||||||
|
" {",
|
||||||
|
" if (vis[x])",
|
||||||
|
" {",
|
||||||
|
" return true;",
|
||||||
|
" }",
|
||||||
|
" if (!vis[x] && isCycle(x, adj, vis, s))",
|
||||||
|
" {",
|
||||||
|
" return true;",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" return false;",
|
||||||
|
"}",
|
||||||
|
"void CycleDFS()",
|
||||||
|
"{",
|
||||||
|
" int n, m;",
|
||||||
|
" cin >> n >> m;",
|
||||||
|
" vector<bool> vis(n, false);",
|
||||||
|
" vector<vector<int>> adj(n);",
|
||||||
|
" for (int i = 0; i < m; i++)",
|
||||||
|
" {",
|
||||||
|
" int u, v;",
|
||||||
|
" cin >> u >> v;",
|
||||||
|
" adj[u].pb(v);",
|
||||||
|
" }",
|
||||||
|
"",
|
||||||
|
" for (int i = 0; i < n; i++)",
|
||||||
|
" {",
|
||||||
|
" if (!vis[i] && isCycle(i, adj, vis, -1))",
|
||||||
|
" {",
|
||||||
|
"",
|
||||||
|
" cout << true << endl;",
|
||||||
|
" return;",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" cout << false << endl;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Is Cycle is Present in Graph using DFS"
|
||||||
|
},
|
||||||
|
"Is Cycle is Present in Graph using BFS": {
|
||||||
|
"prefix": "isCycleBfs",
|
||||||
|
"body": [
|
||||||
|
"void CycleBFS()",
|
||||||
|
"{",
|
||||||
|
" int n, m;",
|
||||||
|
" cin >> n >> m;",
|
||||||
|
" vector<vector<int>> adj(n);",
|
||||||
|
" vector<int> indeg(n, 0);",
|
||||||
|
" for (int i = 0; i < m; i++)",
|
||||||
|
" {",
|
||||||
|
" int u, v;",
|
||||||
|
" cin >> u >> v;",
|
||||||
|
" adj[u].pb(v);",
|
||||||
|
" indeg[v]++;",
|
||||||
|
" }",
|
||||||
|
" queue<int> q;",
|
||||||
|
" for (int i = 0; i < indeg.size(); i++)",
|
||||||
|
" {",
|
||||||
|
" if (indeg[i] == 0)",
|
||||||
|
" {",
|
||||||
|
" q.push(i);",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" int count = 0;",
|
||||||
|
" while (!q.empty())",
|
||||||
|
" {",
|
||||||
|
" int node = q.front();",
|
||||||
|
"",
|
||||||
|
" q.pop();",
|
||||||
|
" for (auto x : adj[node])",
|
||||||
|
" {",
|
||||||
|
" indeg[x]--;",
|
||||||
|
" if (indeg[x] == 0)",
|
||||||
|
" {",
|
||||||
|
" q.push(x);",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" if (count != n)",
|
||||||
|
" {",
|
||||||
|
" cout << true << endl;",
|
||||||
|
" }",
|
||||||
|
" else",
|
||||||
|
" {",
|
||||||
|
" cout << false << endl;",
|
||||||
|
" }",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Is Cycle is Present in Graph using BFS"
|
||||||
|
},
|
||||||
|
"Shortest Path Direct Acyclic Graph": {
|
||||||
|
"prefix": "shortesPathDAG",
|
||||||
|
"body": [
|
||||||
|
"void TopodfsDAG(int node, vector<pair<int, int>> adj[], vector<bool> &vis, stack<int> &st)",
|
||||||
|
"{",
|
||||||
|
" vis[node] = true;",
|
||||||
|
" for (auto x : adj[node])",
|
||||||
|
" {",
|
||||||
|
" if (!vis[x.first])",
|
||||||
|
" {",
|
||||||
|
" TopodfsDAG(x.first, adj, vis, st);",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" st.push(node);",
|
||||||
|
"}",
|
||||||
|
"void shortesPathDAG()",
|
||||||
|
"{",
|
||||||
|
" int n, m;",
|
||||||
|
" cin >> n >> m;",
|
||||||
|
" vector<bool> vis(n + 1, false);",
|
||||||
|
" vector<pair<int, int>> adj[n];",
|
||||||
|
" for (int i = 0; i < m; i++)",
|
||||||
|
" {",
|
||||||
|
" int u, v, w;",
|
||||||
|
" cin >> u >> v >> w;",
|
||||||
|
" adj[u].pb({v, w});",
|
||||||
|
" }",
|
||||||
|
" int src;",
|
||||||
|
" cin >> src;",
|
||||||
|
" stack<int> st;",
|
||||||
|
" for (int i = 0; i < n; i++)",
|
||||||
|
" {",
|
||||||
|
" if (!vis[i])",
|
||||||
|
" {",
|
||||||
|
"",
|
||||||
|
" TopodfsDAG(i, adj, vis, st);",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" int dist[n];",
|
||||||
|
" for (int i = 0; i < n; i++)",
|
||||||
|
" {",
|
||||||
|
" dist[i] = INT_MAX;",
|
||||||
|
" }",
|
||||||
|
" dist[src] = 0;",
|
||||||
|
" while (!st.empty())",
|
||||||
|
" {",
|
||||||
|
" int node = st.top();",
|
||||||
|
" st.pop();",
|
||||||
|
" if (dist[node] != INT_MAX)",
|
||||||
|
" {",
|
||||||
|
" for (auto x : adj[node])",
|
||||||
|
" {",
|
||||||
|
" if (dist[x.first] > dist[node] + x.second)",
|
||||||
|
" {",
|
||||||
|
" dist[x.first] = dist[node] + x.second;",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" for (int i = 0; i < n; i++)",
|
||||||
|
" (dist[i] == INT_MAX) ? cout << \"INFINITY \" : cout << dist[i] << \" \";",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Shortest Path Direct Acyclic Graph"
|
||||||
|
},
|
||||||
|
"Dijsktra Algorithm": {
|
||||||
|
"prefix": "dijsktra",
|
||||||
|
"body": [
|
||||||
|
"void dijsktra()",
|
||||||
|
"{",
|
||||||
|
" int n, m, source;",
|
||||||
|
" cin >> n >> m;",
|
||||||
|
" vector<pair<int, int>> g[n + 1];",
|
||||||
|
"",
|
||||||
|
" int a, b, wt;",
|
||||||
|
" for (int i = 0; i < m; i++)",
|
||||||
|
" {",
|
||||||
|
" cin >> a >> b >> wt;",
|
||||||
|
" g[a].push_back(make_pair(b, wt));",
|
||||||
|
" g[b].push_back(make_pair(a, wt));",
|
||||||
|
" }",
|
||||||
|
" cin >> source;",
|
||||||
|
"",
|
||||||
|
" priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;",
|
||||||
|
" vector<int> disTo(n + 1, INT_MAX);",
|
||||||
|
"",
|
||||||
|
" disTo[source] = 0;",
|
||||||
|
"",
|
||||||
|
" pq.push(make_pair(0, source));",
|
||||||
|
"",
|
||||||
|
" while (!pq.empty())",
|
||||||
|
" {",
|
||||||
|
" int dist = pq.top().first;",
|
||||||
|
" int prev = pq.top().second;",
|
||||||
|
" pq.pop();",
|
||||||
|
"",
|
||||||
|
" vector<pair<int, int>>::iterator it;",
|
||||||
|
"",
|
||||||
|
" for (it = g[prev].begin(); it != g[prev].end(); it++)",
|
||||||
|
" {",
|
||||||
|
" int next = it->first;",
|
||||||
|
" int nextDist = it->second;",
|
||||||
|
"",
|
||||||
|
" if (disTo[next] > disTo[prev] + nextDist)",
|
||||||
|
" {",
|
||||||
|
" disTo[next] = disTo[prev] + nextDist;",
|
||||||
|
" pq.push(make_pair(disTo[next], next));",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" cout << \"The distances from source, \" << source << \", are : \" << endl;",
|
||||||
|
" for (int i = 1; i <= n; i++)",
|
||||||
|
" {",
|
||||||
|
" cout << disTo[i] << \" \";",
|
||||||
|
" }",
|
||||||
|
" cout << endl;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Dijsktra Algorithm"
|
||||||
|
},
|
||||||
|
"BellmanFord Algorithm": {
|
||||||
|
"prefix": "bellmanFord",
|
||||||
|
"body": [
|
||||||
|
"void bellmanFord()",
|
||||||
|
"{",
|
||||||
|
" int N, m;",
|
||||||
|
" cin >> N >> m;",
|
||||||
|
" vector<node> edges;",
|
||||||
|
" for (int i = 0; i < m; i++)",
|
||||||
|
" {",
|
||||||
|
" int u, v, wt;",
|
||||||
|
" cin >> u >> v >> wt;",
|
||||||
|
" edges.push_back(node(u, v, wt));",
|
||||||
|
" }",
|
||||||
|
" int src;",
|
||||||
|
" cin >> src;",
|
||||||
|
"",
|
||||||
|
" int inf = 1e9;",
|
||||||
|
" vector<int> dist(N, inf);",
|
||||||
|
" dist[src] = 0;",
|
||||||
|
"",
|
||||||
|
" for (int i = 0; i <= N - 1; i++)",
|
||||||
|
" {",
|
||||||
|
" for (auto it : edges)",
|
||||||
|
" {",
|
||||||
|
" if (dist[it.u] + it.wt < dist[it.v])",
|
||||||
|
" {",
|
||||||
|
" dist[it.v] = dist[it.u] + it.wt;",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" int fl = 0;",
|
||||||
|
" for (auto it : edges)",
|
||||||
|
" {",
|
||||||
|
" if (dist[it.u] + it.wt < dist[it.v])",
|
||||||
|
" {",
|
||||||
|
" cout << \"NegativeCycle\" << endl;",
|
||||||
|
" fl = 1;",
|
||||||
|
" break;",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" if (!fl)",
|
||||||
|
" {",
|
||||||
|
" for (int i = 0; i < N; i++)",
|
||||||
|
" {",
|
||||||
|
" cout << i << \" \" << dist[i] << endl;",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "BellmanFord Algorithm"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,50 @@
|
||||||
|
{
|
||||||
|
"Taking input in n size int array": {
|
||||||
|
"prefix": "inai",
|
||||||
|
"body": [
|
||||||
|
"int n;",
|
||||||
|
" cin>>n;",
|
||||||
|
" int a[n];",
|
||||||
|
" for(int i=0;i<n;i++){",
|
||||||
|
" cin>>a[i];",
|
||||||
|
" }"
|
||||||
|
],
|
||||||
|
"description": "Taking input in n size int array"
|
||||||
|
},
|
||||||
|
"Taking input in n size long long int array": {
|
||||||
|
"prefix": "inal",
|
||||||
|
"body": [
|
||||||
|
"long long int n;",
|
||||||
|
" cin>>n;",
|
||||||
|
" long long int a[n];",
|
||||||
|
" for(long long int i=0;i<n;i++){",
|
||||||
|
" cin>>a[i];",
|
||||||
|
" }"
|
||||||
|
],
|
||||||
|
"description": "Taking input in n size long long int array"
|
||||||
|
},
|
||||||
|
"Taking input in n size int vector": {
|
||||||
|
"prefix": "invi",
|
||||||
|
"body": [
|
||||||
|
"int n;",
|
||||||
|
" cin>>n;",
|
||||||
|
" vector<int> v(n)",
|
||||||
|
" for(int i=0;i<n;i++){",
|
||||||
|
" cin>>v[i];",
|
||||||
|
" }"
|
||||||
|
],
|
||||||
|
"description": "Taking input in n size int array"
|
||||||
|
},
|
||||||
|
"Taking input in n size long long int vector": {
|
||||||
|
"prefix": "invl",
|
||||||
|
"body": [
|
||||||
|
"long long int n;",
|
||||||
|
" cin>>n;",
|
||||||
|
" vector<long long int> v(n);",
|
||||||
|
" for(long long int i=0;i<n;i++){",
|
||||||
|
" cin>>v[i];",
|
||||||
|
" }"
|
||||||
|
],
|
||||||
|
"description": "Taking input in n size long long int vector"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,45 @@
|
||||||
|
{
|
||||||
|
"Google KickStart C++ Template": {
|
||||||
|
"prefix": "kickstart",
|
||||||
|
"body": [
|
||||||
|
"#include <bits/stdc++.h>",
|
||||||
|
"using namespace std;",
|
||||||
|
"#define int long long int",
|
||||||
|
"#define ll long long",
|
||||||
|
"#define endl \"\\n\"",
|
||||||
|
"#define tc \\",
|
||||||
|
" int t; \\",
|
||||||
|
" cin >> t; \\",
|
||||||
|
" while (t--)",
|
||||||
|
"#define pb push_back",
|
||||||
|
"#define mp make_pair",
|
||||||
|
"#define pii pair<ll, ll>",
|
||||||
|
"",
|
||||||
|
"void fastio()",
|
||||||
|
"{",
|
||||||
|
" ios_base::sync_with_stdio(0);",
|
||||||
|
" cin.tie(0);",
|
||||||
|
" cout.tie(0);",
|
||||||
|
"}",
|
||||||
|
"void solve()",
|
||||||
|
"{",
|
||||||
|
"}",
|
||||||
|
"signed main()",
|
||||||
|
"{",
|
||||||
|
"",
|
||||||
|
" fastio();",
|
||||||
|
" int t;",
|
||||||
|
" cin >> t;",
|
||||||
|
" for (int case_i = 1; case_i <= t; case_i++)",
|
||||||
|
" {",
|
||||||
|
"",
|
||||||
|
" cout << \"Case #\" << case_i << \": \";",
|
||||||
|
" solve();",
|
||||||
|
" }",
|
||||||
|
"",
|
||||||
|
" return 0;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Google KickStart C++ Template"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,42 @@
|
||||||
|
{
|
||||||
|
"Binary Search ": {
|
||||||
|
"prefix": "bsearch",
|
||||||
|
"body": [
|
||||||
|
"int binarySearch(int a[], int l, int r, int x)",
|
||||||
|
"{",
|
||||||
|
" if (r >= l)",
|
||||||
|
" {",
|
||||||
|
" int mid = l + (r - l) / 2;",
|
||||||
|
"",
|
||||||
|
" if (a[mid] == x)",
|
||||||
|
" {",
|
||||||
|
" return mid;",
|
||||||
|
" }",
|
||||||
|
"",
|
||||||
|
" if (a[mid] > x)",
|
||||||
|
" {",
|
||||||
|
" return binarySearch(a, l, mid - 1, x);",
|
||||||
|
" }",
|
||||||
|
" return binarySearch(a, mid + 1, r, x);",
|
||||||
|
" }",
|
||||||
|
" return -1;",
|
||||||
|
"}",
|
||||||
|
"int main()",
|
||||||
|
"{",
|
||||||
|
" int n;",
|
||||||
|
" cin >> n;",
|
||||||
|
" int a[n];",
|
||||||
|
" for (int i = 0; i < n; i++)",
|
||||||
|
" {",
|
||||||
|
" cin >> a[i];",
|
||||||
|
" }",
|
||||||
|
" int x;",
|
||||||
|
" cin >> x;",
|
||||||
|
" cout << binarySearch(a, 0, n - 1, x);",
|
||||||
|
"",
|
||||||
|
" return 0;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Binary Search"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,310 @@
|
||||||
|
{
|
||||||
|
"Insertion Sort ": {
|
||||||
|
"prefix": "insort",
|
||||||
|
"body": [
|
||||||
|
"void swap(int *xp, int *yp)",
|
||||||
|
"{",
|
||||||
|
" int temp = *xp;",
|
||||||
|
" *xp = *yp;",
|
||||||
|
" *yp = temp;",
|
||||||
|
"}",
|
||||||
|
"",
|
||||||
|
"void insertionSort(int a[], int n)",
|
||||||
|
"{",
|
||||||
|
" int key;",
|
||||||
|
" for (int i = 1; i < n; i++)",
|
||||||
|
" {",
|
||||||
|
" key = a[i];",
|
||||||
|
" int j = i - 1;",
|
||||||
|
" while (j >= 0 && a[j] > key)",
|
||||||
|
" {",
|
||||||
|
" a[j + 1] = a[j];",
|
||||||
|
" j--;",
|
||||||
|
" }",
|
||||||
|
" a[j + 1] = key;",
|
||||||
|
" }",
|
||||||
|
"}",
|
||||||
|
"//int main()",
|
||||||
|
"//{",
|
||||||
|
" int n;",
|
||||||
|
" cin >> n;",
|
||||||
|
" int a[n];",
|
||||||
|
" for (int i = 0; i < n; i++)",
|
||||||
|
" {",
|
||||||
|
" cin >> a[i];",
|
||||||
|
" }",
|
||||||
|
" insertionSort(a, n);",
|
||||||
|
" for (int i = 0; i < n; i++)",
|
||||||
|
" {",
|
||||||
|
" cout << a[i] << \" \";",
|
||||||
|
" }",
|
||||||
|
"//return 0;",
|
||||||
|
"//}"
|
||||||
|
],
|
||||||
|
"description": "Insertion Sort "
|
||||||
|
},
|
||||||
|
"Bubble Sort ": {
|
||||||
|
"prefix": "bubsort",
|
||||||
|
"body": [
|
||||||
|
"void swap(int *xp, int *yp)",
|
||||||
|
"{",
|
||||||
|
" int temp = *xp;",
|
||||||
|
" *xp = *yp;",
|
||||||
|
" *yp = temp;",
|
||||||
|
"}",
|
||||||
|
"",
|
||||||
|
"void recurBubbleSort(int arr[], int n)",
|
||||||
|
"{",
|
||||||
|
" // Base case",
|
||||||
|
" if (n == 1)",
|
||||||
|
" return;",
|
||||||
|
"",
|
||||||
|
" // One pass of bubble sort. After",
|
||||||
|
" // this pass, the largest element",
|
||||||
|
" // is moved (or bubbled) to end.",
|
||||||
|
" for (int i = 0; i < n - 1; i++)",
|
||||||
|
" if (arr[i] > arr[i + 1])",
|
||||||
|
" swap(arr[i], arr[i + 1]);",
|
||||||
|
"",
|
||||||
|
" // Largest element is fixed,",
|
||||||
|
" // recur for remaining array",
|
||||||
|
" bubbleSort(arr, n - 1);",
|
||||||
|
"}",
|
||||||
|
"",
|
||||||
|
"void bubbleSort(int a[], int n)",
|
||||||
|
"{",
|
||||||
|
" for (int i = 0; i < n - 1; i++)",
|
||||||
|
" {",
|
||||||
|
" bool flag = false;",
|
||||||
|
" for (int j = 0; j < n - i - 1; j++)",
|
||||||
|
" {",
|
||||||
|
" if (a[j] > a[j + 1])",
|
||||||
|
" {",
|
||||||
|
" swap(&a[j], &a[j + 1]);",
|
||||||
|
" flag = true;",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" if (flag == false)",
|
||||||
|
" {",
|
||||||
|
" break;",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
"}",
|
||||||
|
"int main()",
|
||||||
|
"{",
|
||||||
|
" int n;",
|
||||||
|
" cin >> n;",
|
||||||
|
" int a[n];",
|
||||||
|
" for (int i = 0; i < n; i++)",
|
||||||
|
" {",
|
||||||
|
" cin >> a[i];",
|
||||||
|
" }",
|
||||||
|
" bubbleSort(a, n);",
|
||||||
|
" for (int i = 0; i < n; i++)",
|
||||||
|
" {",
|
||||||
|
" cout << a[i] << \" \";",
|
||||||
|
" }",
|
||||||
|
" return 0;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Bubble Sort "
|
||||||
|
},
|
||||||
|
"Selection Sort ": {
|
||||||
|
"prefix": "selsort",
|
||||||
|
"body": [
|
||||||
|
"void swap(int *xp, int *yp)",
|
||||||
|
"{",
|
||||||
|
" int temp = *xp;",
|
||||||
|
" *xp = *yp;",
|
||||||
|
" *yp = temp;",
|
||||||
|
"}",
|
||||||
|
"",
|
||||||
|
"void selectionSort(int a[], int n)",
|
||||||
|
"{",
|
||||||
|
" int min_idx;",
|
||||||
|
" for (int i = 0; i < n - 1; i++)",
|
||||||
|
" {",
|
||||||
|
" min_idx = i;",
|
||||||
|
" for (int j = i + 1; j < n; j++)",
|
||||||
|
" {",
|
||||||
|
" if (a[min_idx] > a[j])",
|
||||||
|
" {",
|
||||||
|
" min_idx = j;",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" swap(&a[min_idx], &a[i]);",
|
||||||
|
" }",
|
||||||
|
"}",
|
||||||
|
"int main()",
|
||||||
|
"{",
|
||||||
|
" int n;",
|
||||||
|
" cin >> n;",
|
||||||
|
" int a[n];",
|
||||||
|
" for (int i = 0; i < n; i++)",
|
||||||
|
" {",
|
||||||
|
" cin >> a[i];",
|
||||||
|
" }",
|
||||||
|
" selectionSort(a, n);",
|
||||||
|
" for (int i = 0; i < n; i++)",
|
||||||
|
" {",
|
||||||
|
" cout << a[i] << \" \";",
|
||||||
|
" }",
|
||||||
|
" return 0;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Selection Sort "
|
||||||
|
},
|
||||||
|
"Merge Sort ": {
|
||||||
|
"prefix": "mersort",
|
||||||
|
"body": [
|
||||||
|
"void swap(int *xp, int *yp)",
|
||||||
|
"{",
|
||||||
|
" int temp = *xp;",
|
||||||
|
" *xp = *yp;",
|
||||||
|
" *yp = temp;",
|
||||||
|
"}",
|
||||||
|
"void merge(int array[], int const left, int const mid, int const right)",
|
||||||
|
"{",
|
||||||
|
" auto const subArrayOne = mid - left + 1;",
|
||||||
|
" auto const subArrayTwo = right - mid;",
|
||||||
|
"",
|
||||||
|
" // Create temp arrays",
|
||||||
|
" auto *leftArray = new int[subArrayOne],",
|
||||||
|
" *rightArray = new int[subArrayTwo];",
|
||||||
|
"",
|
||||||
|
" // Copy data to temp arrays leftArray[] and rightArray[]",
|
||||||
|
" for (auto i = 0; i < subArrayOne; i++)",
|
||||||
|
" leftArray[i] = array[left + i];",
|
||||||
|
" for (auto j = 0; j < subArrayTwo; j++)",
|
||||||
|
" rightArray[j] = array[mid + 1 + j];",
|
||||||
|
"",
|
||||||
|
" auto indexOfSubArrayOne = 0, // Initial index of first sub-array",
|
||||||
|
" indexOfSubArrayTwo = 0; // Initial index of second sub-array",
|
||||||
|
" int indexOfMergedArray = left; // Initial index of merged array",
|
||||||
|
"",
|
||||||
|
" // Merge the temp arrays back into array[left..right]",
|
||||||
|
" while (indexOfSubArrayOne < subArrayOne && indexOfSubArrayTwo < subArrayTwo)",
|
||||||
|
" {",
|
||||||
|
" if (leftArray[indexOfSubArrayOne] <= rightArray[indexOfSubArrayTwo])",
|
||||||
|
" {",
|
||||||
|
" array[indexOfMergedArray] = leftArray[indexOfSubArrayOne];",
|
||||||
|
" indexOfSubArrayOne++;",
|
||||||
|
" }",
|
||||||
|
" else",
|
||||||
|
" {",
|
||||||
|
" array[indexOfMergedArray] = rightArray[indexOfSubArrayTwo];",
|
||||||
|
" indexOfSubArrayTwo++;",
|
||||||
|
" }",
|
||||||
|
" indexOfMergedArray++;",
|
||||||
|
" }",
|
||||||
|
" // Copy the remaining elements of",
|
||||||
|
" // left[], if there are any",
|
||||||
|
" while (indexOfSubArrayOne < subArrayOne)",
|
||||||
|
" {",
|
||||||
|
" array[indexOfMergedArray] = leftArray[indexOfSubArrayOne];",
|
||||||
|
" indexOfSubArrayOne++;",
|
||||||
|
" indexOfMergedArray++;",
|
||||||
|
" }",
|
||||||
|
" // Copy the remaining elements of",
|
||||||
|
" // right[], if there are any",
|
||||||
|
" while (indexOfSubArrayTwo < subArrayTwo)",
|
||||||
|
" {",
|
||||||
|
" array[indexOfMergedArray] = rightArray[indexOfSubArrayTwo];",
|
||||||
|
" indexOfSubArrayTwo++;",
|
||||||
|
" indexOfMergedArray++;",
|
||||||
|
" }",
|
||||||
|
"}",
|
||||||
|
"",
|
||||||
|
"// begin is for left index and end is",
|
||||||
|
"// right index of the sub-array",
|
||||||
|
"// of arr to be sorted */",
|
||||||
|
"void mergeSort(int array[], int const begin, int const end)",
|
||||||
|
"{",
|
||||||
|
" if (begin >= end)",
|
||||||
|
" return; // Returns recursively",
|
||||||
|
"",
|
||||||
|
" auto mid = begin + (end - begin) / 2;",
|
||||||
|
" mergeSort(array, begin, mid);",
|
||||||
|
" mergeSort(array, mid + 1, end);",
|
||||||
|
" merge(array, begin, mid, end);",
|
||||||
|
"}",
|
||||||
|
"int main()",
|
||||||
|
"{",
|
||||||
|
" int n;",
|
||||||
|
" cin >> n;",
|
||||||
|
" int a[n];",
|
||||||
|
" for (int i = 0; i < n; i++)",
|
||||||
|
" {",
|
||||||
|
" cin >> a[i];",
|
||||||
|
" }",
|
||||||
|
" mergeSort(a, 0, n - 1);",
|
||||||
|
" for (int i = 0; i < n; i++)",
|
||||||
|
" {",
|
||||||
|
" cout << a[i] << \" \";",
|
||||||
|
" }",
|
||||||
|
" return 0;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Merge Sort "
|
||||||
|
},
|
||||||
|
"Quick Sort ": {
|
||||||
|
"prefix": "qsort",
|
||||||
|
"body": [
|
||||||
|
"void swap(int *xp, int *yp)",
|
||||||
|
"{",
|
||||||
|
" int temp = *xp;",
|
||||||
|
" *xp = *yp;",
|
||||||
|
" *yp = temp;",
|
||||||
|
"}",
|
||||||
|
"int partition(int a[], int l, int h)",
|
||||||
|
"{",
|
||||||
|
" int i = l;",
|
||||||
|
" int j = h;",
|
||||||
|
" int pivot = a[l];",
|
||||||
|
" while (i < j)",
|
||||||
|
" {",
|
||||||
|
" do",
|
||||||
|
" {",
|
||||||
|
" i++;",
|
||||||
|
" } while (a[i] <= pivot);",
|
||||||
|
" do",
|
||||||
|
" {",
|
||||||
|
" j--;",
|
||||||
|
" } while (a[j] > pivot);",
|
||||||
|
" if (i < j)",
|
||||||
|
" {",
|
||||||
|
" swap(&a[i], &a[j]);",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" swap(&a[l], &a[j]);",
|
||||||
|
" return j;",
|
||||||
|
"}",
|
||||||
|
"void quickSort(int a[], int l, int h)",
|
||||||
|
"{",
|
||||||
|
" if (l < h)",
|
||||||
|
" {",
|
||||||
|
" int pi = partition(a, l, h);",
|
||||||
|
" quickSort(a, l, pi);",
|
||||||
|
" quickSort(a, pi + 1, h);",
|
||||||
|
" }",
|
||||||
|
"}",
|
||||||
|
"int main()",
|
||||||
|
"{",
|
||||||
|
" int n;",
|
||||||
|
" cin >> n;",
|
||||||
|
" int a[n];",
|
||||||
|
" for (int i = 0; i < n; i++)",
|
||||||
|
" {",
|
||||||
|
" cin >> a[i];",
|
||||||
|
" }",
|
||||||
|
" quickSort(a, 0, n - 1);",
|
||||||
|
" for (int i = 0; i < n; i++)",
|
||||||
|
" {",
|
||||||
|
" cout << a[i] << \" \";",
|
||||||
|
" }",
|
||||||
|
" return 0;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Quick Sort "
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,84 @@
|
||||||
|
{
|
||||||
|
"CP Starter Template": {
|
||||||
|
"prefix": "cpp",
|
||||||
|
"body": [
|
||||||
|
"#include <bits/stdc++.h>",
|
||||||
|
"using namespace std;",
|
||||||
|
"#define ll long long",
|
||||||
|
"#define endl '\\n'",
|
||||||
|
"#define tc \\",
|
||||||
|
" ll tc; \\",
|
||||||
|
" cin >> tc; \\",
|
||||||
|
" while (tc--)",
|
||||||
|
"#define pb push_back",
|
||||||
|
"#define mp make_pair",
|
||||||
|
"const ll MOD = 1e9 + 7;",
|
||||||
|
"",
|
||||||
|
"void fastio()",
|
||||||
|
"{",
|
||||||
|
" ios_base::sync_with_stdio(0);",
|
||||||
|
" cin.tie(0);",
|
||||||
|
" cout.tie(0);",
|
||||||
|
"}",
|
||||||
|
"",
|
||||||
|
"void solve()",
|
||||||
|
"{${1:cursor}",
|
||||||
|
"}",
|
||||||
|
"",
|
||||||
|
"int main()",
|
||||||
|
"{",
|
||||||
|
" fastio();",
|
||||||
|
" ll t;",
|
||||||
|
" cin >> t;",
|
||||||
|
" for (ll i = 1; i <= t; i++)",
|
||||||
|
" {",
|
||||||
|
" solve();",
|
||||||
|
" }",
|
||||||
|
"",
|
||||||
|
" return 0;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Creates Basic CPP Starter Template"
|
||||||
|
},
|
||||||
|
"C++ Starter Template": {
|
||||||
|
"prefix": "c++",
|
||||||
|
"body": [
|
||||||
|
"#include <bits/stdc++.h>",
|
||||||
|
"using namespace std;",
|
||||||
|
"",
|
||||||
|
"int main()",
|
||||||
|
"{ ",
|
||||||
|
" return 0;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Creates Simple C++ Starter Template"
|
||||||
|
},
|
||||||
|
"DSA Specific Starter Template": {
|
||||||
|
"prefix": "dsa",
|
||||||
|
"body": [
|
||||||
|
"#include <bits/stdc++.h>",
|
||||||
|
"using namespace std;",
|
||||||
|
"class Solution",
|
||||||
|
"{",
|
||||||
|
" public:",
|
||||||
|
" void solve(){",
|
||||||
|
" }",
|
||||||
|
" ",
|
||||||
|
"};",
|
||||||
|
"int main()",
|
||||||
|
"{ ",
|
||||||
|
" return 0;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Create Basic DSA Starter Template"
|
||||||
|
},
|
||||||
|
"Fast Input Output": {
|
||||||
|
"prefix": "fastio",
|
||||||
|
"body": [
|
||||||
|
"ios_base::sync_with_stdio(0);",
|
||||||
|
" cin.tie(0);",
|
||||||
|
" cout.tie(0);"
|
||||||
|
],
|
||||||
|
"description": "Fast Input Output"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,228 @@
|
||||||
|
{
|
||||||
|
"palindrome-String ": {
|
||||||
|
"prefix": "palindrome",
|
||||||
|
"body": [
|
||||||
|
"bool ispalindrome(string s)",
|
||||||
|
"{",
|
||||||
|
" int n=s.size();",
|
||||||
|
" for(int i=0;i<n;i++) if(s[i]!=s[n-i-1]) return 0;",
|
||||||
|
" return 1;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "String "
|
||||||
|
},
|
||||||
|
"pangram-string": {
|
||||||
|
"prefix": "pangram",
|
||||||
|
"body": [
|
||||||
|
"bool ispangram(string s)",
|
||||||
|
"{",
|
||||||
|
" int n=s.size();",
|
||||||
|
" int f[26]={0}; ",
|
||||||
|
" for(int i=0;i<n;i++) ",
|
||||||
|
" {",
|
||||||
|
" if(s[i]>='a' && s[i]<='z') ",
|
||||||
|
" f[s[i]-97]++;",
|
||||||
|
" else",
|
||||||
|
" if(s[i]>='A' && s[i]<='Z') ",
|
||||||
|
" f[s[i]-65]++;",
|
||||||
|
" }",
|
||||||
|
" for(int i=0;i<26;i++)",
|
||||||
|
" if(f[i]==0) return 0;",
|
||||||
|
" return 1;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "pangram-string"
|
||||||
|
},
|
||||||
|
"anagram-strings": {
|
||||||
|
"prefix": "anagram",
|
||||||
|
"body": [
|
||||||
|
"bool isanagram(string a, string b)",
|
||||||
|
"{",
|
||||||
|
" if(a.size()!=b.size()) return 0;",
|
||||||
|
" int f[26]={0}; ",
|
||||||
|
" for(int i=0;i<a.size();i++) ",
|
||||||
|
" {",
|
||||||
|
" if(a[i]>='a' && a[i]<='z') ",
|
||||||
|
" f[a[i]-97]++;",
|
||||||
|
" else",
|
||||||
|
" if(a[i]>='A' && a[i]<='Z') ",
|
||||||
|
" f[a[i]-65]++;",
|
||||||
|
" }",
|
||||||
|
" for(int i=0;i<b.size();i++) ",
|
||||||
|
" {",
|
||||||
|
" if(b[i]>='a' && b[i]<='z') ",
|
||||||
|
" f[b[i]-97]--;",
|
||||||
|
" else",
|
||||||
|
" if(b[i]>='A' && b[i]<='Z') ",
|
||||||
|
" f[b[i]-65]--;",
|
||||||
|
" }",
|
||||||
|
" for(int i=0;i<26;i++)",
|
||||||
|
" if(f[i]!=0) return 0;",
|
||||||
|
" return 1;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "anagram-strings"
|
||||||
|
},
|
||||||
|
"string->decimal": {
|
||||||
|
"prefix": "strtodec",
|
||||||
|
"body": [
|
||||||
|
"long long int string_to_decimal(string s)",
|
||||||
|
" {",
|
||||||
|
" long long int ans = 0, p = 1;",
|
||||||
|
" for (int i = s.size() - 1; i >= 0; i--)",
|
||||||
|
" {",
|
||||||
|
" ans += p * (s[i] - '0');",
|
||||||
|
" p = p * 10;",
|
||||||
|
" }",
|
||||||
|
" return ans;",
|
||||||
|
" }"
|
||||||
|
],
|
||||||
|
"description": "string->decimal"
|
||||||
|
},
|
||||||
|
"decimal->string": {
|
||||||
|
"prefix": "dectostr",
|
||||||
|
"body": [
|
||||||
|
"string decimal_to_string(long long int n)",
|
||||||
|
" {",
|
||||||
|
" if (n == 0)",
|
||||||
|
" return 0;",
|
||||||
|
" string ans = \"\";",
|
||||||
|
" while (n > 0)",
|
||||||
|
" {",
|
||||||
|
" ans = ans + (char)((n % 10) + 48);",
|
||||||
|
" n = n / 10;",
|
||||||
|
" }",
|
||||||
|
" reverse(ans.begin(), ans.end());",
|
||||||
|
" return ans;",
|
||||||
|
" }"
|
||||||
|
],
|
||||||
|
"description": "decimal->string"
|
||||||
|
},
|
||||||
|
"occurences of pattern in a string ": {
|
||||||
|
"prefix": "kmp",
|
||||||
|
"body": [
|
||||||
|
"vector<int> kmp(string s,string t) //returns the indices of occurences of pattern t in string s",
|
||||||
|
"{",
|
||||||
|
" if(s.size()<t.size())",
|
||||||
|
" return {};",
|
||||||
|
" ",
|
||||||
|
" int n=s.size(),m=t.size();",
|
||||||
|
" int lps[m+1]={0};",
|
||||||
|
" int len = 0,i=1,j;",
|
||||||
|
" while (i < m) {",
|
||||||
|
" if (t[i] ==t[len]) lps[i++] = ++len;",
|
||||||
|
" else",
|
||||||
|
" {",
|
||||||
|
" if (len != 0) len = lps[len - 1];",
|
||||||
|
" else lps[i++] = 0;",
|
||||||
|
" ",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" vector<int>ans;",
|
||||||
|
" i=0;j=0;",
|
||||||
|
" while (i<n) {",
|
||||||
|
" if (t[j] ==s[i]) { j++; i++; }",
|
||||||
|
" ",
|
||||||
|
" if (j == m) {",
|
||||||
|
" ans.pb(i-j);",
|
||||||
|
" j=lps[j - 1];",
|
||||||
|
" }",
|
||||||
|
" else if (i < n && t[j]!=s[i]) {",
|
||||||
|
" ",
|
||||||
|
" if (j != 0) j = lps[j - 1];",
|
||||||
|
" else i++; }",
|
||||||
|
" }",
|
||||||
|
" return ans;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "occurences of pattern in a string "
|
||||||
|
},
|
||||||
|
"count of unique characters in a string": {
|
||||||
|
"prefix": "distict_characters",
|
||||||
|
"body": [
|
||||||
|
"int distinct_str(string s) {",
|
||||||
|
" sort(s.begin(), s.end());",
|
||||||
|
" return unique(s.begin(), s.end()) - s.begin();",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "count of unique characters in a string"
|
||||||
|
},
|
||||||
|
|
||||||
|
"longest common subsequence of two strings": {
|
||||||
|
"prefix": "lc_subsequence",
|
||||||
|
"body": [
|
||||||
|
"string lc_subsequence(string a,string b)",
|
||||||
|
"{",
|
||||||
|
" string ans=\"\";",
|
||||||
|
" int n=a.size(),m=b.size(),i,j;",
|
||||||
|
"",
|
||||||
|
" int dp[n][m];",
|
||||||
|
" dp[0][0]= a[0]==b[0]?1:0;",
|
||||||
|
" for(i=1;i<n;i++) dp[i][0]= a[i]==b[0]?1:dp[i-1][0];",
|
||||||
|
" for(j=1;j<m;j++) dp[0][j]= a[0]==b[j]?1:dp[0][j-1];",
|
||||||
|
"",
|
||||||
|
" for(i=1;i<n;i++)",
|
||||||
|
" {",
|
||||||
|
" for(j=1;j<m;j++)",
|
||||||
|
" {",
|
||||||
|
" if(a[i]==b[j]) dp[i][j]=dp[i-1][j-1]+1;",
|
||||||
|
" else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" i = n-1, j = m-1;",
|
||||||
|
" while (i>=0 && j>=0)",
|
||||||
|
" {",
|
||||||
|
" if (a[i] == b[j])",
|
||||||
|
" {",
|
||||||
|
" ans+=a[i]; ",
|
||||||
|
" i--; j--;",
|
||||||
|
" }",
|
||||||
|
" else if (dp[i-1][j] > dp[i][j-1]) i--;",
|
||||||
|
" else j--;",
|
||||||
|
" }",
|
||||||
|
" reverse(ans.begin(),ans.end());",
|
||||||
|
" return ans;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "longest common subsequence of two strings"
|
||||||
|
},
|
||||||
|
"longest common substring of two strings": {
|
||||||
|
"prefix": "lc_substring",
|
||||||
|
"body": [
|
||||||
|
"string lc_substring(string a,string b)",
|
||||||
|
"{",
|
||||||
|
" string ans=\"\";",
|
||||||
|
" int n=a.size(),m=b.size(),i,j;",
|
||||||
|
" if(n==0 || m==0) return \"\" ;",
|
||||||
|
" int dp[n][m];",
|
||||||
|
" dp[0][0]= a[0]==b[0]?1:0;",
|
||||||
|
" for(i=1;i<n;i++) dp[i][0]= a[i]==b[0]?1:0;",
|
||||||
|
" for(j=1;j<m;j++) dp[0][j]= a[0]==b[j]?1:0;",
|
||||||
|
" int len=0,row,col;",
|
||||||
|
" for(i=1;i<n;i++)",
|
||||||
|
" {",
|
||||||
|
" for(j=1;j<m;j++)",
|
||||||
|
" {",
|
||||||
|
" if(a[i]==b[j]) dp[i][j]=dp[i-1][j-1]+1;",
|
||||||
|
" else dp[i][j]= 0;",
|
||||||
|
"",
|
||||||
|
" if (len < dp[i][j]) {",
|
||||||
|
" len = dp[i][j];",
|
||||||
|
" row = i;",
|
||||||
|
" col = j;",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" if(len==0) return \"\";",
|
||||||
|
" while (dp[row][col] != 0) {",
|
||||||
|
" ans+=a[row];",
|
||||||
|
" row--;",
|
||||||
|
" col--;",
|
||||||
|
" }",
|
||||||
|
" reverse(ans.begin(),ans.end());",
|
||||||
|
" return ans;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "longest common substring of two strings"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
|
||||||
|
|
@ -0,0 +1,377 @@
|
||||||
|
{
|
||||||
|
"Tree Structure": {
|
||||||
|
"prefix": "tstruct",
|
||||||
|
"body": [
|
||||||
|
"struct Node",
|
||||||
|
"{",
|
||||||
|
" int data;",
|
||||||
|
" Node *left;",
|
||||||
|
" Node *right;",
|
||||||
|
"",
|
||||||
|
" Node(int k)",
|
||||||
|
" {",
|
||||||
|
" data = k;",
|
||||||
|
" left = right = NULL;",
|
||||||
|
" }",
|
||||||
|
"};"
|
||||||
|
],
|
||||||
|
"description": "Tree Structure"
|
||||||
|
},
|
||||||
|
"Tree Inorder": {
|
||||||
|
"prefix": "inorder",
|
||||||
|
"body": [
|
||||||
|
"void inOrder(Node *temp)",
|
||||||
|
"{",
|
||||||
|
" if (temp != NULL)",
|
||||||
|
" {",
|
||||||
|
" inOrder(temp->left);",
|
||||||
|
" cout << temp->data << ' ';",
|
||||||
|
" inOrder(temp->right);",
|
||||||
|
" }",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Tree Inorder"
|
||||||
|
},
|
||||||
|
"Tree Preorder": {
|
||||||
|
"prefix": "preorder",
|
||||||
|
"body": [
|
||||||
|
"void preOrder(Node *temp)",
|
||||||
|
"{",
|
||||||
|
" if (temp != NULL)",
|
||||||
|
" {",
|
||||||
|
" cout << temp->data << ' ';",
|
||||||
|
" preOrder(temp->left);",
|
||||||
|
" preOrder(temp->right);",
|
||||||
|
" }",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Tree Preorder"
|
||||||
|
},
|
||||||
|
"Tree Postorder": {
|
||||||
|
"prefix": "postorder",
|
||||||
|
"body": [
|
||||||
|
"int c = 0;",
|
||||||
|
"void postOrder(Node *temp)",
|
||||||
|
"{",
|
||||||
|
" if (temp != NULL)",
|
||||||
|
" {",
|
||||||
|
" postOrder(temp->left);",
|
||||||
|
" postOrder(temp->right);",
|
||||||
|
" cout << temp->data << ' ';",
|
||||||
|
" if (temp->right == NULL && temp->left == NULL)",
|
||||||
|
" {",
|
||||||
|
" c++;",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Tree Postorder"
|
||||||
|
},
|
||||||
|
"Level Order Traversal Tree": {
|
||||||
|
"prefix": "levelOrder",
|
||||||
|
"body": [
|
||||||
|
"void levelOrder(Node *root)",
|
||||||
|
"{",
|
||||||
|
" if (root == NULL)",
|
||||||
|
" {",
|
||||||
|
" return;",
|
||||||
|
" }",
|
||||||
|
" queue<Node *> q;",
|
||||||
|
"",
|
||||||
|
" q.push(root);",
|
||||||
|
" q.push(NULL);",
|
||||||
|
"",
|
||||||
|
" while (!q.empty())",
|
||||||
|
" {",
|
||||||
|
" Node *node = q.front();",
|
||||||
|
" q.pop();",
|
||||||
|
"",
|
||||||
|
" if (node != NULL)",
|
||||||
|
" {",
|
||||||
|
" cout << node->data << \" \";",
|
||||||
|
" if (node->left)",
|
||||||
|
" {",
|
||||||
|
" q.push(node->left);",
|
||||||
|
" }",
|
||||||
|
" if (node->right)",
|
||||||
|
" {",
|
||||||
|
" q.push(node->right);",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" else if (!q.empty())",
|
||||||
|
" {",
|
||||||
|
" q.push(NULL);",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Level Order Traversal Tree"
|
||||||
|
},
|
||||||
|
"Tree Size": {
|
||||||
|
"prefix": "treeSize",
|
||||||
|
"body": [
|
||||||
|
"int getSize(Node *root)",
|
||||||
|
"{",
|
||||||
|
" if (root == NULL)",
|
||||||
|
" {",
|
||||||
|
" return 0;",
|
||||||
|
" }",
|
||||||
|
" return 1 + getSize(root->left) + getSize(root->right);",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Tree Size"
|
||||||
|
},
|
||||||
|
"Tree Height": {
|
||||||
|
"prefix": "treeHeight",
|
||||||
|
"body": [
|
||||||
|
"int height(Node *root)",
|
||||||
|
"{",
|
||||||
|
" if (root == NULL)",
|
||||||
|
" {",
|
||||||
|
" return 0;",
|
||||||
|
" }",
|
||||||
|
" return max(height(root->left), height(root->right)) + 1;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Tree Height"
|
||||||
|
},
|
||||||
|
"Tree Simple Diameter": {
|
||||||
|
"prefix": "treeDiaSimple",
|
||||||
|
"body": [
|
||||||
|
"int diameter(Node *root)",
|
||||||
|
"{",
|
||||||
|
" if (root == NULL)",
|
||||||
|
" {",
|
||||||
|
" return 0;",
|
||||||
|
" }",
|
||||||
|
"",
|
||||||
|
" int currDiameter = height(root->left) + height(root->right) + 1;",
|
||||||
|
"",
|
||||||
|
" int lD = diameter(root->left);",
|
||||||
|
" int rD = diameter(root->right);",
|
||||||
|
"",
|
||||||
|
" return max(currDiameter, max(lD, rD));",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Tree Simple Diameter"
|
||||||
|
},
|
||||||
|
"Tree Optimized Diameter": {
|
||||||
|
"prefix": "treeDiaOpt",
|
||||||
|
"body": [
|
||||||
|
"int optDiameter(Node *root, int *ht)",
|
||||||
|
"{",
|
||||||
|
" if (root == NULL)",
|
||||||
|
" {",
|
||||||
|
" *ht = 0;",
|
||||||
|
" return 0;",
|
||||||
|
" }",
|
||||||
|
"",
|
||||||
|
" int lh = 0, rh = 0;",
|
||||||
|
"",
|
||||||
|
" int lD = optDiameter(root->left, &lh);",
|
||||||
|
" int rD = optDiameter(root->right, &rh);",
|
||||||
|
" int currDiameter = lh + rh + 1;",
|
||||||
|
" *ht = max(lh, rh) + 1;",
|
||||||
|
"",
|
||||||
|
" return max(currDiameter, max(lD, rD));",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Tree Optimized Diameter"
|
||||||
|
},
|
||||||
|
"Tree Node Sum": {
|
||||||
|
"prefix": "treeSum",
|
||||||
|
"body": [
|
||||||
|
"int getSum(Node *root)",
|
||||||
|
"{",
|
||||||
|
" if (root == NULL)",
|
||||||
|
" {",
|
||||||
|
" return 0;",
|
||||||
|
" }",
|
||||||
|
" return getSum(root->left) + getSum(root->right) + root->data;",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Tree Node Sum"
|
||||||
|
},
|
||||||
|
"Tree Node Maximum": {
|
||||||
|
"prefix": "treeMax",
|
||||||
|
"body": [
|
||||||
|
"int MAX(Node *root)",
|
||||||
|
"{",
|
||||||
|
" if (root == NULL)",
|
||||||
|
" {",
|
||||||
|
" return INT_MIN;",
|
||||||
|
" }",
|
||||||
|
"",
|
||||||
|
" return max(root->data, max(MAX(root->left), MAX(root->right)));",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Tree Node Maximum"
|
||||||
|
},
|
||||||
|
"Tree Right View": {
|
||||||
|
"prefix": "treeRightView",
|
||||||
|
"body": [
|
||||||
|
"void rightView(Node *root)",
|
||||||
|
"{",
|
||||||
|
" if (root == NULL)",
|
||||||
|
" {",
|
||||||
|
" return;",
|
||||||
|
" }",
|
||||||
|
" queue<Node *> q;",
|
||||||
|
" q.push(root);",
|
||||||
|
"",
|
||||||
|
" while (!q.empty())",
|
||||||
|
" {",
|
||||||
|
" int n = q.size();",
|
||||||
|
" for (int i = 0; i < n; i++)",
|
||||||
|
" {",
|
||||||
|
" Node *curr = q.front();",
|
||||||
|
" q.pop();",
|
||||||
|
"",
|
||||||
|
" if (i == n - 1)",
|
||||||
|
" {",
|
||||||
|
" cout << curr->data << \" \";",
|
||||||
|
" }",
|
||||||
|
" if (curr->left != NULL)",
|
||||||
|
" {",
|
||||||
|
" q.push(curr->left);",
|
||||||
|
" }",
|
||||||
|
" if (curr->right != NULL)",
|
||||||
|
" {",
|
||||||
|
" q.push(curr->right);",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Tree Right View"
|
||||||
|
},
|
||||||
|
"Tree Left View": {
|
||||||
|
"prefix": "treeLeftView",
|
||||||
|
"body": [
|
||||||
|
"void leftView(Node *root)",
|
||||||
|
"{",
|
||||||
|
" if (root == NULL)",
|
||||||
|
" {",
|
||||||
|
" return;",
|
||||||
|
" }",
|
||||||
|
" queue<Node *> q;",
|
||||||
|
" q.push(root);",
|
||||||
|
"",
|
||||||
|
" while (!q.empty())",
|
||||||
|
" {",
|
||||||
|
" int n = q.size();",
|
||||||
|
" for (int i = 1; i <= n; i++)",
|
||||||
|
" {",
|
||||||
|
" Node *curr = q.front();",
|
||||||
|
" q.pop();",
|
||||||
|
"",
|
||||||
|
" if (i == 1)",
|
||||||
|
" {",
|
||||||
|
" cout << curr->data << \" \";",
|
||||||
|
" }",
|
||||||
|
" if (curr->left != NULL)",
|
||||||
|
" {",
|
||||||
|
" q.push(curr->left);",
|
||||||
|
" }",
|
||||||
|
" if (curr->right != NULL)",
|
||||||
|
" {",
|
||||||
|
" q.push(curr->right);",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
" }",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Tree Left View"
|
||||||
|
},
|
||||||
|
"Tree Balanced or Not Simple": {
|
||||||
|
"prefix": "isBalancedSimple",
|
||||||
|
"body": [
|
||||||
|
"bool isBalanced(Node *root)",
|
||||||
|
"{",
|
||||||
|
" if (root == NULL)",
|
||||||
|
" {",
|
||||||
|
" return true;",
|
||||||
|
" }",
|
||||||
|
" if (isBalanced(root->left) == false)",
|
||||||
|
" {",
|
||||||
|
" return false;",
|
||||||
|
" }",
|
||||||
|
" if (isBalanced(root->right) == false)",
|
||||||
|
" {",
|
||||||
|
" return false;",
|
||||||
|
" }",
|
||||||
|
" if (abs(height(root->left) - height(root->right)) <= 1)",
|
||||||
|
" {",
|
||||||
|
" return true;",
|
||||||
|
" }",
|
||||||
|
" else",
|
||||||
|
" {",
|
||||||
|
" return false;",
|
||||||
|
" }",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Tree Balanced or Not Simple"
|
||||||
|
},
|
||||||
|
"Tree Balanced or Not Optimized": {
|
||||||
|
"prefix": "isBalancedOpt",
|
||||||
|
"body": [
|
||||||
|
"bool isOptBalanced(Node *root, int *ht)",
|
||||||
|
"{",
|
||||||
|
" if (root == NULL)",
|
||||||
|
" {",
|
||||||
|
" return true;",
|
||||||
|
" }",
|
||||||
|
" int lh = 0, rh = 0;",
|
||||||
|
" if (isOptBalanced(root->left, &lh) == false)",
|
||||||
|
" {",
|
||||||
|
" return false;",
|
||||||
|
" }",
|
||||||
|
" if (isOptBalanced(root->right, &rh) == false)",
|
||||||
|
" {",
|
||||||
|
" return false;",
|
||||||
|
" }",
|
||||||
|
" *ht = max(lh, rh) + 1;",
|
||||||
|
" if (abs(lh - rh) <= 1)",
|
||||||
|
" {",
|
||||||
|
" return true;",
|
||||||
|
" }",
|
||||||
|
" else",
|
||||||
|
" {",
|
||||||
|
" return false;",
|
||||||
|
" }",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Tree Balanced or Not Optimized"
|
||||||
|
},
|
||||||
|
"Flatten Binary Tree": {
|
||||||
|
"prefix": "flatten",
|
||||||
|
"body": [
|
||||||
|
"void flatten(Node *root)",
|
||||||
|
"{",
|
||||||
|
" if (root == NULL || (root->left == NULL || root->right == NULL))",
|
||||||
|
" {",
|
||||||
|
" return;",
|
||||||
|
" }",
|
||||||
|
"",
|
||||||
|
" if (root->left != NULL)",
|
||||||
|
" {",
|
||||||
|
" flatten(root->left);",
|
||||||
|
"",
|
||||||
|
" Node *temp = root->right;",
|
||||||
|
" root->right = root->left;",
|
||||||
|
" root->left = NULL;",
|
||||||
|
"",
|
||||||
|
" Node *t = root->right;",
|
||||||
|
"",
|
||||||
|
" while (t->right != NULL)",
|
||||||
|
" {",
|
||||||
|
" t = t->right;",
|
||||||
|
" }",
|
||||||
|
" t->right = temp;",
|
||||||
|
" }",
|
||||||
|
" flatten(root->right);",
|
||||||
|
"}"
|
||||||
|
],
|
||||||
|
"description": "Flatten Binary Tree"
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue