Arrays Flashcards

1
Q

Rotation of array O(n) time, O(1) space

A

define ll long long

#include 
using namespace std;
ll gcd(ll a,ll b)
{
    if(b==0)
        return a;
    return gcd(b,a%b);
}
int main() {
	ll t;
	cin>>t;
	while(t--)
	{
	    ll n,d,i,j;
	    cin>>n>>d;
	    ll a[n];
	    for(i=0;i>a[i];
	    }
	    ll g = gcd(d,n);
	    for(i=0;i=n)
	                k=k-n;
            if(k==i)
                break;
	            a[j]=a[k];
	            j=k;
	        }
	        a[j]=temp;
	    }
	    for(i=0;i
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Rotation of array O(n) time, O(d) space

A

define ll long long

#include
using namespace std;
int main() {
	ll t;
	cin>>t;
	while(t--)
	{
	    ll n,d,i,j;
	    cin>>n>>d;
	    vector a;
	    for(i=0;i>x;
	        a.push_back(x);
	    }
	    d = d%n;
	    vector save;
	    for(i=0;i
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Find Transition point in array containing 0 and 1, O(logn)

A
int transitionPoint(int arr[], int n) {
    int l=0,r=n-1;
    while(l<=r)
    {
        int mid = (l+r)/2;
        if(arr[mid]<1)
        {
            l=mid+1;
        }
        else
        {
            if(arr[mid-1]==1)
            {
                r=mid-1;
            }
            else
            {
                return mid;
            }
        }
    }
    return -1;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Submatrix sum queries (given 2-d array, find sum from (x1,y1) to (x2,y2) ) O(1) time for each query.

A

define ll long long

#include 
using namespace std;
int main() {
	ll t;
	cin>>t;
	while(t--)
	{
	    ll n,m,i,j,x1,y1,x2,y2,sum=0;
	    cin>>n>>m;
	    ll a[n+1][m+1];
	    for(i=1;i<=n;i++)
	    {
	        for(j=1;j<=m;j++)
	        {
	            cin>>a[i][j];
	        }
	    }
	    cin>>x1>>y1>>x2>>y2;
	    for(i=1;i<=n;i++)
	    {
	        for(j=2;j<=m;j++)
	        {
	            a[i][j]+=a[i][j-1];
	        }
	    }
	    for(i=2;i<=n;i++)
	    {
	        for(j=1;j<=m;j++)
	        {
	            a[i][j]+=a[i-1][j];
	        }
	    }
	    if(x2>=1 &amp;&amp; y2>=1)
	        sum+=a[x2][y2];
	    if(x1-1>=1 &amp;&amp; y2>=1)
	        sum-=a[x1-1][y2];
	    if(x2>=0 &amp;&amp; y1-1>=1)
	        sum-=a[x2][y1-1];
	    if(x1-1>=1 &amp;&amp; y1-1>=1)
	        sum+=a[x1-1][y1-1];
	    cout
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Count number of 0’s in sorted array of 0 and 1 O(logn)

A

define ll long long

#include 
using namespace std;
int main() {
	ll t;
	cin>>t;
	while(t--)
	{
	    ll n,i;
    	cin>>n;
    	ll a[n];
    	for(i=0;i>a[i];
    	}
    	ll l=0,r=n-1,ans=-1;
    	while(l<=r)
    	{
    	    ll mid = (l+r)/2;
    	    if(a[mid]>0)
    	    {
    	        l=mid+1;
    	    }
    	    if(a[mid]==0)
    	    {
    	        if(a[mid-1]>0)
    	        {
    	            ans = mid;
    	            break;
    	        }
    	        else
    	        {
    	            r=mid-1;
    	        }
    	    }
    	}
    	if(ans==-1)
    	    ans=n;
    	cout<
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Rotate a 2-D (N X N) array 90 degree clockwise without using extra space O(N^2)

A

define ll long long

#include 
using namespace std;
int main() {
    ll t;
    cin>>t;
    while(t--)
    {
        ll n,i,j;
        cin>>n;
        ll a[n][n];
        for(i=0;i>a[i][j];
            }
        }
        //find A transpose
        for(i=0;i
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Magic Array (i

A
#include 
using namespace std;
#define ll long long
#define N 1000000
int main() {
	ll t;
	cin>>t;
	while(t--)
	{
	    ll n,i,j;
	    cin>>n;
	    ll count[N],ans=0;
	    vector a;
	    ll st[n];
	    memset(count,0,sizeof(count));
	    for(i=0;i>x;
	        a.push_back(x);
	    }
	    for(i=0;i
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Product array puzzle (product of all array values except the value of index itself) Space : O(n) Time: O(n)

A
vector productExceptSelf(vector&amp; nums) {
        int n=nums.size(),i;
        vector p(n);
        int temp=1;
        if(n==1)
        {
            p.push_back(0);
            return p;
        }
        for(i=0;i=0;i--)
        {
            p[i] = p[i]*temp;
            temp*=nums[i]; 
        }
        return p;
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Kadanes’s algorithm (Maximum sum contiguous subarray) O(n)

A

define ll long long

#include 
using namespace std;
int main() {
	ll t;
	cin>>t;
	while(t--)
	{
	    ll n,i;
	    cin>>n;
	    ll a[n];
	    for(i=0;i>a[i];
	    }
	    ll maxm = INT_MIN ,sum = 0;
	    for(i=0;isum)
	        {
	            sum=a[i];
	        }
	        else
	        {
	            sum+=a[i];
	        }
	        if(sum>maxm)
	        {
	            maxm = sum;
	        }
	    }
	    cout<
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Maximum product subarray O(n)

A

define ll long long

#include 
using namespace std;
int main() {
	ll t;
	cin>>t;
	while(t--)
	{
	    ll n,i;
	    cin>>n;
	    ll a[n];
	    for(i=0;i>a[i];
	    }
	    ll maxm = a[0], minm = a[0] , ans = a[0] , flag=0;
	    for(i=1;i
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Convert all 0’s to 5 in a number ‘n’ O(k) where k is number of digits in n. (other method - recursion)

A
int convertFive(int n) {
    int d = 1,num = n,f = 0;
if(n==0)
    return 5;
    while(num>0)
    {
        if(num%10==0)
        {
            f = 5*d + f;
        }
        d*=10;
        num/=10;
    }
    return (n + f);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Maximum sum of lengths of non-overlapping subarrays with k as the max element O(n)

A

define ll long long

#include 
using namespace std;
int main() {
	ll t;
	cin>>t;
	while(t--)
	{
	    ll n,k,i,l=0,count=0,maxm=INT_MIN;
	    cin>>n;
	    ll a[n];
	    for(i=0;i>a[i];
	    }
	    cin>>k;
	    for(i=0;i
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Find next greater number with same set of digits O(n) - spoj

A
#include 
using namespace std;
#define ll long long
#define N 1000000
int main() {
    ll t;
    cin>>t;
    while(t--)
    {
        ll n,i,save=-1,next,small=-1;
        cin>>n;
        ll a[n];
        for(i=0;i>a[i];
        }
        for(i=n-1;i>=1;i--)
        {
            if(a[i]>a[i-1])
            {
                save=i-1;
                next = i;
                break;
            }
        }
        if(save==-1)
        {
            cout<
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Count pairs with given sum, time : O(n), space: O(n) using map method

A

define ll long long

using namespace std;

int main() {
	ll t;
	cin>>t;
	while(t--)
	{
	    ll n,k,i,count=0;
	    cin>>n>>k;
	    ll a[n];
	    map mp;
	    for(i=0;i>a[i];
	        mp[a[i]]++;
	    }
	    for(i=0;i
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

First negative integer in every window of size k O(n) - Another approach in queue flashcard using deque.

A

define ll long long

#include 
using namespace std;
int main() {
	ll t;
	cin>>t;
	while(t--)
	{
	    ll n,k,i,j;
	    cin>>n;
	    ll a[n];
	    deque d;
	    for(i=0;i>a[i];
	    }
	    cin>>k;
	    for(i=0;i
How well did you know this?
1
Not at all
2
3
4
5
Perfectly