forked from AdarshAddee/Hacktoberfest2022_for_Beginers
-
Notifications
You must be signed in to change notification settings - Fork 0
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
30 changed files
with
1,881 additions
and
3 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,20 @@ | ||
{ | ||
"configurations": [ | ||
{ | ||
"name": "Win32", | ||
"includePath": [ | ||
"${workspaceFolder}/**" | ||
], | ||
"defines": [ | ||
"_DEBUG", | ||
"UNICODE", | ||
"_UNICODE" | ||
], | ||
"compilerPath": "D:\\MinGW\\bin\\gcc.exe", | ||
"cStandard": "gnu17", | ||
"cppStandard": "gnu++14", | ||
"intelliSenseMode": "windows-gcc-x86" | ||
} | ||
], | ||
"version": 4 | ||
} |
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,41 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
int binarySearch(int arr[], int left, int right, int x) { | ||
while (left <= right) { | ||
int mid = left + (right - left) / 2; | ||
|
||
if (arr[mid] == x) { | ||
return mid; | ||
} else if (arr[mid] < x) { | ||
left = mid + 1; | ||
} else { | ||
right = mid - 1; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
int main() { | ||
int myarr[10]; | ||
int num; | ||
int output; | ||
|
||
cout << "Please enter 10 elements ASCENDING order" << endl; | ||
for (int i = 0; i < 10; i++) { | ||
cin >> myarr[i]; | ||
} | ||
cout << "Please enter an element to search" << endl; | ||
cin >> num; | ||
|
||
output = binarySearch(myarr, 0, 9, num); | ||
|
||
if (output == -1) { | ||
cout << "No Match Found" << endl; | ||
} else { | ||
cout << "Match found at position: " << output << endl; | ||
} | ||
|
||
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,57 @@ | ||
#include<iostream> | ||
#include<stdio.h> | ||
using namespace std; | ||
#define INFINITY 9999 | ||
#define max 5 | ||
void dijkstra(int G[max][max],int n,int startnode); | ||
int main() { | ||
int G[max][max]={{0,1,0,3,10},{1,0,5,0,0},{0,5,0,2,1},{3,0,2,0,6},{10,0,1,6,0}}; | ||
int n=5; | ||
int u=0; | ||
dijkstra(G,n,u); | ||
return 0; | ||
} | ||
void dijkstra(int G[max][max],int n,int startnode) { | ||
int cost[max][max],distance[max],pred[max]; | ||
int visited[max],count,mindistance,nextnode,i,j; | ||
for(i=0;i<n;i++) | ||
for(j=0;j<n;j++) | ||
if(G[i][j]==0) | ||
cost[i][j]=INFINITY; | ||
else | ||
cost[i][j]=G[i][j]; | ||
for(i=0;i<n;i++) { | ||
distance[i]=cost[startnode][i]; | ||
pred[i]=startnode; | ||
visited[i]=0; | ||
} | ||
distance[startnode]=0; | ||
visited[startnode]=1; | ||
count=1; | ||
while(count<n-1) { | ||
mindistance=INFINITY; | ||
for(i=0;i<n;i++) | ||
if(distance[i]<mindistance&&!visited[i]) { | ||
mindistance=distance[i]; | ||
nextnode=i; | ||
} | ||
visited[nextnode]=1; | ||
for(i=0;i<n;i++) | ||
if(!visited[i]) | ||
if(mindistance+cost[nextnode][i]<distance[i]) { | ||
distance[i]=mindistance+cost[nextnode][i]; | ||
pred[i]=nextnode; | ||
} | ||
count++; | ||
} | ||
for(i=0;i<n;i++) | ||
if(i!=startnode) { | ||
cout<<"\nDistance of node"<<i<<"="<<distance[i]; | ||
cout<<"\nPath="<<i; | ||
j=i; | ||
do { | ||
j=pred[j]; | ||
cout<<"<-"<<j; | ||
}while(j!=startnode); | ||
} | ||
} |
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,68 @@ | ||
#include<iostream> | ||
|
||
using namespace std; | ||
|
||
void heapify(int arr[], int n, int i) | ||
{ | ||
int largest = i; | ||
int l = 2*i + 1; | ||
int r = 2*i + 2; | ||
|
||
//If left child is larger than root | ||
if (l < n && arr[l] > arr[largest]) | ||
largest = l; | ||
|
||
//If right child largest | ||
if (r < n && arr[r] > arr[largest]) | ||
largest = r; | ||
|
||
//If root is nor largest | ||
if (largest != i) | ||
{ | ||
swap(arr[i], arr[largest]); | ||
|
||
//Recursively heapifying the sub-tree | ||
heapify(arr, n, largest); | ||
} | ||
} | ||
|
||
void heapSort(int arr[], int n) | ||
{ | ||
|
||
for (int i = n / 2 - 1; i >= 0; i--) | ||
heapify(arr, n, i); | ||
|
||
//One by one extract an element from heap | ||
for (int i=n-1; i>=0; i--) | ||
{ | ||
//Moving current root to end | ||
swap(arr[0], arr[i]); | ||
|
||
//Calling max heapify on the reduced heap | ||
heapify(arr, i, 0); | ||
} | ||
} | ||
|
||
//Function to print array | ||
void display(int arr[], int n) | ||
{ | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cout << arr[i] << "\t"; | ||
} | ||
cout << "\n"; | ||
} | ||
|
||
|
||
int main() | ||
{ | ||
int arr[] = {1, 14, 3, 7, 0}; | ||
int n = sizeof(arr)/sizeof(arr[0]); | ||
cout << "Unsorted array \n"; | ||
display(arr, n); | ||
|
||
heapSort(arr, n); | ||
|
||
cout << "Sorted array \n"; | ||
display(arr, 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,58 @@ | ||
// Github Username: amitShindeGit | ||
#include <iostream> | ||
using namespace std; | ||
|
||
class Solution{ | ||
public: | ||
// arr: input array | ||
// n: size of array | ||
//Function to find the sum of contiguous subarray with maximum sum. | ||
long long max(long long x, long long y){ | ||
if(x > y){ | ||
return x; | ||
}else{ | ||
return y; | ||
} | ||
} | ||
|
||
long long maxSubarraySum(int arr[], int n){ | ||
|
||
// Your code here | ||
long long max_current = arr[0]; | ||
long long max_global = arr[0]; | ||
|
||
for(int i=1; i<=n-1; i++){ | ||
max_current = max(arr[i], max_current + arr[i]); | ||
// cout << max_current << " " << max_global << endl; | ||
|
||
if(max_current > max_global){ | ||
max_global = max_current; | ||
} | ||
} | ||
|
||
return max_global; | ||
|
||
} | ||
}; | ||
|
||
|
||
int main() | ||
{ | ||
int t,n; | ||
|
||
cin>>t; //input testcases | ||
while(t--) //while testcases exist | ||
{ | ||
|
||
cin>>n; //input size of array | ||
|
||
int a[n]; | ||
|
||
for(int i=0;i<n;i++) | ||
cin>>a[i]; //inputting elements of array | ||
|
||
Solution ob; | ||
|
||
cout << ob.maxSubarraySum(a, n) << endl; | ||
} | ||
} |
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,66 @@ | ||
#include<iostream> | ||
#define MAX 10 | ||
using namespace std; | ||
struct product | ||
{ | ||
int product_num; | ||
int profit; | ||
int weight; | ||
float ratio; | ||
float take_quantity; | ||
}; | ||
int main() | ||
{ | ||
product P[MAX],temp; | ||
int i,j,total_product,capacity; | ||
float value=0; | ||
cout<<"ENTER NUMBER OF ITEMS : "; | ||
cin>>total_product; | ||
cout<<"ENTER CAPACITY OF SACK : "; | ||
cin>>capacity; | ||
cout<<"\n"; | ||
for(i=0;i<total_product;++i) | ||
{ | ||
P[i].product_num=i+1; | ||
cout<<"ENTER PROFIT AND WEIGHT OF PRODUCT "<<i+1<<" : "; | ||
cin>>P[i].profit>>P[i].weight; | ||
P[i].ratio=(float)P[i].profit/P[i].weight; | ||
P[i].take_quantity=0; | ||
} | ||
//HIGHEST RATIO BASED SORTING | ||
for(i=0;i<total_product;++i) | ||
{ | ||
for(j=i+1;j<total_product;++j) | ||
{ | ||
if(P[i].ratio<P[j].ratio) | ||
{ | ||
temp=P[i]; | ||
P[i]=P[j]; | ||
P[j]=temp; | ||
} | ||
} | ||
} | ||
for(i=0;i<total_product;++i) | ||
{ | ||
if(capacity==0) | ||
break; | ||
else if(P[i].weight<capacity) | ||
{ | ||
P[i].take_quantity=1; | ||
capacity-=P[i].weight; | ||
} | ||
else if(P[i].weight>capacity) | ||
{ | ||
P[i].take_quantity=(float)capacity/P[i].weight; | ||
capacity=0; | ||
} | ||
} | ||
cout<<"\n\nPRODUCTS TO BE TAKEN -"; | ||
for(i=0;i<total_product;++i) | ||
{ | ||
cout<<"\nTAKE PRODUCT "<<P[i].product_num<<" : "<<P[i].take_quantity*P[i].weight<<" UNITS"; | ||
value+=P[i].profit*P[i].take_quantity; | ||
} | ||
cout<<"\nTHE KNAPSACK VALUE IS : "<<value; | ||
return 0; | ||
} |
Oops, something went wrong.