A blog on programming and software design interview problems and solutions. Also there are some application development related stuff.
Pages
Friday, June 18, 2021
Saturday, January 25, 2020
Introduction to Template and Function Pointer in C++
#include <iostream>
using namespace std;
template<typename T>
bool ascending(T a, T b) {
return a > b;
}
template<typename T>
bool descending(T a, T b) {
return a < b;
}
template<typename T>
void sort(T *p, int size, bool (*f)(T a, T b)) {
for (int i = 0; i < size; i++) {
for(int j = i; j < size; j++) {
if (f(p[i], p[j])) {
swap(p[i], p[j]);
}
}
}
}
template<typename T>
void swap(T *a, T *b) {
T temp = *a;
*a = *b;
*b = temp;
}
template<typename T>
void print(T* q, int size) {
for(int i = 0; i < size; i++) {
cout<<q[i]<<" ";
}
cout<<endl;
}
void expSwap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
int main()
{
int i = 10, j = 20;
expSwap(&i, &j);
cout<<i<<" "<<j<<endl;
int a = 0, b = 1;
swap(a, b);
cout<<a<<" "<<b<<endl;
double c = 1.05, d = 2.05;
swap(c, d);
cout<<c<<" "<<d<<endl;
int A[] = {5, 1, 2, 3, 4};
print(A, 5);
sort(A, 5, ascending);
print(A, 5);
sort(A, 5, descending);
print(A, 5);
char C[] = {'e', 'a', 'b', 'c', 'd'};
print(C, 5);
sort(C, 5, ascending);
print(C, 5);
sort(C, 5, descending);
print(C, 5);
double D[] = {5.05, 1.01, 2.02, 3.03, 4.04};
print(D, 5);
sort(D, 5, ascending);
print(D, 5);
sort(D, 5, descending);
print(D, 5);
return 0;
}
Saturday, June 2, 2018
Alexa Skill Shopping List- Python
The following Python code asks for user consent to access the Alexa Shopping List.
Once permission is granted in the companion app, the skill can get the items in customer's Shopping List and add items to the Shopping List.
Once permission is granted in the companion app, the skill can get the items in customer's Shopping List and add items to the Shopping List.
def build_speechlet_response(title, output, reprompt_text, should_end_session):
return {
'outputSpeech': {
'type': 'PlainText',
'text': output
},
'card': {
'type': 'Simple',
'title': "SessionSpeechlet - " + title,
'content': "SessionSpeechlet - " + output
},
'reprompt': {
'outputSpeech': {
'type': 'PlainText',
'text': reprompt_text
}
},
'shouldEndSession': should_end_session
}
def build_permissions_consent_response(title, output, reprompt_text, should_end_session):
return {
'outputSpeech': {
'type': 'PlainText',
'text': output
},
'card': {
'type': 'AskForPermissionsConsent',
'permissions': [
"read::alexa:household:list",
"write::alexa:household:list"
]
},
'reprompt': {
'outputSpeech': {
'type': 'PlainText',
'text': reprompt_text
}
},
'shouldEndSession': should_end_session
}
def build_response(session_attributes, speechlet_response):
return {
'version': '1.0',
'sessionAttributes': session_attributes,
'response': speechlet_response
}
def handle_get_shopping_list_intent(intent, session, context):
session_attributes = {}
if "attributes" in session and "userInfo" in session["attributes"]:
session_attributes["userInfo"] = session["attributes"]["userInfo"]
card_title = "Get Shopping List Intent"
reprompt_text = "Please ask me product question"
should_end_session = False
try:
url = "https://api.amazonalexa.com:443/v2/householdlists/"
request = urllib2.Request(url, \
headers = {"Authorization": "Bearer " + context["System"]["apiAccessToken"]})
response = json.loads(urllib2.urlopen(request).read())
alexaList = response["lists"]
shoppingList = {}
for item in response["lists"]:
print(item)
if item["name"] == "Alexa shopping list":
shoppingList = item
print(shoppingList)
url = "https://api.amazonalexa.com:443"
for item in shoppingList["statusMap"]:
if item["status"] == "active":
url += item["href"]
request = urllib2.Request(url, \
headers = {"Authorization": "Bearer " + context["System"]["apiAccessToken"]})
response = json.loads(urllib2.urlopen(request).read())
print(response)
if len(response["items"]) == 0:
speech_output = "Currently there are no items in your Alexa Shopping List"
else:
item_set = Set([])
speech_output = "Currently the following items are in your Alexa Shopping List: "
for i in range(0, len(response["items"])):
item_set.add(response["items"][i]["value"])
for item in item_set:
speech_output += item + ", "
except urllib2.HTTPError as err:
if err.code == 403:
speech_output = "Please grant shopping list permissions to this skill in your Alexa app."
return build_response({}, build_permissions_consent_response(
card_title, speech_output, reprompt_text, should_end_session))
else:
speech_output = "Something went wrong while getting your shopping list"
except:
speech_output = "Something went wrong while getting your shopping list"
return build_response({}, build_speechlet_response(
card_title, speech_output, reprompt_text, should_end_session))
def handle_add_shopping_list_intent(intent, session, context):
session_attributes = {}
if "attributes" in session and "userInfo" in session["attributes"]:
session_attributes["userInfo"] = session["attributes"]["userInfo"]
card_title = "Add to Shopping List Intent"
reprompt_text = "Please ask me product question"
should_end_session = False
if "value" not in intent["slots"]["product"]:
return build_response(session_attributes, build_speechlet_response_with_directive_nointent())
product_name = intent["slots"]["product"]["value"]
product_name = spell_check(product_name)
try:
url = "https://api.amazonalexa.com:443/v2/householdlists/"
request = urllib2.Request(url, \
headers = {"Authorization": "Bearer " + context["System"]["apiAccessToken"]})
response = json.loads(urllib2.urlopen(request).read())
alexaList = response["lists"]
shoppingList = {}
for item in response["lists"]:
print(item)
if item["name"] == "Alexa shopping list":
shoppingList = item
print(shoppingList)
url = "https://api.amazonalexa.com:443"
for item in shoppingList["statusMap"]:
if item["status"] == "active":
href = item["href"].replace("active", "items")
url += href
print(url)
data = {"value": product_name, "status": "active"}
#data = urllib.urlencode(data)
request = urllib2.Request(url)
headers = {"Authorization": "Bearer " + context["System"]["apiAccessToken"], "Content-Type": "application/json"}
request.add_header("Content-Type", "application/json")
request.add_header("Authorization", "Bearer " + context["System"]["apiAccessToken"])
result = urllib2.urlopen(request, json.dumps(data))
response = json.loads(result.read())
print(response)
if result.getcode() >= 200 and result.getcode() < 300:
speech_output = product_name + " is added to your Alexa Shopping List"
else:
speech_output = "Something went wrong while adding " + product_name + " to your Shopping List"
except urllib2.HTTPError as err:
if err.code == 403:
speech_output = "Please grant shopping list permissions to this skill in your Alexa app."
return build_response({}, build_permissions_consent_response(
card_title, speech_output, reprompt_text, should_end_session))
else:
speech_output = "Something went wrong while adding " + product_name + " to your shopping list"
except:
speech_output = "Something went wrong while adding " + product_name + " to your Shopping List"
return build_response({}, build_speechlet_response(
card_title, speech_output, reprompt_text, should_end_session))
def on_intent(intent_request, session, context):
""" Called when the user specifies an intent for this skill """
print("on_intent requestId=" + intent_request['requestId'] +
", sessionId=" + session['sessionId'])
intent = intent_request['intent']
intent_name = intent_request['intent']['name']
# Dispatch to your skill's intent handlers
if intent_name == "GetShoppingListIntent":
return handle_get_shopping_list_intent(intent, session, context)
elif intent_name == "AddToShoppingList":
return handle_add_shopping_list_intent(intent, session, context)
# --------------- Main handler ------------------
def lambda_handler(event, context):
""" Route the incoming request based on type (LaunchRequest, IntentRequest,
etc.) The JSON body of the request is provided in the event parameter.
"""
print("event.session.application.applicationId=" +
event['session']['application']['applicationId'])
"""
Uncomment this if statement and populate with your skill's application ID to
prevent someone else from configuring a skill that sends requests to this
function.
"""
# if (event['session']['application']['applicationId'] !=
# "amzn1.echo-sdk-ams.app.[unique-value-here]"):
# raise ValueError("Invalid Application ID")
if event['request']['type'] == "LaunchRequest":
return on_launch(event['request'], event['session'])
elif event['request']['type'] == "IntentRequest":
return on_intent(event['request'], event['session'], event['context'])
elif event['request']['type'] == "SessionEndedRequest":
return on_session_ended(event['request'], event['session'])
Alexa Skill Account Linking- Python
For setting up account linking in Alexa Skill, follow the steps here.
Once setup is done, and intent handler needs to be implemented in the Python code:
This will open a Login with Amazon screen where customers can sign in using their Amazon Account Credentials. Once logged-in successfully, the account is linked and next time onwards the user token comes as part of the session request.
Sample Request after Account Linking:
Once setup is done, and intent handler needs to be implemented in the Python code:
# --------------- Helpers that build all of the responses ----------------------
def build_user_authentication_response():
return {
"version": "1.0",
"response": {
"outputSpeech": {
"type": "PlainText",
"text": " Please use the companion app to authenticate on Amazon to start using this skill"
},
"card": {
"type": "LinkAccount"
},
"shouldEndSession": false
},
"sessionAttributes": {}
}
# --------------- Method that gets called when there is an intent request ----------------------
def on_intent(intent_request, session, context):
""" Called when the user specifies an intent for this skill """
print("on_intent requestId=" + intent_request['requestId'] +
", sessionId=" + session['sessionId'])
intent = intent_request['intent']
intent_name = intent_request['intent']['name']
if intent_name == "SayHello":
return build_user_authentication_response()
# --------------- Main handler ------------------
def lambda_handler(event, context):
""" Route the incoming request based on type (LaunchRequest, IntentRequest,
etc.) The JSON body of the request is provided in the event parameter.
"""
print("event.session.application.applicationId=" +
event['session']['application']['applicationId'])
"""
Uncomment this if statement and populate with your skill's application ID to
prevent someone else from configuring a skill that sends requests to this
function.
"""
# if (event['session']['application']['applicationId'] !=
# "amzn1.echo-sdk-ams.app.[unique-value-here]"):
# raise ValueError("Invalid Application ID")
if event['request']['type'] == "LaunchRequest":
return on_launch(event['request'], event['session'])
elif event['request']['type'] == "IntentRequest":
return on_intent(event['request'], event['session'], event['context'])
elif event['request']['type'] == "SessionEndedRequest":
return on_session_ended(event['request'], event['session'])
This will open a Login with Amazon screen where customers can sign in using their Amazon Account Credentials. Once logged-in successfully, the account is linked and next time onwards the user token comes as part of the session request.
Sample Request after Account Linking:
{
"version": "1.0",
"session": {
"new": true,
"sessionId": "session-id",
"application": {
"applicationId": "application-id"
},
"user": {
"userId": "user-id",
"accessToken": "access-token",
"permissions": {
"consentToken": "consent-token"
}
}
},
"context": {
"AudioPlayer": {
"playerActivity": "IDLE"
},
"Display": {},
"System": {
"application": {
"applicationId": "application-id"
},
"user": {
"userId": "user-id",
"accessToken": "access-token",
"permissions": {
"consentToken": "consent-token"
}
},
"device": {
"deviceId": "device-id",
"supportedInterfaces": {
"AudioPlayer": {},
"Display": {
"templateVersion": "1.0",
"markupVersion": "1.0"
}
}
},
"apiEndpoint": "https://api.eu.amazonalexa.com",
"apiAccessToken": "api-access-token"
}
},
"request": {
"type": "LaunchRequest",
"requestId": "request-id",
"timestamp": "2018-06-03T05:22:36Z",
"locale": "en-US",
"shouldLinkResultBeReturned": false
}
}
Saturday, February 3, 2018
Number of ways to have breakfast
Problem:
Find the number of ways you can have breakfast in ‘n’ days, given Bread-butter can be eaten every day, Pizza can be eaten every alternate day and Burger can be eaten every two days. Only one item can be eaten on a given day.
Solution:
Let us call the sequence of breakfast item on the days as timetable. And the condition of whether a particular item can be consumed on a given day as constraint.
Approach 1:
Create the timetable while checking that the constraints are met.
Approach 2:
Generate all possible timetables and eliminate the timetables that do not meet the constraints.
Java Program:
Sample Output:
Unit Tests:
Find the number of ways you can have breakfast in ‘n’ days, given Bread-butter can be eaten every day, Pizza can be eaten every alternate day and Burger can be eaten every two days. Only one item can be eaten on a given day.
Solution:
Let us call the sequence of breakfast item on the days as timetable. And the condition of whether a particular item can be consumed on a given day as constraint.
Approach 1:
Create the timetable while checking that the constraints are met.
Approach 2:
Generate all possible timetables and eliminate the timetables that do not meet the constraints.
Java Program:
package com.sourabh.practice;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Breakfast {
public static void main(String[] args) {
String[] menu = new String[]{"Bread-butter", "Pizza", "Burger"};
int[] constraint = new int[]{1, 2, 3};
List<List<String>> timeTableList = new ArrayList<>();
List<String> timeTable = new ArrayList<>();
int n = 3;
Breakfast breakfast = new Breakfast();
breakfast.countNumberOfWaysEvaluationApproach(menu, constraint, timeTableList, timeTable, n);
System.out.println(timeTableList.size());
}
public void countNumberOfWaysEvaluationApproach(String[] menu, int[] constraint, List<List<String>> timeTableList, List<String> timeTable, int n) {
if(timeTable.size() == n) {
// For unit-testing purpose
List<String> output = new ArrayList<>();
for(String food : timeTable) {
System.out.print(food + " ");
output.add(food);
}
System.out.println();
timeTableList.add(output);
} else {
List<String> possibilities = new ArrayList<>();
boolean found = false;
for(int i=0; i<constraint.length; i++) {
for(int j=1; j<constraint[i]; j++) {
if(timeTable.size() - j >=0 && timeTable.get(timeTable.size() - j).equals(menu[i])) {
found = true;
}
}
if(!found) {
possibilities.add(menu[i]);
} else {
found = false;
}
}
for(String possibility : possibilities) {
timeTable.add(possibility);
countNumberOfWaysEvaluationApproach(menu, constraint, timeTableList, timeTable, n);
timeTable.remove(timeTable.size() - 1);
}
}
}
public void countNumberOfWaysExhaustiveApproach(String[] menu, int[] constraint, List<List<String>> timeTableList, List<String> timeTable, int n) {
generatePermutation(menu, timeTableList, timeTable, n);
Iterator<List<String>> iterator = timeTableList.iterator();
while(iterator.hasNext()) {
List<String> output = iterator.next();
boolean passed = true;
for(int i=0; i<menu.length; i++) {
int occ1 = -1;
int occ2 = -1;
for(int j = 0; j < output.size(); j++) {
if(output.get(j).equals(menu[i])) {
if(occ1 < 0) {
occ1 = j;
} else {
occ2 = occ1;
occ1 = j;
}
}
if(occ1 >= 0 && occ2 >= 0 && occ1 > occ2 && occ1 - occ2 < constraint[i]) {
passed = false;
}
}
}
if(!passed) {
iterator.remove();
}
}
for(List<String> output : timeTableList) {
for(String food : output) {
System.out.print(food + " ");
}
System.out.println();
}
System.out.println(timeTableList.size());
}
public void generatePermutation(String[] menu, List<List<String>> timeTableList, List<String> timeTable, int n) {
if(timeTable.size() == n) {
// For unit-testing purpose
List<String> output = new ArrayList<>();
for(String food : timeTable) {
output.add(food);
}
timeTableList.add(output);
} else {
for(int i=0; i<menu.length; i++) {
timeTable.add(menu[i]);
generatePermutation(menu, timeTableList, timeTable, n);
timeTable.remove(timeTable.size() - 1);
}
}
}
}
Sample Output:
Bread-butter Bread-butter Bread-butter Bread-butter Bread-butter Pizza Bread-butter Bread-butter Burger Bread-butter Pizza Bread-butter Bread-butter Pizza Burger Bread-butter Burger Bread-butter Bread-butter Burger Pizza Pizza Bread-butter Bread-butter Pizza Bread-butter Pizza Pizza Bread-butter Burger Pizza Burger Bread-butter Pizza Burger Pizza Burger Bread-butter Bread-butter Burger Bread-butter Pizza Burger Pizza Bread-butter 15
Unit Tests:
package com.sourabh.practice;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
public class BreakfastTests {
Breakfast breakfast = new Breakfast();
@Test
public void testCountNumberOfWaysInTermsOfFrequency() {
String[] menu = new String[]{"Bread-butter", "Pizza", "Burger"};
int[] constraint = new int[]{1, 2, 3};
List<List<String>> timeTableList = new ArrayList<>();
List<String> timeTable = new ArrayList<>();
int n = 3;
Breakfast breakfast = new Breakfast();
breakfast.countNumberOfWaysEvaluationApproach(menu, constraint, timeTableList, timeTable, n);
boolean passed = true;
for(List<String> output : timeTableList) {
for(int i=0; i<menu.length; i++) {
int occ1 = -1;
int occ2 = -1;
for(int j = 0; j < output.size(); j++) {
if(output.get(j).equals(menu[i])) {
if(occ1 < 0) {
occ1 = j;
} else {
occ2 = occ1;
occ1 = j;
}
}
if(occ1 >= 0 && occ2 >= 0 && occ1 > occ2 && occ1 - occ2 < constraint[i]) {
passed = false;
}
}
}
}
Assert.assertTrue(passed);
}
@Test
public void testCountNumberOfWaysInTermsOfExhaustiveness() {
String[] menu = new String[]{"Bread-butter", "Pizza", "Burger"};
int[] constraint = new int[]{1, 2, 3};
List<List<String>> timeTableList = new ArrayList<>();
List<String> timeTable = new ArrayList<>();
int n = 3;
Breakfast breakfast = new Breakfast();
breakfast.countNumberOfWaysExhaustiveApproach(menu, constraint, timeTableList, timeTable, n);
boolean passed = true;
for(List<String> output : timeTableList) {
for(int i=0; i<menu.length; i++) {
int occ1 = -1;
int occ2 = -1;
for(int j = 0; j < output.size(); j++) {
if(output.get(j).equals(menu[i])) {
if(occ1 < 0) {
occ1 = j;
} else {
occ2 = occ1;
occ1 = j;
}
}
if(occ1 >= 0 && occ2 >= 0 && occ1 > occ2 && occ1 - occ2 < constraint[i]) {
passed = false;
}
}
}
}
Assert.assertTrue(passed);
}
@Test
public void testCountNumberOfWaysInTermsOfAccuracy() {
String[] menu = new String[]{"Bread-butter", "Pizza", "Burger"};
int[] constraint = new int[]{1, 2, 3};
List<List<String>> timeTableList = new ArrayList<>();
List<String> timeTable = new ArrayList<>();
int n = 3;
Breakfast breakfast = new Breakfast();
Long start1 = System.currentTimeMillis();
breakfast.countNumberOfWaysEvaluationApproach(menu, constraint, timeTableList, timeTable, n);
Long end1 = System.currentTimeMillis();
int size1 = timeTableList.size();
timeTableList = new ArrayList<>();
timeTable = new ArrayList<>();
Long start2 = System.currentTimeMillis();
breakfast.countNumberOfWaysExhaustiveApproach(menu, constraint, timeTableList, timeTable, n);
Long end2 = System.currentTimeMillis();
int size2 = timeTableList.size();
Assert.assertEquals(size1, size2);
System.out.println(end1 - start1);
System.out.println(end2 - start2);;
}
}
Tuesday, June 27, 2017
Find the common vacant meeting slot
Problem:
Given the meeting slots of several persons, find the common vacant slot in which all the persons can meet.
Input and Output format:
The first line of input contains a single integer n denoting the number of persons. Next n lines of input contain the start and end time of meetings of each person in HH:MM format separated by a space character.
The first line of output should contain the number of distinct vacant slots followed by those many lines containing the start and end time of each vacant slot in HH:MM format separated by a space character.
Sample Input:
4
08:30 09:00
08:45 09:30
09:00 09:30
10:00 11:00
Sample Output:
3
00:00 08:30
09:30 10:00
11:00 23:59
Java Program:
Given the meeting slots of several persons, find the common vacant slot in which all the persons can meet.
Input and Output format:
The first line of input contains a single integer n denoting the number of persons. Next n lines of input contain the start and end time of meetings of each person in HH:MM format separated by a space character.
The first line of output should contain the number of distinct vacant slots followed by those many lines containing the start and end time of each vacant slot in HH:MM format separated by a space character.
Sample Input:
4
08:30 09:00
08:45 09:30
09:00 09:30
10:00 11:00
Sample Output:
3
00:00 08:30
09:30 10:00
11:00 23:59
Java Program:
package com.sourabh.first;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CommonMeeting {
public static void main(String[] args) {
int A[] = new int[86401];
Scanner scanner = new Scanner(System.in);
int persons = Integer.parseInt(scanner.nextLine());
List<Integer> meetingStartHour = new ArrayList<>();
List<Integer> meetingStartMinute = new ArrayList<>();
List<Integer> meetingStartSecond = new ArrayList<>();
List<Integer> meetingEndHour = new ArrayList<>();
List<Integer> meetingEndMinute = new ArrayList<>();
List<Integer> meetingEndSecond = new ArrayList<>();
List<Integer> vacantStartHour = new ArrayList<>();
List<Integer> vacantStartMinute = new ArrayList<>();
List<Integer> vacantStartSecond = new ArrayList<>();
List<Integer> vacantEndHour = new ArrayList<>();
List<Integer> vacantEndMinute = new ArrayList<>();
List<Integer> vacantEndSecond = new ArrayList<>();
for(int i=0;i<persons;i++)
{
String meeting = scanner.nextLine();
//System.out.println(meeting);
String[] parts = meeting.split(" ");
String[] start = parts[0].split(":");
String[] end = parts[1].split(":");
meetingStartHour.add(Integer.parseInt(start[0]));
meetingStartMinute.add(Integer.parseInt(start[1]));
meetingStartSecond.add(Integer.parseInt(start[2]));
meetingEndHour.add(Integer.parseInt(end[0]));
meetingEndMinute.add(Integer.parseInt(end[1]));
meetingEndSecond.add(Integer.parseInt(end[2]));
}
for(int i=0;i<persons;i++)
{
for(int j = 3600 * meetingStartHour.get(i)
+ 60 * meetingStartMinute.get(i)
+ meetingStartSecond.get(i);
j <= 3600 * meetingEndHour.get(i)
+ 60 * meetingEndMinute.get(i)
+ meetingEndSecond.get(i);
j++) {
A[j]=1;
}
}
if(A[0] == 0)
{
vacantStartHour.add(0);
vacantStartMinute.add(0);
vacantStartSecond.add(0);
}
for(int i=0;i<86400;i++)
{
if(A[i]==1 && A[i+1]==0)
{
vacantStartHour.add(i/3600);
vacantStartMinute.add((i%3600)/60);
vacantStartSecond.add(i%60);
}
else if(A[i]==0 && A[i+1]==1)
{
vacantEndHour.add(i/3600);
vacantEndMinute.add((i%3600)/60);
vacantEndSecond.add(i%60);
}
}
if(A[86399] == 0)
{
vacantEndHour.add(86399/3600);
vacantEndMinute.add((86399%3600)/60);
vacantEndSecond.add(59);
}
System.out.println(vacantStartHour.size());
for(int i=0;i<vacantStartHour.size();i++)
{
String vacantSlotStart = format(vacantStartHour.get(i)) + ":" +
format(vacantStartMinute.get(i)) + ":" +
format(vacantStartSecond.get(i));
String vacantSlotEnd = format(vacantEndHour.get(i)) + ":" +
format(vacantEndMinute.get(i)) + ":" +
format(vacantEndSecond.get(i));
System.out.println(vacantSlotStart + " " + vacantSlotEnd);
}
}
private static String format(Integer time) {
if(time < 10) {
return "0" + time;
}
else {
return time.toString();
}
}
}
Longest subarray containing all values greater than a given value
Problem:
Given the temperature trends over a number of days, find the longest streak of continuous days having temperatures greater than a given temperature k.
Input and Output format:
The first line of input contains two space separated integers n and k. The second line of input contains n space separated integers denoting the temperatures.
The output should contain three integers denoting the length of the longest streak of days having temperatures greater than k, the starting index of the longest streak and the ending index of the longest streak. Index starts from 1.
Sample Input:
5 30
29 31 28 32 33
Sample Output:
2 4 5
Explanation:
The longest streak of days having temperatures greater than 30 are days 4 and 5 with a length of 2.
Given the temperature trends over a number of days, find the longest streak of continuous days having temperatures greater than a given temperature k.
Input and Output format:
The first line of input contains two space separated integers n and k. The second line of input contains n space separated integers denoting the temperatures.
The output should contain three integers denoting the length of the longest streak of days having temperatures greater than k, the starting index of the longest streak and the ending index of the longest streak. Index starts from 1.
Sample Input:
5 30
29 31 28 32 33
Sample Output:
2 4 5
Explanation:
The longest streak of days having temperatures greater than 30 are days 4 and 5 with a length of 2.
Subscribe to:
Posts (Atom)
