Skip to content

Commit

Permalink
asked by Top MNCs
Browse files Browse the repository at this point in the history
  • Loading branch information
ptarafdar001 authored Jan 4, 2021
1 parent 8af6247 commit 9d2bdf4
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 0 deletions.
44 changes: 44 additions & 0 deletions circuMaxSumSubArray.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// maximum Circular SubArray sum problem

#include<iostream>
#include<climits>
using namespace std;

int kadane(int arr[], int n){
int currSum = 0;
int maxSum = INT_MIN;
for(int i=0; i<n; i++){
currSum += arr[i];
if(currSum<0)
currSum = 0;
maxSum = max(maxSum, currSum);
}
return maxSum;
}

int main()
{
int n;
cin>>n;

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

int wrapsum= 0;
int nonwrapsum = 0;

nonwrapsum = kadane(arr, n);

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

wrapsum = totalSum-(-kadane(arr, n));

cout<<max(wrapsum, nonwrapsum)<<endl;
return 0;
}
45 changes: 45 additions & 0 deletions firstReptingele.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// first repeating element
// Amazon, Oracle

#include"bits/stdc++.h"
using namespace std;

int main()
{
int n;
cin>>n;

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

const int N = 1e6+2;
int idx[N];
for(int i=0; i<n; i++)
{
idx[i]=-1;
}

int minidx = INT_MAX;

for(int i=0; i<n; i++)
{
if(idx[arr[i]] != -1)
{
minidx = min(minidx, idx[arr[i]]);
}
else
{
idx[arr[i]] = i;
}
}

if(minidx == INT_MAX)
cout<<"-1"<<endl;
else
cout<<minidx+1<<endl;

return 0;
}
62 changes: 62 additions & 0 deletions maxSumSubArray.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// maximum subArray sum problem

#include<iostream>
#include<climits>
using namespace std;

int main()
{
int n;
cin>>n;

int arr[n];
for(int i=0; i<n; i++){
cin>>arr[i];
}
// Brute force Approach - O(n3)

/*int maxSum = INT_MIN;
for(int i=0; i<n; i++){
for(int j=i; j<n; j++){
int sum = 0;
for(int k=i; k<=j; k++){
sum += arr[k];
//cout<<arr[k]<<" ";
}
//cout<<endl;
maxSum=max(maxSum, sum);
}
}*/

// cumiletive Sum Approach- O(n2)

/*int currSum[n+1];
currSum[0] = 0;
for(int i=1; i<=n; i++){
currSum[i] = currSum[i-1]+arr[i-1];
}
int maxSum = INT_MIN;
for(int i=1; i<=n; i++){
int sum=0;
for(int j=0; j<i; j++){
sum = currSum[i]-currSum[j];
maxSum = max(sum, maxSum);
}
}*/

// Kadane's Algorithm - O(n)

int currentSum = 0;
int maxSum = INT_MIN;
for(int i=0; i<n; i++){
currentSum += arr[i];
if (currentSum<0)
currentSum = 0;
maxSum = max(maxSum, currentSum);
}

cout<<maxSum<<endl;
return 0;
}
59 changes: 59 additions & 0 deletions pairSum.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Pair Sum problem

#include<iostream>
#include<climits>
#include<algorithm>

using namespace std;

bool pairsum(int arr[], int n, int k){

// time - O(n2)
/*for(int i=0; i<n-1; i++){
for(int j=i+1; j<n; j++){
if(arr[i]+arr[j]==k){
cout<<i<<" "<<j<<endl;
return true;
}
}
}
return false;
*/

// time -- O(n)

int low = 0;
int high = n-1;

while(low<high){
if(arr[low]+arr[high]==k){
cout<<low<<" "<<high<<endl;
return true;
}
else if(arr[low]+arr[high]>k){
high--;
}
else{
low++;
}
}
return false;
}

int main()
{
int n;
cin>>n;

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

cout<<pairsum(arr, n, k)<<endl;

return 0;

}
41 changes: 41 additions & 0 deletions smallPositiveMissingNum.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// smallest positive missing number
// Accolite, Amazon, Sumsung, Snapdeal

#include"bits/stdc++.h"
using namespace std;

int main()
{
int n;
cin>>n;

int arr[n];
for(int i=0; i<n; i++)
{
cin>>arr[i];
}
const int N = 1e6+2;
bool check[N];
for(int i=0; i<n; i++)
{
check[i] = 0;
}
for(int i=0; i<n; i++)
{
if(arr[i]>=0)
{
check[arr[i]] = 1;
}
}
int ans = -1;
for(int i=1; i<N; i++)
{
if(check[i]==false)
{
ans = i;
break;
}
}
cout<< ans << endl;
return 0;
}
51 changes: 51 additions & 0 deletions subArrayGivenSum.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//subArray with given Sum
// Google, Amazon, Facebook, VISA

#include"bits/stdc++.h"
using namespace std;

int main()
{
int n, s;
cin>>n >>s;

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

int i=0, j=0, st=-1, en=-1, sum=0;

while(j<n && sum+arr[j]<=s)
{
sum +=arr[j];
j++;
}

if(sum == s)
{
cout<< i+1<<" "<<j <<endl;
return 0;
}

while(j<n)
{
sum += arr[j];
while(sum>s)
{
sum -= arr[i];
i++;
}
if(sum==s)
{
st = i+1;
en = j+1;
break;
}
j++;
}
cout<<st <<" " <<en <<endl;

return 0;
}

0 comments on commit 9d2bdf4

Please sign in to comment.