Skip to content

Commit

Permalink
new problems solved
Browse files Browse the repository at this point in the history
  • Loading branch information
Mukulguptaiit committed Feb 23, 2025
1 parent a03d89a commit 1ef02cf
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Josephus Problem I.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

#include <bits/stdc++.h>
using namespace std;
class Node{
public:
int data ;
Node* next;
Node* prev;
Node(int val){
data =val;
next= nullptr;
prev = nullptr;
}
};

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin>>n;
Node* temp=new Node(1);
Node* first= temp;
for(int i=2;i<=n;i++){
Node* prev = temp;
temp = new Node(i);
temp->prev = prev;
prev->next = temp;
}
temp->next= first;
first->prev= temp;
first=first->next;
while(first->next->data!=first->data && first->prev->data != first->data){
cout<<first->data<<" ";
Node* prev = first->prev;
prev->next = first->next;
first->next->prev = prev;
first= first->next->next;
}
cout<<first->data;
return 0;
}
24 changes: 24 additions & 0 deletions Josephus Problem II.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#define int long long
#define endl '\n'

signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
indexed_set s;
int n,k; cin>>n>>k;
for (int i = 1; i <= n; i++)
s.insert(i);

int ind = k%n;
while(n--) {
auto y = s.find_by_order(ind);
cout<<*y<<' ';
s.erase(y);
if (n) ind = (ind%n + k)%n;
}
}

0 comments on commit 1ef02cf

Please sign in to comment.