Tuesday, January 21, 2020

Converting Integers to Roman Numerals - Java


     Roman Number are a system of numerical notations used by the Romans.

1= I
5= V
10= X
50= L
100= C
500= D
1000= M = I
5000V
10,000X
50,000L
100,000C = I
500,000D = V
1,000,000M = X
5,000,000V = L
10,000,000X = C
50,000,000L = D
100,000,000 C = M
500,000,000 D
1,000,000,000M
nn = 1000×nnnn = 100,000×nnnn = 1,000,000×nn

                                                                                                                                                            























import java.util.*; class RomanCounting { public static void main(String[] args) { int num; System.out.println("please the number smallest than 999999"); System.out.println("According to automated program show 5000 as |V| and 10000 as |X| " + "and \n 50000 " + "as |L| and 100000 as |C| and 500000 as |D| and last as 1000000 as |M| "); Scanner obj = new Scanner(System.in); num = obj.nextInt(); String str = String.valueOf(num); char []arr = str.toCharArray(); int len = str.length(); int count[] = new int[10]; for(int i=0;i<len;i++){ count[i] = (int)arr[i]-48; } String ones[] = new String[]{"","I","II","III","IV","V","VI","VII","VIII","IX","X"}; String tens[] = new String[]{"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC","C"}; String hundred[] = new String[]{"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM","M"}; String thousand[] = new String[]{"","M","MM","MMM","MMMM","|V|","|V|M","|V|MM","|V|MMM", "M|X|","|X|"}; String TenThousand[] = new String[]{"","|X|","|X||X|","|X||X||X|","|X||L|","|L||X|", "|L||X||X|","|L||X||X|","|C||L|","|C|"}; String[] HundredThousand = new String[]{"","|C|","|C||C|","|C||C||C|","|C||D|","|D|", "|D||C|","|D||C||C|","|D||M|","|M|"}; if(len==6){ String romancounting = HundredThousand[count[0]]+ TenThousand[count[1]]+ thousand[count[2]] + hundred[count[3]] + tens[count[4]] + ones[count[5]]; System.out.println(romancounting); } else if(len==5){ String romancounting = TenThousand[count[0]]+ thousand[count[1]]+ hundred[count[2]] + tens[count[3]] + ones[count[4]]; System.out.println(romancounting); } else if(len==4){ String romancounting = thousand[count[0]]+ hundred[count[1]] + tens[count[2]] + ones[count[3]]; System.out.println(romancounting); } else if(len==3) { String romancounting = hundred[count[0]]+tens[count[1]]+ones[count[2]]; System.out.println(romancounting); } else if(len==2){ String romancounting = tens[count[0]]+ones[count[1]]; System.out.println(romancounting); } else{ System.out.println("Sorrt But number is invalid! OutOfRange👌"); } System.out.println("Thanks for using your automated software"); } }

For Output : OUTPUT

Friday, January 17, 2020

Count the Occurrence of Character in the Linked List

~ Create an array.
~ One By One place the  character on array  on based on ASCII Value .
~ then, check the value of every index and find greater value/Occurence of Character.
public class LinkedlistOperation {

 Node head;
     class Node{
        int data;
        Node next;
           Node(int d){
               data =d;
               next = null;
       }    }


 public void OccurrenceNumber(){
    Node current= head;
    int arr[] = new int[256];
    while(current!=null){
       (arr[current.data])++;
       current =current.next;
          }
    System.out.println("Occurrence of all character are :");

    for(int i=0;i<256;i++){
        if(arr[i]>0){
          System.out.println(i+":" +arr[i]+" ");
   } } }


 public static void main(String ...args){
 LinkedlistOperation list = new LinkedlistOperation();
   list.push(1);
   list.push(1);
   list.push(3);
   list.push(4);
   list.push(2);
   list.push(2);
   list.push(15);
   list.push(20);
   list.push(1);
   list.push(1);
   list.push(3);
   list.push(4);
   list.push(5);
  list.OccurrenceNumber()  }
 OUTPUT :

Occurence of all Character are :
1:4 
2:2 
3:2 
4:2 
5:1 
15:1 
20:1 


Thursday, January 16, 2020

Insertion Node in the Linkelist.

In this post, methods to insert a new node in linked list are discussed. A node can be added in three ways
1) At the front of the linked list
2) After a given node.
3) At the end of the linked list
public class Linkedlist {
 Node head;
 class Node{
  int data;
  Node next;
  Node(int d){
   data =d;
   next=null;
  } 
 }
// INSERT THE NODE AT THE BEGIN OF LINKEDLIST.

 
 public void insertAtfront(int new_data){
  
//  Node temp = head;
  Node new_node = new Node(new_data);
  new_node.next = head;
  head = new_node;
  
 } 
// INSERT THE NODE AT THE GIVEN POSITION IN LINKEDLIST.

 public void insertAtGiven(Node prev_node,int new_data)
 {
  if(prev_node == null){
   System.out.print("previous node can't be null");
   return;
   
  }
  Node new_node = new Node(new_data);
  new_node.next = prev_node.next;
  prev_node.next = new_node;
 }
// INSERT THE NODE  AT THE END OF THE LINKEDLIST.  

 public void insertAtEnd(int new_data){
  
  Node new_node = new Node(new_data);
  new_node.next = null;
  Node last = head;
  while(last.next!=null){
   last = last.next;
  
  }
  last.next= new_node;
  return;
  
 }
// TRAVERSING THE ELEMENT OF LINKEDLIST

 
 public void showlist(){
  Node tnode= head;
  while(tnode!=null){
   System.out.print(tnode.data+" ");
   tnode = tnode.next;
  }
 }
 // MAIN FUNCTION 

public static void main(String ...args) { list.insertAtfront(10); list.insertAtfront(12); list.insertAtfront(14); list.insertAtfront(16); list.insertAtfront(18); list.insertAtfront(19); list.insertAtGiven(12,20); list.insertAtGiven(20,22); list.insertAtEnd(24); list.showlist(); } }

Thursday, December 19, 2019

Launch of 5G chipsets by Qualcomm for AR/VR Headsets

Virtual reality (VR) and augmented reality (AR) is all set to transform the way users used to access technology. The headsets can take you to places and show things that don't even exist in the real world. Qualcomm's all-new AR/VR tech brings 5G support and caters to the needs of standalone VR, AR headsets.

Qualcomm claims that the XR2 chip is in line with the company's new mobile chips, including the most awaited Snapdragon 865, 765, and 765G. These chipsets support 5G mobile network connectivity. Qualcomm believes that the future of mobile devices is an aggregate of different display technologies. The VR headsets can be used for various uses cases, right from web browsing, to recipe tutorials and even GPS navigation.

In an official statement, Qualcomm said, "Multiple OEMs are committed to commercializing devices with the Snapdragon XR2 Platform, and other customers are in various stages of prototyping and evaluation."

The XR2 5G platform is designed to support 3K x 3K displays at 90Hz. It will offer accelerated AI processing, and work with up to seven simultaneous camera feeds. The XR2 is a successor to the Snapdragon XR1 which was made especially for the low-end AR and VR headsets.

The latest iteration of the chipset targets high-end standalone headsets including Oculus Quest 2, Magic Leap 2, and similar next-generation devices. The new platform offers notable improvements. Qualcomm claims that the CPU and GPU performance by 2x, pixel throughput for video playback by 4x, and up to 6x resolution per-eye compared to Snapdragon 835.

The simultaneous camera will support advanced tracking, both for the environment and the user. Oculus Quest features four cameras that can track the position of headset and controllers. XR2 offers low-latency pass-through, which improves the pass-through video experience on high-end headsets.

C++ program to convert Sorted Array to binary search tree

Binary Search Tree is a node-based binary tree data structure which has the following properties:
  • The left subtree of a node contains only nodes with keys lesser than the node%u2019s key.
  • The right subtree of a node contains only nodes with keys greater than the node%u2019s key.
  • The left and right subtree each must also be a binary search tree.
Algorithm
The mid-value of the given sorted array would represent the root of one possible BST that can be constructed from the given array elements.
1. Find the middle element of the array and make it root(Now the array is divided into two halves left and right, where the left half is our left subtree and right half, is our right subtree)
2. Recursively do the same for the left half and right half.
       (a) Get the middle of left half and make it left child of the root created into Step 1.
       (b) Get the middle of right half and make it right child of the root created into Step 1.
3. The base condition that would terminate the recursion would then be if low boundary index            exceeds high boundary index, in which case return null.

#include<iostream> using namespace std; struct Node { int data; Node *left; //pointer points to left node Node *right; //pointer points to right node }; /*function that returns new node*/ Node *newNode(int x) { Node *temp = new Node(); //temp points to the dyanamically formed node temp->data = x; temp->left = NULL; temp->right = NULL; return temp; //return the address of the node temp is pointing } Node *arrayToBST(int *arr, int start, int end) { /* Base Case*/ if(start > end) { return NULL; } /*Get the middle element and make it root*/ int mid = (start + end)/2; Node *root = newNode(arr[mid]); /* Recursively construct the left subtree and make it left child of root */ root->left = arrayToBST(arr,start,mid-1); /* Recursively construct the right subtree and make it right child of root */ root->right = arrayToBST(arr,mid+1,end); return root; } /*Function inOrder prints inorder traversal of the tree inorder traversal prints element in sorted order
*/ void inOrder(Node *node) { if(node == NULL) return; inOrder(node->left); cout<<node->data<<" "; inOrder(node->right); } int main() { int arr[] = {2, 4, 5, 6, 7, 10, 11, 15}; /*Number of elements in the array (total size of the array / size of one element ) In this case elements are integer */ int n = sizeof(arr)/sizeof(arr[0]); Node *root = arrayToBST(arr,0,n-1); inOrder(root); return 0; }

OUTPUT:
2 4 5 6 7 10 11 15

Our inorder traversal prints elements in sorted order, so our binary search tree is correct.
Time Complexity

T(n) = 2T(n/2) + C
By Master Theorem
T(n) = O(n)

Dynamic Programming - Rod Cutting Problem

This is one of the famous interview questions and most of you faced this question in the interview.
Question: Given a rod of length n and list of prices of rod of length i where 1 <= i <= n, find the optimal way to cut rod into smaller rods in order to maximize profit.
Program:
#include<iostream>
#include<algorithm>

using namespace std;

#define ROD_CUTS 4
#define LEN      5

int rodSize[ROD_CUTS] = {1, 2, 3, 4};
int rodSizeCost[ROD_CUTS] = {2, 5, 7, 8};
int costMatrix[ROD_CUTS][LEN+1] = {0};

int findCost(int i, int j)
{
   if(i < 1)
      return costMatrix[i][j-rodSize[i]]+rodSizeCost[i];
   else
   {
      if(j < rodSize[i])
         return costMatrix[i-1][j];
      else
         return std::max(costMatrix[i][j-rodSize[i]]+rodSizeCost[i], costMatrix[i-1][j]);
   }
}

int main()
{
   for(int i=0; i<ROD_CUTS; ++i)
   {
      for(int j=1; j<=LEN; ++j)
      {
         costMatrix[i][j] = findCost(i, j);
      }
   }
   cout << "Maximum Profit = " << costMatrix[ROD_CUTS-1][LEN] << endl;
   return 0;
}

Output:
Maximum Profit = 12

Implementing Stack using Linked List

QUESTION-

You need to implement a Stack class using linked list. The data members should be private. Implement the following public functions :

1. Constructor -

Initialises the data member (i.e. head to null).

2. push :

This function should take one argument of type int and has return type void. This function should insert an element in the stack. Time complexity should be O(1).

3. pop :

This function takes no input arguments and has return type int. This should removes the last element which is entered and return that element as an answer. Return 0 if stack is empty.

Time complexity should be O(1).

4. top :

This function takes no input arguments and has return type int. This should return the last element which is entered and return that element as an answer. Return 0 if stack is empty.

Time complexity should be O(1).

5. size :

Return the size of stack i.e. count of elements which are present ins stack right now. Time complexity should be O(1).

6. isEmpty :

Checks if the stack is empty or not. Return true or false.


SOURCE CODE-
#include<iostream>
using namespace std;

class Node {
    public :
    int data;
    Node *next;
    
    Node(int data) {
        this -> data = data;
        next = NULL;
    }
};

class Stack {
    Node *head;
    int size; // number of elements present in stack
    
    public :
    
    Stack() {
      size=0;
      head=NULL;
    }
    
    int getSize() {
        return size;
    }
    
    bool isEmpty() {
        return size==-1;
    }
    
    void push(int element) {
        Node* current=new Node(element);
      size++;
      current->next=head;
      head=current;
    }
    
    int pop() {
        // Return 0 if stack is empty. Don't display any other message
      if(head==NULL)
        return 0;
      size--;
      int data=head->data;
        head=head->next;
      return data;
    }
    
    int top() {
        // Return 0 if stack is empty. Don't display any other message
      if(head==NULL)
        return 0;
        return head->data;
    } 
};

#include "Stack.h"
int main() {
    
    Stack st;
    
    int choice;
    cin >> choice;
    int input;
    
    while (choice !=-1) {
        if(choice == 1) {
            cin >> input;
            st.push(input);
        }
        else if(choice == 2) {
            int ans = st.pop();
            if(ans != 0) {
                cout << ans << endl;
            }
            else {
                cout << "-1" << endl;
            }
        }
        else if(choice == 3) {
            int ans = st.top();
            if(ans != 0) {
                cout << ans << endl;
            }
            else {
                cout << "-1" << endl;
            }
        }
        else if(choice == 4) {
            cout << st.getSize() << endl;
        }
        else if(choice == 5) {
            cout << st.isEmpty() << endl;
        }
        cin >> choice;
    }
    
}

INPUT-
1 3 1 4 1 63 1 21 1 9 2 3 1 7 2 4 2 2 3 1 799 1 33 1 35 3 2 2 1 97 2 3 2 1 22 1 999 1 23 1 98 2 2 5 3 -1

OUTPUT-
9
21
7
4
21
63
4
35
35
33
97
799
799
98
23
false
999

Arrays in Solidity Programming Language.

Arrays Solidity supports both generic and byte arrays. It supports both fixed size and dynamic arrays. It also supports multidimensional ...