Search an Element in an array
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的更多相关文章
- [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 ...
- 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array
乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...
- 【刷题-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 ...
- 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 ...
- 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 ...
- 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 ...
- leetcode面试准备:Kth Largest Element in an Array
leetcode面试准备:Kth Largest Element in an Array 1 题目 Find the kth largest element in an unsorted array. ...
- Majority Element in an Array
Problem Statement Given a large array of non-negative integer numbers, write a function which determ ...
- Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...
随机推荐
- WebVR认识
什么是WebVR WebVR是一种开放标准,可以在浏览器中体验VR,目标是让每个人都能轻松进入虚拟现实体验.
- JAVA基础-JDBC二(常用的开源工具)
一.连接池 在实际的开发应用中,我们常常会对数据库进行大量的高并发的访问,而最原始的连接和操作方式并不能满足这种大量的访问,程序员为了追求更方便.更快捷.更科学安全的开发.第三方的工具类和Dao层的框 ...
- Python资料汇总(建议收藏)
整理汇总,内容包括长期必备.入门教程.练手项目.学习视频. 一.长期必备. 1. StackOverflow,是疑难解答.bug排除必备网站,任何编程问题请第一时间到此网站查找. https://st ...
- nginx的基础应用(续)
nginx的基础应用(续) 一.简介 上一篇文章我们介绍了nginx的基础应用,其中讲到了nginx作为代理服务器的使用,但是漏了一个重要的,也是使用非常普遍的特性--负载均衡.今天,我们将这段内容补 ...
- 关于AJAX使用中出现中文乱码的问题
自己初学AJAX,遇到一个棘手的bug就是xmlHttp.open("get",url,false);url里面有中文,结果出现乱码.不过最后在百度,和诸位博主的帮助下,成功解决了 ...
- js获取地址栏URL上的参数
获取地址栏上的URL参数现在最简单通用的方法应该就是下面这种了. function getUrlParam (name) { var reg = new RegExp('(^|&)' + na ...
- Java二分法
public class Dichotomy { //定义查找次数 static int count = 0; public static void main(Str ...
- TP3.2 图片上传及缩略图
基于TP自带的上传文件的类, Think/Upload.class.php 设置表单的enctype属性 下面是上传的具体方法 /** * 图片上传处理 * @param [String] $path ...
- ArcGIS教程:创建特征
摘要 创建由输入样本数据和一组栅格波段定义的类的 ASCII 特征文件. 使用方法 · 输出特征文件应使用扩展名 .gsg. · 输入栅格波段和输入栅格或要素样本数据必须具有重叠范围.将仅为公共区域计 ...
- java上传图片剪切工具类
package com.up.util; import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.io. ...