1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
damaskus [11]
3 years ago
13

1. Write a recursive method to determine if a character is in a list of characters in O(logN) time. Mathematically prove (as we

did in class) that T(N) = O(logN). You can assume that this list is sorted lexicographically.
2. Write a function that determines if a string has the same number of 0’s and 1’s using a stack. The function must run in O(N) time. You can assume there already exists a stack class and can just use it
Computers and Technology
1 answer:
sergeinik [125]3 years ago
5 0

Answer:

1)

public class BinarySearch {

// Returns index of x if it is present in arr[l..

// r], else return -1

int binarySearch(char arr[], int l, int r, char x)

{

if (r >= l) {

int mid = l + (r - l) / 2;

 

// If the element is present at the

// middle itself

if (arr[mid] == x)

return mid;

 

// If element is smaller than mid, then

// it can only be present in left subarray

if (arr[mid] > x)

return binarySearch(arr, l, mid - 1, x);

 

// Else the element can only be present

// in right subarray

return binarySearch(arr, mid + 1, r, x);

}

 

// We reach here when element is not present

// in array

return -1;

}

 

// Driver method to test above

public static void main(String args[])

{

BinarySearch ob = new BinarySearch();

char arr[] = {'a','c','e','f','g','h'};

int n = arr.length;

char x = 'g';

int result = ob.binarySearch(arr, 0, n - 1, x);

if (result == -1)

System.out.println("Character not present");

else

System.out.println("Character found at index " + result);

}

}

2)

import java.io.*;

import java.util.*;

public class Test{

public static boolean checksame(String s)

{

 

Stack<Integer> stack= new Stack<Integer>();

for(int i=0;i<s.length();i++)

{

if(s.charAt(i)=='0')

{

if(stack.empty()==false)

{

if((Integer)stack.peek()==1)

stack.pop();

else

stack.push(0);

}

else

{

stack.push(0);

}

}

else if(s.charAt(i)=='1')

{

if(stack.empty()==false)

{

if((Integer)stack.peek()==0)

stack.pop();

 

else

stack.push(1);

}

else

{

stack.push(1);

}

 

}

}

return stack.empty();

}

public static void main(String []args){

System.out.println(checksame("a0w1220"));

}

}

Explanation:

You might be interested in
Windows stores information from the Credential Manager application in secure folders called
Delicious77 [7]

Windows stores information from the Credential Manager application in secure folders called<u> VAULTS.</u>

<u></u>

Explanation:

  • A credential vault is a database used to store passwords and similar cryptographic key material.
  • The most common data stored in a credential vault are current and historical passwords to privileged accounts.
  • All credentials are saved in special folders on the computer, a place called Vaults. Moreover, you can back up all credentials to a file and restore them on to different computer.
  • Vault is a shareware program that acts as a bank vault, or safe, where you can keep you private information or files hidden and secure.
  • Everything in the vault is protected with an advanced encryption, and requires a password (your password) to open the vault to access the information.
8 0
2 years ago
A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Ab
Pavlova-9 [17]

Answer:

Following are the program in the C++ Programming Language.

//header file

#include <iostream>

//header file for string

#include <string>

#include<cstring>

//using namespace

using namespace std;

//define Function to checks the string is passed is a Palindrome or not

bool is_Palin(string strng, int start_Position, int end_Position)

{

//set the if Conditional statement

if(start_Position >= end_Position)

{

return true;

}

//Check if the character is not the alphabet

if(!isalpha(strng[start_Position]))

{

//Update the starting position  

start_Position += 1;

//call Recursive function

return is_Palin(strng, start_Position++, end_Position);

}

//Check if the character is not the alphabet

if(!isalpha(strng[end_Position]))

{

//Update the end position

end_Position -= 1;

//call Recursive function

return is_Palin(strng, start_Position, end_Position--);

}

//Check if the characters are same or not same

if(tolower(strng[start_Position]) != tolower(strng[end_Position]))

{

return false;

}

//Update the positions

start_Position = start_Position + 1;

end_Position = end_Position - 1;

//call Recursive function

return is_Palin(strng, start_Position, end_Position);

}

//define Main function

int main()

{

string strng;

//get string from the user

cout << "\n\n Enter a string: ";

getline(cin, strng);

//Check for the palindrome

if(is_Palin(strng, 0, strlen(strng.c_str())-1))

{

 //then print message

cout << "\n Is a Palindrome!!!! \n";

}

//otherwise

else

{

//then print message

cout << "\n Is not a Palindrome!!!! \n";

}

return 0;

}

<u>Output:</u>

Enter a string:  Able was I, ere I saw Elba

Is a Palindrome!!!!

Explanation:

Here, we define a boolean data type function "is_Palin" to check the string which passed through is palindrome or not and pass and pass two integer type arguments for starting position and ending position and pass one string data type argument to pass string, inside the function.

  • Set the if conditional statement to check the condition if the start_position of the string is greater than end _position of the string then return true.
  • Set the if conditional statement to check the condition the start_position of the staring is alphabet then, update the start_position and call the recursion function.
  • Again set the if conditional statement to check the condition the end_position of the staring is alphabet then, update the end_position and call the recursion function.
  • Set the if conditional statement to check the condition if the start_position of the string is not equal to the end _position of the string then, return false.
  • Then, update the start_position and end _position of the string increment by 1 then, call the recursive function and then, close the function.

Finally, we define the main function and pass string through calling and passing and argument list in its parameter.  

5 0
2 years ago
Jack has a fear of getting up in front of a group of people and giving a presentation. When he gave his last presentation, he ta
dezoksy [38]

Answer:

Jack has to be very calm and try his best to present himself in an outward way and think about the fact that by the end of the night no one will remember that he messed up, I feel like he'll be okay :)

7 0
2 years ago
Read 2 more answers
c++ 2.30 LAB: Phone number breakdown Given a long long integer representing a 10-digit phone number, output the area code, prefi
Nady [450]

Answer:

#include <iostream>

using namespace std;

int main()

{

   //declare variable to store phone numbers,its area code, prefix and line number.

   long phone_number;

   int area_code, prefix,line_number;

   cout<<"Enter 10-digit phone number:"<<endl;

   //input 10-digit phone number

   cin>>phone_number;

   //logic to find area_code, prefix, and line_number.

   area_code = phone_number/10000000;

   prefix = (phone_number/10000)%1000;

   line_number = phone_number%10000;

   //output phone number in asked format.

   cout<<area_code<<"-"<<prefix<<"-"<<line_number<<endl;

   return 0;

}

Output:

Enter 10-digit phone number:

8005551212

800-555-1212

Explanation:

In the above program 10 digit phone number entered by user will be stored in the variable phone_number.

Then using this phone_number variable area_code, prefix, and line number are calculated using the following logic:

area_code = phone_number/10000000;

prefix = (phone_number/10000)%1000;

line_number = phone_number%10000;

Finally these area_code, prefix, and line_number are displayed with hyphen in between using cout statement.

5 0
2 years ago
ShellShock had the potential for an unauthorized user to gain access to a server. It affected many internet-facing services, whi
mina [271]

Answer: Windows

Explanation:

ShellShock: Also called Bashdoor, it is a security bug which is found in the Bash shell of operating system e.g. Unix. It allows the hacker to execute scripts and commands by gaining unauthorized access. This affects many machines, devices that use Bash. Servers like web servers are most affected as most of them run Unix based OS like OS X. Most vulnerable are those who use Unix or Linux operating systems.  

Microsoft Windows does not use Bash so it does not directly affect Windows os. For those who are using Windows PC, Windows phones and websites that are developed by MS software are non-vulnerable to ShellShock.

4 0
2 years ago
Other questions:
  • What role do career pathways play?
    11·2 answers
  • In this assignment, you are going to add conditional formatting to the spreadsheet you used in the previous
    13·1 answer
  • Which of the following characteristics relates to authentication header (AH)? It is a document that defines or describes compute
    11·1 answer
  • This type of technology does not come with a keyboard or mouse for input
    11·2 answers
  • A haiku is a three-line poem in which the first line contains five syllables, the second line contains seven syllables, and the
    12·1 answer
  • Assume that the array monthSales of integers has already been declared and that its elements contain sales data for the 12 month
    13·1 answer
  • Your supervisor has asked you to set up a RAID hard drive array in a tower system, which has a motherboard that uses the B360 ch
    10·1 answer
  • Brian has created the following selection sort class in Java. In which line is the index of the smallest value returned? In whic
    8·1 answer
  • How is primary storage different from secondary storage? Select the TWO correct statements. The CPU can only read and write data
    12·2 answers
  • What is multimedia hard ware? Write any four examples of multimedia hardware. <br>​
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!