Saturday 28 December 2019

Input:
Graph(directed and undirected)

Objective:
Finding Mother Vertex. 

Approach:
Simple approach using DFS, more time complexity. This helps just to understand DFS better.
Implemented using java.



package dfs;
import java.io.*;
import java.util.*;


public class Graph {

int V;
LinkedList<Integer> adj[];

Graph(int V)
{
this.V = V;
adj = new LinkedList[V];
for(int i=0;i<V;i++)
adj[i] = new LinkedList();
}

void addEdge(int u,int v)
{
adj[u].add(v);
}

void DFS_Util(boolean visited[], int s)
{
visited[s] = true;
// System.out.println(s+" ");

Iterator<Integer> i = adj[s].listIterator();
while(i.hasNext())
{
int n = i.next();
if(!visited[n])
DFS_Util(visited,n);
}
}

int DFS(int s)
{
int ans = 0;
boolean visited[] = new boolean[V];
DFS_Util(visited,s);
for(int i=0;i<V;i++)
{
if(visited[i] == false)
ans=0;
else
ans=1;
}
return ans;
}


public static void main(String args[])
    {
        Graph g = new Graph(7);
       
        g.addEdge(0, 1);
        g.addEdge(0, 2);
        g.addEdge(1, 3);
        g.addEdge(4, 1);
        g.addEdge(6, 4);
        g.addEdge(5, 6);
        g.addEdge(5, 2);
        g.addEdge(6, 0);
     
        for(int i=0;i<7;i++)
        {
        if(g.DFS(i) != 1)
        continue;
        else
        {
        System.out.println(i);
        break;
        }
        }
    }


}

Sunday 17 March 2019

Input: 


The number of node(optional/removable) v , then the number of edges e, then the edges one by one and lastly the starting node.

Objective:


DFS algorithm 
Using recursive method, hence no pushing and popping into any stack.
Input  is taken by adjacency list approach


Approach:


Using c++ stl, vector (each element of the vector is a list of elements). 

Try it here


 #include <bits/stdc++.h>
#define MAX_SIZE 100
using namespace std;


 void dfs(std::vector<int>*, int, bool*); 

void dfs(vector <int> gph[MAX_SIZE], int ss, bool visited[MAX_SIZE])
{
    visited[ss]=true;
    cout<<" "<<ss<<endl;
    //s.push(ss);
    
    for(int j=0;j<gph[ss].size();j++)
    {
        int t=gph[ss][j];
        if((visited[t])==false)
            dfs(gph,gph[ss][j],visited);
        //cout<<" "<<g[s][j]<<endl;
        
    }
    
}

int main()
{
    int v;
    int e;
    
    vector <int> g[MAX_SIZE];
    bool visited[MAX_SIZE];
    
    for(int i=0;i<v;i++)
    {
        visited[i]=false;
    }
    
    
    
    int start;
    
    cin>>v;
    cin>>e;
    int x;
    int y;
    for(int i=0;i<e;i++)
    {
        cin>>x>>y;
        g[x].push_back(y); 
        g[y].push_back(x); //because its undirected here
        
    }
    cin>>start;

    dfs(g, start, visited);


    return 0;
}

Saturday 9 March 2019

 

The Best Player from HackerEarth 
Try here

Approach:


Using c++ stl, pair, sort method



Run and check at  here


#include<bits/stdc++.h>
#define MAX_SIZE 1000
using namespace std;

bool sorting_it(const pair<string,long> &a, const pair<string,long> &b ) {
    
    
    if(a.second!=b.second)
        return (a.second>b.second);
    else
        return(a.first<b.first);
    
}

int main()
{
    long N;
    long T;
    cin>>N>>T;
    string str;
    long q;
    pair<string,long> p[MAX_SIZE];
    for(long i=0;i<N;i++)
    {
        cin>>str>>q;
        p[i]=make_pair(str,q);
    }
    sort(p,p+N,sorting_it);
    for(long i=0;i<T;i++)
    {
       cout<<p[i].first<<endl;
      
    }
    return 0;
}  

Saturday 2 March 2019

Input: 


T number of test-cases, each with n number of strings

Objective:


To find the most repeating string (in dictionary order) for each test case.

Approach:


Using c++ stl, map

Try it here


 #include <bits/stdc++.h>  
 using namespace std;  
 int main()  
 {  
   int t;  
   cin>>t;  
   while(t--){  
        int n; cin>>n;  
        unordered_map<string, int> um;  
        string word;  
        for(int i = 0; i < n; i++){  
          cin>>word;  
          um[word]++;  
        }  
        unordered_map<string, int> :: iterator it;  
        string res; int max = 0;  
        for(auto it : um){  
          if(it.second > max){  
            max = it.second;  
            res = it.first;  
          }  
          else if(it.second == max && it.first < res){  
            max = it.second;  
            res = it.first;  
          }  
        }  
        cout<<res<<endl;  
      }  
      return 0;  
 }  

Friday 1 March 2019

Input: 


2 arrays (one with x co-ordinates and another with y co-ordinates) to describe the points

Objective:


To arrange in ascending order of the x-ordinates and if same then descending order of their y-ordinates

Approach:


Using c++ stl, vector, pair, sort method

(It was asked in amazon 2019 campus recruitment for 20 marks)

Run and check at  here


 #include<bits/stdc++.h>  
 using namespace std;  
 bool sortBylogic(const pair<int,int> &a,  
 const pair<int,int> &b)  
 {  
 if(a.first!=b.first)  
 return(a.first < b.first);  
 else  
 return(a.second > b.second);  
 }  
 int main()  
 {  
 //{4,3},{2,1},{2,6},{3,0} => 2,6 2,1 3,0 4,3  
 vector< pair <int,int> > pts; //both x and y coordinates of the point will be stored here as pairs  
 int inX[]={4,2,2,3};  
 int inY[]={3,1,6,0};  
 for(int i=0;i<4;i++)  
 {  
 pts.push_back(make_pair(inX[i],inY[i]));  
 }  
 sort(pts.begin(),pts.end(),sortBylogic);  
 for (int i=0; i<4; i++)  
 {  
 cout << pts[i].first << " "<< pts[i].second << endl;  
 }  
 }