Update appNew update is available. Click here to update.
About
National Institute Of Technology, Silchar, Assam 2023
C++ - Default language
My Stats
EXP gained
yellow-spark
33326
Level
7 (Expert)
Community stats
Discussions
0
Upvotes
0
Know more
Weekend contest rating
Contest attended
Problems solved
2021 2023
Better than %
Weekend contest rating
Contest attended
Problems solved
2021 2023
Better than %
560
Total problems solved
351
Easy
174
Moderate
35
Hard
0
Ninja
Jan Jan Feb Feb Mar Mar Apr Apr May May Jun Jun Jul Jul Aug Aug Sep Sep Oct Oct Nov Nov Dec Dec

Current streak:

0 days

Longest streak:

9 days

Less

More

Achievements
4
Ronin
Topics
Trees
Binary Search
Recursion
+ 1 more
8
Samurai
Topics
Arrays
Sorting
Strings
+ 5 more
Discussions
Simple C++ solution
Interview problems
#include <bits/stdc++.h> 
bool patternMatches(string word, string pattern){
    if(word.size() != pattern.size()) return false;
    unordered_map<char,char> mpp;
    unordered_set<char> usedChars;

    for(int i=0; i<word.size(); i++){
        if(mpp.find(word[i]) == mpp.end() && usedChars.find(pattern[i]) == usedChars.end()){
            mpp[word[i]] = pattern[i];
            usedChars.insert(pattern[i]);
        }
        else if(mpp[word[i]] != pattern[i]) return false;
    }

    return true;
}
vector<string> matchSpecificPattern(vector<string> words, int n, string pattern){
    // Write your code here.
    vector<string> ans;

    for(int i=0; i<n; i++){
        if(patternMatches(words[i], pattern)) ans.push_back(words[i]);
    }

    return ans;
}
profile
kanishkaray12
Published On 12-Nov-2023
39 views
0 replies
0 upvotes
Easy C++ solution
Interview problems
#include <bits/stdc++.h> 
vector<int> rotateMatRight(vector<vector<int>> mat, int n, int m, int k) {
	// Write your code here.
	k = k%m;
	vector<int> ans;
	for(int i=0; i<n; i++){
		reverse(mat[i].begin(), mat[i].end());
		reverse(mat[i].begin(), mat[i].begin()+k);
		reverse(mat[i].begin()+k, mat[i].end());

		for(int j=0; j<m; j++) ans.push_back(mat[i][j]);
	}

	return ans;
}
profile
kanishkaray12
Published On 25-Sep-2023
67 views
0 replies
0 upvotes
Easy simple solution in c++
Interview problems
#include <bits/stdc++.h>
using namespace std;

string reverseStringWordWise(string input)
{
    //Write your code here
    reverse(input.begin(), input.end());  //reverse whole input sentence
    int start=0;
    while(start<input.size()){      //reverse each word
        int end = start;
        while(end<input.size() && input[end]!=' ') end++;
        reverse(input.begin()+start, input.begin()+end);
        start = end+1;
    }
    
    return input;
}

int main()
{
    string s;
    getline(cin, s);
    string ans = reverseStringWordWise(s);
    cout << ans << endl;
}
profile
kanishkaray12
Published On 24-Sep-2023
281 views
0 replies
0 upvotes
Easy c++ solution without stack
Interview problems
int findCelebrity(int n) {
 	// Write your code here.
    int celebrity=0;
    for(int i=1;i<n;i++){
        if(knows(celebrity,i)) celebrity=i;
    }
    for(int i=0;i<n;i++){
        if(i!=celebrity && (knows(celebrity,i)==1 || knows(i,celebrity)==0)){
            return -1;
        }
    }
    return celebrity;
}
profile
kanishkaray12
Published On 07-Sep-2023
353 views
1 replies
2 upvotes
C++ easy solution
Interview problems
vector<int> findStockSpans(vector<int>& prices) {
    // Write your code here.
    stack<int> s;
    int n = prices.size();
    vector<int> ans(n);
    
    for(int i=0; i<n; i++){
        while(!s.empty() && prices[i] > prices[s.top()]) s.pop();
        ans[i] = s.empty() ? i+1 : i-s.top();
        s.push(i);
    }

    return ans;
}
profile
kanishkaray12
Published On 07-Sep-2023
299 views
0 replies
1 upvotes
Easy C++ solution | Sorting
Interview problems
int calculateMinPatforms(int at[], int dt[], int n) {
    // Write your code here.
    sort(at, at+n);
    sort(dt, dt+n);

    int currPlatformsUsed = 1, maxPlatformsUsed = 1;

    int i = 1, j = 0; // i points to next arrival, j points to departure

    while(i<n && j<n){
        if(at[i] <= dt[j]){
            currPlatformsUsed++;
            i++;

            maxPlatformsUsed = max(maxPlatformsUsed,currPlatformsUsed);
        }else{
            currPlatformsUsed--;
            j++;
        }
    }

    return maxPlatformsUsed;
}
profile
kanishkaray12
Published On 11-Jul-2023
268 views
0 replies
0 upvotes
C++ easy solution using Fermat's Little Theorem
Interview problems
#include <bits/stdc++.h> 

long long mod_exp(long long base, long long exp, long long mod) {
    if (exp == 0) return 1;
    if (exp&1==0) {  //exp is odd
        long long temp = mod_exp(base, exp / 2, mod);
        return (temp * temp) % mod;
    } else {    //exp is even
        return (base * mod_exp(base, exp - 1, mod)) % mod;
    }
}

int powerOfPower(int A, int B, int C, int M) 
{
    // Write your code her
    
    //Using Fermat's Little Theorem : a^(b^c)%m == a^(b^c % (m-1)) % m
    //First we calculate B_pow_C=(b^c % (m-1))
    //Then we calculate result=(a^B_pow_C) % m
    
    long long B_pow_C = mod_exp(B, C, M - 1); 
    long long result = mod_exp(A, B_pow_C, M);

    return result;
}
profile
kanishkaray12
Published On 18-May-2023
559 views
2 replies
0 upvotes
C++ solution
Interview problems
#include <bits/stdc++.h> 
int findRowK(vector< vector< int> > &mat) {
    // Write your code here
    int n=mat.size();
    bool flag;
    for(int i=0;i<n;i++){
        flag=true;
        for(int j=0;j<n;j++){
          if (i != j && (mat[i][j] == 1 || mat[j][i] == 0)) {
            flag = false;
            break;
          }
        }

        if(flag==true) return i;
    }

    return -1;
}
profile
kanishkaray12
Published On 11-May-2023
239 views
2 replies
3 upvotes
C++ solution optimised
Interview problems
  1. Initialize a counter count to 0. This counter will be used to track the number of modifications made in the array.
  2. Iterate through the array from index 1 (second element) to index n-1 (last element). During this iteration, we compare each element with its previous element.
  3. If we find an element arr[i] that is smaller than its previous element arr[i - 1], then we need to modify either arr[i] or arr[i - 1] so that they are in non-decreasing order.
  4. Increment the counter count by 1 since a modification is needed.
  5. Now, we have two options for modification: a) Modify arr[i - 1] such that it becomes equal to arr[i]. This can be done when there's no risk of violating non-decreasing order with elements before i-1. b) Modify arr[i] such that it becomes equal to arr[i - 1]. This can be done otherwise.
  6. The condition for choosing option (a) is checking if (i - 2 >= 0) and (arr[i - 2] > arr[i]). If this condition holds true, then modifying arr[i - 1] would violate the non-decreasing order with elements before i-1, so we choose option (b).
  7. After iterating through all elements, if our counter cnt is still less than or equal to 1, that means we have made at most one modification, and the array can be made non-decreasing. So, we return true. Otherwise, we return false.
#include <bits/stdc++.h> 
bool isPossible(int *arr, int n)
{
    //  Write your code here.
    if(n<=2) return true;

    int count=0;

    for(int i=1;i<n && count<=1 ;i++){
      if (arr[i] < arr[i - 1]) {
        count++;

        if (i-2>=0 && arr[i-2]>arr[i]) arr[i] = arr[i-1];
        else arr[i-1] = arr[i];
      }
    }

    return count<=1;
}
profile
kanishkaray12
Published On 01-May-2023
243 views
1 replies
6 upvotes
C++ Easy Solution
Interview problems
int simpleString(string s){
    // Write your code here
    int count=0;
    for(int i=0;i<s.size();i++){
        if(s[i]==s[i+1]){
            count++;
            i++;
        }
    }
    return count;
}
profile
kanishkaray12
Published On 24-Nov-2022
15 views
0 replies
0 upvotes