-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/dscnsec/DSC-NSEC-Algorithms…
… into master pulling from upstream
- Loading branch information
Showing
5 changed files
with
431 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* missing_id_chalti.cpp | ||
* | ||
* Description :- | ||
* its a little bit tricky mathematics based question rathar than dsa based. | ||
* Here the if the number is grater than 2 then the ans will be ceil of (n-2)/x+1 ; | ||
* but if n is less equal 2 then the ans will be 1; | ||
* | ||
* lets say for example, 7 and 3. n=7 and x=3. | ||
* stack1 contains 1,2 | ||
* stack2 contains 3,4,5 | ||
* stack3 contains 6,7,8. So answer would be 3. | ||
* so here the ans (n-2)/x will be 1.66 and ceil(1.66) = 2after adding 1 it will be 3. | ||
* | ||
* time complexity;- O(n) , space complexity:-O(n) | ||
* @author[chaltidutta](https://github.com/chaltidutta) | ||
* **/ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
void solve(){ | ||
|
||
int n,x; | ||
int ans; | ||
cin>>n>>x; | ||
if(n>2){ | ||
ans=ceil(double(n-2)/x)+1; | ||
} | ||
else{ | ||
ans=1; | ||
} | ||
cout<<ans<<endl; | ||
} | ||
|
||
int main(){ | ||
int t; | ||
cin>>t; | ||
while(t-->0){ | ||
solve(); | ||
} | ||
return 0; | ||
} | ||
|
||
/* | ||
Example Input :- | ||
4 | ||
7 3 | ||
1 5 | ||
22 5 | ||
987 13 | ||
Example Output :- | ||
3 | ||
1 | ||
5 | ||
77 | ||
*/ |
73 changes: 73 additions & 0 deletions
73
4. Stack/destroy_asteroid/[CPP]destroy_asteroid_csubhradipta.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* @file: [CPP]destroy_asteroid_csubhradipta.cpp | ||
* @brief: Find the state of the asteroids after all collisions | ||
* @details: | ||
* Compare the (top) and (top - 1) element | ||
* If they moved towards each other, the larger one will be kept and smaller one will be popped | ||
* Again, if both are of same size and moving towards each other, both will be popped | ||
* Else both will be kept | ||
* Space Complexity : O(n) | ||
* Time Complexity : O(n) | ||
* @author [Subhradipta Choudhury](https://github.com/csubhradipta) | ||
*/ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
void solve(){ | ||
int n; | ||
cin>>n; | ||
vector<int> asteroid(n); | ||
stack<int> s; | ||
|
||
for(int i = 0; i < n; i++){ | ||
cin>>asteroid[i]; | ||
} | ||
|
||
int ast; | ||
|
||
for(int i = 0; i < n; i++){ | ||
|
||
ast = asteroid[i]; | ||
|
||
if(ast >= 0) | ||
s.push(ast); //moving right | ||
|
||
else { //moving left | ||
|
||
if((s.size() == 0) || s.top() < 0) | ||
s.push(ast); //push, as previous also moving left | ||
|
||
else if(abs(s.top()) == abs(ast)) //both equal in size, hence destroyed | ||
s.pop(); | ||
|
||
else if (abs(s.top()) < abs(ast)){ //previous is smaller, hence destroy previous | ||
s.pop(); | ||
i--; //check this with previous stack element. | ||
} | ||
} | ||
} | ||
|
||
vector<int> result; | ||
|
||
while(!s.empty()){ | ||
result.push_back(s.top()); | ||
s.pop(); | ||
} | ||
|
||
reverse(result.begin(), result.end()); | ||
|
||
for(int i = 0; i < result.size(); i++) | ||
cout<<result[i]<<" "; | ||
cout<<endl; | ||
} | ||
|
||
int main() | ||
{ | ||
int t; | ||
cin>>t; | ||
while(t--){ | ||
solve(); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* PriorityTasksArnab.java | ||
* Given an array of priority values,we have to check if the value next is of higher priority or not.If yes, then print that value,else | ||
* Print -1.At the end consider it as circular array,and for the last value check if any higher value to it exists in the array uptill (n-2)th element as the (n-1)th | ||
* element is the last value itself,so we would check before that value only if any value higher to it is present or not. | ||
* If yes,then print it,else print -1. | ||
* Description:- | ||
* Create a stack for storing indices and accordingly reach values of stack.Now start the loop,if stack is empty then push index | ||
* to stack.Else continue checking as if a value higher than the current peek value of stack is there or not | ||
* If yes then pop from stack and continue popping unless a higher value to the current value is achieved and push them to corresponding | ||
* index from stack.And at the same time check if array index has reached n-1,If yes then break from loop. | ||
* After the loop,we have only indices of maximum elements left in stack,hence just pop() all indices and at that indices assign -1 to output array | ||
* Time Complexity-O(n) Space Complexity-O(n) | ||
* @author [codebook-2000](https://github.com/codebook-2000) | ||
*/ | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.util.Stack; | ||
|
||
public class PriorityTasksArnab { | ||
static int[] solve(int[] arr, int n) { | ||
Stack<Integer> index = new Stack<Integer>();//creating the stack to store respective | ||
//indices and from indices we would compare values | ||
|
||
int[] ans = new int[n];//Our output array | ||
|
||
int j = 0; | ||
while (true) //No condition barrier at while loop | ||
{ | ||
if (index.isEmpty() == true) { | ||
index.push(j % n);//If stack is empty,push the first index | ||
j++; | ||
} else { | ||
if (arr[index.peek()] < arr[j % n])//If a higher value than stack peek value is | ||
{//reached then pop the current index from stack and | ||
int in = index.pop();//at that index push the current value to the output array; | ||
ans[in] = arr[j % n]; | ||
if (in == n - 1)//check the same condition here | ||
break; | ||
} else { //If smaller or equal value is present then push index to stack | ||
index.push(j % n); | ||
j++; | ||
} | ||
} | ||
} | ||
while (index.isEmpty() == false) //Now for the elements which are maximum and would | ||
{//get -1 in their indices | ||
int in = index.pop(); | ||
ans[in] = -1; | ||
} | ||
|
||
return ans;//return the array | ||
} | ||
|
||
public static void main(String[] args) throws java.lang.Exception { | ||
// your code goes here | ||
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); | ||
int t = Integer.parseInt(buf.readLine()); | ||
StringBuilder sb = new StringBuilder(); | ||
for (int i = 0; i < t; i++) { | ||
int n = Integer.parseInt(buf.readLine()); //Reading the input | ||
String[] st1 = (buf.readLine()).split(" "); | ||
|
||
int[] arr = new int[n]; | ||
for (int j = 0; j < n; j++) | ||
arr[j] = Integer.parseInt(st1[j]); | ||
|
||
int[] ans = solve(arr, n); //Calling the method solve each time and storing it in arraylist | ||
|
||
for (int j = 0; j < ans.length; j++) | ||
sb.append(ans[j] + " "); //Appending the arraylist | ||
sb.append("\n"); | ||
} | ||
System.out.println(sb); //Printing it | ||
} | ||
} | ||
|
||
/* | ||
Input:- | ||
3 | ||
3 | ||
1 2 1 | ||
4 | ||
1 3 4 2 | ||
6 | ||
3 2 6 7 1 2 | ||
Output;- | ||
2 -1 2 | ||
3 4 -1 3 | ||
6 6 7 -1 2 3 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/** | ||
* GroupingMerlin.java | ||
* Remove y elements after the 1st x elements in a linked list | ||
* | ||
* Description- | ||
* If linked list size is <3, the list wont change at all | ||
* Otherwise, we take odd as our first odd node, even as our first even node, and start iterating from the 3rd node | ||
* In case it is an odd node, we add it after odd pointer | ||
* In case it is an even node, we add it after even pointer | ||
* Finally, we add the even list to the tail of odd list | ||
* | ||
* Time Complexity-O(n), Space Complexity-O(1) | ||
* | ||
* @author merlin[https://github.com/m-e-r-l-i-n] | ||
*/ | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
class Node | ||
{ | ||
int v; | ||
Node next; | ||
Node(int x) | ||
{ | ||
v=x; | ||
next=null; | ||
} | ||
} | ||
|
||
class GroupingMerlin | ||
{ | ||
public static void main(String args[])throws Exception | ||
{ | ||
BufferedReader bu=new BufferedReader(new InputStreamReader(System.in)); | ||
StringBuilder sb=new StringBuilder(); | ||
int t=Integer.parseInt(bu.readLine()); | ||
while(t-->0) | ||
{ | ||
int n=Integer.parseInt(bu.readLine()); | ||
int i; | ||
Node head=null,ptr=null; | ||
String s[]=bu.readLine().split(" "); | ||
for(i=0;i<n;i++) | ||
{ | ||
int x=Integer.parseInt(s[i]); | ||
Node cur=new Node(x); | ||
if(head==null) | ||
{ | ||
head=cur; | ||
ptr=head; | ||
} | ||
else | ||
{ | ||
ptr.next=cur; | ||
ptr=ptr.next; | ||
} | ||
} | ||
|
||
groupOddEven(head); | ||
while(head!=null) | ||
{ | ||
sb.append(head.v+" "); | ||
head=head.next; | ||
} | ||
sb.append("\n"); | ||
} | ||
System.out.print(sb); | ||
} | ||
|
||
static void groupOddEven(Node head) | ||
{ | ||
if(head==null || head.next==null || head.next.next==null) return; | ||
Node cur=head.next.next; | ||
Node odd=head,even=head.next,etop=even; //initializing the 1st 2 | ||
int o=1; | ||
while(cur!=null) | ||
{ | ||
if(o==1) //odd node | ||
{ | ||
odd.next=cur; | ||
odd=odd.next; | ||
} | ||
else //even node | ||
{ | ||
even.next=cur; | ||
even=even.next; | ||
} | ||
o^=1; //o will be 1 for odd 0 for even | ||
cur=cur.next; | ||
} | ||
odd.next=etop; | ||
even.next=null; | ||
} | ||
} | ||
|
||
|
||
/* | ||
sample i/p- | ||
1 | ||
6 | ||
1 5 8 9 15 2 | ||
sample o/p- | ||
1 8 15 5 9 2 | ||
*/ |
Oops, something went wrong.