0% found this document useful (0 votes)
78 views13 pages

Ice Cream Change Problem and Tree Algorithms

The document describes 5 coding problems related to data structures and algorithms: 1) Checking if change can be provided to all people in a queue buying ice cream given their currency denominations. 2) Partitioning a linked list around a given value x such that nodes less than x come before nodes greater than or equal to x. 3) Finding the kth largest element in a binary search tree without modifying the tree. 4) Calculating the number of turns needed to reach from one node to another in a binary tree given their data values. 5) The problems include code snippets providing sample inputs/outputs and solving each problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views13 pages

Ice Cream Change Problem and Tree Algorithms

The document describes 5 coding problems related to data structures and algorithms: 1) Checking if change can be provided to all people in a queue buying ice cream given their currency denominations. 2) Partitioning a linked list around a given value x such that nodes less than x come before nodes greater than or equal to x. 3) Finding the kth largest element in a binary search tree without modifying the tree. 4) Calculating the number of turns needed to reach from one node to another in a binary tree given their data values. 5) The problems include code snippets providing sample inputs/outputs and solving each problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Problem 2: Given an array of N integers where A[i] denotes the currency of note that the i-

th person has. The possible currencies are 5, 10, and 20. All the N people are standing in a
queue waiting to buy an ice cream from X, which costs Rs 5. Initially, X has an initial
balance of 0. Check if X will be able to provide change for all peoplewho are waiting to buy
ice cream.

Code :
#include <bits/stdc++.h>
using namespace std;

int isChangeable(int notes[], int n)

{
int fiveCount = 0;
int tenCount = 0;

for (int i = 0; i < n; i++) {

if (notes[i] == 5)
fiveCount++;
else if (notes[i] == 10) {

if (fiveCount > 0) {
fiveCount--;
tenCount++;
}
else

return 0;
}
else {

if (fiveCount > 0 && tenCount > 0) {

fiveCount--;
tenCount--;
}

else if (fiveCount >= 3) {


fiveCount -= 3;
}
else
return 0;

}
}

return 1;
}

int main()
{
int n;
cout<<"Enter number of customers : ";

cin>>n;
int* a = new int[n];
cout<<"Enter currencies with all customers : ";
for(int i=0;i<n;i++){
cin>>a[i];

if (isChangeable(a, n))
cout << "YES, all people can buy ice cream";
else
cout << "NO, all people can not buy ice cream ";
return 0;
}

Output :

Problem 3: Given a linked list and a value x, partition it such that all nodes less than xcome
before nodes greater than or equal to x.
Code:
#include <bits/stdc++.h>

using namespace std;

class Node{
public:
int data;

Node* next;
Node(int x){
data=x;
next=NULL;
}

};
Node * transform(Node * head,int x){
Node *le=NULL,*ls=NULL,*he=NULL,*hs=NULL;
while(head!=NULL){
if(head->data<x){

if(ls==NULL){
ls=le=head;
}
else{
le->next=head;

le=head;
}
}
else{
if(hs==NULL){

hs=he=head;
}
else{
he->next=head;
he=head;

}
}
head=head->next;
}
he->next=NULL;

le->next=hs;
return ls;
}

void print(Node *head){

while(head!=NULL){
cout <<head->data <<" -> " ;
head=head->next;
}
cout <<"NULL" <<endl;
}
int main()
{

Node *head=new Node(1);


head->next=new Node(4);
head->next->next=new Node(3);
head->next->next->next=new Node(5);
head->next->next->next->next=new Node(2);

head->next->next->next->next->next=new Node(4);
head->next->next->next->next->next->next=new Node(6);

print(head);
int x=4;

cout<<"partioning factor = "<<x<<endl;


Node *newhead=transform(head,x);
print(newhead);
return 0;
}

Output:

Problem 4: Given a Binary search tree. Your task is to write a complete function whichwill
return the Kth largest element without doing any modification in Binary Search Tree.
Code:
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node *left, *right;
};

// helper function to create a new Node


Node* newNode(int data)
{
Node* temp = new Node;
temp->data = data;
temp->right = temp->left = NULL;
return temp;
}

Node* KthLargestUsingMorrisTraversal(Node* root, int k)


{
Node* curr = root;
Node* Klargest = NULL;

// count variable to keep count of visited Nodes


int count = 0;

while (curr != NULL) {


// if right child is NULL
if (curr->right == NULL) {

// first increment count and check if count = k


if (++count == k)
Klargest = curr;

// otherwise move to the left child


curr = curr->left;
}

else {

// find inorder successor of current Node


Node* succ = curr->right;

while (succ->left != NULL && succ->left != curr)


succ = succ->left;

if (succ->left == NULL) {

// set left child of successor to the


// current Node
succ->left = curr;

// move current to its right


curr = curr->right;
}

// restoring the tree back to original binary


// search tree removing threaded links
else {

succ->left = NULL;
if (++count == k)
Klargest = curr;

// move current to its left child


curr = curr->left;
}
}
}

return Klargest;
}

int main()
{
// Your C++ Code
/* Constructed binary tree is
4
/ \
2 7
/ \ / \
1 3 6 10 */

Node* root = newNode(4);


root->left = newNode(2);
root->right = newNode(7);
root->left->left = newNode(1);
root->left->right = newNode(3);
root->right->left = newNode(6);
root->right->right = newNode(10);

int k;
cout<<"Enter the value of k : ";
cin>>k;
cout << "Finding K-th largest Node in BST : "
<< KthLargestUsingMorrisTraversal(root, k)->data;

return 0;
}
Output:

Problem 5: Given a binary tree and data value of two of its nodes. Find the number of
turns needed to reach from one node to another in the given binary tree.
Code:
#include <bits/stdc++.h>
using namespace std;

struct Node {
struct Node* left, *right;
int key;
};

Node* newNode(int key)


{
Node* temp = new Node;
temp->key = key;
temp->left = temp->right = NULL;
return temp;
}

struct Node* findLCA(struct Node* root,int n1, int n2)


{
if (root == NULL)
return NULL;
if (root->key == n1 || root->key == n2)
return root;

Node* left_lca = findLCA(root->left, n1, n2);


Node* right_lca = findLCA(root->right, n1, n2);
if (left_lca && right_lca)
return root;
return (left_lca != NULL) ? left_lca :right_lca;
}

bool CountTurn(Node* root, int key, bool turn,int* count)


{
if (root == NULL)
return false;

if (root->key == key)
return true;
if (turn == true) {
if (CountTurn(root->left, key, turn, count))
return true;
if (CountTurn(root->right, key, !turn, count)) {
*count += 1;
return true;
}
}
else
{
if (CountTurn(root->right, key, turn, count))
return true;
if (CountTurn(root->left, key, !turn, count)) {
*count += 1;
return true;
}
}
return false;
}

int NumberOFTurn(struct Node* root, int first,int second)


{
struct Node* LCA = findLCA(root, first, second);

if (LCA == NULL)
return -1;
int Count = 0;

if (LCA->key != first && LCA->key != second) {


if (CountTurn(LCA->right, second, false,&Count)|| CountTurn(LCA->left,
second, true,&Count));
if (CountTurn(LCA->left, first, true,&Count)|| CountTurn(LCA->right, first,
false,&Count));
return Count + 1;
}

if (LCA->key == first) {
CountTurn(LCA->right, second, false, &Count);
CountTurn(LCA->left, second, true, &Count);
return Count;
} else {
CountTurn(LCA->right, first, false, &Count);
CountTurn(LCA->left, first, true, &Count);
return Count;
}
}

int main()
{
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
root->left->left->left = newNode(8);
root->right->left->left = newNode(9);
root->right->left->right = newNode(10);
int turn = 0;
int fn,sn;
cout<<"Enter first node : ";
cin>>fn;
cout<<"Enter second node : ";
cin>>sn;
if ((turn = NumberOFTurn(root, fn, sn)))
cout << turn << endl;
else
cout << "Not Possible" << endl;

return 0;
}
Output:

You might also like