Posts

Showing posts with the label Data structures

Intro to Hashing problems made easy

/*--- Below are 3 problems i have used hasing technique, After seeing below problems you'll be 100% clear on the basic hasing needed for competative programming First Repeating Element  Given an array arr[] of size N. The task is to find the first repeating element in an array of integers, i.e., an element that occurs more than once and whose index of first occurrence is smallest. Input : The first line contains an integer T denoting the total number of test cases. In each test cases, First line is number of elements in array N and second its values. Output: In each separate line print the index of first repeating element, if there is not any repeating element then print “-1” (without quotes). Use 1 Based Indexing. Constraints: 1 <= T <= 500 1 <= N <= 106 0 <= Ai <= 106 Example: Input: 1 7 1 5 3 4 3 5 6 Output: 2 Explanation: Testcase 1: 5 is appearing twice and its first appearence is at index 2 which is less than 3 whose first occur...

Codechef Operations in a matrix

/* Sakshi had a matrix with  N N  rows (numbered  1 1  through  N N ) and  M M  columns (numbered  1 1  through  M M ). Initially, all cells of this matrix contained  0 0 -s. Let's denote a cell in row  r r  and column  c c  by  ( r , c ) ( r , c ) . Sakshi is well-known for troubling others. This time, her friends Nikki and Mansi planned to take revenge and teach her a lesson, so they changed her matrix by performing the following operation  Q Q  times: Choose any valid cell  ( x , y ) ( x , y ) . Add  1 1  to all the cells in row  x x . Add  1 1  to all the cells in column  y y . For each valid  i i , the cell chosen in the  i i -th operation was  ( X i , Y i ) ( X i , Y i ) . Nikki and Mansi challenged Sakshi to find the number of cells in the resulting matrix which contain odd integers. Sakshi is not good at math, since she has spent...