Skip to content

Instantly share code, notes, and snippets.

#include <iostream>
#include <string>
using namespace std;
void removeChars(string &str, char ch)
{
int i;
int resInd = 0; //Result string index
for( i = 0; i < str.length(); i++ )
{
@ravichandrae
ravichandrae / ReverseWords.cpp
Last active December 25, 2015 09:19
This program reverses the words in the given string.
#include < iostream > #include < string > #include < algorithm >
using namespace std;
void reverseWords(string & str) {
//reverse the entire string
reverse(str.begin(), str.end());
string::iterator strIt;
string::iterator wordBegin = str.begin();
@ravichandrae
ravichandrae / MajorityNumber.java
Created October 13, 2013 02:30
This java program implements the moore's voting algorithm to find the majority number ( which appears in more than half) in the array
public class Main {
public static void main(String[] args)
{
int [] array = {6,2,3,6,3,6,6,6};
System.out.println("Majority number is: " + getMajorityNum(array));
}
public static int getMajorityNum(int[] array)
{
//Initialize first num as majority num
int majorNum = array[0];
@ravichandrae
ravichandrae / MoveZeroes2End.java
Last active December 25, 2015 10:49
This program moves all the zeros to the end of array.
/**
* Created with IntelliJ IDEA.
* User: Ravi
* Date: 10/13/13
* Time: 9:56 PM
* To change this template use File | Settings | File Templates.
*/
public class MoveZerosDemo {
public static void main(String[] args)
{
@ravichandrae
ravichandrae / PrintLastKLines.java
Created October 14, 2013 01:42
This program prints the last k lines from a given file.
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* User: Ravi
* Date: 10/14/13
* Time: 6:36 AM
* To change this template use File | Settings | File Templates.
@ravichandrae
ravichandrae / NthNodeFromEnd.cpp
Created October 15, 2013 01:12
This program prints the nth node from the end of a linked list.
#include <iostream>
using namespace std;
//Node definition for a single linked list
class SLLNode
{
private:
//data member
int data;
@ravichandrae
ravichandrae / FindLeastDiffNumArray.java
Created October 15, 2013 02:38
This program finds the closest number in the given sorted array
/**
* Created with IntelliJ IDEA.
* User: Ravi
* Date: 10/15/13
* Time: 7:09 AM
* To change this template use File | Settings | File Templates.
*/
public class FindLeastDiffNumArray {
public static void main(String[] args)
{
@ravichandrae
ravichandrae / ArrangeAlternate.java
Created October 16, 2013 05:16
This program converts an array of format {a1,a2,...an,b1,b2,...bn} to {a1,b1,a2,b2,...an,bn}
/**
* Created with IntelliJ IDEA.
* User: Ravi
* Date: 10/15/13
* Time: 7:51 PM
* To change this template use File | Settings | File Templates.
*/
public class ArrangeAlternate {
public static void main( String[] args )
{
@ravichandrae
ravichandrae / BiggestElementsRight.java
Created October 17, 2013 00:55
This program prints the array of biggest element to the right of given array
/**
* Created with IntelliJ IDEA.
* User: Ravi
* Date: 10/17/13
* Time: 6:01 AM
* To change this template use File | Settings | File Templates.
*/
public class BiggestElementsRight {
public static void main(String [] args)
{
@ravichandrae
ravichandrae / SearchSortedMatrix.java
Last active December 25, 2015 18:09
This program searches for the given element in a row wise and column wise sorted matrix
/**
* Created with IntelliJ IDEA.
* User: Ravi
* Date: 10/17/13
* Time: 6:41 AM
* To change this template use File | Settings | File Templates.
*/
public class SearchSortedMatrix {
public static void main(String [] args)
{