Given an integer array and an element x, find if element is present in array or not. If element is present, then print index of its first occurrence. Else print -1.

Input:

First line contains an integer, the number of test cases 'T' Each test case should contain an integer, size of array 'N' in the first line. In the second line Input the integer elements of the array in a single line separated by space. Element X should be inputted in the third line after entering the elements of array.

Output:

print the output in a separate line returning the index of the element X.If element not present then print -1.

Constraints:

1 <= T <= 100

1 <= N <= 100

1 <= Arr[i] <= 100

Example:

Input:
1
4
1 2 3 4
3

Output:
2

Explanation:
There is one test case with array as {1, 2, 3 4} and element to be searched as 3.  Since 3 is present at index 2, output is 2

我的代码实现:

#include <iostream>

using namespace std;

int main()
{
    int T;
    cin>>T;
    while(T--){
        int N,j,X,index=-1;
        cin>>N;
        int *Arr=new int[N];
        for(j=0;j<N;j++)
        {
            cin>>Arr[j];
        }
        cin>>X;
        for(j=0;j<N;j++)
        {
            if(Arr[j]==X)
            {
                index=j;
                break;
            }
        }
        cout<<index<<endl;
    }
    return 0;
}

  

Search an Element in an array的更多相关文章

  1. [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)

    原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an ...

  2. 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array

    乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...

  3. 【刷题-LeetCode】215. Kth Largest Element in an Array

    Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is t ...

  4. check the element in the array occurs more than half of the array length

    Learn this from stackflow. public class test { public static void main(String[] args) throws IOExcep ...

  5. Kth Largest Element in an Array

    Find K-th largest element in an array. Notice You can swap elements in the array Example In array [9 ...

  6. leetcode@ [315/215] Count of Smaller Numbers After Self / Kth Largest Element in an Array (BST)

    https://leetcode.com/problems/count-of-smaller-numbers-after-self/ You are given an integer array nu ...

  7. leetcode面试准备:Kth Largest Element in an Array

    leetcode面试准备:Kth Largest Element in an Array 1 题目 Find the kth largest element in an unsorted array. ...

  8. Majority Element in an Array

    Problem Statement Given a large array of non-negative integer numbers, write a function which determ ...

  9. Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)

    本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...

随机推荐

  1. 在没有DOM操作的日子里,我是怎么熬过来的(中)

    前言 继上篇推送之后,在掘金.segmentfault.简书.博客园等平台上迅速收到了不俗的反馈,大部分网友都留言说感同身受,还有不少网友追问中篇何时更新.于是,闰土顺应呼声,在这个凛冽的寒冬早晨,将 ...

  2. Hadoop实战训练————MapReduce实现PageRank算法

    经过一段时间的学习,对于Hadoop有了一些了解,于是决定用MapReduce实现PageRank算法,以下简称PR 先简单介绍一下PR算法(摘自百度百科:https://baike.baidu.co ...

  3. python中函数的参数解析

    python中函数的各种参数梳理: 1.形参:函数定义时传入的参数 2.实参:函数调用时传入的参数 (有形参必传实参,形参里自身特点可不传的,可传可不传) 3.缺省参数:不传为默认值,传了会覆盖(下面 ...

  4. NSNotification

    1.什么是NSNotification 每个运行中的application都有一个NSNotificationCenter的成员变量,它的功能就类似与公共栏,对象在这里注册关注每个确定Notifica ...

  5. 升级PyCham到2017.3后import sys模块报错的问题

    今天PyCharm提示升级后选择了更新,根据提示更新成功(2017.3)后发现总是报无法找到sys模块的错误,截图如下: 其实有一条红线留在那里也不影响运行和使用,但总看着不爽. 经过一番研究,由于我 ...

  6. 51Nod 1352 集合计数 扩展欧几里得

    基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 给出N个固定集合{1,N},{2,N-1},{3,N-2},...,{N-1,2},{N,1}.求出有多少个集合满足 ...

  7. 关于PHP输出字符串多了两个字节的BUG

    近日IOS开发那边小伙伴跟我说,解析服务器发回的字符信息时候出现bug. 明明利用Log输出来的是字符串"hello"  可是利用length计算就是多出来两个字节,比如这里是7. ...

  8. 刚从it培训班出来的学生如何走向工作岗位

    大家好,这是我本人在博客园的第一篇博文. 相信很多人都是从 it 培训班学习然后加入到程序员这个大家族,或多或少,有些人会和博主有一样的感受,所以此篇博文我们不讨论技术,博主也是刚从培训班坑里跳出来正 ...

  9. QQ互联申请及配置

    今天要说的只是针对QQ互联的操作,其他的互联请参考相关网站. 第一步:需要申请API接口的两码 自行登录QQ互联https://connect.qq.com/index.html,然后按照要求申请就O ...

  10. python爬虫(七)_urllib2:urlerror和httperror

    urllib2的异常错误处理 在我们用urlopen或opener.open方法发出一个请求时,如果urlopen或opener.open不能处理这个response,就产生错误. 这里主要说的是UR ...