Intro to Tutorial Challenges Hackerrank problem solution

Intro to Tutorial Challenges Hackerrank

Sample Challenge
This is a simple challenge to get things started. Given a sorted array (ar) and a number (V), can you print the index location of V in the array?

The next section describes the input format. You can often skip it, if you are using included methods.

Input Format
There will be three lines of input:

V – the value that has to be searched.
n – the size of the array.
ar – n numbers that make up the array.
Output Format
Output the index of V in the array.

The next section describes the constraints and ranges of the input. You should check this section to know the range of the input.

Constraints
1<=N<=1000 -1000<=V<=1000 V{ar} It is guaranteed that V will occur in ar exactly once. This "sample" shows the first input test case. It is often useful to go through the sample to understand a challenge.Sample Input

4
6
1 4 5 7 9 12

Sample Output

1
Explanation
V=4. The value 4 is the 2nd element in the array, but its index is 1 since array indices start from 0, so the answer is 1.

Solution
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
int v, n, a[1000],i;
    scanf("%d",&v);
   scanf("%d",&n);
    for(i=0; i<n;i++){
        scanf("%d",&a[i]);
    }
    for(i=0; i<n;i++){
        if(a[i]==v)
            printf("%d",i);
    }
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}
V=input();
N=int(input());
A=input();
A=A.split();
print(A.index(V))
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner in = new Scanner(System.in);
           int v = in.nextInt();
           int n = in.nextInt();
        
           int[] ar = new int[n];
           for(int i=0;i<n;i++){
              ar[i]=in.nextInt();
           }  
        
           int i=0;
           while (ar[i]<v){
                i++;
           }
           System.out.println(i);
        
    }
}
#include<string>
#include<iostream>
#include<iterator>
#include<algorithm>
#include<vector>
using namespace std;

class Binary
{
public:
	
	int Search(vector<int>V, int Value)
	{
		int low = 0; int high = V.size();
		while (low <= high)
		{
			int mid = low + (high - low) / 2;
			if (Value == V[mid])
			{
				return mid;
			}
			else if (V[mid] < Value)
			{
				low = mid + 1;
			}
			else
			{
				high = mid - 1;
			}
		}
		cout << "Target wasn't found" << endl;
	}
};




//Binary Search Implementation
vector<int>Ans;
Binary B;
auto main()->int
{
	int n;
	cin >> n;
	int l;
    cin>>l;
	for (int i = 0; i != l; i++)
	{
		int p;
		cin >> p;
		Ans.push_back(p);
	}
	cout<<B.Search(Ans,n);
	return 0;
}

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.