Two children, Lily and Ron, want to share a chocolate bar. Each of the squares has an integer on it.
Lily decides to share a contiguous segment of the bar selected such that:
The length of the segment matches Ron's birth month, and,
The sum of the integers on the squares is equal to his birth day.
Determine how many ways she can divide the chocolate.
Example
Lily wants to find segments summing to Ron's birth day, with a length equalling his birth month, . In this case, there are two segments meeting her criteria: and .
Function Description
Complete the birthday function in the editor below.
birthday has the following parameter(s):
int s[n]: the numbers on each of the squares of chocolate
int d: Ron's birth day
int m: Ron's birth month
Returns
int: the number of ways the bar can be divided
Input Format
The first line contains an integer , the number of squares in the chocolate bar. The second line contains space-separated integers , the numbers on the chocolate squares where . The third line contains two space-separated integers, and , Ron's birth day and his birth month.
Solution:
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
importstatic java.util.stream.Collectors.joining;
importstatic java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'birthday' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER_ARRAY s
* 2. INTEGER d
* 3. INTEGER m
*/
publicstaticint birthday(List<Integer> s, int d, int m) {
// Write your code here
int n=s.size();
int count=0;
for(int i=0;i<=n-m;i++) // i iterraion upto array length -subarray length
{
int sum=0;
for(int j=i;j<i+m;j++) // j iteration upto current i and next m element -1
Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order , find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length. Return the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2. The tests are generated such that there is exactly one solution. You may not use the same element twice. Your solution must use only constant extra space . Example 1: Input: numbers = [2,7,11,15], target = 9 Output: [1,2] Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2]. Example 2: Input: numbers = [2,3,4], target = 6 Output: [1,3] Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3. We return [1, 3]. Example 3: Input: numbers = [-1,0], target = -1 Output: [1,2] Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1,...
In Java , Comparable and Comparator both are interfaces used to sort objects. Comparable It defines natural ordering within a class. It is implemented inside the class being sorted. It uses the compareTo() method. It allows sorting by one one . Comparable provides a single sorting sequence . In other words, we can sort the collection on the basis of a single element such as id, name, and price. Comparable affects the original class , i.e., the actual class is modified. Comparable is present in java.lang package. We can sort the list elements of Comparable type by Collections.sort(List) method. Only one compareTo() method can be implemented, restricting flexibility. Comparator It defines custom sorting logic It is implemented in a separate class or using lambda expressions . It uses the compare() method. It allows sorting by multiple criteria . The Comparator provides multiple sorting...
Given an integer array nums, return true if there exists a triple of indices (i, j, k) such that i < j < k and nums[i] < nums[j] < nums[k]. If no such indices exists, return false. Example 1: Input: nums = [1,2,3,4,5] Output: true Explanation: Any triplet where i < j < k is valid. Example 2: Input: nums = [5,4,3,2,1] Output: false Explanation: No triplet exists. Example 3: Input: nums = [2,1,5,0,4,6] Output: true Explanation: The triplet (3, 4, 5) is valid because nums[3] == 0 < nums[4] == 4 < nums[5] == 6. Brute Force : Failed for some testcase class Solution { public boolean increasingTriplet ( int [] nums ) { int n = nums . length ; int i = 0 ; int j =i+ 1 ; int k =j+ 1 ; while (i <n && j<n && k<n) { if (nums...
Comments
Post a Comment