Posts

problem no 76:21 stick problem

  Question : lets say you have 21 sticks.There is a competion in between you and your computer.yours will be first turn.you can pick maximum 4 sticks .after your turn computer will pick some sticks.THE ONE WHO PICKS UP THE 21ST STICK WILL BE THE LOOSER.        Now your task is to write the program in python in such a way that computer will not loose at any cost.     Answer:The following program written in python  is in favour of computer.This program always  makes computer win.   stick= 21 rem_stick= 21 while stick!= 1 : print( "its your turn" ) print( "note that you cannot pick more than 4 sticks" ) p=int(input()) z= 5 -p print( "its my turn" ) print( "i will pick" ,z, "sticks" ) rem_stick=rem_stick-( 5 ) print( "only" ,rem_stick, "remaining now" ) if (rem_stick== 1 ): break ; print( "only one stick is remaining" ) print( "youre a looser ,i won" )

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++)         {    ...