-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
74 changed files
with
1,858 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,10 @@ | ||
for _ in range(int(input())): | ||
n=int(input()) | ||
f=[] | ||
for i in range(n): | ||
l=list(map(int,input().split())) | ||
f.append(l) | ||
f.sort(key=lambda x:(x[0],-x[1],x[2])) | ||
for i in f: | ||
print(*i) | ||
|
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,14 @@ | ||
def bino(n, k): | ||
C = [[0 for x in range(k+1)] for x in range(n+1)] | ||
for i in range(n+1): | ||
for j in range(min(i, k)+1): | ||
if j == 0 or j == i: | ||
C[i][j] = 1 | ||
else: | ||
C[i][j] = C[i-1][j-1] + C[i-1][j] | ||
|
||
return C[n][k] | ||
|
||
for _ in range(int(input())): | ||
n,r=map(int,input().split()) | ||
print(bino(n,r)%(10**9+7)) |
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,107 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
|
||
/* Representation of a box */ | ||
struct Box | ||
{ | ||
// h --> height, w --> width, d --> depth | ||
int h, w, d; // for simplicity of solution, always keep w <= d | ||
}; | ||
|
||
// A utility function to get minimum of two intgers | ||
int min (int x, int y) | ||
{ return (x < y)? x : y; } | ||
|
||
// A utility function to get maximum of two intgers | ||
int max (int x, int y) | ||
{ return (x > y)? x : y; } | ||
|
||
/* Following function is needed for library function qsort(). We | ||
use qsort() to sort boxes in decreasing order of base area. | ||
Refer following link for help of qsort() and compare() | ||
http://www.cplusplus.com/reference/clibrary/cstdlib/qsort/ */ | ||
int compare (const void *a, const void * b) | ||
{ | ||
return ( (*(Box *)b).d * (*(Box *)b).w ) - | ||
( (*(Box *)a).d * (*(Box *)a).w ); | ||
} | ||
|
||
/* Returns the height of the tallest stack that can be | ||
formed with give type of boxes */ | ||
int maxStackHeight( Box arr[], int n ) | ||
{ | ||
/* Create an array of all rotations of given boxes | ||
For example, for a box {1, 2, 3}, we consider three | ||
instances{{1, 2, 3}, {2, 1, 3}, {3, 1, 2}} */ | ||
Box rot[3*n]; | ||
int index = 0; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
// Copy the original box | ||
rot[index].h = arr[i].h; | ||
rot[index].d = max(arr[i].d, arr[i].w); | ||
rot[index].w = min(arr[i].d, arr[i].w); | ||
index++; | ||
|
||
// First rotation of box | ||
rot[index].h = arr[i].w; | ||
rot[index].d = max(arr[i].h, arr[i].d); | ||
rot[index].w = min(arr[i].h, arr[i].d); | ||
index++; | ||
|
||
// Second rotation of box | ||
rot[index].h = arr[i].d; | ||
rot[index].d = max(arr[i].h, arr[i].w); | ||
rot[index].w = min(arr[i].h, arr[i].w); | ||
index++; | ||
} | ||
|
||
// Now the number of boxes is 3n | ||
n = 3*n; | ||
|
||
/* Sort the array 'rot[]' in non-increasing order | ||
of base area */ | ||
qsort (rot, n, sizeof(rot[0]), compare); | ||
|
||
// Uncomment following two lines to print all rotations | ||
// for (int i = 0; i < n; i++ ) | ||
// printf("%d x %d x %d\n", rot[i].h, rot[i].w, rot[i].d); | ||
|
||
/* Initialize msh values for all indexes | ||
msh[i] --> Maximum possible Stack Height with box i on top */ | ||
int msh[n]; | ||
for (int i = 0; i < n; i++ ) | ||
msh[i] = rot[i].h; | ||
|
||
/* Compute optimized msh values in bottom up manner */ | ||
for (int i = 1; i < n; i++ ) | ||
for (int j = 0; j < i; j++ ) | ||
if ( rot[i].w < rot[j].w && | ||
rot[i].d < rot[j].d && | ||
msh[i] < msh[j] + rot[i].h | ||
) | ||
{ | ||
msh[i] = msh[j] + rot[i].h; | ||
} | ||
|
||
|
||
/* Pick maximum of all msh values */ | ||
int max = -1; | ||
for ( int i = 0; i < n; i++ ) | ||
if ( max < msh[i] ) | ||
max = msh[i]; | ||
|
||
return max; | ||
} | ||
|
||
/* Driver program to test above function */ | ||
int main() | ||
{ | ||
Box arr[] = { {4, 6, 7}, {1, 2, 3}, {4, 5, 6}, {10, 12, 32} }; | ||
int n = sizeof(arr)/sizeof(arr[0]); | ||
|
||
printf("The maximum possible height of stack is %d\n", | ||
maxStackHeight (arr, n) ); | ||
|
||
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,21 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
const int MAX_CHAR = 26; | ||
void sortByPattern(string &str, string pat) | ||
{ | ||
int count[MAX_CHAR] = {0}; | ||
for (int i = 0 ; i < str.length(); i++) | ||
count[str[i] - 'a']++; | ||
int index = 0; | ||
for (int i = 0; i < pat.length(); i++) | ||
for (int j = 0; j < count[pat[i] - 'a']; j++) | ||
str[index++] = pat[i]; | ||
} | ||
int main() | ||
{ | ||
string pat = "bca"; | ||
string str = "abc"; | ||
sortByPattern(str, pat); | ||
cout << str; | ||
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,14 @@ | ||
import math | ||
def phi(n): | ||
result=1 | ||
for i in range(2,n): | ||
if math.gcd(i,n)==1: | ||
result+=1 | ||
return result | ||
|
||
for _ in range(int(input())): | ||
z=int(input()) | ||
if phi(z)%2==0: | ||
print("YES",phi(z)) | ||
else: | ||
print("NO") |
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,25 @@ | ||
prime=[True for i in range(10**4+10)] | ||
def SOE(n): | ||
p=2 | ||
while p*p<=n: | ||
if prime[p]: | ||
for i in range(p*p,n,p): | ||
prime[i]=False | ||
p+=1 | ||
|
||
SOE(10**3+4) | ||
for _ in range(int(input())): | ||
n=input() | ||
f=len(n) | ||
d=[] | ||
fl=0 | ||
for _ in range(len(n)): | ||
z=n[-1]+n[:f-1] | ||
if prime[int(z)]: | ||
fl=1 | ||
break | ||
if fl==1: | ||
print("Yes") | ||
else: | ||
print("No") | ||
|
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,16 @@ | ||
def count(S, m, n): | ||
table = [[0 for x in range(m)] for x in range(n+1)] | ||
for i in range(m): | ||
table[0][i] = 1 | ||
for i in range(1, n+1): | ||
for j in range(m): | ||
x = table[i - S[j]][j] if i-S[j] >= 0 else 0 | ||
y = table[i][j-1] if j >= 1 else 0 | ||
table[i][j] = x + y | ||
return table[n][m-1] | ||
|
||
for _ in range(int(input())): | ||
m=int(input()) | ||
l=list(map(int,input().split())) | ||
n=int(input()) | ||
print(count(l,m,n)) |
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,20 @@ | ||
prime=[True for i in range(10**4+5)] | ||
def SOE(n): | ||
p=2 | ||
while p*p<=n: | ||
if prime[p]: | ||
for i in range(p*p,n,p): | ||
prime[i]=False | ||
p+=1 | ||
|
||
SOE(10**4+2) | ||
for _ in range(int(input())): | ||
x,y=map(int,input().split()) | ||
z=x+y | ||
ans=0 | ||
for i in range(z+1,z+100): | ||
if prime[i]: | ||
ans=i | ||
break | ||
print(abs(ans-z)) | ||
|
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,70 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
void print(vector <int> q) | ||
{cout<<"("; | ||
for(int i=0;i<q.size()-1;i++) | ||
cout<<q[i]<<" "; | ||
cout<<q[q.size()-1]<<")"; | ||
|
||
} | ||
void sum(vector <int> v,vector <int> q,int k,int s,int j,bool &r) | ||
{ | ||
if(s==k) | ||
{ r=true; | ||
print(q); | ||
return; | ||
} | ||
for(int i=j;i<v.size();i++) | ||
{ | ||
if(s>k) | ||
return; | ||
|
||
q.push_back(v[i]); | ||
|
||
sum(v,q,k,s+v[i],j,r); | ||
j++; | ||
if(q.size()==0) | ||
return; | ||
q.pop_back(); | ||
|
||
} | ||
|
||
} | ||
int main() { | ||
//code | ||
int t; | ||
cin>>t; | ||
while(t--) | ||
{ | ||
int n; | ||
cin>>n; | ||
vector <int> v; | ||
set<int> w; | ||
for(int i=0;i<n;i++) | ||
{ | ||
int x; | ||
cin>>x; | ||
w.insert(x); | ||
|
||
} | ||
set <int> ::iterator it=w.begin(); | ||
while(it!=w.end()) | ||
{ | ||
v.push_back(*it); | ||
it++; | ||
} | ||
int k; | ||
cin>>k; | ||
vector <int> q; | ||
sort(v.begin(),v.end()); | ||
int s=0; | ||
int j=0; | ||
bool r=false; | ||
sum(v,q,k,s,j,r); | ||
if(!r) | ||
cout<<"Empty"; | ||
cout<<endl; | ||
} | ||
return 0; | ||
} |
Binary file not shown.
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,29 @@ | ||
def countsto(arr,n,k): | ||
c=0 | ||
r=1 | ||
l=0 | ||
arr.sort() | ||
while r<n: | ||
if r==l: | ||
r+=1 | ||
elif (arr[r]-arr[l])==k: | ||
c+=1 | ||
l+=1 | ||
r+=1 | ||
try: | ||
while(arr[r]==arr[l]): | ||
r+=1 | ||
l+=1 | ||
except: | ||
if r==n: | ||
break | ||
elif(arr[r]-arr[l])>k: | ||
l+=1 | ||
else: | ||
r+=1 | ||
return c | ||
for _ in range(int(input())): | ||
n=int(input()) | ||
l=list(map(int,input().split())) | ||
k=int(input()) | ||
print(countsto(l,n,k)) |
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,31 @@ | ||
#include<iostream> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#define RANGE 255 | ||
using namespace std; | ||
void countSort(char arr[]) | ||
{ | ||
char output[strlen(arr)]; | ||
int count[RANGE + 1], i; | ||
memset(count, 0, sizeof(count)); | ||
for(i = 0; arr[i]; ++i) | ||
++count[arr[i]]; | ||
for (i = 1; i <= RANGE; ++i) | ||
count[i] += count[i-1]; | ||
for (i = 0; arr[i]; ++i) | ||
{ | ||
output[count[arr[i]]-1] = arr[i]; | ||
--count[arr[i]]; | ||
} | ||
for (i = 0; arr[i]; ++i) | ||
arr[i] = output[i]; | ||
} | ||
int main() | ||
{ | ||
char arr[] = "geeksforgeeks";; | ||
|
||
countSort(arr); | ||
|
||
printf(arr); | ||
return 0; | ||
} |
Binary file not shown.
Oops, something went wrong.