Skip to content

Commit

Permalink
Merge branch 'main' into patch-3
Browse files Browse the repository at this point in the history
  • Loading branch information
AdarshAddee committed Oct 4, 2022
2 parents 41966b5 + 59e68b5 commit f399295
Show file tree
Hide file tree
Showing 12 changed files with 518 additions and 25 deletions.
31 changes: 31 additions & 0 deletions C++/ComparingTwoFractions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// fikriks

#include<stdio.h>

using namespace std;

int main()
{
int pecahan_pembilang1, pecahan_penyebut1, pecahan_pembilang2, pecahan_penyebut2;
float hasil1, hasil2;

printf("%s\n", "Membandingkan Dua Bilangan Pecahan");
printf("Masukan Bilangan Pecahan Ke-1 (Format Input = 1/2) = ");
scanf("%i/%i", &pecahan_pembilang1, &pecahan_penyebut1);

printf("Masukan Bilangan Pecahan Ke-2 (Format Input = 1/2) = ");
scanf("%i/%i", &pecahan_pembilang2, &pecahan_penyebut2);

hasil1 = (float) pecahan_pembilang1 / pecahan_penyebut1;
hasil2 = (float) pecahan_pembilang2 / pecahan_penyebut2;

if(hasil1 == hasil2){
printf("Pecahan Ke-1 (%i/%i) Sama Dengan Pecahan Ke-2 (%i/%i)", pecahan_pembilang1, pecahan_penyebut1, pecahan_pembilang2, pecahan_penyebut2);
}else if(hasil1 < hasil2){
printf("Pecahan Ke-1 (%i/%i) Lebih Kecil Dari Pecahan Ke-2 (%i/%i)", pecahan_pembilang1, pecahan_penyebut1, pecahan_pembilang2, pecahan_penyebut2);
}else if(hasil1 > hasil2){
printf("Pecahan Ke-1 (%i/%i) Lebih Besar Dari Pecahan Ke-2 (%i/%i)", pecahan_pembilang1, pecahan_penyebut1, pecahan_pembilang2, pecahan_penyebut2);
}

return 0;
}
77 changes: 77 additions & 0 deletions C++/Connected_components.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
░░░░░░░░░▄░░░░░░░░░░░░░░▄░░░░
░░░░░░░░▌▒█░░░░░░░░░░░▄▀▒▌░░░
░░░░░░░░▌▒▒█░░░░░░░░▄▀▒▒▒▐░░░
░░░░░░░▐▄▀▒▒▀▀▀▀▄▄▄▀▒▒▒▒▒▐░░░
░░░░░▄▄▀▒░▒▒▒▒▒▒▒▒▒█▒▒▄█▒▐░░░
░░░▄▀▒▒▒░░░▒▒▒░░░▒▒▒▀██▀▒▌░░░
░░▐▒▒▒▄▄▒▒▒▒░░░▒▒▒▒▒▒▒▀▄▒▒▌░░
░░▌░░▌█▀▒▒▒▒▒▄▀█▄▒▒▒▒▒▒▒█▒▐░░
░▐░░░▒▒▒▒▒▒▒▒▌██▀▒▒░░░▒▒▒▀▄▌░
░▌░▒▄██▄▒▒▒▒▒▒▒▒▒░░░░░░▒▒▒▒▌░
▀▒▀▐▄█▄█▌▄░▀▒▒░░░░░░░░░░▒▒▒▐░
▐▒▒▐▀▐▀▒░▄▄▒▄▒▒▒▒▒▒░▒░▒░▒▒▒▒▌
▐▒▒▒▀▀▄▄▒▒▒▄▒▒▒▒▒▒▒▒░▒░▒░▒▒▐░
░▌▒▒▒▒▒▒▀▀▀▒▒▒▒▒▒░▒░▒░▒░▒▒▒▌░
░▐▒▒▒▒▒▒▒▒▒▒▒▒▒▒░▒░▒░▒▒▄▒▒▐░░
░░▀▄▒▒▒▒▒▒▒▒▒▒▒░▒░▒░▒▄▒▒▒▒▌░░
░░░░▀▄▒▒▒▒▒▒▒▒▒▒▄▄▄▀▒▒▒▒▄▀░░░
░░░░░░▀▄▄▄▄▄▄▀▀▀▒▒▒▒▒▄▄▀░░░░░
░░░░░░░░░▒▒▒▒▒▒▒▒▒▒▀▀░░░░░░░░
*/

// Github: GarvitV957

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lli long long int
#define vi vector<int>
#define vvi vector<vi>
#define vll vector<long long>
#define vb vector<bool>
#define pb push_back
#define pii pair<int,int>
#define all(x) x.begin(),x.end()

int N=1e6 +1;
vvi adj(N);
vi vis(N,0);

vvi cc;
vi current_comp;

void dfs(int i){
vis[i]=1;
current_comp.pb(i);
for(auto v:adj[i]){
if(!vis[v]){
dfs(v);
}
}
}

int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);

int n,e;
cin>>n>>e;
for(int i=0;i<e;i++){
int x,y;
cin>>x>>y;
adj[x].pb(y),adj[y].pb(x);
}
int c=0;
for(int i=1;i<=n;i++){
if(!vis[i]){
current_comp.clear();
dfs(i);
cc.pb(current_comp);
c++;
}
}
cout<<c<<endl;
cout<<cc.size()<<endl;
return 0;
}
65 changes: 65 additions & 0 deletions C++/Counting_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// C++ Program for counting sort
#include <bits/stdc++.h>
#include <string.h>
using namespace std;
#define RANGE 200

// The main function that sort
// the given string arr[] in
// alphabetical order
void countSort(char arr[])
{
// The output character array
// that will have sorted arr
char output[strlen(arr)];

// Create a count array to store count of individual
// characters and initialize count array as 0
int count[RANGE + 1], i;
memset(count, 0, sizeof(count));

// Store count of each character
for (i = 0; arr[i]; ++i)
++count[arr[i]];

// Change count[i] so that count[i] now contains actual
// position of this character in output array
for (i = 1; i <= RANGE; ++i)
count[i] += count[i - 1];

// Build the output character array
for (i = 0; arr[i]; ++i) {
output[count[arr[i]] - 1] = arr[i];
--count[arr[i]];
}

/*
For Stable algorithm
for (i = sizeof(arr)-1; i>=0; --i)
{
output[count[arr[i]]-1] = arr[i];
--count[arr[i]];
}
For Logic : See implementation
*/

// Copy the output array to arr, so that arr now
// contains sorted characters
for (i = 0; arr[i]; ++i)
arr[i] = output[i];
}

// Driver code
int main()
{
char arr[] = "countingsortincpp";

countSort(arr);

cout << "Sorted character array is " << arr;
return 0;
}
//github : atinder11


61 changes: 61 additions & 0 deletions C++/Merge_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
chiragchandnani10
https://github.com/chiragchandnani10
*/

#include<bits/stdc++.h>
using namespace std;
void merging(int input[],int start,int end){
int mid = (start+end)/2;
int i=start, j=mid+1, k=start;
int ans[end+1];
while(i<=mid&&j<=end){
if(input[i]<input[j]){
ans[k]=input[i];
i++;
k++;
}
else{
ans[k]=input[j];
j++;
k++;
}
}
while(i<=mid){
ans[k]=input[i];
i++;
k++;
}
while(j<=end){
ans[k]=input[j];
j++;
k++;
}
for(int s=start;s<=end;s++){
input[s]=ans[s];
}
}

void merge_sort(int input[],int start,int end){
if(start>=end){
return;
}
int mid = ((start+end)/2);
merge_sort(input,start,mid);
merge_sort(input,mid+1,end);
merging(input,start,end);


}



void mergeSort(int input[], int size){
// Write your code here

merge_sort(input,0,size-1);



}

50 changes: 50 additions & 0 deletions C++/bucketSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Authors Name : Utkarsh Tyagi
Date Modified: 1 October, 2022
*/
// C++ program to sort an
// array using bucket sort
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

// Function to sort arr[] of
// size n using bucket sort
void bucketSort(float arr[], int n)
{

// 1) Create n empty buckets
vector<float> b[n];

// 2) Put array elements
// in different buckets
for (int i = 0; i < n; i++) {
int bi = n * arr[i]; // Index in bucket
b[bi].push_back(arr[i]);
}

// 3) Sort individual buckets
for (int i = 0; i < n; i++)
sort(b[i].begin(), b[i].end());

// 4) Concatenate all buckets into arr[]
int index = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < b[i].size(); j++)
arr[index++] = b[i][j];
}

/* Driver program to test above function */
int main()
{
float arr[]
= { 0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434 };
int n = sizeof(arr) / sizeof(arr[0]);
bucketSort(arr, n);

cout << "Sorted array is \n";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
53 changes: 53 additions & 0 deletions C++/divisible_subarrays_using_pigeon_hole_principle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Github : vinaypathak07
// Pigeonhole Principle
// Divisible SubArrays
#include <iostream>
#include <cstring>

using namespace std;

#define lol long

lol a[1000005], frequency[1000005];

int main() {

// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif

int t;
cin >> t;

while (t--) {
int n;
cin >> n;

memset(frequency, 0, sizeof(frequency));
frequency[0] = 1;

lol sum = 0;

for (int i = 0; i < n; i++) {
cin >> a[i];

sum += a[i];
sum %= n;
sum = (sum + n) % n; // sum+n because if we encountered negative value
frequency[sum] += 1;
}

lol ans = 0;

for (int i = 0; i < n; i++) {
if (frequency[i] > 1) {
lol m = frequency[i];
ans += (m * (m - 1)) / 2;
}
}
cout << ans << endl;
}

return 0;
}
17 changes: 17 additions & 0 deletions C++/factorialprogram.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include<iostream>
using namespace std;
// github username navdeepk037 https://github.com/navdeepk037
int fact(int n)
{
int factorial=1;
for(int i=n;i>=1;i--)
factorial=factorial*i;
return factorial;
}
int main(){
int n;
cout<<"enter the number ";
cin>>n;
cout<<"the factorial of the number is "<<fact(n);

}
Loading

0 comments on commit f399295

Please sign in to comment.