Program to Find Subarray With Largest Sum
Given an array of size n with integer values. Write a program to find the subarray having largest sum.
For eg:
Input array — int arr[] = {3,-1,-4,4,-1,2,1,-5,4}
Largest subarray sum – {4,-1,2,1} = 6
To make technologies simpler
Given an array of size n with integer values. Write a program to find the subarray having largest sum.
For eg:
Input array — int arr[] = {3,-1,-4,4,-1,2,1,-5,4}
Largest subarray sum – {4,-1,2,1} = 6
Given a binary string contains 0’s and 1’s. Write a program to find the longest contiguous occurrence of 0’s and 1’s.
For example:
Input string — 0 1 0 0 0 1 1 0 1 1 0 1 1 1 0 1 0 0 0 1
Length_0: 3 Length_1: 3
Input string — 1 0 0 1 1 1 0 1 1 0 0 0 0 0 1 0 1 0 1 0
Length_0: 5 Length_1: 3
Given an array of size “n” and an integer “k” where k < n. Write a program to return the count of distinct elements in every subarray of size “k”.
For eg:
int arr[] = {1,2,3,4,1,3,4} , window size k = 4
For 1st window of size 4 (index 0-3) – subarray = {1,2,3,4}, distinct element = 4
For 2nd window of size 4 (index 1-4) – subarray = {2,3,4,1}, distinct element = 4
For 3rd window of size 4 (index 2-5) – subarray = {3,4,1,3}, distinct element = 3
For 4th window of size 4 (index 3-6) – subarray = {4,1,3,4}, distinct element = 3
We have to design a hit counter which can counts the number of hits received in a specified time interval. This method should also provide total number of hits received in the specified time interval.
(more…)In a given array, each element appears thrice whereas there is an element which appears only once. Write the program to find the element appearing once.
For eg:
int B[] = {1,1,1,2,2,2,4,3,4,3,4,3,6,7,6,6};
In this above array, element “7” is occurring only once.
(more…)Given an array and a given SUM, find all the triplets present in the array whose sum is equal to given SUM. Print all such triplets in the array.
For eg:
Let’s assume.
int arr[] = {5, 7, 9, 1, 6, 8};
int sum = 15;
Possible combinations are, {5, 9, 1}, {1, 6, 8}
Given an array and a given SUM, find all the combinations present in the array whose sum is equal to given SUM. Print all such combinations in the array.
For eg:
Let’s assume
int arr[] = {5, 7, 9, 1, 6, 8};
int sum = 15;
Possible combinations are, {7, 8}, {9, 6}