Count Substrings[GeeksforGeeks solution]

Question: Given a binary string, count number of substrings that start and end with 1. For example, if the input string is “00100101”, then there are three substrings “1001”, “100101” and “101”.

Input:
The first line contains T denoting the number of testcases. Then follows description of testcases.
Each case contains a string containing 0’s and 1’s.
Output:
For each test case, output a single line denoting number of substrings possible.

Constraints:
1<=T<=100
1<=Lenght of String<=100

Example:
Input:
1
10101

Output:
3

CODE:

#include <iostream>
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() 
{ 
//code int t; 
cin>> t; 
while(t--) 
{    
    int ans =0;    
    int count=0;
    string s;
    cin>>s;
    int n = s.length();
    for(int i=0;i<n;i++)
    {        
         if(s[i] == '1')
         {
            count++;
         }
    }    
    ans = (count * (count-1))/2;
    cout<<ans<<endl; 
} 
return 0;
}

FOR IDE:

https://ide.geeksforgeeks.org/HFIXGHuLP6

EASY?

 

Advertisements

Find Merge Point of Two Lists [hackerRank Solution]

Question:

Given pointers to the head nodes of  linked lists that merge together at some point, find the Node where the two lists merge. It is guaranteed that the two head Nodes will be different, and neither will be NULL.

In the diagram below, the two lists converge at Node x:

[List #1] a--->b--->c
                     \
                      x--->y--->z--->NULL
                     /
     [List #2] p--->q

 

Complete the int FindMergeNode(Node* headA, Node* headB) method so that it finds and returns the data value of the Node where the two lists merge.

Continue reading “Find Merge Point of Two Lists [hackerRank Solution]”

Delete duplicate-value nodes from a sorted linked list[HackerRank solution]

Question:

You’re given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete as few nodes as possible so that the list does not contain any value more than once. The given head pointer may be null indicating that the list is empty.

For now do not be concerned with the memory deallocation. In common abstract data structure scenarios, deleting an element might also require deallocating the memory occupied by it. For an initial intro to the topic of dynamic memory please consult: http://www.cplusplus.com/doc/tutorial/dynamic/

Continue reading “Delete duplicate-value nodes from a sorted linked list[HackerRank solution]”

Get Node Value [HackerRank solution]

You’re given the pointer to the head node of a linked list and a specific position. Counting backwards from the tail node of the linked list, get the value of the node at the given position. A position of 0 corresponds to the tail, 1 corresponds to the node before the tail and so on.

Input Format
You have to complete the int GetNode(Node* head, int positionFromTail) method which takes two arguments – the head of the linked list and the position of the node from the tail. positionFromTail will be at least 0 and less than the number of nodes in the list. You should NOT read any input from stdin/console.

Continue reading “Get Node Value [HackerRank solution]”

Monk and Rotation [Hacker earth]

PROBLEM:

Monk loves to preform different operations on arrays, and so being the principal of Hackerearth School, he assigned a task to his new student Mishki. Mishki will be provided with an integer array AA of size NN and an integer KK , where she needs to rotate the array in the right direction by K steps and then print the resultant array. As she is new to the school, please help her to complete the task.

Input:
The first line will consists of one integer TT denoting the number of test cases.
For each test case:
1) The first line consists of two integers NN and KKNN being the number of elements in the array and KK denotes the number of steps of rotation.
2) The next line consists of NN space separated integers , denoting the elements of the array AA.

Continue reading “Monk and Rotation [Hacker earth]”

USING SCANNER FOR INPUT IN JAVA

Scanner is a class in java.util package used for obtaining the input of the primitive types like int, double etc. and strings.

In Java, we input with the help of the Scanner class. Java has a number of predefined classes which we can use.

import java.util.Scanner;  // This will import just the Scanner class
import java.util.*;  // This will import the entire java.util package

Continue reading “USING SCANNER FOR INPUT IN JAVA”

Sum of digits in c or c++ without pointers

 

Screen Shot 2017-08-16 at 1.30.35 PM

SO, the main logic behind this is=:

b = a%10; // we took the remainder after dividing by 10 thats the last digit

sum = sum+ (b*c); // adding last digit to sum

a = a/10; //to remove last digit from a

c= c/10; //to remove last digit from a

and in last we will return sum.

Append in rails

Screen Shot 2017-07-27 at 12.05.43 AM

THESE both line ; the line 3 and 9 meant same.

You can use either of them. These lines are used when you want to append html pages using ajax.

What is Bitcoin?

The founder of Bitcoin is Satoshi Nakamoto, an anonymous online alias for a forum poster. The founder remains unknown, and no one truly knows who the actual founder is.

Bitcoin is the first global, decentralized currency that allows you to send money from one person to another without involving a third party broker, such as a bank. You only need your computer to make transactions because Bitcoin is fundamentally software.

As a decentralized currency, Bitcoin isn’t controlled by anyone. It’s open so that anyone can benefit from it.

You might think that the lack of control could mean chaos, but that’s not true at all. That’s because Blockchain, the technology behind Bitcoin is one of the most accurate and secure systems ever created.

A blockchain is a ledger that keeps records of digital transactions. Instead of having a central administrator, like a bank or the government, blockchain organizes data in batches called blocks. These data batches use cryptographic validation to link themselves together. In other words, each block identifies and references the previous block by a hash value, forming an unbroken chain.

Blockchain solves two of the most challenging problems of digital transactions: controlling the information and avoiding duplication. When a purchase is carried out, the ledger records it and sends it out to the entire network. Computers all over the world then compete to confirm the operation by solving complex math equations. The first to figure out the answer and validate the block receives a reward in Bitcoins (this process is called mining.)

The validated block is timestamped and added to a chain in chronological order. The entire chain is continually updated so that it’s always an accurate representation of who owns what at any given time.

The legality of Bitcoin depends on where you’re located. Bitcoin was legalized as a formal method of payment in Japan this year, and India might be next. In most countries, however, it somewhat operates in a gray zone, with no official ban or approval of Bitcoin.

The founder of Bitcoin is Satoshi Nakamoto, an anonymous online alias for a forum poster. The founder remains unknown, and no one truly knows who the actual founder is.

At the moment, Bitcoin is owned by the community as a whole, as it is an open source initiative, with no single owner behind it. It is a community movement.

Create a free website or blog at WordPress.com.

Up ↑