Asked inAmazon,Intern

Given a sorted array of integers, find the frequency of 2 in the array in log(n) time.

AnswerBot
2y

Find frequency of 2 in sorted array of integers in log(n) time.

  • Use binary search to find first and last occurrence of 2 in array.

  • Calculate frequency by subtracting last index from first index and addi...read more

Anonymous
1y

Time complexity isO(log)n

Anonymous
1y

Time complexity isO(log)n

Shiva Kumar
1y

Time complexity isO(log)n

Usha Sharma
1y

Time complexity is o(logn)

Anonymous
1y

Time complexity is O(log(n).

Anonymous
1y

Ues binary search to find and last occurrence of 2in array

Shashi Rajak
1y

Time complexity is 0

Jakkula Sai kundana
1y

Time complexity is 0(log(n))

Anonymous
1y

Time complexity is o(login)

Anonymous
1y

Calculate frequency by subtracting last index from first index and adding 1

R Vijay Rams
1y

Calculate frequency by subtracting last index from first index and adding 1

SURAJ SHARMA
1y

Do this without traversing the complete array expected time complexity is less then 0(n)

Rupali Raghorte
2y

Calculate frequency by substacting last index from 1 index and adding1

Anonymous
2y

Time complexity is O(login)

Anonymous
2y

Frequency of 2in the array is<

Anonymous
2y

Use binary search to find first and last occurrence of in array

Antyala Ramya
2y
Calculate frequency by substacting last index from 1 index and adding1
jayasree Matla
2y

Given sorted array arr positive

Anonymous
2y
  • Calculate frequency by subtracting last index from first index and adding 1.

SURE PAVAN KUMAR
2y
  • Time complexity is O(log(n))

Anonymous
2y

Use binary search to find first and last occurrence of 2 in arrayUse binary search to find first and last occurrence of 2 in array

Harshal Dhamankar
2y
    Use binary search to find the first occurrence of 2 in the array.Use binary search to find the last occurrence of 2 in the array.Return the difference of the two indices plus 1.

For example, if the arr...read more

Kasthuri
2y
    Use binary search to find first and last occurrence of 2 in array.Calculate frequency by subtracting last index from first index and adding 1.Time complexity is O(log(n)).
Kasthuri
2y

// C++ program to count occurrences of an element

#include<bits/.h>

using namespace std;

// Returns number of times x occurs in arr[0.]

int(int arr[], int n, int x)

{

int res = 0;

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

...read more

Vani Kranthi
2y

First occurence,last occurence

Shiek Mohammed. S
2y

Time complexity is O(log(n)).

Mohd Aijaz
2y

Last occurrance - first occurrance+1

dilsha jamshad
2y

Add 2 array then divided by total number of araay

Azmi Arshi
2y

Last occurrence - first occurrence + 1

neeraj suryavanshi
2y

(last_occurence - first_occurence + 1)

Sumit
2y

LastLast occurrence -first occurrence+1

Ahbab Ahmad
2y

Last occurrence -first occurrence+1

ARFAT SAGRI
2y

Th e idea take from last occurence

Harshitha Bangaru
2y

Last  occurrence and first occurrence+1  or any alphabet like (X or Y) which means that can be mention anything in first and last . 

Gopi Krishna palaparthi
2y

Last_occurence-frist_occurence+1

Rabi Rajak
3y

(last_occurence - first_occurence + 1)

Amar Bhorawat
3y
(last_occurence - first_occurence + 1)
Anonymous
3y
(last_occurence - first_occurence + 1)
Anonymous
4y
The idea is to use the Binary Search to find the first and the last occurrence of 2 and find the frequency using (last_occurence - first_occurence + 1)
Select
Add answer anonymously...

Amazon Intern interview questions & answers

An Intern was asked 2mo agoQ. Describe a graph-related LeetCode question that can be solved using recursion.
An Intern was asked 8mo agoQ. Explain your approach to time management.
An Intern was asked 8mo agoQ. What was your percentage score?

Popular interview questions of Intern

An Intern was asked 2mo agoQ1. Describe a graph-related LeetCode question that can be solved using recursion.
An Intern was asked 9mo agoQ2. Explain your approach to time management.
An Intern was asked 9mo agoQ3. What was your percentage score?
Amazon Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits