id
int64
0
755k
file_name
stringlengths
3
109
file_path
stringlengths
13
185
content
stringlengths
31
9.38M
size
int64
31
9.38M
language
stringclasses
1 value
extension
stringclasses
11 values
total_lines
int64
1
340k
avg_line_length
float64
2.18
149k
max_line_length
int64
7
2.22M
alphanum_fraction
float64
0
1
repo_name
stringlengths
6
65
repo_stars
int64
100
47.3k
repo_forks
int64
0
12k
repo_open_issues
int64
0
3.4k
repo_license
stringclasses
9 values
repo_extraction_date
stringclasses
92 values
exact_duplicates_redpajama
bool
2 classes
near_duplicates_redpajama
bool
2 classes
exact_duplicates_githubcode
bool
2 classes
exact_duplicates_stackv2
bool
1 class
exact_duplicates_stackv1
bool
2 classes
near_duplicates_githubcode
bool
2 classes
near_duplicates_stackv1
bool
2 classes
near_duplicates_stackv2
bool
1 class
10,236
inserting_a_node.cpp
OpenGenus_cosmos/code/data_structures/src/Linked_List/inserting_a_node.cpp
#include <iostream> using namespace std; typedef struct node Node; struct node { int data; Node *next_pointer; }; Node *create_node(); void insert_in_beginning(int value, Node **start); void insert_in_ending(int value, Node **start); void insert_in_specific_position(int value, Node *start); void linked_list_...
1,756
C++
.cpp
62
23.693548
57
0.610351
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
10,237
sort_a_linked_list.cpp
OpenGenus_cosmos/code/data_structures/src/Linked_List/sort_a_linked_list.cpp
#include <iostream> using namespace std; typedef struct node Node; struct node { int data; Node *next_pointer; }; Node *create_node(); void insert_in_beginning(int value, Node **start); void insert_in_ending(int value, Node **start); void insert_in_specific_position(int value, Node *start); void linked_list_...
2,261
C++
.cpp
75
25.533333
70
0.625287
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
10,238
linked_list_palindrome.cpp
OpenGenus_cosmos/code/data_structures/src/Linked_List/linked_list_palindrome.cpp
// Check if the liked list is palindrome or not bool checkpalindrome(vector<int> arr){ int s = 0; int e = arr.size() -1; while(s<=e){ if(arr[s] != arr[e]){ return 0; } s++; e--; } return 1; } bool isPalin...
551
C++
.cpp
23
14.782609
48
0.442966
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
10,239
Count_nodes_of_Linked_List.cpp
OpenGenus_cosmos/code/data_structures/src/Linked_List/Count_nodes_of_Linked_List.cpp
// count the Number of Nodes n a Linked List int getCount(struct Node* head){ int cnt=0; Node *curr = head; while(curr != NULL){ cnt++; curr = curr->next; } return cnt; }
230
C++
.cpp
10
16.1
44
0.525346
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
10,240
reverse_linked_list_in_k_groups.cpp
OpenGenus_cosmos/code/data_structures/src/Linked_List/reverse_linked_list_in_k_groups.cpp
#include <iostream> using namespace std; // Definition for singly-linked list. struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(nullptr) {} }; class Solution { public: ListNode* reverseKGroup(ListNode* head, int k) { if (k <= 1 || !head) { return head; // ...
2,543
C++
.cpp
82
22.768293
78
0.542366
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
10,242
deleting_a_node.cpp
OpenGenus_cosmos/code/data_structures/src/Linked_List/deleting_a_node.cpp
#include <iostream> using namespace std; typedef struct node Node; struct node { int data; Node *next_pointer; }; Node *create_node(); void insert_in_ending(int value, Node **start); void insert_in_specific_position(int value, Node *start); void linked_list_print(Node *start); void deleting(int value, Node *...
2,178
C++
.cpp
77
23.441558
76
0.606989
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
10,243
Remove_duplicates_in_unsorted_linked_list.cpp
OpenGenus_cosmos/code/data_structures/src/Linked_List/Remove_duplicates_in_unsorted_linked_list.cpp
// Removethe duplicate elements in the linked list Node * removeDuplicates( Node *head) { Node* temp1=head; Node* temp2=head->next; unordered_set<int> s; while(temp2!=NULL) { s.insert(temp1->data); if(s.find(temp2->data)!=s.end()) { ...
635
C++
.cpp
24
14.25
50
0.43686
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
10,244
pairwise_swap_on_linked_list.cpp
OpenGenus_cosmos/code/data_structures/src/Linked_List/pairwise_swap_on_linked_list.cpp
// Swaps the data of linked list in pairs struct Node* pairwise_swap(struct Node* head) { Node* curr = head; while(curr!= NULL && curr->next != NULL){ swap(curr->data, curr->next->data); curr = curr->next->next; } return head; }
266
C++
.cpp
10
22
46
0.611111
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
10,245
reversing_linkedlist.cpp
OpenGenus_cosmos/code/data_structures/src/Linked_List/reversing_linkedlist.cpp
#include<bits/stdc++.h> using namespace std; struct Node{ int value; struct Node* next; Node(int value){ this->value=value; next=NULL; } }; struct LL{ Node* head; LL(){ head=NULL; } void reverse(){ Node* curr=head; Node* prev=NULL,*next=NULL; while(curr!=NULL){ next=curr->next; curr->n...
805
C++
.cpp
53
12.830189
40
0.65287
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
10,247
support_vector_machine.cpp
OpenGenus_cosmos/code/artificial_intelligence/src/support_vector_machine/support_vector_machine.cpp
#include <iostream> #include <vector> using namespace std; class SVMModel { public: vector<double> weights; double bias; SVMModel(int numFeatures) : weights(numFeatures, 0), bias(0) {} }; class SVM { private: static const double LEARNING_RATE; static const int MAX_ITERATIONS; public: static ...
2,070
C++
.cpp
56
28.910714
96
0.578289
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
10,248
k_nearest_neighbours.cpp
OpenGenus_cosmos/code/artificial_intelligence/src/k_nearest_neighbours/k_nearest_neighbours.cpp
// Part of Cosmos by OpenGenus #include <cmath> #include <iomanip> #include <iostream> #include <vector> struct Point { float w, x, y, z; // w, x, y and z are the values of the dataset float distance; // distance will store the distance of dataset point from // the unknown test point ...
4,070
C++
.cpp
116
29.146552
79
0.485634
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,250
main.cpp
OpenGenus_cosmos/code/artificial_intelligence/src/image_processing/canny/main.cpp
// // main.cpp // Canny Edge Detector // #include <iostream> #include <string.h> #define _USE_MATH_DEFINES #include <cmath> #include <vector> #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include "canny.h" int main() { std::string filePath = "lena.jpg"; //Filepath of input imag...
364
C++
.cpp
18
18.333333
64
0.722222
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,256
salesman.cpp
OpenGenus_cosmos/code/artificial_intelligence/src/tsp/salesman.cpp
#include <climits> #include <vector> #include <iostream> #include <algorithm> #include <cmath> using namespace std; #define fio std::ios_base::sync_with_stdio(false); cin.tie(NULL) #define ll long long #define vi vector<int> #define all(a) a.begin(...
5,356
C++
.cpp
159
26.90566
92
0.563342
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,257
naive_bayes.cpp
OpenGenus_cosmos/code/artificial_intelligence/src/naive_bayes/naive_bayes.cpp
#include <iostream> #include <string> #include <vector> #include <random> #include <set> #include <map> #include <unordered_map> using namespace std; unordered_map<string, int> count(pair<vector<string>, vector<string>> &feature, vector<string> &position) { unordered_map<string, int> d; for (int i = 0; i < 2;...
4,940
C++
.cpp
174
19.885057
142
0.468143
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
10,260
addition_using_bits_test.cpp
OpenGenus_cosmos/code/bit_manipulation/test/addition_using_bits_test.cpp
#include <assert.h> #include "./../src/addition_using_bits/addition_using_bits.cpp" // Part of Cosmos by OpenGenus Foundation int main() { // Testing bitwiseAddition function assert(bitwiseAddition(10, 5) == (10 + 5)); assert(bitwiseAddition(5, 10) == (5 + 10)); assert(bitwiseAddition(0, 1) == (0 + 1))...
1,018
C++
.cpp
23
40.217391
63
0.628399
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,261
xor_swap.cpp
OpenGenus_cosmos/code/bit_manipulation/src/xor_swap/xor_swap.cpp
#include <iostream> using namespace std; void xor_swap(int * a, int * b) { *a = *a ^ *b; *b = *a ^ *b; *a = *a ^ *b; } int main() { int a = 10, b = 15; cout << "Before swapping: A = " << a << " and B = " << b << "\n"; xor_swap(&a, &b); cout << "After swapping: A = " << a << " and B = " ...
351
C++
.cpp
16
18.5625
69
0.43465
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,262
first_set_bit.cpp
OpenGenus_cosmos/code/bit_manipulation/src/first_set_bit/first_set_bit.cpp
// Part of Cosmos (OpenGenus) #include <bits/stdc++.h> using namespace std; int returnFirstSetBit(int n) { if(n == 0) return 0; int position = 1; int m = 1; while (!(n & m)) { m = m << 1; position++; } return (1 << (position - 1)); } int main() { int n; cin...
386
C++
.cpp
23
12.652174
41
0.523677
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,265
thrice_unique_number.cpp
OpenGenus_cosmos/code/bit_manipulation/src/thrice_unique_number/thrice_unique_number.cpp
/* * * Part of Cosmos by OpenGenus Foundation * Find unique number in an array where every element occours thrice except one.Find that unique Number * */ #include <iostream> using namespace std; int n; int a[104] = {1, 1, 1, 3, 3, 2, 3}; int f() { int count[65] = {0}; for (int i = 0; i < n; i++) { ...
732
C++
.cpp
39
13.846154
103
0.464646
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,267
subset_mask_generator.cpp
OpenGenus_cosmos/code/bit_manipulation/src/subset_generation/subset_mask_generator.cpp
#include <iostream> typedef unsigned long long ll; //Loops over all subsets of the bits in to_mask. Except to_mask itself //For test input (111) //110 //101 //100 //010 //001 //000 void generate_masks(ll to_mask) { for (int mask = to_mask; mask;) { --mask &= to_mask; std::cout << mask << std:...
374
C++
.cpp
22
14.363636
70
0.635057
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,270
maximum_xor_value.cpp
OpenGenus_cosmos/code/bit_manipulation/src/maximum_xor_value/maximum_xor_value.cpp
#include <iostream> using namespace std; int maxXor(int l, int r) { int num = l ^ r, max = 0; while (num > 0) { max <<= 1; max |= 1; num >>= 1; } return max; } int main() { int res, _l, _r; cin >> _l; cin >> _r; res = maxXor(_l, _r); cout << res; ...
332
C++
.cpp
22
10.772727
29
0.455738
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,271
addition_using_bits.cpp
OpenGenus_cosmos/code/bit_manipulation/src/addition_using_bits/addition_using_bits.cpp
#include <iostream> // Part of Cosmos by OpenGenus Foundation int bitwiseAddition(int n, int m) { while (m != 0) { int carry = n & m; // Variable carry keeps track of bits that carry over n = n ^ m; // This adds up the individual bits m = carry << 1; // Shift carry over bi...
556
C++
.cpp
22
20.545455
81
0.580827
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,272
hamming_distance.cpp
OpenGenus_cosmos/code/bit_manipulation/src/hamming_distance/hamming_distance.cpp
/* * * Part of Cosmos by OpenGenus Foundation * * The Hamming distance between two integers is the number of positions * at which the corresponding bits are different. * * Given two integers x and y, calculate the Hamming distance. */ int hammingDistance(int x, int y) { int temp = x ^ y; int count = 0;...
482
C++
.cpp
21
19.428571
71
0.658696
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,275
power_of_4.cpp
OpenGenus_cosmos/code/bit_manipulation/src/power_of_4/power_of_4.cpp
// part of cosmos repository #include <iostream> using namespace std ; // Function to check if the number is a power of 4 bool isPowerOfFour(int num) { // First if the num is +ve , // then it is a power of 2 , // then (4^n - 1) % 3 == 0 // another proof: // (1) 4^n - 1 = (2^n + ...
819
C++
.cpp
21
33.761905
211
0.560203
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
10,278
lonely_integer.cpp
OpenGenus_cosmos/code/bit_manipulation/src/lonely_integer/lonely_integer.cpp
/* * Part of Cosmos by OpenGenus Foundation * The Lonely Integer Problem * Given an array in which all the no. are present twice except one, find that lonely integer. */ #include <iostream> using namespace std; int LonelyInteger(int *a, int n) { int lonely = 0; //finds the xor sum of the array. for (i...
510
C++
.cpp
21
21.238095
94
0.631687
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,279
sum_equals_xor.cpp
OpenGenus_cosmos/code/bit_manipulation/src/sum_equals_xor/sum_equals_xor.cpp
/* * Counts the number of values between 0 and a given number that satisfy the condition x+n = x^n (0<=x<=n) */ #include <iostream> #include <cmath> using namespace std; long solve(long n) { long c = 0; while (n) { c += n % 2 ? 0 : 1; n /= 2; } c = pow(2, c); return c; } int ...
427
C++
.cpp
25
13.52
106
0.5475
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,281
count_set_bits_lookup_table.cpp
OpenGenus_cosmos/code/bit_manipulation/src/count_set_bits/count_set_bits_lookup_table.cpp
// Part of Cosmos by OpenGenus Foundation #include <vector> #include <iostream> #include <sstream> #include <algorithm> #include <random> #include <chrono> // I'm not crazy, I generated this lookup table with a Ruby script ;) const unsigned bits[] = { 0, // 0 1, // 1 1, // 10 2, // 11 1, // 100 ...
7,545
C++
.cpp
343
16.965015
93
0.524042
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
true
false
10,282
brian_kernighan_algorithm.cpp
OpenGenus_cosmos/code/bit_manipulation/src/count_set_bits/brian_kernighan_algo/brian_kernighan_algorithm.cpp
//Brian Kernighan’s Algorithm. This programme uses O(logn) to count set bits. #include <iostream> int countSetBits(int n) { // base case if (n == 0) { return 0; } else { // if last bit is set, add 1 else add 0 return (n & 1) + countSetBits(n >> 1); } } int main() { int n; std::cout << "...
413
C++
.cpp
23
15.347826
79
0.620779
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,283
twice_unique_number.cpp
OpenGenus_cosmos/code/bit_manipulation/src/twice_unique_number/twice_unique_number.cpp
#include <iostream> using namespace std; //Part of Cosmos by OpenGenus Foundation void findUnique2(int *a, int n) { int res = 0; for (int i = 0; i < n; i++) res = res ^ a[i]; // find the rightmost set bit in res int i = 0; int temp = res; while (temp > 0) { if (temp & 1) ...
768
C++
.cpp
35
16.685714
40
0.491713
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,286
bit_division.cpp
OpenGenus_cosmos/code/bit_manipulation/src/bit_division/bit_division.cpp
/* Problem Statement : Divide two numbers using bitwise operators (i.e without using arithmetic operators) */ #include <iostream> using namespace std; int bitDivision(int numerator, int denominator) { int current = 1; int answer = 0; if (denominator > numerator) return 0; else if (denominat...
1,154
C++
.cpp
48
18.958333
98
0.623626
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,288
worst_fit.cpp
OpenGenus_cosmos/code/operating_system/src/memory_management/partitioned_allocation/worst_fit.cpp
/* * Part of Cosmos by OpenGenus Foundation */ #include <iostream> #include <vector> #include <queue> #include <unistd.h> class process { public: size_t size; pid_t id; }; class memory { public: size_t size; pid_t id; std::queue<process> allocated_processess; void push(const proces...
4,841
C++
.cpp
143
25.153846
138
0.526896
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
10,289
next_fit.cpp
OpenGenus_cosmos/code/operating_system/src/memory_management/partitioned_allocation/next_fit.cpp
/* * Part of Cosmos by OpenGenus Foundation */ #include <iostream> #include <vector> #include <queue> #include <unistd.h> class process { public: size_t size; pid_t id; }; class memory { public: size_t size; pid_t id; std::queue<process> allocated_processess; void push(const proces...
4,694
C++
.cpp
138
25.550725
180
0.53109
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
10,290
best_fit.cpp
OpenGenus_cosmos/code/operating_system/src/memory_management/partitioned_allocation/best_fit.cpp
/* * Part of Cosmos by OpenGenus Foundation */ #include <iostream> #include <vector> #include <queue> #include <unistd.h> class process { public: size_t size; pid_t id; }; class memory { public: size_t size; pid_t id; std::queue<process> allocated_processess; void push(const proces...
5,040
C++
.cpp
148
25.128378
138
0.524362
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
10,291
first_fit.cpp
OpenGenus_cosmos/code/operating_system/src/memory_management/partitioned_allocation/first_fit.cpp
/* * Part of Cosmos by OpenGenus Foundation */ #include <iostream> #include <vector> #include <queue> #include <unistd.h> class process { public: size_t size; pid_t id; }; class memory { public: size_t size; pid_t id; std::queue<process> allocated_processess; void push(const proces...
4,576
C++
.cpp
138
24.695652
108
0.525292
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
10,292
lru.cpp
OpenGenus_cosmos/code/operating_system/src/memory_management/least_recently_used/lru.cpp
/* Least Recently Used Page Replacement Algorithm implemented using a stack */ #include <bits/stdc++.h> using namespace std; /* A function that finds and returns the index of the current page in the page table If it is not present it would return -1 */ int find(int current_page,vector<int>& page_table){ for(in...
4,221
C++
.cpp
99
36.070707
130
0.625307
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
false
false
true
false
true
false
10,293
shortest_seek_time_first.cpp
OpenGenus_cosmos/code/operating_system/src/scheduling/shortest_seek_time_first/shortest_seek_time_first.cpp
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n; cout << "Enter number of process: "; cin >> n; vector<int> burstTime(n); // Array of burst times of processes cout << "Enter Burst time: \n"; for (int i = 0; i < n; i++) { cout << "P...
1,513
C++
.cpp
44
28.863636
97
0.572988
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,294
job_sequencing.cpp
OpenGenus_cosmos/code/operating_system/src/scheduling/job_sequencing/job_sequencing.cpp
#include <iostream> #include <vector> #include <algorithm> using namespace std; struct Job { char id; int deadline; int profit; }; bool jobComparator(const Job &a, const Job &b) { return a.profit > b.profit; } void jobSequence(vector<Job> &jobs) { int n = jobs.size(); sort(jobs.begin(),...
1,260
C++
.cpp
49
18.734694
75
0.524014
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
10,295
round_robin_scheduling.cpp
OpenGenus_cosmos/code/operating_system/src/scheduling/round_robin_scheduling/round_robin_scheduling.cpp
#include <iostream> #include <vector> using namespace std; int main() { int flag = 0, timeQuantum; int n, remain; int waitTime = 0; cout << "Enter Total Process:\n"; cin >> n; remain = n; vector<int> arrivalTime(n); vector<int> burstTime(n); vector<int> remainingTime(n); for (i...
1,568
C++
.cpp
55
21
76
0.523494
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,298
banker_safety.cpp
OpenGenus_cosmos/code/operating_system/src/deadlocks/bankers_algorithm/banker_safety.cpp
// Part of Cosmos by OpenGenus Foundation. // Banker's Algorithm: Safety Algorithm #include <cstdio> int main() { // Initialize int available[10], allocation [10][10], maximum[10][10]; int noOfProcesses, noOfResources, need[10][10]; int work[10], finish[10] = {0}, i; //Inputs pri...
2,916
C++
.cpp
86
22.860465
69
0.463371
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,300
producer_consumer.cpp
OpenGenus_cosmos/code/operating_system/src/concurrency/producer_consumer/producer_consumer.cpp
#include <iostream> #include <ctime> #include <cstdlib> #include <thread> #include <mutex> #include <vector> #include <condition_variable> const int N = 100; std::vector<int> shared_buffer(N); std::mutex lock_buffer; std::condition_variable cond; void producer() { while (true) { std::unique_lock<std:...
1,406
C++
.cpp
50
22.36
66
0.59584
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,301
test_subset_sum.cpp
OpenGenus_cosmos/code/dynamic_programming/test/subset_sum/test_subset_sum.cpp
/* Part of Cosmos by OpenGenus Foundation */ #ifndef SUBSET_SUM_TEST #define SUBSET_SUM_TEST #include <iostream> #include <list> #include <cassert> #include "../../src/subset_sum/subset_sum.cpp" int main() { using namespace std; int vz[0]; int v[] = {1, 2, 15, 8, 5}; list<int> l{1, 2, 15, 8, 5}; ...
682
C++
.cpp
23
26.434783
69
0.627301
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,302
longest_repeating_subsequence_test.cpp
OpenGenus_cosmos/code/dynamic_programming/test/longest_repeating_subsequence/longest_repeating_subsequence_test.cpp
/* Part of Cosmos by OpenGenus Foundation */ #include <iostream> #include <string> #include <cassert> #include "../../src/longest_repeating_subsequence/longest_repeating_subsequence.cpp" int main() { using namespace std; std::string s1 = "aab"; assert(longestRepeatingSubsequence(s1) == 1); }
308
C++
.cpp
11
25.636364
84
0.734694
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,307
longest_palindromic_substring.cpp
OpenGenus_cosmos/code/dynamic_programming/src/longest_palindromic_substring/longest_palindromic_substring.cpp
/* Part of Cosmos by OpenGenus Foundation */ #include <iostream> #include <string> using namespace std; int longestPalSubstr(string str) { int n = str.size(); bool ispal[n][n]; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) ispal[i][j] = false; // All substrings of len...
1,298
C++
.cpp
45
21.311111
83
0.493966
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,308
friends_pairing.cpp
OpenGenus_cosmos/code/dynamic_programming/src/friends_pairing/friends_pairing.cpp
// Dynamic Programming solution to friends pairing problem in C++ // Contributed by Santosh Vasisht // Given n friends, each one can remain single or can be paired up with some other friend. // Each friend can be paired only once. // Find out the total number of ways in which friends can remain single or can be p...
807
C++
.cpp
27
24.555556
93
0.624837
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,309
unique_paths.cpp
OpenGenus_cosmos/code/dynamic_programming/src/unique_paths/unique_paths.cpp
// Part of Cosmos by OpenGenus Foundation #include<bits/stdc++.h> using namespace std; //Tabulation method // Time complexity: O(m*n) int uniquePaths(int m, int n) { int dp[m][n]; for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ if(i==0 || j==0) ...
1,618
C++
.cpp
55
20.836364
73
0.466278
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
10,311
numeric_keypad_problem.cpp
OpenGenus_cosmos/code/dynamic_programming/src/numeric_keypad_problem/numeric_keypad_problem.cpp
#include <bits/stdc++.h> using namespace std; vector<vector<int>> nums; void populate() { nums.resize(10); nums[0].push_back(0); nums[0].push_back(8); nums[1].push_back(1); nums[1].push_back(2); nums[1].push_back(4); nums[2].push_back(1); nums[2].push_back(2); nums[2].push_back(3);...
1,744
C++
.cpp
73
17.479452
64
0.470624
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,314
Longest_Common_Substring_rename.cpp
OpenGenus_cosmos/code/dynamic_programming/src/longest_common_substring/Longest_Common_Substring_rename.cpp
// Space Complexity: O(n) // Time Complexity: O(m*n) #include <iostream> #include <vector> std::string LongestCommonSubstring(std::string string1, std::string string2) { std::string temp; // longest string is string1 and the smallest string is string2 if (string2.size() > string1.size()) { tem...
2,213
C++
.cpp
67
25.597015
119
0.541452
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,315
longest_common_substring_2.cpp
OpenGenus_cosmos/code/dynamic_programming/src/longest_common_substring/longest_common_substring_2.cpp
/* * Part of Cosmos by OpenGenus Foundation * finding longest common substring between two strings by dynamic programming */ #include <string> #include <iostream> #include <cstring> using namespace std; int longestCommonSubString(string s1, string s2) { int T[600][600]; //array lengt...
1,237
C++
.cpp
39
26.282051
117
0.553691
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,316
Solution.cpp
OpenGenus_cosmos/code/dynamic_programming/src/Count_Subsequence_Having_Product_Less_Than_K/Solution.cpp
#include<iostream> #include<cstring> using namespace std; int dp[10005][10005]; int helper(int a[], int n, int k , int product) { // base case if(n==0) { return 0; } if(product > k) { return 0; } if(dp[n][product] !=-1) { return dp[n][product]; } int...
939
C++
.cpp
52
13.230769
63
0.52662
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
10,318
maximum_product_subarray.cpp
OpenGenus_cosmos/code/dynamic_programming/src/maximum_product_subarray/maximum_product_subarray.cpp
/* Read Problem Description Here - https://leetcode.com/problems/maximum-product-subarray/ Test Cases - Input: [2,3,-2,4] Output: 6 Input: [-2,0,-1] Output: 0 */ #include<iostream> #include<climits> using namespace std; int maxProdSub(int* arr, int n) { int positiveProd = 1, negativeProd = 1; int ans = INT_MI...
1,128
C++
.cpp
42
24.452381
87
0.684407
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,321
longest_increasing_subsequence.cpp
OpenGenus_cosmos/code/dynamic_programming/src/longest_increasing_subsequence/longest_increasing_subsequence.cpp
/* Part of Cosmos by OpenGenus Foundation */ #include <iostream> #include <vector> #include <algorithm> using namespace std; // Bottom-up O(n^2) approach int lis(int v[], int n) { int dp[n], ans = 0; for (int i = 0; i < n; ++i) { dp[i] = 1; for (int j = 0; j < i; ++j) if (v[j...
1,025
C++
.cpp
41
19.926829
79
0.507677
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,323
best_time_to_sell_stock_II.cpp
OpenGenus_cosmos/code/dynamic_programming/src/best_time_to_sell_stock_II/best_time_to_sell_stock_II.cpp
// You are given an integer array prices where prices[i] is the price of a given stock on the ith day. // On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day. // Find and return the max...
887
C++
.cpp
22
35.363636
188
0.607226
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,325
Printing_longest_common_subsequence.cpp
OpenGenus_cosmos/code/dynamic_programming/src/longest_common_subsequence/Printing_longest_common_subsequence.cpp
//Printing longest common sub-sequence #include <iostream> #include<bits/stdc++.h> using namespace std; //Helper function to print LCA string LCA(string s1,string s2){ int m=s1.length(); int n=s2.length(); int dp[m+1][n+1]; //initialising first row for(int i=0; i<=m; i++){ ...
1,239
C++
.cpp
51
15.647059
53
0.449606
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
10,327
min_skips.cpp
OpenGenus_cosmos/code/dynamic_programming/src/min_rests_skipped_to_reach_on_time/min_skips.cpp
// Part of Cosmos by OpenGenus Foundation #include<bits/stdc++.h> int minSkips(vector<int>& dist, int speed, int reachTime) { /* dist: array containing lengths of roads speed: the constant speed of travel reachTime: the time before/at which you have to reach the office. You start at time=0. ...
2,028
C++
.cpp
55
28.272727
117
0.570231
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,332
trapping_rain_water.cpp
OpenGenus_cosmos/code/dynamic_programming/src/trapping_rain_water/trapping_rain_water.cpp
/* Problem Description - https://leetcode.com/problems/trapping-rain-water/description/ Test Cases - Input: [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6 */ #include<iostream> #include<vector> using namespace std; int trapRainWater(int *arr, int n) { if(n < 3) return 0; vector<pair<int,int>>indexes; ...
1,750
C++
.cpp
60
20.666667
95
0.528338
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,333
subset_sum.cpp
OpenGenus_cosmos/code/dynamic_programming/src/subset_sum/subset_sum.cpp
/* Part of Cosmos by OpenGenus Foundation */ #ifndef SUBSET_SUM #define SUBSET_SUM #include <iterator> /* * Check whether is possible to * get value sum from a subset * of the [begin:end) */ // complexity: time: O(sum * n), space: O(sum) template<typename _BidirectionalIter, typename _ValueType = typ...
2,055
C++
.cpp
69
25
93
0.518444
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,334
longest_repeating_subsequence.cpp
OpenGenus_cosmos/code/dynamic_programming/src/longest_repeating_subsequence/longest_repeating_subsequence.cpp
// Part of Cosmos by OpenGenus Foundation //dynamic programming || Longest repeating subsequence #include <iostream> #include <string> #include <vector> int longestRepeatingSubsequence(std::string s) { int n = s.size(); // Obtaining the length of the string std::vector<std::vector<int>>dp(n + 1, std::vector...
749
C++
.cpp
21
29.47619
75
0.556017
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,335
longest_common_subsequence_substring_problem.cpp
OpenGenus_cosmos/code/dynamic_programming/src/longest_common_subsequence_substring/longest_common_subsequence_substring_problem.cpp
// This is a new dynamic programming problem. I have published a research paper // about this problem under the guidance of Professor Rao Li (University of // South Carolina, Aiken) along with my friend. The paper has been accepted by // the Journal of Mathematics and Informatics.The problem is a variation of the // st...
2,198
C++
.cpp
46
39.847826
121
0.563755
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
10,336
newman_conway_dp.cpp
OpenGenus_cosmos/code/dynamic_programming/src/newman_conway/newman_conway_dp.cpp
#include <iostream> class NewmanConwaySequence { public: NewmanConwaySequence(unsigned int n) : n(n) {} void calculateNewmanConwaySequenceTermDP(bool flag); private: unsigned int n; }; void NewmanConwaySequence::calculateNewmanConwaySequenceTermDP(bool flag) { if(flag == true) { unsigned i...
1,605
C++
.cpp
53
24.245283
144
0.579935
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,338
weighted_job_scheduling.cpp
OpenGenus_cosmos/code/dynamic_programming/src/weighted_job_scheduling/weighted_job_scheduling.cpp
/* * Part of Cosmos by OpenGenus Foundation * C++ program for weighted job scheduling using Dynamic Programming and Binary Search */ #include <vector> #include <iostream> #include <algorithm> using namespace std; // A job has start time, finish time and profit. //index is used to store its given position as we wi...
3,509
C++
.cpp
108
26.759259
113
0.589873
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,339
mincoinchange.cpp
OpenGenus_cosmos/code/dynamic_programming/src/coin_change/mincoinchange.cpp
// dynamic programming | coin change | C++ // Part of Cosmos by OpenGenus Foundation // A C++ program to find the minimum number of coins required from the list of given coins to make a given value // Given a value V, if we want to make change for V cents, and we have infinite supply of each of C = { C1, C2, .. , Cm} ...
1,362
C++
.cpp
34
32.264706
120
0.511698
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,340
coinchange.cpp
OpenGenus_cosmos/code/dynamic_programming/src/coin_change/coinchange.cpp
// dynamic programming | coin change | C++ // Part of Cosmos by OpenGenus Foundation #include <iostream> #include <vector> using namespace std; int coinWays(int amt, vector<int>& coins) { // init the dp table vector<int> dp(amt+1, 0); int n = coins.size(); dp[0] = 1; // base case for (int j = 0; j...
780
C++
.cpp
25
25.6
57
0.542553
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
10,341
boolean_parenthesization.cpp
OpenGenus_cosmos/code/dynamic_programming/src/boolean_parenthesization/boolean_parenthesization.cpp
// dynamic programming | boolean parenthesization | C++ // Part of Cosmos by OpenGenus Foundation #include <iostream> #include <cstring> // Dynamic programming implementation of the boolean // parenthesization problem using 'T' and 'F' as characters // and '&', '|' and '^' as the operators int boolean_parenthesizatio...
1,734
C++
.cpp
60
18.8
77
0.334135
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,342
wildcard.cpp
OpenGenus_cosmos/code/dynamic_programming/src/wildcard_matching/wildcard.cpp
//Part of Cosmos by OpenGenus Foundation bool isMatch(string s, string p) { /* s is the string p is the pattern containing '?' and '*' apart from normal characters. */ int m=s.length(), n=p.length(); bool dp[m+1][n+1]; // Initialise each cell of the dp table with false for(int...
1,427
C++
.cpp
42
22.880952
75
0.431835
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,343
longest_common_increasing_subsequence.cpp
OpenGenus_cosmos/code/dynamic_programming/src/longest_common_increasing_subsequence/longest_common_increasing_subsequence.cpp
/* Part of Cosmos by OpenGenus Foundation */ #include <iostream> using namespace std; int main() { int n, m, a[502] = {}, b[502] = {}, d[502][502] = {}, z = 0; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= m; i++) cin >> b[i]; for (int i = 1; i <= n;...
658
C++
.cpp
26
18.076923
64
0.336508
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,344
solution.cpp
OpenGenus_cosmos/code/dynamic_programming/src/Climbing Stairs/solution.cpp
// recursion // TC - O(2^n) , SC - O(1) class Solution { public: int func(int n){ if(n <= 1){ return n; } return func(n-1) + func(n-2); } int climbStairs(int n) { return func(n+1); } }; // memoisation // TC - O(n), SC - O(n) + O(n) class Solution { pu...
1,222
C++
.cpp
59
14.372881
53
0.441921
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
10,345
bitmask_dp_prob#1.cpp
OpenGenus_cosmos/code/dynamic_programming/src/bitmask_dp/bitmask_dp_prob#1.cpp
/* Part of Cosmos by OpenGenus Foundation */ /* Problem Statement: There are k types ties, uniquely identified by id from 1 to k. n ppl are invited to a party. Each of them have a collection of ties. To look unique, they decide that none of them will wear same type of tie. Count the number of ways th...
1,846
C++
.cpp
56
27.678571
94
0.634615
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
10,347
maximum_weight_independent_set_of_path_graph.cpp
OpenGenus_cosmos/code/dynamic_programming/src/maximum_weight_independent_set_of_path_graph/maximum_weight_independent_set_of_path_graph.cpp
#include <iostream> using namespace std; int main() { int n; cout << "Enter the number of vertices of the path graph : "; cin >> n; int g[n]; cout << "Enter the vertices of the graph : "; for (int i = 0; i < n; i++) cin >> g[i]; int sol[n + 1]; sol[0] = 0; sol[1] = g[0]; ...
748
C++
.cpp
31
18.193548
79
0.435754
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,348
shortest_common_supersequence.cpp
OpenGenus_cosmos/code/dynamic_programming/src/shortest_common_supersequence/shortest_common_supersequence.cpp
/* Part of Cosmos by OpenGenus Foundation */ #include <iostream> #include <cstring> using namespace std; const int MAX = 1010; int memo[MAX][MAX]; // used for memoization // Function to find length of shortest common supersequence // between sequences X[0..m-1] and Y[0..n-1] int SCSLength(string& X, string& Y, int...
1,289
C++
.cpp
39
28.051282
71
0.585149
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,350
no_consec_1.cpp
OpenGenus_cosmos/code/dynamic_programming/src/no_consec_ones/no_consec_1.cpp
#include <iostream> #include <vector> using namespace std; int countNonConsecutiveOnes(int n) { vector<int> endingZero(n), endingOne(n); endingZero[0] = endingOne[0] = 1; for (int i = 1; i < n; i++) { endingZero[i] = endingZero[i - 1] + endingOne[i - 1]; endingOne[i] = endingZero[i - 1]...
712
C++
.cpp
24
25.541667
80
0.645773
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,351
longestbitonicseq.cpp
OpenGenus_cosmos/code/dynamic_programming/src/longest_bitonic_sequence/longestbitonicseq.cpp
#include <iostream> #include <vector> using namespace std; // Part of Cosmos by OpenGenus Foundation int lBitconSeq(vector<int> v, int n) { vector<int> lis, lds; // lis stands for longest increasing subsequence and lds stands for longest decreasing subsequence if (n == 0) return 0; // in case tha ar...
1,434
C++
.cpp
45
25.8
128
0.509025
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,354
Knapsack_DP_all3tech.cpp
OpenGenus_cosmos/code/dynamic_programming/src/knapsack/Knapsack_DP_all3tech.cpp
///////////////////////////////RECURSIVE APPROACH//////////////////////////////////////////////////////// #include <iostream> #include <bits/stdc++.h> using namespace std; int knapSack(int W,int *wt,int *val,int n) { if(n==0||W==0) { return 0; } if(wt[n]<=W) { return max(val[n]+knapS...
4,798
C++
.cpp
153
25.078431
108
0.5
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
10,356
die_simulation.cpp
OpenGenus_cosmos/code/dynamic_programming/src/die_simulation/die_simulation.cpp
// Part of OpenGenus cosmos #define ll long long const int mod = 1e9+7; // Function to add in presence of modulus. We find the modded answer // as the number of sequences becomes exponential fairly quickly ll addmod(ll a, ll b) { return (a%mod + b%mod) % mod; } ll dieSimulator_util(int dex, vector<int>& maxrol...
2,431
C++
.cpp
63
31.587302
120
0.620881
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,358
palindrome_partition.cpp
OpenGenus_cosmos/code/dynamic_programming/src/palindrome_partition/palindrome_partition.cpp
/* Part of Cosmos by OpenGenus Foundation */ #include <iostream> #include <cstring> #include <string> #define MAX 1010 using namespace std; int isPal[MAX][MAX], memo[MAX]; /* * Checks if s[i...j] is palindrome * in a top-down fashion. Result is * stored in isPal matrix. * Time complexity: O(n^2) */ int is...
1,568
C++
.cpp
60
21.95
74
0.556153
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,359
number_of_substrings.cpp
OpenGenus_cosmos/code/dynamic_programming/src/number_of_substring_divisible_by_8_but_not_3/number_of_substrings.cpp
#include <bits/stdc++.h> using namespace std; int no_of_substr(string str, int len, int k) { int count = 0; // iterate over the string for (int i = 0; i < len; i++) { int n = 0; // consider every substring starting from index 'i' for (int j = i; j < len; j++) { ...
746
C++
.cpp
28
19.892857
89
0.49789
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,360
maximum_sum_increasing_subsequence.cpp
OpenGenus_cosmos/code/dynamic_programming/src/maximum_sum_increasing_subsequence/maximum_sum_increasing_subsequence.cpp
#include <bits/stdc++.h> #include <vector> using namespace std; int maxSum(int arr[], int n){ int i, j, max = 0; vector<int> dp(n); for (i = 0; i < n; i++){ dp[i] = arr[i]; } for (i = 1; i < n; i++ ){ for (j = 0; j < i; j++ ){ if (arr[i] > arr[j] && dp[i] < dp[j] + arr[i]){ dp[i] = dp[j] + arr[i]; ...
621
C++
.cpp
31
17.645161
84
0.499142
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
10,362
test_sort.cpp
OpenGenus_cosmos/code/sorting/test/test_sort.cpp
/* * Part of Cosmos by OpenGenus Foundation */ /* * guide * * 1. substitute iterator (col:28) * 2. substitute sort algorithm (col: 70) * 3. run */ #define CATCH_CONFIG_MAIN #ifndef SORT_TEST #define SORT_TEST #include "../../../test/c++/catch.hpp" #include <forward_list> #include <list> #include <deque> #incl...
4,996
C++
.cpp
153
22.006536
90
0.51858
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,365
merge_sort.cpp
OpenGenus_cosmos/code/sorting/src/merge_sort/merge_sort.cpp
/* * Part of Cosmos by OpenGenus Foundation * * merge sort synopsis * * namespace merge_sort_impl { * template<typename _Random_Acccess_Iter> * _Random_Acccess_Iter * advance(_Random_Acccess_Iter it, std::ptrdiff_t n); * } // merge_sort_impl * * template<typename _Random_Acccess_Iter, typename _Compare> * v...
2,157
C++
.cpp
72
26.013889
87
0.671491
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,366
merge_sort_linked_list.cpp
OpenGenus_cosmos/code/sorting/src/merge_sort/merge_sort_linked_list.cpp
/* Here ListNode is class containing 'next' for next node, and value for value at node. */ ListNode* mergeSort(ListNode* head) { if (head==NULL||head->next==NULL) { return head; } ListNode* mid = getMid(head); ListNode* leftHalf = mergeSort(head); ListNode* rightHalf ...
1,321
C++
.cpp
57
15.385965
69
0.49183
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,367
slow_sort.cpp
OpenGenus_cosmos/code/sorting/src/slow_sort/slow_sort.cpp
/* Part of Cosmos by OpenGenus Foundation */ #pragma once #include <bits/stdc++.h> using namespace std; /** * @brief implement the slowSort Algorithm * * @param vect_ vector to be sorted * @param i ith index of vector * @param j jth index of vector */ void slowSort( vector<int32_t>& vect_, int i, int j ) { ...
1,256
C++
.cpp
57
18.070175
91
0.570826
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,368
comb_sort.cpp
OpenGenus_cosmos/code/sorting/src/comb_sort/comb_sort.cpp
#include <iostream> // Part of Cosmos by OpenGenus Foundation using namespace std; int updateGap(int gap) { gap = (gap * 10) / 13; if (gap < 1) return 1; else return gap; } void combSort(int ar[], int n) { int gap = n; int flag = 0; while (gap > 1 || flag == 1) { ga...
919
C++
.cpp
45
13.844444
55
0.41791
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,371
selection_sort.cpp
OpenGenus_cosmos/code/sorting/src/selection_sort/selection_sort.cpp
/* * Part of Cosmos by OpenGenus Foundation * * selection sort synopsis * * template<typename _Input_Iter, typename _Compare> * void * selectionSort(_Input_Iter begin, _Input_Iter end, _Compare compare); * * template<typename _Input_Iter> * void * selectionSort(_Input_Iter begin, _Input_Iter end); */ #incl...
1,022
C++
.cpp
36
23.583333
78
0.648318
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,375
pancake_sort.cpp
OpenGenus_cosmos/code/sorting/src/pancake_sort/pancake_sort.cpp
#include <iostream> #include <vector> #include <algorithm> template <typename T> void pancakeSort(std::vector<T>& container) { for (int size = container.size(); size > 1; --size) { // Find position of max element int maxPos = std::max_element(container.begin(), container.begin() + size) - cont...
1,443
C++
.cpp
45
27.266667
104
0.637219
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
10,377
sleep_sort.cpp
OpenGenus_cosmos/code/sorting/src/sleep_sort/sleep_sort.cpp
/* Part of Cosmos by OpenGenus Foundation */ #include <iostream> #include <chrono> #include <vector> #include <thread> using namespace std; int main(int argc, char** argv) { vector<thread> threads; for (auto i = 1; i < argc; ++i) { threads.emplace_back( [i, &argv]() { ...
548
C++
.cpp
24
16.791667
57
0.543353
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,378
shell_sort.cpp
OpenGenus_cosmos/code/sorting/src/shell_sort/shell_sort.cpp
#include <iostream> #include <vector> #include <algorithm> using namespace std; // Part of Cosmos by OpenGenus Foundation void shellSort(vector<int> &ar) { size_t j; for (size_t gap = ar.size() / 2; gap > 0; gap /= 2) for (size_t i = gap; i < ar.size(); i++) { int temp = ar[i]; ...
758
C++
.cpp
29
20.655172
65
0.522696
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,382
dutch_national_flag.cpp
OpenGenus_cosmos/code/sorting/src/quick_sort/dutch_national_flag.cpp
/* Part of Cosmos by OpenGenus Foundation */ #include <vector> #include <stdlib.h> #include <iostream> using namespace std; // Dutch National Flag Sort for array items 0,1,2 void flagSort(vector<int> &v) { int lo = 0; int hi = v.size() - 1; int mid = 0; while (mid <= hi) switch (v[mid]) ...
1,131
C++
.cpp
58
14.517241
49
0.501883
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,383
quick_sort.cpp
OpenGenus_cosmos/code/sorting/src/quick_sort/quick_sort.cpp
/* * Part of Cosmos by OpenGenus Foundation * * quick sort synopsis * * namespace quick_sort_impl { * struct quick_sort_tag {}; * struct iterative_quick_sort_tag :quick_sort_tag {}; * struct recursive_quick_sort_tag :quick_sort_tag {}; * * template<typename _Random_Acccess_Iter, typename _Compare> * _Random_...
4,359
C++
.cpp
133
26.601504
93
0.609687
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,384
quick_sort_three_way.cpp
OpenGenus_cosmos/code/sorting/src/quick_sort/quick_sort_three_way.cpp
/* Part of Cosmos by OpenGenus Foundation */ #include <vector> #include <stdlib.h> #include <iostream> using namespace std; /* UTILITY FUNCTIONS */ void swap(int &a, int &b) { int temp = a; a = b; b = temp; } void printarr(vector<int> &v) { for (size_t i = 0; i < v.size(); ++i) cout << v[i] ...
1,614
C++
.cpp
76
16.460526
65
0.516087
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,386
counting_sort.cpp
OpenGenus_cosmos/code/sorting/src/counting_sort/counting_sort.cpp
#include <iostream> #include <vector> #include <climits> /* * Part of Cosmos by OpenGenus Foundation */ using namespace std; void countingSort(vector<int> arr, vector<int>& sortedA) { int m = INT_MIN; for (size_t i = 0; i < arr.size(); i++) if (arr[i] > m) m = arr[i]; int freq[m + 1...
958
C++
.cpp
39
19.333333
65
0.496725
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,388
radix_sort.cpp
OpenGenus_cosmos/code/sorting/src/radix_sort/radix_sort.cpp
/* Part of Cosmos by OpenGenus Foundation */ #include <iostream> #include <vector> #include <cmath> void radix_sort(std::vector<int> &data) { using namespace std; vector<int> tmp[10]; //store 0~9; int max_data = *(max(std::begin(data), std::end(data))); int n = 1; while (n <= max_data) { ...
1,034
C++
.cpp
43
17.372093
74
0.474059
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,390
insertion_sort.cpp
OpenGenus_cosmos/code/sorting/src/insertion_sort/insertion_sort.cpp
/* * Part of Cosmos by OpenGenus Foundation * * insertion sort synopsis * * template<typename _Bidirectional_Iter, typename _Compare> * void * insertionSort(_Bidirectional_Iter begin, _Bidirectional_Iter end, _Compare compare); * * template<typename _Bidirectional_Iter> * void * insertionSort(_Bidirectional_...
1,302
C++
.cpp
44
24.022727
87
0.655227
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,391
median_sort.cpp
OpenGenus_cosmos/code/sorting/src/median_sort/median_sort.cpp
#include <iostream> using namespace std; // Part of Cosmos by OpenGenus Foundation void swap(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } int Partition(int a[], int low, int high) { int pivot, index, i; index = low; pivot = high; for (i = low; i < high; i++) i...
2,275
C++
.cpp
104
13.451923
69
0.357973
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,392
median_sort_fast.cpp
OpenGenus_cosmos/code/sorting/src/median_sort/median_sort_fast.cpp
#include <vector> using namespace std; // Part of Cosmos by OpenGenus Foundation int selectPivotIndex(const vector<int>& A, const int left, const int right) { const int mid = (left + right) / 2; // median of three if (A[left] > A[mid]) // this is really insertion sort { if (A[left] > A[right]) ...
2,157
C++
.cpp
78
20.782051
77
0.523855
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,393
bubble_sort_efficient.cpp
OpenGenus_cosmos/code/sorting/src/bubble_sort/bubble_sort_efficient.cpp
#include <vector> #include <iostream> using namespace std; void bubbleSort(vector<int> &v) { for (size_t i = 0; i < v.size() - 1; i++) { bool isSorted = 1; for (size_t j = 0; j < v.size() - 1 - i; j++) if (v[j] > v[j + 1]) { swap(v[j], v[j + 1]); ...
1,266
C++
.cpp
46
20.217391
134
0.479769
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,394
bubble_sort_recursive.cpp
OpenGenus_cosmos/code/sorting/src/bubble_sort/bubble_sort_recursive.cpp
#include <iostream> #include <vector> // using namespace std; /* This program is the bubble sort implementation in C++ using recursion*/ void bubbleSort(std::vector<int>& v, int n) { // Base Case if (n == 1) return; // sorting in a pass for (int i = 0; i < n - 1; i++){ //comparing the ...
1,135
C++
.cpp
41
22.585366
83
0.550046
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,395
bubble_sort.cpp
OpenGenus_cosmos/code/sorting/src/bubble_sort/bubble_sort.cpp
/* * Part of Cosmos by OpenGenus Foundation * * bubble sort synopsis * * template<typename _Bidirectional_Iter, typename _Compare> * void * bubbleSort(_Bidirectional_Iter begin, _Bidirectional_Iter end, _Compare compare); * * template<typename _Bidirectional_Iter> * void * bubbleSort(_Bidirectional_Iter begi...
1,308
C++
.cpp
45
22.533333
86
0.616057
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,397
cycle_sort.cpp
OpenGenus_cosmos/code/sorting/src/cycle_sort/cycle_sort.cpp
// C++ program to impleament cycle sort #include <iostream> #include <vector> using namespace std; // Part of Cosmos by OpenGenus Foundation // Function sort the array using Cycle sort void cycleSort (vector<int>& arr) { // traverse array elements and put it to on // the right place for (size_t cycle_start ...
1,910
C++
.cpp
60
23.8
78
0.532609
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,398
policy_design.cpp
OpenGenus_cosmos/code/design_pattern/src/policy_based_design/policy_design.cpp
/* * policy_design.cpp * * Created: 3/15/2018 1:14:41 AM * Author: n-is * email: 073bex422.nischal@pcampus.edu.np */ #include <iostream> class Classical { public: Classical(int a) { } void solve() { std::cout << "The differential equation was solved by the classical" << ...
920
C++
.cpp
52
13.711538
79
0.607683
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,400
singleton_pattern.cpp
OpenGenus_cosmos/code/design_pattern/src/OOP_patterns/singleton_pattern/singleton_pattern.cpp
#include <string> #include <iostream> template<typename T> class Singleton { public: static T* GetInstance(); static void destroy(); private: Singleton(Singleton const&) { }; Singleton& operator=(Singleton const&) { }; protected: static T* m_instance; Singleton() { ...
1,239
C++
.cpp
66
15.166667
61
0.628768
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,401
factorial.cpp
OpenGenus_cosmos/code/divide_conquer/src/factorial/factorial.cpp
#include<iostream> int factorial(int num) { if (num == 0 || num == 1) { return num; } return (num * factorial(num - 1)); } int main() { int num; while (true) { std::cin >> num; std::cout << "> " << factorial(num) << '\n'; } return 0; }
291
C++
.cpp
15
14.733333
52
0.481752
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false
10,402
search_in_a_rotated_array.cpp
OpenGenus_cosmos/code/divide_conquer/src/search_in_a_rotated_array/search_in_a_rotated_array.cpp
/* Problem statement: Given a sorted and rotated array A of N distinct elements which is rotated at some point, and given an element K. The task is to find the index of the given element K in the array A. */ #include <iostream> #include <vector> #define ll long long int ll findPivot(std::vector<ll> &v, ll ...
2,170
C++
.cpp
91
16.175824
75
0.44836
OpenGenus/cosmos
13,556
3,669
2,596
GPL-3.0
9/20/2024, 9:26:25 PM (Europe/Amsterdam)
false
false
true
false
false
true
false
false