Question

I need the code for those contracts

I need the code for those contracts
media%2Ffe9%2Ffe913456-fbef-4865-a5bf-20
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

// class to represent the required data structure
class myStructure
{
// A resizable array
vector <int> arr;

// A hash where keys are array elements and vlaues are
// indexes in arr[]
map <int, int> Map;

public:
// A Theta(1) function to add an element to MyDS
// data structure
void add(int x)
{
// If ekement is already present, then noting to do
if(Map.find(x) != Map.end())
return;

// Else put element at the end of arr[]
int index = arr.size();
arr.push_back(x);

// and hashmap also
Map.insert(std::pair<int,int>(x, index));
}

// function to remove a number to DS in O(1)
void remove(int x)
{
// element not found then return
if(Map.find(x) == Map.end())
return;

// remove element from map
int index = Map.at(x);
Map.erase(x);

// swap with last element in arr
// then remove element at back
int last = arr.size() - 1;
swap(arr[index], arr[last]);
arr.pop_back();

// Update hash table for new index of last element
Map.at(arr[index]) = index;
}

// Returns index of element if element is present, otherwise null
int search(int x)
{
if(Map.find(x) != Map.end())
return Map.at(x);
return -1;
}

// Returns a random element from myStructure
int getRandom()
{
// Find a random index from 0 to size - 1
srand (time(NULL));
int random_index = rand() % arr.size();

// Return element at randomly picked index
return arr.at(random_index);
}   
};

// Driver main
int main()
{
myStructure ds;
ds.add(10);
ds.add(20);
ds.add(30);
ds.add(40);
cout << ds.search(30) << endl;
ds.remove(20);
ds.add(50);
cout << ds.search(50) << endl;
cout << ds.getRandom() << endl;
}

Add a comment
Know the answer?
Add Answer to:
I need the code for those contracts
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi I need some help with this I just need the code and the collection name...

    Hi I need some help with this I just need the code and the collection name is research only the code I dont need any screenshots of the output. it should be for companies.json using the research collection as like this db.research.aggregate({}) but I don't know how to do the rest. This is the database but it is hard to paste it all so I will paste some and it should be create. Please I need this to be done...

  • they need matlab code please help i need the code i need picture for the graph...

    they need matlab code please help i need the code i need picture for the graph i need it as soon as possible

  • i need to do Gauss seidel method in python code please. i need the code copy...

    i need to do Gauss seidel method in python code please. i need the code copy paste . thanks in advance.

  • Hi I need this question rewritten with the correct verb: Neither of the two contracts were...

    Hi I need this question rewritten with the correct verb: Neither of the two contracts were approved by our client.

  • I need some help with a html code, The code for the CRUD terms on the...

    I need some help with a html code, The code for the CRUD terms on the left side of the section box. I need the lines under create, read, update, delete to be the same color as the section box. Thank you! How do I make the CRUD i.e to "create" "new" on whats inside the section box. create read update delete

  • I need help with some java code concerning the comparison of two arrays. Let's say I...

    I need help with some java code concerning the comparison of two arrays. Let's say I have two arrays. Array A and Array B. Array A has 7 integers on each line, and there are hundreds of lines. Array B is the same as Array A except every line of integers have been sorted in ascending order. In Array A there are already lines that are sorted, so some lines in Array A will be equal in Array B. What...

  • I'm trying to recreate an experiment from class but I'm missing some code. I need an...

    I'm trying to recreate an experiment from class but I'm missing some code. I need an arduino uno program that will ask the user to enter a duty cycle and frequency, and to then generate a square pulse width (0-5V) based on those values.

  • (I don't need code for this question I only need explanation how to solve the question.)...

    (I don't need code for this question I only need explanation how to solve the question.) The following code causes an exception error: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class Ex02 {     public static void main(String[] args) throws IOException {         BufferedReader userInput = new BufferedReader             (new InputStreamReader(System.in));         ArrayList<String> myArr = new ArrayList<String>();         myArr.add("Zero");         myArr.add("One");         myArr.add("Two");         myArr.add("Three");                 System.out.println(myArr.get(7));                     } } Starting with this provided code,...

  • I need the Matlab code and the Solution to the problem.

    I need the Matlab code and the Solution to the problem.

  • Ex. 18-123-Long-term construction contracts (essay). In accounting for long-term construction contracts (those taking longer than one...

    Ex. 18-123-Long-term construction contracts (essay). In accounting for long-term construction contracts (those taking longer than one year to complete), the two methods commonly followed are percentage-of-completion and completed- contract Instructions (a) Discuss how earnings on long-term construction contracts are recognized and computed under these two methods. (b) Under what circumstances should one method be used over the other? (c) How are job costs and interim billings reflected on the balance sheet under the percentage-of-completion method and the completed-contract method?

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT