Posts

Problem no 6: Find the union of 2 arrays.

  Union of two arrays Basic Accuracy: 47.56% Submissions: 8647 Points: 1 Given two arrays A and B of size N and M respectively. The task is to find union between these two arrays. Union of the two arrays can be defined as the set containing distinct elements from both the arrays. If there are repetitions, then only one occurrence of element should be printed in union. Example 1: Input: 5 3 1 2 3 4 5 1 2 3 Output: 5 Explanation: 1, 2, 3, 4 and 5 are the elements which comes in the union set of both arrays.  S...

Problem no 7: Cyclically rotate the array by one.

  Cyclically rotate an array by one Basic Accuracy: 77.14% Submissions: 4605 Points: 1 Given an array, rotate the array by one position in clock-wise direction.   Example 1: Input: N = 5 A[] = {1, 2, 3, 4, 5} Output: 5 1 2 3 4   Example 2: Input: N = 8 A[] = {9, 8, 7, 6, 4, 2, 1, 3} Output: 3 9 8 7 6 4 2 1     Answer:   void rotate(int arr[], int n) { int num=arr[n-1]; for(int i=n-1;i>0;i--) { arr[i]=arr[i-1]; } arr[0]=num; for(int i=0;i<n;i++)...

Problem no 4: sort the array of 0's,1's,and 2's.

Sort an array of 0s, 1s and 2s Easy Accuracy: 51.36% Submissions: 42188 Points: 2 Given an array of size N containing only 0s, 1s, and 2s; sort the array in ascending order. Example 1: Input: N = 5 arr[]= {0 2 1 2 0} Output: 0 0 1 2 2 Explanation: 0s 1s and 2s are segregated into ascending order. Answer: without using sorting algorithm. void sort012(int a[], int n) {         int countofzero=0;    int countofone=0;    int countoftwo=0;      for(int i=0;i<n;i++)    {       if(a[i]==0)     ...

Problem no 5 :Move all the negative elements to left side of the array.

QUESTION: Move all negative numbers to beginning and positive to end with constant extra space Difficulty Level :EASY   An array contains both positive and negative numbers in random order. Rearrange the array elements so that all negative numbers appear before all positive numbers. Examples :   Input: -12, 11, -13, -5, 6, -7, 5, -3, -6 Output: -12 -13 -5 -7 -3 -6 11 6 5 ANSWER:                   void rearrange(int arr[],int n) {     int j=0;     for(int i=0;i<n;i++)     {  ...

Middle of 3 Elements

Question:                     Given three distinct numbers A, B and C. Find the number with value in middle (Try to do it with minimum comparisons). Example 1: Input: A = 978, B = 518, C = 300 Output: 518 Explanation: Since 518>300 and 518<978, so 518 is the middle element. ANSWER:   int middle(int A, int B, int C){         int arr[3];         arr[0]=A;         arr[1]=B;         arr[2]=C;          sort(arr, arr + 3);          return arr[1];     }

kadanes algorithm.

 Question is: Given an array arr of N integers. Find the contiguous sub-array with maximum sum.   Example 1: Input: N = 5 arr[] = {1,2,3,-2,5} Output: 9 Explanation: Max subarray sum is 9 of elements (1, 2, 3, -2, 5) which is a contiguous subarray. Example 2: Input: N = 4 arr[] = {-1,-2,-3,-4} Output: -1 Explanation: Max subarray sum is -1  of element (-1)     THE CODE TO SOLVE THIS QUESTION IS:      int maxSubarraySum(int arr[], int n){ int max_so_far=INT_MIN; int max_uptohere=0; for(int i=0;i<n;i++) { max_uptohere=max_uptohere+arr[i]; if(max_uptohere<0) { max_uptohere=0; } if(max_so_far<max_uptohere) { max_so_far=max_uptohere; } } return max_so_far; }