Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cpp prob #78

Merged
merged 2 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions C++ Problem/Arrays/find_second_largest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

#include <iostream>
using namespace std;

// returns the index of second largest
// if second largest does not exist return -1
int secondLargest(int arr[], int n) {
int index;
int max = arr[0];
int second_max =0;
for(int i=0; i<n; i++){
if(arr[i]>max){
max = arr[i];
index = i;
}
}
for(int i=0; i<n; i++){
if(i!= index){
if(arr[i]>second_max){
second_max = arr[i];
}
}
}
return second_max;
}

int main() {
int arr[] = {10, 12, 20, 4};
int index = secondLargest(arr, sizeof(arr)/sizeof(arr[0]));
if (index == -1)
cout << "Second Largest do not exist";
else
cout << "Second largest : " << index;
}
//Time Complexity: O(n).
Binary file added C++ Problem/Arrays/find_second_largest.exe
Binary file not shown.
48 changes: 48 additions & 0 deletions C++ Problem/Arrays/left_rotate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <iostream>
#include <cmath>
using namespace std;


void leftRotate(int arr[], int d, int n)
{
int temp[d];

for(int i = 0; i < d; i++)
{
temp[i] = arr[i];
}

for(int i = d; i < n; i++)
{
arr[i - d] = arr[i];
}

for(int i = 0; i < d; i++)
{
arr[n - d + i] = temp[i];
}
}

int main() {

int arr[] = {1, 2, 3, 4, 5}, n = 5, d = 2;

cout<<"Before Rotation"<<endl;

for(int i = 0; i < n; i++)
{
cout<<arr[i]<<" ";
}

cout<<endl;

leftRotate(arr, d, n);

cout<<"After Rotation"<<endl;

for(int i = 0; i < n; i++)
{
cout<<arr[i]<<" ";
}

}
Binary file added C++ Problem/Arrays/left_rotate.exe
Binary file not shown.
17 changes: 17 additions & 0 deletions C++ Problem/Arrays/max_consecutive_1s.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include<iostream>
using namespace std;
int max_1s(bool arr[],int n){
int count =0;
for(int i=0; i<n; i++)
if (arr[i+1] == arr[i] == 1)
count ++;
return count;

}
int main(){
bool arr[]={1,0,1,1,0,1,1,1};
int res=max_1s(arr,sizeof(arr)/sizeof(arr[0]) );
cout<<res;

return 0;
}
Binary file added C++ Problem/Arrays/max_consecutive_1s.exe
Binary file not shown.
42 changes: 42 additions & 0 deletions C++ Problem/Arrays/remove_duplicates.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include<iostream>
using namespace std;
int remove_duplicate(int arr[], int n){
int temp[n];
temp[0] = arr[0]; //as always first element must be there
int res =1;
for(int i=1; i<n; i++){
if(temp[res-1]!=arr[i]){
temp[res++]=arr[i];
}
}
for(int i=0;i<res; i++){
arr[i] = temp[i]; //create a temporary array

}
return res;
}

int main(){
int arr[] = {10, 20, 20, 30, 30, 30}, n = 6;

cout<<"Before Removal"<<endl;

for(int i = 0; i < n; i++)
{
cout<<"arr["<<i<<"] = "<<arr[i]<<" "<<endl;
}

cout<<endl;

n=remove_duplicate(arr, n);

cout<<"After Removal"<<endl;

for(int i = 0; i < n; i++)
{
cout<<"arr["<<i<<"] = "<<arr[i]<<" "<<endl;
}

cout<<endl;

}
Binary file added C++ Problem/Arrays/remove_duplicates.exe
Binary file not shown.
19 changes: 19 additions & 0 deletions C++ Problem/Bit Manipulation/check_kth_bit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include<iostream>
using namespace std;
void check_kth(int n, int k){
int temp = 1<<(k-1); //left shift 1 binary by k-1
if((temp & n)!=0)
cout<<"set"; //if temp and 1 and logic is non zero
else
cout<<"not set";//if it's zero.

//T.C = O(1) S.C = O(1)
}

int main(){
int n = 5, k = 1;

check_kth(n, k);

return 0;
}
25 changes: 25 additions & 0 deletions C++ Problem/Sorting/bubble_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <iostream>
using namespace std;

void swap(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}
int bubble(int arr[], int n){
for(int i =0; i<n-1; i++){
for(int j=0; j<n-i-1; j++)
if(arr[j]>arr[j+1])
swap(&arr[j], &arr[j+1]);
}
}
//Time Complexity: O(N2)

int main(){
int arr[]={34,32,89,43,2,10,7,14};
int n = sizeof(arr)/sizeof(arr[0]);
bubble(arr,n);
for(int i=0; i<n; i++){
cout<<"arr["<<i<<"] = "<<arr[i]<<endl;
}
}
47 changes: 47 additions & 0 deletions C++ Problem/Sorting/merge_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include<iostream>
using namespace std;
void merge(int arr[], int si, int mid, int ei){

int a = (ei-si+1); int x=0; int merged[a]; //we created a new array which will store the sorted elements.
int indx1 = si; int indx2 = mid+1;

while(indx1<= mid && indx2<=ei){
if(arr[indx1]<arr[indx2]){
merged[x++]=arr[indx1++];
}
else{
merged[x++]=arr[indx2++];
}
}
while(indx1 <= mid)
merged[x++]=arr[indx1++];
while(indx2<=ei)
merged[x++]=arr[indx2++];

for(int i=0, j=si; i<a; i++,j++){
arr[j]=merged[i];

}

}


void divide(int arr[], int si, int ei){
if(si>=ei){
return;
}
int mid = si +(ei-si)/2; //we write this bcz it helps in better space complexity.
divide(arr, si,mid);
divide(arr,mid+1,ei);
merge(arr,si,mid,ei);

}
int main(){
int arr[]={6,3,9,5,2,8};
int n = sizeof(arr)/sizeof(arr[0]);
divide(arr,0,n-1);
for(int i=0; i<n; i++){
cout<<arr[i]<<endl;
}
return 0;
}
40 changes: 40 additions & 0 deletions C++ Problem/Sorting/quick_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include<iostream>
using namespace std;

int partition(int arr[],int si,int ei){
int pivot = arr[ei];
int i = si-1;
for(int j=si; j<ei; j++){
if(arr[j]<pivot)
{
i++; //swap
int temp = arr[i];
arr[i] = arr[j];
arr[j] = arr[i];
}
}
//swap pivot
i++;
int temp = arr[i];
arr[i]= arr[ei];
arr[ei] = temp;
return i;


}
int quick(int arr[], int si, int ei){
if(si<ei){
int pi=partition(arr,si,ei); //pi= pivot_index
quick(arr,si, pi-1);
quick(arr,pi+1,ei);
}
}
int main(){
int arr[]={2,1,3,0,6,4,8};
int n = sizeof(arr)/sizeof(arr[0]);
quick(arr,0,n-1);
for(int i=0; i<n; i++){
cout<<arr[i]<<endl;
}
return 0;
}
35 changes: 35 additions & 0 deletions C++ Problem/Sorting/selection_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

#include <iostream>
using namespace std;

int swap(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}

void selectionSort(int arr[], int n)
{
// One by one move boundary of unsorted subarray
for (int i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;

// Swap the found minimum element with the first element
swap(&arr[min_idx], &arr[i]);
}
}
//Time Complexity: O(N2)

int main(){
int arr[]={34,32,89,43,2,10,7,14};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr,n);
for(int i=0; i<n; i++){
cout<<"arr["<<i<<"] = "<<arr[i]<<endl;
}
}