Posts

Showing posts from 2021

11011 Sequence Detector

Image
  module dff( input d,clk,rst, output reg q,qbar ); always @(posedge clk ,negedge rst) begin if(rst==0) begin q<=0;qbar<=1;end else begin q<=d; qbar<=~d; end end endmodule module dff_tb(); reg d,clk; wire q,qbar; dff dff_i1(d,clk,q,qbar); initial begin clk=0;d=0; #10d=1; end always #5 clk=~clk; endmodule module seq_detector(     input x,clk,rst,     output z ); wire d0,d1,d2; wire q0,q1,q2; wire q0bar,q1bar,q2bar; wire w1,w2,w3,w4,w5,w6; and and_i1(w1,q2bar,q0bar);//w1=q2'q0' xor xor_i1(w2,x,q1);//w2=x^q1 and and_i2(d0,w1,w2);//d0=w1w2 dff dff_i1(d0,clk,rst,q0,q0bar); and and_i3(w3,q2bar,q1bar,q0,x);//w3=q2'q1'q0x and and_i4(w4,q2,q1bar,q0bar,x);//w4=q2q1'q0'x and and_i5(w5,q2bar,q1,q0bar);//w5=q2'q1q0' or or_i1(d1,w3,w4,w5); dff dff_i2(d1,clk,rst,q1,q1bar); and and_i6(d2,q2bar,q1,q0,x); dff dff_i3(d2,clk,rst,q2,q2bar); and and_i7(z,q2,q1bar,q0bar,x); endmodule module seq_detector_tb(); reg x,clk,rst; wire z; seq_detector seq_detector_i1(x,cl...

RISC V Code

 module processor (clk1, clk2); input clk1,clk2;// Two-phase clock reg [31:0] PC, IF_ID_IR, IF_ID_NPC; reg [31:0] ID_EX_IR, ID_EX_NPC, ID_EX_A, ID_EX_B, ID_EX_Imm; reg [2:0] ID_EX_type, EX_MEM_type, MEM_WB_type; reg [31:0] EX_MEM_IR, EX_MEM_ALUOut, EX_MEM_B;  reg EX_MEM_cond; reg [31:0] MEM_WB_IR, MEM_WB_ALUOut, MEM_WB_LMD; reg [31:0] Reg [0:31];// Register bank (32 x 32) reg [31:0] Mem [0:1023];// 1024 x 32 memory parameter ADD=6'b000000,SUB=6'b000001, AND=6'b000010, OR=6'b000011, SLT=6'b000100, MUL=6'b000101, HLT=6'b111111, LW=6'b001000, SW=6'b001001, ADDI=6'b001010, SUBI=6'b001011, SLTI=6'b001100, BNEQZ=6'b001101, BEQZ=6'b001110; parameter RR_ALU=3'b000, RM_ALU=3'b001, LOAD=3'b010, BRANCH=3'b100, HALT=3'b101,STORE=3'b011; reg HALTED;// Set after HLT instruction is completed (in WB stage) reg TAKEN_BRANCH;// Required to disable instructions after branch always @ (posedge clk1) // IF Stage if (HALTED == 0)...

Subsets of arr bitwise

 void subarray(int*arr, int n){     //hashmap     for(int i=0;i<=(1<<n);i++){         for(int j=0;j<n;j++){             if(i&(1<<j)){                 cout<<arr[j]<<" ";             }         }         cout<<endl;     }      }

Set bits in log(n)

 int set_bits(int n){     int  count=0;     while(n){         n=n&(n-1);         count++;     }     return count; }

is power of 2 (Bitwise)

 bool is_pow2(int n){         while((n/2)>0){         int m=(n%2);                  //cout<<m;         if(m==1){             return false;         }         n=n/2;              }     return true; }

Precomputation

 // Online C++ compiler to run C++ program online #include <iostream> using namespace std; const int m=1e9+7; const int n=1e5+10; long long fact[n]; int main() {     fact[0]=fact[1]=1;     for(int i=2;i<n;i++){         fact[i]=fact[i-1]*i;     }     int num;     cin>>num;     cout<<fact[num];               return 0; }

Jiya's Sequence

  Problem Jiya likes a sequence if all its elements when multiplied yields a number , whose least significant digit is either 2, 3 or 5. Given the number of  test case t.The first line of each test case is a number n.Next line contains n integers - A 1 ,A 2 ,......A n .For each given test case tell whether the given sequence will be liked by Jiya. INPUT FORMAT- First line constains t number of test cases. Every test case has first line as the number of intergers the sequence contains, followed by sequence in the next line. OUTPUT FORMAT - Print "YES" or "NO",whether Jiya likes the sequence or not. CONSTRAINTS- 1≤t≤100 1≤n≤15 1≤A i ≤10, for all i   HINT -  Least significant digit of a number can be obtained by taking the number % 10. Mind the case of output to be printed. Sample Input 2 5 2 2 2 2 2 4 2 2 2 2 Sample Output YES NO Time Limit: 1 Memory Limit: 256 Source Limit: Explanation For the first testcase,considering the sequence 2 2 2 2 2, when we multi...

CapitalBaazi

  Problem Cheems doesn't like extra content so coming straight to the point. Given the input of one line, having words separated by a single space, print each word of the input, each in a new line. Also, change all the alphabets to uppercase along with doing this. Input:- Given in one line, all letters are lowercase only, separated by a single space. Characters used in input are from 'a' to 'z' only ( 26 in total ) , both included. Length of input   ≤   10 4 Output:- Output each word with characters in uppercase. Note:- A testcase contains only one word as the input to get partial marks. Also, the ideal solution has been provided, you can test any of your legal input to get its answer. Sample Input subscribe to the luv channel Sample Output SUBSCRIBE TO THE LUV CHANNEL Time Limit: 1 Memory Limit: 256 Source Limit: Explanation Every word of the input has been placed in the new line with characters in uppercase as well. #include < iostream > #incl...

Subarray with given sum

  Given an unsorted array   A  of size   N   that contains only non-negative integers, find a continuous sub-array which adds to a given number   S .   Example 1: Input: N = 5, S = 12 A[] = {1,2,3,7,5} Output: 2 4 Explanation: The sum of elements from 2nd position to 4th position is 12.   Example 2: Input: N = 10, S = 15 A[] = {1,2,3,4,5,6,7,8,9,10} Output: 1 5 Explanation: The sum of elements from 1st position to 5th position is 15.   Your Task: You don't need to read input or print anything. The task is to complete the function  subarraySum () which takes arr, N and S as input parameters and returns a list containing the starting and ending positions of the first such occurring subarray from the left where sum equals to S. The two indexes in the list should be according to 1-based indexing. If no such subarray is found, return an array consisting only one element that is -1.   Expected Time Complexity:...

MOZSATLA - Sharmeen and the Lost Array

  MOZSATLA - Sharmeen and the Lost Array no tags   Sharmeen loves array very much. Many days ago she wrote an array ( of n elements and the index of this array is 1 based ) in her notebook, but unfortunately she lost the notebook. She want to restore the array. The only clue she know that is if in any position(i) of the array (1<= i < n ), the element in this position is greater than, equal or less than the next position(i+1) element.             Can you help her to restore the array? Input In the first line given an integer t ( 1 <= t <= 100 ), which is the number of test cases. For each test case, In the first line there will be given a positive integer n ( 1 <= n <= 10^5 ) which is the size of the lost array. In the next line there will be n - 1 intergers X i ( 0 <= X i  <= 2 ). If, X i  = 0 , then i th   element is equal to (i+1) th  element of the lost array. If, X i ...

Micro and Array Update

  Problem Micro purchased an array  A  having  N  integer values. After playing it for a while, he got bored of it and decided to update value of its element. In one second he can increase value of each array element by  1 . He wants each array element's value to become greater than or equal to  K . Please help Micro to find out the minimum amount of time it will take, for him to do so. Input: First line consists of a single integer,  T , denoting the number of test cases. First line of each test case consists of two space separated integers denoting  N  and  K . Second line of each test case consists of  N  space separated integers denoting the array  A . Output: For each test case, print the minimum time in which all array elements will become greater than or equal to  K . Print a new line after each test case. Constraints: 1 ≤ T ≤ 5 1 ≤ N ≤ 10 5 1 ≤ A [ i ] , K ≤ 10 6 Sample Input 2 3 4 1 2 5 3 2 2 5 5 Sample ...