HackerRank Solutions

codewars-Pick Peaks Python

Description:

In this kata, you will create an object that returns the positions and the values of the “peaks” (or local maxima) of a numeric array.

For example, the array arr = [ 0 , 1 , 2 , 5 , 1 , 0 ] has a peak in position 3 with a value of 5 (arr[3] = 5)

The output will be returned as an object with two properties: pos and peaks. Both of these properties should be arrays. If there is no peak in the given array, then the output should be {pos: [], peaks: []}. Continue reading “codewars-Pick Peaks Python”

codewars-Sum by Factors Python

Description:

Given an array of positive or negative integers

I= [i1,..,in]

you have to produce a sorted array P of the form

[ [p, sum of all ij of I for which p is a prime factor (p positive) of ij] ...]

P will be sorted by increasing order of the prime numbers. The final result has to be given as a string in Java, C# or C++ and as an array of arrays in other languages. Continue reading “codewars-Sum by Factors Python”

hackerrank-Even Tree c++

You are given a tree (a simple connected graph with no cycles). The tree has nodes numbered from to .

Find the maximum number of edges you can remove from the tree to get a forest such that each connected component of the forest contains an even number of vertices.

Input Format
The first line of input contains two integers and . is the number of vertices, and is the number of edges.
The next lines contain two integers and which specifies an edge of the tree.

Output Format

Print the number of removed edge.

Constraints

Note: The tree in the input will be such that it can always be decomposed into components containing an even number of nodes.

Sample Input

10 9
2 1
3 1
4 3
5 2
6 1
7 2
8 6
9 8
10 8

Sample Output

2

Explanation
On removing edges and , we can get the desired result.

Original tree:

Decomposed tree:


#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

int getTotalNodes(list&li,vector<list >&adj){
    if(li.empty()) return 0;
    int count=li.size();
    list::iterator it;
    for(it=li.begin();it!=li.end();++it){
        count+=getTotalNodes(adj[*it],adj);
    }
    return count;
}

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int m,n,x,y;
    cin>>n>>m;
    vector<list > adj(n);
    for(int i=0;i>x>>y;
        if(x<y)adj[x-1].push_back(y-1);
        else  adj[y-1].push_back(x-1);
    }
    list::iterator it;
    int c=0;
    for(int i=0;i<n;i++){
        if(adj[i].empty())continue;
        for(it=adj[i].begin();it!=adj[i].end();++it){
            if((getTotalNodes(adj[*it],adj)+1)%2==0){
                // adj[i].remove(*it);
                c++;
            }
        }
    }
    cout<<c;
    return 0;
}

 

codewars-Merged String Checker Python

Description:

At a job interview, you are challenged to write an algorithm to check if a given string, s, can be formed from two other strings, part1 and part2.

The restriction is that the characters in part1 and part2 are in the same order as in s.

The interviewer gives you the following example and tells you to figure out the rest from the given test cases. Continue reading “codewars-Merged String Checker Python”

codewars-Double Cola Python

Description:

Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a “Double Cola” drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can, drinks it and gets to the end of the queue as two Leonards, and so on.

Continue reading “codewars-Double Cola Python”