Skip to main content

Posts

Rotten Oranges

Task: Given a M*N matrix, each cell in the matrix with the 0 or 1 0r 2 values which means: 0: Empty cell 1: Cells have fresh oranges 2: Cells have rotten oranges We have to determine what is the minimum time required to rot all oranges. A rotten orange at index [i,j] can rot other fresh orange in indexes [i-1,j], [i+1,j], [i,j-1], [i,j+1] ( up, down, left and right) in the unit time. If it is impossible to rot every orange then return -1. Example: 1 3 5 2 1 0 2 1 1 0 1 2 1 1 0 0 2 1 Output: 2 Here is the Code: Program in C++: #include <iostream> #include <bits/stdc++.h> using namespace std; int rottenOranges(int m,int n) {     int a[m][n];     queue<pair<int,int>> q;     int p=0;     for(int i=0;i<m;i++)     {         for(int j=0;j<n;j++)         {             cin>>a[i][j];             if(a[i][j]==2)             {                 q.push(make_pair(i,j));                 p++;             }         }     }     int t=0,x,y,

All pairs with a given Sum

Task: Given two unsorted arrays A and B with N and M distinct elements respectively, Find all pairs from both arrays whose sum is equal to X. Example: Input: 1 5 5 9 1 2 4 5 7 5 6 3 4 8 Output: 1 8, 4 5, 5 4 Here is the Code: Program in C++: #include<iostream> #include<bits/stdc++.h> using namespace std; int main()  { int t; cin>>t; while(t--) {     int n,m,x;     cin>>n>>m>>x;     int a[n],b[m];     for(int i=0;i<n;i++)     {         cin>>a[i];     }     sort(a,a+n);     map<int,int> s;     for(int i=0;i<m;i++)     {         cin>>b[i];         s[b[i]]++;     }     int c=0;     for(int i=0;i<n;i++)     {         if(s.find(x-a[i])!=s.end())         {             if(c!=0) cout<<","<<" ";             cout<<a[i]<<" "<<x-a[i];             c++;         }     }     if(c==0) cout<<-1;     cout<<endl;

Next Greater Number Set Digits

Task: Given a number N, Find the smallest number with same digits of n and that greater than n. Example: Input: 1 143 Output: 314 Here is the Code: Program in C++: #include<iostream> #include<bits/stdc++.h> using namespace std; string nextGreaterNumber(string s) {     for(int i=1;i<s.length();i++)     {         if(s[i-1]>=s[i])         {             if(i+1==s.length()) return "not possible";         }         else         {             break;         }     }     stack<int> st;     for(int i=s.length()-1;i>=0;i--)     {         if(s[i-1]>=s[i])         {             st.push(i);         }         else         {             st.push(i);             int t;             while(!st.empty()&&s[i-1]<s[st.top()])             {                 t=st.top();                 st.pop();             }             swap(s[i-1],s[t]);             reverse(s.begin()+i,s.end());             return s;         }     } } int main()  { int t; scanf("%d\n&

Trapping Rain Water

Task:  Given an array of size N non-negative integers represents the blocks heights and every block width is 1 unit. Find how much rain water trapped in between blocks after raining. Example: Input: 1 6 3 0 0 2 0 4 Output: 10 // 3 + 3 + 1+ 3. Here is the Code: Program in C++: #include <iostream> using namespace std; int trappingRainWater(int n,int *a) {     int i=0,j=n-1,k,trw=0;     int left_max=a[i],right_max=a[j];     while(i!=j)     {         if(left_max>right_max)         {             k=j-1;             if(a[k]<right_max)             {                 trw+=right_max-a[k];             }             else             {                 right_max=a[k];             }             j--;         }         else         {             k=i+1;             if(a[k]<left_max)             {                 trw+=left_max-a[k];             }             else             {                 left_max=a[k];             }             i++;         }     }     return trw; } int main() { int

Spirally Traversing a Matrix

Task: Given a M*N matrix, Traverse and print the values of matrix in spiral form. Example: 1 4 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Here is the Code: Program in C++: #include<iostream> using namespace std; void spiralForm(int a[][10],int i1,int i2,int j1,int j2) {     if(i1==i2)     {         while(j1<=j2)         {             cout<<a[i1][j1]<<" ";              j1++;         }         return;     }     else if(j1==j2)     {         while(i1<=i2)         {             cout<<a[i1][j1]<<" ";             i1++;         }         return;     }     else     {         int i,j;         i=i1,j=j1;         while(j<=j2)         {             cout<<a[i][j]<<" ";             j++;         }         i++,j--;         while(i<=i2)         {             cout<<a[i][j]<<" ";             i++;         }         i--,j--;         while(j>=j1)         {