You are on page 1of 2

6/20/22, 10:54 PM Q2: (05+05 Marks) Write The Code For: A) Suppose Y... | Chegg.

com

  Home Study tools


 My courses
 My books My folder Career Life 

Find solutions for your homework Search

home / study / engineering / computer science / computer science questions and answers / q2: (05+05 marks) write the code for: a) suppose you are …

Question: Q2: (05+05 Marks) Write the code for: a) Suppose you are worki… Post a question
Answers from our experts for your tough
homework questions

Enter question

Continue to post
18 questions remaining

My Textbook Solutions

Show transcribed image text

Expert Answer Astronomy Fundament... Fundament...


0th Edition
7th Edition 6th Edition

Anonymous answered this View all solutions


Was this answer helpful? 0 1
35 answers

Solution:

Q)a

This is the concept of binary serach

#include <bits/stdc++.h>

using namespace std;

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

   if (r >= l) {

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

       if (arr[mid] == x)

           return


mid;

       if (arr[mid] > x)

           return


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

       return binarySearch(arr, mid + 1,


r, x);

   }

   return -1;

int main(void)

int n=2500;

int arr[n];

for(int i=1;i<=n;i++

cin>>arr[i]=i;

   int x = 886;

   int n = sizeof(arr) / sizeof(arr[0]);

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

   (result == -1)

       cout << "Element is not


present in array"

       cout << "Element is present


at index " << result;

   return 0;

b)

View comments (1)




Questions viewed by other students

Q: LANGUAGE : C++.
Vikas is given a bag which consists of
numbers (integers) blocks, Vikas has to organize the numbers
again
in the same order as he has inserted it into the bag, i.e., the
first number inserted into the bag by Vikas should be
picked up
first followed by other numbers in series. Help Vikas to complete
this work in O(n) time complexity with the
condition to use one
extra...

A: See step-by-step answer

Q: b) Consider the following weighted connected graph. Given that the graph starts from node, write a C++ code to find
the shortest path to other vertices (Kruskal's algorithm) S 10. 11 B A 12 2 С D 2 E

A: See answer

Show more 

https://www.chegg.com/homework-help/questions-and-answers/q2-05-05-marks-write-code-suppose-working-organization-given-bundle-files-containing-2500--q92205625 1/2
6/20/22, 10:54 PM Q2: (05+05 Marks) Write The Code For: A) Suppose Y... | Chegg.com

  Home Study tools


 My courses
 My books My folder Career Life 
COMPANY LEGAL & POLICIES CHEGG PRODUCTS AND SERVICES CHEGG NETWORK CUSTOMER SERVICE

About Chegg Advertising Choices Cheap Textbooks Chegg Math Solver EasyBib Customer Service
Chegg For Good Cookie Notice Chegg Coupon Mobile Apps Internships.com Give Us Feedback
College Marketing General Policies Chegg Play Sell Textbooks Thinkful Manage Subscription
Corporate Development Intellectual Property Rights Chegg Study Help Solutions Manual
Investor Relations Terms of Use College Textbooks Study 101
Jobs Global Privacy Policy eTextbooks Textbook Rental
Join Our Affiliate Program DO NOT SELL MY INFO Flashcards Used Textbooks
Media Center Honor Code Learn Digital Access Codes
Site Map Honor Shield Uversity Chegg Life
Chegg Writing

© 2003-2022 Chegg Inc. All rights reserved.

https://www.chegg.com/homework-help/questions-and-answers/q2-05-05-marks-write-code-suppose-working-organization-given-bundle-files-containing-2500--q92205625 2/2

You might also like