Posts

Showing posts from August, 2021

Problem no 75: returning sum of left diagonal elements in 2d array and max of 1d array in Arraylist.

 problem: Given a integer  n . We have  n*n  values of a 2-d array, and   n  values of 1-d array. Task is to find the sum of the left diagonal values of the 2-d array and the max element of the 1-d array and print them with space in between. Example 1: ​Input : arr[][] = {{1,2,3}, {4,5,6}, {7, 8,9}} and N = 3 brr[] = {3, 6, 9} Output : 15 9 Explanation: 1 2 3 4 5 6 7 8 9 So, this sum of left diagonal (1+ 5 + 9) = 15 The maximum element in an array brr is 9 So, will return {15, 9} as an answer. ​Example 2: Input : arr[][] = {{1,2}, {1, 2}} and N = 2 brr[] = {10, 1} Output : 3 10   Your Task: This is a function problem. The input is already taken care of by the driver code. You only need to complete the function  array()  that takes a two-dimension array  (a) , another one dimension array  (b),  sizeOfArray  (n) , and return the ArrayList which is having the sum of the diagonal element...

problem no 74: given is the string consisting of the lower case alphabets find the repeated charachter first in the string.

 problem: Given a string consisting of lowercase english alphabets. Find the repeated character present  first  in the string. Example 1: Input: S = "geeksforgeeks" Output: g Explanation : g, e, k and s are the repeating characters. Out of these, g occurs first. ​ Example 2: Input : S = "abcde" Output: -1 Explanation : No repeating character present. Your Task: You don't need to read input or print anything. Your task is to complete the function  firstRep()  which takes the string S as input and returns the the first repeating character in the string. In case there's no repeating character present, return '#'. code: my approach is we will take two maps. map1 and map2. map1 will store the count of each charachter in the string. we will iterate the map1 and see if there is any element inside map1  whose count is greater than 1.if count of any charachter inside map1 is greater than 1 then we will check the first index of that charachter inside th...

problem no 73:find the first element to occur k times inside an array.

 problem statement: Given an array of  N  integers. Find the first element that occurs  K  number of times.    Example 1: Input : N = 7, K = 2 A[] = {1, 7, 4, 3, 4, 8, 7} Output : 4 Explanation: Both 7 and 4 occur 2 times. But 4 is first that occurs 2 times.   Your Task:   You don't need to read input or print anything. Your task is to complete the function  firstElementKTime()  which takes the array  A[] , its size  N  and an integer  K  as inputs and  returns  the required answer. If answer is not present in the array, return  -1 . code:  int firstElementKTime(int a[], int n, int k)     {         map<int,int> mapp;         int got=0;         int z=0;         for(int i=0;i<n;i++)         {             mapp[a[i]]++;   ...

problem no 72: find the difference between highest occurence and lowest occurance of any element inside an array.

  Given an array, the task is to find the difference between highest occurrence and lowest occurrence of any number in an array. Note:  If only one type of element is present in the array return 0.   Example 1: Input: arr[] = [1, 2, 2] Output: 1 Explanation: Lowest occurring element (1) occurs once. Highest occurring element (2) occurs 2 times   Example 2: Input : arr[] = [7, 8, 4, 5, 4, 1, 1, 7, 7, 2, 5] Output : 2 Explanation : Lowest occurring element (5) occurs once. Highest occurring element (1 or 7) occurs 3 times   Example 3: Input : arr[] = [1, 1, 1, 3, 3, 3] Output : 0   Your Task:   You don't need to read input or print anything. Your task is to complete the function  leftElement()  which takes the array  arr[]  and its size  N   as inputs and returns the difference between highest occurrence and lowest occurrence of any number in the array. code: int findDiff(int arr[], int n) {      ...

problem no 71: 2 sorted arrays are given ,count the number of pairs whose sum is equal to x.

 problem: Given two sorted arrays(arr1[] and arr2[]) of size  M  and  N  of distinct elements. Given a value  Sum . The problem is to count all pairs from both arrays whose sum is equal to  Sum. Note:  The pair has an element from each array. Example 1: Input: M=4, N=4 , Sum = 10 arr1[] = {1, 3, 5, 7} arr2[] = {2, 3, 5, 8} Output: 2 Explanation : The pairs are:  (5, 5) and (7, 3).   Example 2: Input: N=4, M=4, sum=5 arr1[] = {1, 2, 3, 4} arr2[] = {5, 6, 7, 8} Output: 0   Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function  countPairs () that takes  array arr1, array arr2, integer M, integer N and integer x  as parameters and return the count of all pairs from both arrays whose sum is equal to x. code:     int countPairs(int arr1[], int arr2[],  int m, int n, int x)...

problem no 70: check if the given array forms Arithematic Progression or not.

 problem: Given an array of  N  integers. Write a program to check whether an arithmetic progression can be formed using all the given elements.    Example 1: Input: N=4 arr[] = { 0,12,4,8 } Output: YES Explanation : Rearrange given array as {0, 4, 8, 12} which forms an arithmetic progression. Example 2: Input: N=4 arr[] = {12, 40, 11, 20} Output: NO   Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function  checkIsAP () that takes  array arr and integer N  as parameters and return  true  for "Yes" and  false  for "No". code: bool checkIsAP(int arr[], int n)     {         sort(arr,arr+n);         int diff=arr[1]-arr[0];         int flagforap=1;         for(int i=2;i<n-1;i++)         {    ...

problem no 69: find the minimum number inside the array which is repeating K times.

 question: Given an array  arr  of size  N , the goal is to find out the smallest number that is repeated exactly ‘ K ’ times.   Example 1: Input: N=5, K=2 arr[] = { 2 2 1 3 1 } Output: 1 Explanation : Here in array, 2 is repeated 2 times, 1 is repeated 2 times, 3 is repeated 1 time. Hence 2 and 1 both are repeated 'k' times i.e 2 and min(2, 1) is 1 .   Example 2: Input: N=4, K=1 arr[] = { 3 5 3 2 } Output: 2 Explanation: Both 2 and 5 are repeating 1 time but min(5, 2) is 2.   Your Task: You just need to complete the function  findDuplicate () that takes  array arr, integer N and integer K  as parameters and  returns  the required answer. Note-  If there is no such element then return  -1 . code:   int findDuplicate(int arr[], int N, int K)      {        map<int,int > mapp;       for(int i=0;i<N;i++)       {     ...

problem no 68 : find the triplet inside array whose sum is zero.

Find triplets with zero sum  Basic  Accuracy:   48.42%  Submissions:   96881  Points:   1 Given an array of integers. Check whether it contains a triplet that sums up to zero.  Example 1: Input : n = 5, arr[] = {0, -1, 2, -3, 1} Output : 1 Explanation : 0, -1 and 1 forms a triplet with sum equal to 0. Example 2: Input : n = 3, arr[] = {1, 2, 3} Output : 0 Explanation : No triplet with zero sum exists. Your Task: You don't need to read input or print anything. Your task is to complete the boolean function  findTriplets()  which takes the array arr[] and the size of the array (n) as inputs and print 1 if the function returns true else print false if the function return false.  code:  bool findTriplets(int arr[], int n)     {           int found=0;          int l,r;          sort(arr,arr+n);          for(int...

problem no 67: find the element in array which occurs odd number of times.

question: Given an array of N positive integers where all numbers occur even number of times except one number which occurs odd number of times. Find the exceptional number. Example 1: Input: N = 7 Arr[] = {1, 2, 3, 2, 3, 1, 3} Output: 3 Example 2: Input: N = 7 Arr[] = {5, 7, 2, 7, 5, 2, 5} Output: 5 Your Task: You don't need to read input or print anything. Your task is to complete the function  getOddOccurrence()  which takes  arr[]  and  n  as input parameters and returns the exceptional number. code:  int getOddOccurrence(int arr[], int n) {         // code here         map<int,int> mapp;         for(int i=0;i<n;i++)         {             mapp[arr[i]]++;         }                  for(auto ele:mapp)         {       ...