Justice  Reilly

Justice Reilly

1593252960

Find GCD of each subtree of a given node in an N-ary Tree for Q queries-GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

#competitive programming #dynamic programming #greedy #mathematical #recursion #tree #dfs #gcd-lcm #n-ary-tree

What is GEEK

Buddha Community

Find GCD of each subtree of a given node in an N-ary Tree for Q queries-GeeksforGeeks
Justice  Reilly

Justice Reilly

1593252960

Find GCD of each subtree of a given node in an N-ary Tree for Q queries-GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

#competitive programming #dynamic programming #greedy #mathematical #recursion #tree #dfs #gcd-lcm #n-ary-tree

Remove all leaf nodes from a Generic Tree or N-ary Tree

Given a Generic tree, the task is to delete the leaf nodes from the tree.

** Examples:**

Input: 
              5
          /  /  \  \
         1   2   3   8
        /   / \   \
       15  4   5   6 

Output:  
5 : 1 2 3
1 :
2 :
3 :

Explanation: 
Deleted leafs are:
8, 15, 4, 5, 6

Input:      
              8
         /    |    \
       9      7       2
     / | \    |    / / | \ \
    4  5 6    10  11 1 2  2 3
Output:  
8: 9 7 2
9:
7:
2:

**Approach: **Follow the steps given below to solve the problem

  • Take tree into the vector.
  • Traverse the tree and check the condition:
  • If current node is leaf then
  • Delete the leaf from vector
  • Else
  • Recursively call for every child.

Below is the implementation of the above approach:

#data structures #recursion #tree #n-ary-tree #tree-traversal #data analysis

Desmond  Gerber

Desmond Gerber

1619421259

Find A Set Of At Most N/2 Nodes From A Graph Such That All Remaining Nodes Are Directly

Given an integer N, representing the number of nodes present in an undirected graph, with each node valued from 1 to **N, **and a  2D array Edges[][], representing the pair of vertices connected by an edge, the task is to find a set of at most N/2 nodes such that nodes that are not present in the set, are  connected adjacent to any one of the nodes present in the set.

Examples :

Input:_ N = 4, Edges[][2] = {{2, 3}, {1, 3}, {4, 2}, {1, 2}}_

Output:_ 3 2_

Explanation:_ Connections specified in the above graph are as follows:_

_   1_

_ /   _

2 – 3

|

4

Selecting the set {2, 3} satisfies the required conditions.

Input:_ N = 5, Edges[][2] = {{2, 1}, {3, 1}, {3, 2}, {4, 1}, {4, 2}, {4, 3}, {5, 1}, {5, 2}, {5, 3}, {5, 4}}_

Output:_ 1_

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

**Approach: **The given problem can be solved based on the following observations:

  • Assume a node to be the source node, then the distance of each vertex from the source node will be either odd or even.
  • Split the nodes into two different sets based on parity, the size of at least one of the sets will not exceed** N/2**. Since each node of some parity is connected to at least one node of opposite parity, the criteria of choosing at most N/2 nodes is satisfied.

Follow the steps below to solve the problem:

  • Assume any vertex to be the source node.
  • Initialize two sets, say evenParity and **oddParity, **to store the nodes having even and odd distances from the source node respectively.
  • Perform BFS Traversal on the given graph and split the vertices into two different sets depending on the parity of their distances from the source:
  • If the distance of each connected node from the source node is **odd, **then  insert the current node in the set oddParity.
  • If the distance of each connected node from the source node is **even, ** then  insert the current node in the set evenParity.
  • After completing the above steps, print the elements of the set with the minimum size.

#graph #hash #mathematical #tree #node #node.js

Ahebwe  Oscar

Ahebwe Oscar

1620185280

How model queries work in Django

How model queries work in Django

Welcome to my blog, hey everyone in this article we are going to be working with queries in Django so for any web app that you build your going to want to write a query so you can retrieve information from your database so in this article I’ll be showing you all the different ways that you can write queries and it should cover about 90% of the cases that you’ll have when you’re writing your code the other 10% depend on your specific use case you may have to get more complicated but for the most part what I cover in this article should be able to help you so let’s start with the model that I have I’ve already created it.

**Read More : **How to make Chatbot in Python.

Read More : Django Admin Full Customization step by step

let’s just get into this diagram that I made so in here:

django queries aboutDescribe each parameter in Django querset

we’re making a simple query for the myModel table so we want to pull out all the information in the database so we have this variable which is gonna hold a return value and we have our myModel models so this is simply the myModel model name so whatever you named your model just make sure you specify that and we’re gonna access the objects attribute once we get that object’s attribute we can simply use the all method and this will return all the information in the database so we’re gonna start with all and then we will go into getting single items filtering that data and go to our command prompt.

Here and we’ll actually start making our queries from here to do this let’s just go ahead and run** Python manage.py shell** and I am in my project file so make sure you’re in there when you start and what this does is it gives us an interactive shell to actually start working with our data so this is a lot like the Python shell but because we did manage.py it allows us to do things a Django way and actually query our database now open up the command prompt and let’s go ahead and start making our first queries.

#django #django model queries #django orm #django queries #django query #model django query #model query #query with django

Mikel  Okuneva

Mikel Okuneva

1597978800

Find value after N operations to remove N characters of string S with given constraints

Given a string** S** of Size N. Initially, the value of count is 0. The task is to find the value of count after N operations to remove all the N characters of the given string S where each operation is:

  • In each operation, select an alphabetically the smallest character in the string S and remove that character from S and add its index to count.
  • If multiple characters are present then remove the character having the smallest index.

Note: Consider string as 1-based indexing.

Examples:

Input:_ N = 5, S = “abcab” _

Output:_ 8 _

Explanation:

_Remove character ‘a’ then string becomes “bcab” and the count = 1. _

_Remove character ‘a’ then string becomes “bcb” and the count = 1 + 3 = 4. _

_Remove character ‘b’ then string becomes “cb” and the count = 4 + 1 = 5. _

_Remove character ‘b’ then string becomes “c” and the count = 5 + 2 = 7. _

Remove character ‘c’ then string becomes “” and the count = 7 + 1 = 8.

Input:_ N = 5 S = “aabbc” _

Output:_ 5 _

Explanation:

The value after 5 operations to remove all the 5 characters of String aabbc is 5.

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

**Naive Approach: **The idea is to check if the string is empty or not. If it is not empty then following are the steps to solve the problem:

  • Find the first occurrence of the smallest alphabets in the current string, let’s say ind and remove it from the string.
  • Increase the count by **ind **+ 1.
  • Repeat the above step until the string becomes empty.

Below is the implementation of the above approach:

  • C++
  • Java
  • C#

// C++ program for the above approach

#include <iostream>

#include <string>

**using** **namespace** std;

// Function to find the value after

// N operations to remove all the N

// characters of string S

**void** charactersCount(string str, **int** n)

{

**int** count = 0;

// Iterate till N

**while** (n > 0) {

**char** cur = str[0];

**int** ind = 0;

**for** (``**int** j = 1; j < n; j++) {

**if** (str[j] < cur) {

cur = str[j];

ind = j;

}

}

// Remove character at ind and

// decrease n(size of string)

str.erase(str.begin() + ind);

n--;

// Increase count by ind+1

count += ind + 1;

}

cout << count << endl;

}

// Driver Code

**int** main()

{

// Given string str

string str = "aabbc"``;

**int** n = 5;

// Function call

charactersCount(str, n);

**return** 0;

}

Output:

5

Time Complexity:_ O(N2)_

Auxiliary Space:_ O(1)_

**Efficient Approach: **This problem can be solved using the concept of Binary Index Tree or Fenwick Tree. Below are the steps:

  • Initially, Store the values of indices of all the characters/alphabet in increasing order.
  • Start with the smallest alphabet ‘a’ and remove all ‘a’s in the order of there occurrence. After removing, select the next alphabet ‘b’, and repeat this step until all alphabets are removed.
  • Removing the character means that its corresponding value in the array is changed to 0, indicating that it is removed.
  • Before removing, find the position of the character in the string using the sum() method in Fenwick Tree and add the position value to the count.
  • After removing all the characters in the string, the value of count is obtained.

#advanced data structure #divide and conquer #strings #tree #segment-tree #trees