Posts

Showing posts from June, 2021

wrapping/non wrapping kadanes

 #include <iostream> #include <bits/stdc++.h> #include <climits> using namespace std; void display(int *arr, int n){     for(int i=0;i<n;i++){         cout<<arr[i];     } } int kadone(int *arr,int n){     int maxsum=INT_MIN;     int sum=0;     for(int i=0;i<n;i++){         sum=sum+arr[i];         if(sum<0){                          sum=0;         }         maxsum=max(sum,maxsum);              }     return maxsum; } int main() {     int arr[]={-4,4,-6,6,10,-11,12};     int n=sizeof(arr)/sizeof(int);     int maxsum=INT_MIN;     int sum=0;     //int arr_sum[n];     int normal_sum=kadone(arr,n);     cout<<normal_sum<<...

maxsum of subarray: Kadanes Algo

 #include <iostream> #include <bits/stdc++.h> #include <climits> using namespace std; void display(int *arr, int n){     for(int i=0;i<n;i++){         cout<<arr[i];     } } int main() {     int arr[]={-1,4,7,2};     int n=sizeof(arr)/sizeof(int);     int maxsum=INT_MIN;     int sum=0;     for(int i=0;i<n;i++){         for(int j=i;j<n;j++){             sum=0;             for(int k=i;k<=j;k++){                // cout<<arr[k]<<" ";                sum=sum+arr[k];             }             maxsum=max(maxsum,sum);             cout<<endl;         }     }     cout...

print all subarrays

 #include <iostream> #include <bits/stdc++.h> #include <climits> using namespace std; void display(int *arr, int n){     for(int i=0;i<n;i++){         cout<<arr[i];     } } int main() {     int arr[]={1,2,3,8};     int n=sizeof(arr)/sizeof(int);     for(int i=0;i<n;i++){         for(int j=i;j<n;j++){             for(int k=i;k<=j;k++){                 cout<<arr[k]<<" ";             }             cout<<endl;         }     }          return 0; }

Google/fb/paypal

  Find subarray with given sum | Set 1 (Nonnegative Numbers) Difficulty Level :   Medium Last Updated :   29 May, 2021 Given an unsorted array of nonnegative integers, find a continuous subarray which adds to a given number.  Examples :   Input : arr[] = {1, 4, 20, 3, 10, 5}, sum = 33 Ouptut : Sum found between indexes 2 and 4 Sum of elements between indices 2 and 4 is 20 + 3 + 10 = 33 Input : arr[] = {1, 4, 0, 0, 3, 10, 5}, sum = 7 Ouptut : Sum found between indexes 1 and 4 Sum of elements between indices 1 and 4 is 4 + 0 + 0 + 3 = 7 Input : arr[] = {1, 4}, sum = 0 Output : No subarray found There is no subarray with 0 sum There may be more than one subarrays with sum as the given sum. The following solutions print first such subarray.  SOLUTION: #include <iostream> #include <bits/stdc++.h> #include <climits> using namespace std; void display(int *arr, int n){     for(int i=0;i<n;i++){         cout<...

ORACLE/AMAZON

  Find the first repeating element in an array of integers Given an array of integers, find the first repeating element in it. We need to find the element that occurs more than once and whose index of first occurrence is smallest.  SOLUTION: #include <iostream> #include <bits/stdc++.h> #include <climits> using namespace std; void display(int *arr, int n){     for(int i=0;i<n;i++){         cout<<arr[i];     } } int main() {     int arr[]={1,100,30,30,30,100,1};     int n=sizeof(arr)/sizeof(int);     //cout<<n;     int max=INT_MIN;     int count=0;     //int sum=0;     for (int i=0;i<n;i++){         for(int j=i+1;j<n;j++){             if(arr[i]==arr[j]){                 cout<<i+1<<endl;             ...

GOOGLE KICKSTART ROUND D 2020

  Problem Isyana is given the number of visitors at her local theme park on  N  consecutive days. The number of visitors on the i-th day is  V i . A day is  record breaking  if it satisfies both of the following conditions: The number of visitors on the day is strictly larger than the number of visitors on each of the previous days. Either it is the last day, or the number of visitors on the day is strictly larger than the number of visitors on the following day. Note that the very first day could be a record breaking day! Please help Isyana find out the number of record breaking days. Input The first line of the input gives the number of test cases,  T .  T  test cases follow. Each test case begins with a line containing the integer  N . The second line contains  N  integers. The i-th integer is  V i . Output For each test case, output one line containing  Case #x: y , where  x  is the test case number (sta...

GOOGLE KICKSTART ROUND E 2020

  Problem An arithmetic array is an array that contains at least two integers and the differences between consecutive integers are equal. For example, [9, 10], [3, 3, 3], and [9, 7, 5, 3] are arithmetic arrays, while [1, 3, 3, 7], [2, 1, 2], and [1, 2, 4] are not arithmetic arrays. Sarasvati has an array of  N  non-negative integers. The i-th integer of the array is  A i . She wants to choose a contiguous arithmetic subarray from her array that has the maximum length. Please help her to determine the length of the longest contiguous arithmetic subarray. Input The first line of the input gives the number of test cases,  T .  T  test cases follow. Each test case begins with a line containing the integer  N . The second line contains  N  integers. The i-th integer is  A i . Output For each test case, output one line containing  Case #x: y , where  x  is the test case number (starting from 1) and  y  is the leng...