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

Added a problem statement #25

Merged
merged 6 commits into from Oct 13, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions Bit Manipulation/Applications_on_bit.cpp
@@ -0,0 +1,26 @@
#include <iostream>
using namespace std;

int getBit(int n,int pos){
return((n & (1<<pos))!=0);
}
int setBit(int n, int pos){
return (n|(1<<pos));
}
int clearBit(int n, int pos){
int mask=~(1<<pos);
return (n & mask);
}
int updateBit(int n, int pos, int value){
int mask= ~(1<<pos);
n = n & mask;
return (n| (value<<pos));
}
int main()
{
cout<<"get bit is: "<<getBit(5,2)<<endl;
cout<<"set bit is: "<<setBit(5,1)<<endl;
cout<<"cleared bit is: "<<clearBit(5,2)<<endl;
cout<<"updated bit is: "<<updateBit(5,1,1)<<endl;
return 0;
}
16 changes: 16 additions & 0 deletions Problems/SmallestLeft/README.md
@@ -0,0 +1,16 @@
# Smallest Left

## Problem Statement
Given an array a of integers of length n,
find the nearest smaller number for every element such that the smaller element is on left side.
If no small element present on the left print -1.

## Algorithm
A Simple Solution is to use two nested loops. The outer loop starts from the second element,
the inner loop goes to all elements on the left side of the element picked by the outer loop and stops as soon as it finds a smaller element.

## Time Complexity:
O(n2).

## Space Complexity:
O(1)
40 changes: 40 additions & 0 deletions Problems/SmallestLeft/SmallestLeft.cpp
@@ -0,0 +1,40 @@
#include <bits/stdc++.h>
using namespace std;

class Solution{
public:
vector<int> leftSmaller(int n, int a[]){
// code here
vector <int> arr(n,-1);
for(int i =1; i<n;i++){
int temp = INT_MAX;
for(int j=i-1;j>=0;j--){
if(a[j]<a[i]){
arr[i]=a[j];
break;
}
}
}
return arr;
}
};

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

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

Solution ob;
vector<int> ans = ob.leftSmaller(n,a);
for(int i =0; i<n; i++)
cout<<ans[i]<<" ";
cout<<endl;
}
return 0;
}