Posts

Missing Numbers (Hackerrank)

Image
Sometimes the easiest of the questions can become cumbersome and actually put our knowledge to the test. The following question had me writing code for 30 minutes, only to be rejected by the compiler telling me that I was missing something! Question-  Approach -  Make a new array with last element = 0   Input all values of Bigger array Delete similar values found in Smaller array Print all the values which are higher than 0 Now what took me so long? I made nested for loops to check every value of A if it lies in B, if it doesn't, I would append this value to an empty array, sort it and print it. But when i did that, It gave me repeating values and the output was in the form of m+n where m and n are sizes of the array respectively. Hence, I had to do some brainstorming and research which led me to a realization that i can simply add all elements of bigger array brr[m] in a new array C, then delete the same values from C taking elements of arr[n] into consideration!...

Palindrome Index (Hackerrank)

Image
Here we have another Hackerrank question on String manipulation, this one involving the classic Palindrome index.  Question-  Approach - Pretty simple, we just need to check values one by one, iterating a loop from index 0 being compared to index n, where n is the last value in the index, narrowing it down to inner values one by one. The point where the elements do not stay equal is the point where element needs to be removed in order to make the string into a palindrome  The steps are -  initialise i = 0; j= string.length()-1 //last value in the string Run a loop from i to middle of the string, i increments while j decrements, bringing us a section to focus on within the string Run an if condition which checks whether character at i is equal to j or not If it is not equal, we check if the element after character at i is equal to j or not If yes, we check whether the element before j is equal to character at i and character after i is subsequently equal to element at...

Two Characters (Hackerrank)

Image
Hello There! Been a long time since i posted anything here, Oh no no, I didn't forget i had a blog, Just got really busy preparing for my Semester end exams and now i am free! So today we have a question with us from Hackerrank based on String Manipulations. Question-   Approach- First we define all the conditions which will result into a failed attempt to solve the conditions mentioned, by creating a function called - Bool alternate which gives us False conditions if 2 characters chosen are equal, do not belong in the original string or size constraints are not met. Next, in the main function,  Input string s Run a loop for char a, iterating over alphabets a-z Run a nested loop for char b, iterating over alphabets b-z for every iteration, if char a is equal to char b, we move ahead  we check whether char a or b exists in the string s, if they do, we input the value in a mew string t In the end we run alternate function to check false conditions  After running t...

Union and Intersection of Two Linked Lists (Using Hashing)

 Union( List1, List2) --> Create an empty Hash Table, Traverse both lists one by one, for each element visited, look for the element in the hash table, if not present --> add it, if present --> ignore it.  Intersection( List1 , List 2 ) --> Create empty Hash Table, Traverse list 1, Initialize Result List as NULL, insert elements of List 1 in hash table, Traverse List2 and look for the element in the hash table, if present, Insert it into the Result List, else ignore it.  Code--> #include<bits/stdc++.h> using namespace std; struct Node{     int data;     struct Node* next; }; //utility function to insert node  void push(struct Node* head, int new_data) {     struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));     new_node->data= new_data;     new_node->next= head;     head= new_node; } //function to store elements of both lists void store(struct Node* head1, struct No...

Sort a nearly sorted array (or K sorted arrays)

 Given an array of N elements, where each element is at most k away from its target position, devise and algorithm to sort in O(n logk) time Method - We can either use insertion sort code-->  void insertionsort(int arr[], int size) {          int i, key, j;            for(int i=1; i<size; i++)            {   key=arr[i];               j=i-1;               while(j>=0 && arr[j] > key)               {         arr[i+1]=arr[i];                           j-=1;                   }                    arr[j+1]=key;      ...

k largest or smallest elements in an array

 Question- write an efficient program to print k largest elements in an array. Elements in array can be in any order.  Method- Sort array in descending order, print first k elements  code- #include<bits/stdc++.h> using namespace std; void kelements(int arr[], int n, int k) {     sort( arr, arr + n, greater<int>());     for(int i = 0 ; i < k ; i ++)     {               cout<<arr[i]<<" ";     }

Cost of Balloons (Hackerearth)

Image
Problem You are conducting a contest at your college. This contest consists of two problems and  participants. You know the problem that a candidate will solve during the contest. You provide a balloon to a participant after he or she solves a problem. There are only green and purple-colored balloons available in a market. Each problem must have a balloon associated with it as a prize for solving that specific problem. You can distribute balloons to each participant by performing the following operation: Use green-colored balloons for the first problem and purple-colored balloons for the second problem Use purple-colored balloons for the first problem and green-colored balloons for the second problem You are given the cost of each balloon and problems that each participant solve. Your task is to print the minimum price that you have to pay while purchasing balloons. Approach -  First we input all the values and then iterate loops to find if jth element of array [i][j] is 1 or ...