Skip to content

Instantly share code, notes, and snippets.

@Urmila17
Urmila17 / gist:54f2edfe57c9d733a6d80d57f7633f69
Created July 21, 2021 11:17
count inversion - C++ - gfg
class Solution{
public:
// arr[]: Input Array
// N : Size of the Array arr[]
// Function to count inversions in the array.
void mergesort(long long arr[], long long temp[], long long int l, long long int r, long long int inv){
if(l<r){
long long int mid = (l+r)/2;
inv += mergesort(a,temp, l, mid,inv);
inv += mergesort(a,temp, mid+1, r,inv);
@Urmila17
Urmila17 / gist:9df27e53b7b1f7035889b59d69fdacb0
Created July 21, 2021 10:34
MERGE INTERVALS - C++ - LEETCODE
class Solution {
public:
vector<vector<int>> merge(vector<vector<int>>& intervals) {
sort( intervals.begin(), intervals.end());
vector<vector<int>>ans;
int l = intervals[0][0];
int r = intervals[0][1];
int n = intervals.size();
@Urmila17
Urmila17 / gist:793d4b6a18f3c83c0d7d3fb38d46b90c
Created July 21, 2021 07:27
FInd duplicate number - set -> leetcode - C++
class Solution {
public:
int findDuplicate(vector<int>& v) {
set<int> s;
int n= v.size(); int ans;
for(int i=0; i<n; i++){
if(s.find(v[i]) == s.end()){
s.insert(v[i]);
}
else{
@Urmila17
Urmila17 / gist:c4d18d802e5205509221864b88f9fd2f
Created July 7, 2021 04:52
SPOJ => GREEDY --> GERGOVIA
#include<bits/stdc++.h>
#define int long long int
using namespace std;
void solve(){
int n;
cin>>n;
while(true){
int a[n];
class Solution{
public:
//Function to find starting point where the truck can start to get through
//the complete circle without exhausting its petrol in between.
int tour(petrolPump p[],int n)
{
//Your code here
int fuel=0, st=0, shortage =0,i;
for(i=0; i<n; ++i){
@Urmila17
Urmila17 / gist:2955ff04d83d363ed08f83fd8e6baed6
Created June 10, 2021 10:48
REVERSE A QUEUE USING STACK - C++ - GFG
queue<int> rev(queue<int> q)
{
// add code here.
int n = q.size();
stack<int> s;
while (!q.empty()) {
s.push(q.front());
q.pop();
}
while (!s.empty()) {
@Urmila17
Urmila17 / gist:c153226df32c535d5579bf3e5891ca30
Created June 10, 2021 05:12
STACK USING TWO QUEUES - C++ - GFG
int curr=0; queue<int> q1, q2;
//Function to push an element into stack using two queues.
void QueueStack :: push(int x)
{
// Your Code
q2.push(x);
curr++;
while(!q1.empty()){
q2.push(q1.front());
q1.pop();
@Urmila17
Urmila17 / gist:c65ec0fc9367291898fb30924f147959
Created June 10, 2021 04:17
VALID SUBSTRING - C++ - GFG
class Solution {
public:
int findMaxLen(string A) {
// code here
int left = 0, right = 0, ans = 0, n = A.size();
for(int i = 0; i < n; ++i){
if(A[i] == '(')
++left;
else
@Urmila17
Urmila17 / gist:46506877eb40217caad1130ff550bba6
Created June 9, 2021 12:31
MERGE INTERVALS WITH STACK- C++
class Solution {
public:
vector<vector<int>> merge(vector<vector<int>>& intervals) {
sort(intervals.begin(),intervals.end());
stack<vector<int>> st;
int n = intervals.size();
st.push({ intervals[0].first, intervals[0].second });
for(int i=1; i<n; i++){
@Urmila17
Urmila17 / gist:fb4cbe45291f744bc09f468700ab0d87
Created June 9, 2021 08:59
SORT A STACK USING RECURSION - GFG - C++
void insertatbottom(stack<int>&s, int ele){
if(s.empty() || s.top() <= ele ){
s.push(ele);
return;
}
int temp = s.top();
s.pop();
insertatbottom(s,ele);